From c3f08ede87c4fe1935da6c3dfe6188eb7b2f8c1e Mon Sep 17 00:00:00 2001 From: Elisha Azaria Date: Sun, 23 Oct 2022 21:42:29 -0400 Subject: [PATCH] Theme support --- app/build.gradle | 4 +- .../com/elishaazaria/sayboard/AppCtx.java | 28 +++++++++ .../sayboard/SayboardApplication.java | 6 ++ .../com/elishaazaria/sayboard/ime/IME.java | 1 + .../sayboard/ime/ViewManager.java | 60 ++++++++++++++----- .../sayboard/preferences/MyPreferences.java | 20 +++++++ .../preferences/ThemePreferences.java | 23 +++++++ .../settingsfragments/ThemeFragment.java | 13 ++++ app/src/main/res/drawable/ic_arrow_back.xml | 2 +- app/src/main/res/drawable/ic_backspace.xml | 2 +- .../res/drawable/ic_keyboard_arrow_left.xml | 2 +- .../res/drawable/ic_keyboard_arrow_right.xml | 2 +- .../res/drawable/ic_keyboard_backspace.xml | 2 +- .../main/res/drawable/ic_keyboard_return.xml | 2 +- app/src/main/res/drawable/ic_keyboard_tab.xml | 2 +- app/src/main/res/drawable/ic_language.xml | 2 +- app/src/main/res/drawable/ic_mic.xml | 2 +- app/src/main/res/drawable/ic_mic_none.xml | 2 +- app/src/main/res/drawable/ic_mic_off.xml | 2 +- .../main/res/drawable/ic_settings_voice.xml | 2 +- app/src/main/res/layout/ime.xml | 4 +- app/src/main/res/values/integers.xml | 5 ++ app/src/main/res/values/keys.xml | 7 +++ app/src/main/res/values/strings.xml | 4 +- app/src/main/res/xml/theme_preferences.xml | 24 ++++++-- build.gradle | 2 + 26 files changed, 188 insertions(+), 37 deletions(-) create mode 100644 app/src/main/java/com/elishaazaria/sayboard/AppCtx.java create mode 100644 app/src/main/java/com/elishaazaria/sayboard/preferences/MyPreferences.java create mode 100644 app/src/main/java/com/elishaazaria/sayboard/preferences/ThemePreferences.java create mode 100644 app/src/main/res/values/integers.xml create mode 100644 app/src/main/res/values/keys.xml diff --git a/app/build.gradle b/app/build.gradle index 7b2d42d..acfb1a6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -3,6 +3,7 @@ apply plugin: 'com.android.application' repositories { google() mavenCentral() + maven { url "https://jitpack.io" } } android { @@ -45,5 +46,6 @@ dependencies { implementation 'androidx.navigation:navigation-ui:2.5.2' implementation 'androidx.preference:preference:1.2.0' - implementation "org.greenrobot:eventbus:3.3.1" + implementation 'org.greenrobot:eventbus:3.3.1' + implementation 'com.github.martin-stone:hsv-alpha-color-picker-android:3.0.1' } diff --git a/app/src/main/java/com/elishaazaria/sayboard/AppCtx.java b/app/src/main/java/com/elishaazaria/sayboard/AppCtx.java new file mode 100644 index 0000000..e4e4540 --- /dev/null +++ b/app/src/main/java/com/elishaazaria/sayboard/AppCtx.java @@ -0,0 +1,28 @@ +package com.elishaazaria.sayboard; + +import android.annotation.SuppressLint; +import android.content.Context; + +import androidx.annotation.IntegerRes; +import androidx.annotation.StringRes; + +public class AppCtx { + @SuppressLint("StaticFieldLeak") // App context + private static Context appCtx; + + public static void setAppCtx(Context context) { + appCtx = context.getApplicationContext(); + } + + public static Context getAppCtx() { + return appCtx; + } + + public static String getStringRes(@StringRes int res) { + return appCtx.getString(res); + } + + public static int getIntegerRes(@IntegerRes int res){ + return appCtx.getResources().getInteger(res); + } +} diff --git a/app/src/main/java/com/elishaazaria/sayboard/SayboardApplication.java b/app/src/main/java/com/elishaazaria/sayboard/SayboardApplication.java index ffae4f0..e137297 100644 --- a/app/src/main/java/com/elishaazaria/sayboard/SayboardApplication.java +++ b/app/src/main/java/com/elishaazaria/sayboard/SayboardApplication.java @@ -1,6 +1,11 @@ package com.elishaazaria.sayboard; import android.app.Application; +import android.content.Context; + +import androidx.annotation.StringRes; + +import com.elishaazaria.sayboard.preferences.MyPreferences; import org.greenrobot.eventbus.EventBus; @@ -9,5 +14,6 @@ public class SayboardApplication extends Application { public void onCreate() { super.onCreate(); EventBus.builder().logNoSubscriberMessages(false).sendNoSubscriberEvent(false).installDefaultEventBus(); + AppCtx.setAppCtx(this); } } diff --git a/app/src/main/java/com/elishaazaria/sayboard/ime/IME.java b/app/src/main/java/com/elishaazaria/sayboard/ime/IME.java index 12e7e81..83f887a 100644 --- a/app/src/main/java/com/elishaazaria/sayboard/ime/IME.java +++ b/app/src/main/java/com/elishaazaria/sayboard/ime/IME.java @@ -138,6 +138,7 @@ public void onStartInputView(EditorInfo info, boolean restarting) { } modelManager.reloadModels(); + viewManager.refresh(); } @Override diff --git a/app/src/main/java/com/elishaazaria/sayboard/ime/ViewManager.java b/app/src/main/java/com/elishaazaria/sayboard/ime/ViewManager.java index 23a6c49..ef3117c 100644 --- a/app/src/main/java/com/elishaazaria/sayboard/ime/ViewManager.java +++ b/app/src/main/java/com/elishaazaria/sayboard/ime/ViewManager.java @@ -1,6 +1,10 @@ package com.elishaazaria.sayboard.ime; +import android.content.res.ColorStateList; +import android.graphics.drawable.Drawable; +import android.os.Build; import android.text.method.ScrollingMovementMethod; +import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.Button; @@ -9,8 +13,10 @@ import androidx.appcompat.content.res.AppCompatResources; import androidx.constraintlayout.widget.ConstraintLayout; +import androidx.core.widget.TextViewCompat; import com.elishaazaria.sayboard.R; +import com.elishaazaria.sayboard.preferences.ThemePreferences; public class ViewManager { private final IME ime; @@ -56,17 +62,14 @@ public void init() { resultView.setMovementMethod(new ScrollingMovementMethod()); micButton.setOnClickListener(v -> { - if (listener != null) - listener.micClick(); + if (listener != null) listener.micClick(); }); micButton.setOnLongClickListener(v -> listener != null && listener.micLongClick()); backButton.setOnClickListener(v -> { - if (listener != null) - listener.backClicked(); + if (listener != null) listener.backClicked(); }); backspaceButton.setOnClickListener(v -> { - if (listener != null) - listener.backspaceClicked(); + if (listener != null) listener.backspaceClicked(); }); backspaceButton.setOnTouchListener((v, event) -> { if (listener == null) { @@ -76,12 +79,10 @@ public void init() { return listener.backspaceTouched(v, event); }); returnButton.setOnClickListener(v -> { - if (listener != null) - listener.returnClicked(); + if (listener != null) listener.returnClicked(); }); modelButton.setOnClickListener(v -> { - if (listener != null) - listener.modelClicked(); + if (listener != null) listener.modelClicked(); }); @@ -89,10 +90,39 @@ public void init() { if (currentState == STATE_ERROR && !currentErrorMessage.isEmpty()) setErrorState(currentErrorMessage); - else - setUiState(currentState); + else setUiState(currentState); setModelName(modelName); + + setUpTheme(); + } + + private int currentForeground = Integer.MAX_VALUE; + private int currentBackground = Integer.MAX_VALUE; + + private void setUpTheme() { + int foreground = ThemePreferences.getForegroundColor(); + int background = ThemePreferences.getBackgroundColor(); + + if (currentForeground == foreground && currentBackground == background) return; + + currentForeground = foreground; + currentBackground = background; + + overlayView.setBackgroundColor(background); + + ColorStateList foregroundTint = ColorStateList.valueOf(foreground); + micButton.setImageTintList(foregroundTint); + backButton.setImageTintList(foregroundTint); + backspaceButton.setImageTintList(foregroundTint); + returnButton.setImageTintList(foregroundTint); + TextViewCompat.setCompoundDrawableTintList(modelButton, foregroundTint); + modelButton.setTextColor(foreground); + resultView.setTextColor(foreground); + } + + public void refresh() { + setUpTheme(); } public void setUiState(int state) { @@ -134,15 +164,13 @@ public void setUiState(int state) { public void setErrorState(String message) { setUiState(STATE_ERROR); - if (initialized) - resultView.setText(message); + if (initialized) resultView.setText(message); currentErrorMessage = message; } public void setModelName(String modelName) { this.modelName = modelName; - if (initialized) - modelButton.setText(modelName); + if (initialized) modelButton.setText(modelName); } public ConstraintLayout getRoot() { diff --git a/app/src/main/java/com/elishaazaria/sayboard/preferences/MyPreferences.java b/app/src/main/java/com/elishaazaria/sayboard/preferences/MyPreferences.java new file mode 100644 index 0000000..3b0ff70 --- /dev/null +++ b/app/src/main/java/com/elishaazaria/sayboard/preferences/MyPreferences.java @@ -0,0 +1,20 @@ +package com.elishaazaria.sayboard.preferences; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.content.SharedPreferences; + +import com.elishaazaria.sayboard.AppCtx; +import com.elishaazaria.sayboard.R; + +public class MyPreferences { + private static SharedPreferences sharedPref; + + public static SharedPreferences getSharedPref() { + if (sharedPref == null) { + sharedPref = AppCtx.getAppCtx().getSharedPreferences( + AppCtx.getStringRes(R.string.main_shared_pref), Context.MODE_PRIVATE); + } + return sharedPref; + } +} diff --git a/app/src/main/java/com/elishaazaria/sayboard/preferences/ThemePreferences.java b/app/src/main/java/com/elishaazaria/sayboard/preferences/ThemePreferences.java new file mode 100644 index 0000000..9e18842 --- /dev/null +++ b/app/src/main/java/com/elishaazaria/sayboard/preferences/ThemePreferences.java @@ -0,0 +1,23 @@ +package com.elishaazaria.sayboard.preferences; + +import android.graphics.Color; +import android.os.Build; + +import com.elishaazaria.sayboard.AppCtx; +import com.elishaazaria.sayboard.R; + +public class ThemePreferences { + public static int getForegroundColor() { + // TODO: if min-api 26, switch to Color objects + return MyPreferences.getSharedPref() + .getInt(AppCtx.getStringRes(R.string.pref_theme_foreground_c), + AppCtx.getIntegerRes(R.integer.pref_foreground_color_default)); + } + + public static int getBackgroundColor() { + // TODO: if min-api 26, switch to Color objects + return MyPreferences.getSharedPref() + .getInt(AppCtx.getStringRes(R.string.pref_theme_background_c), + AppCtx.getIntegerRes(R.integer.pref_background_color_default)); + } +} diff --git a/app/src/main/java/com/elishaazaria/sayboard/settingsfragments/ThemeFragment.java b/app/src/main/java/com/elishaazaria/sayboard/settingsfragments/ThemeFragment.java index 591c99a..502c048 100644 --- a/app/src/main/java/com/elishaazaria/sayboard/settingsfragments/ThemeFragment.java +++ b/app/src/main/java/com/elishaazaria/sayboard/settingsfragments/ThemeFragment.java @@ -2,14 +2,27 @@ import android.os.Bundle; +import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.PreferenceManager; +import com.elishaazaria.sayboard.AppCtx; import com.elishaazaria.sayboard.R; +import com.rarepebble.colorpicker.ColorPreference; public class ThemeFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + PreferenceManager preferenceManager = getPreferenceManager(); + preferenceManager.setSharedPreferencesName(AppCtx.getStringRes(R.string.main_shared_pref)); setPreferencesFromResource(R.xml.theme_preferences, rootKey); } + + @Override + public void onDisplayPreferenceDialog(Preference preference) { + if (preference instanceof ColorPreference) { + ((ColorPreference) preference).showDialog(this, 0); + } else super.onDisplayPreferenceDialog(preference); + } } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_arrow_back.xml b/app/src/main/res/drawable/ic_arrow_back.xml index 4d99708..1686755 100644 --- a/app/src/main/res/drawable/ic_arrow_back.xml +++ b/app/src/main/res/drawable/ic_arrow_back.xml @@ -2,7 +2,7 @@ android:width="24dp" android:height="24dp" android:autoMirrored="true" - android:tint="@color/colorOnPrimary" + android:tint="#000000" android:viewportWidth="24" android:viewportHeight="24"> diff --git a/app/src/main/res/drawable/ic_keyboard_arrow_left.xml b/app/src/main/res/drawable/ic_keyboard_arrow_left.xml index f18d556..f3dfdd0 100644 --- a/app/src/main/res/drawable/ic_keyboard_arrow_left.xml +++ b/app/src/main/res/drawable/ic_keyboard_arrow_left.xml @@ -1,4 +1,4 @@ - diff --git a/app/src/main/res/drawable/ic_keyboard_arrow_right.xml b/app/src/main/res/drawable/ic_keyboard_arrow_right.xml index 32f84c0..19ec5a5 100644 --- a/app/src/main/res/drawable/ic_keyboard_arrow_right.xml +++ b/app/src/main/res/drawable/ic_keyboard_arrow_right.xml @@ -1,4 +1,4 @@ - diff --git a/app/src/main/res/drawable/ic_keyboard_backspace.xml b/app/src/main/res/drawable/ic_keyboard_backspace.xml index 6e20256..e83b089 100644 --- a/app/src/main/res/drawable/ic_keyboard_backspace.xml +++ b/app/src/main/res/drawable/ic_keyboard_backspace.xml @@ -1,4 +1,4 @@ - diff --git a/app/src/main/res/drawable/ic_keyboard_return.xml b/app/src/main/res/drawable/ic_keyboard_return.xml index 5e02fc6..697b0c1 100644 --- a/app/src/main/res/drawable/ic_keyboard_return.xml +++ b/app/src/main/res/drawable/ic_keyboard_return.xml @@ -1,4 +1,4 @@ - diff --git a/app/src/main/res/drawable/ic_keyboard_tab.xml b/app/src/main/res/drawable/ic_keyboard_tab.xml index 54ee2a7..a041bf4 100644 --- a/app/src/main/res/drawable/ic_keyboard_tab.xml +++ b/app/src/main/res/drawable/ic_keyboard_tab.xml @@ -1,4 +1,4 @@ - diff --git a/app/src/main/res/drawable/ic_language.xml b/app/src/main/res/drawable/ic_language.xml index 4522f8f..043244c 100644 --- a/app/src/main/res/drawable/ic_language.xml +++ b/app/src/main/res/drawable/ic_language.xml @@ -1,7 +1,7 @@ diff --git a/app/src/main/res/drawable/ic_mic_none.xml b/app/src/main/res/drawable/ic_mic_none.xml index 8029ea3..51fccee 100644 --- a/app/src/main/res/drawable/ic_mic_none.xml +++ b/app/src/main/res/drawable/ic_mic_none.xml @@ -1,7 +1,7 @@ diff --git a/app/src/main/res/drawable/ic_settings_voice.xml b/app/src/main/res/drawable/ic_settings_voice.xml index 081f4fd..25faaeb 100644 --- a/app/src/main/res/drawable/ic_settings_voice.xml +++ b/app/src/main/res/drawable/ic_settings_voice.xml @@ -1,7 +1,7 @@ + app:layout_constraintStart_toStartOf="parent" + tools:text="English (US)" /> + + 0xFF377A00 + 0xFFFFFFFF + \ No newline at end of file diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml new file mode 100644 index 0000000..3bfd3a8 --- /dev/null +++ b/app/src/main/res/values/keys.xml @@ -0,0 +1,7 @@ + + + main_shared_pref + c_foreground + c_background + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e2783c3..461bec7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -43,7 +43,9 @@ Theme - Your signature + Foreground color + Background color + Reset Model download diff --git a/app/src/main/res/xml/theme_preferences.xml b/app/src/main/res/xml/theme_preferences.xml index 66c41e0..fb1ffec 100644 --- a/app/src/main/res/xml/theme_preferences.xml +++ b/app/src/main/res/xml/theme_preferences.xml @@ -1,11 +1,25 @@ - + - - - - + + + + + + + + + diff --git a/build.gradle b/build.gradle index 1aeb62e..8773e95 100644 --- a/build.gradle +++ b/build.gradle @@ -8,6 +8,7 @@ buildscript { maven { url 'https://alphacephei.com/maven/' } + maven { url "https://jitpack.io" } } dependencies { classpath 'com.android.tools.build:gradle:7.3.1' @@ -22,5 +23,6 @@ allprojects { maven { url 'https://alphacephei.com/maven/' } + maven { url "https://jitpack.io" } } }