From 39d6709abf48a704827d6d60873db64edecd2039 Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Sun, 3 Jan 2016 20:15:40 -0500 Subject: [PATCH] Migrate more activity theming code from ThemeManager --- .../java/com/moez/QKSMS/ui/ThemeManager.java | 81 +++++-------------- .../com/moez/QKSMS/ui/base/QKActivity.java | 28 ++++++- .../ui/messagelist/MessageListFragment.java | 6 +- .../QKSMS/ui/settings/SettingsFragment.java | 4 +- 4 files changed, 50 insertions(+), 69 deletions(-) diff --git a/QKSMS/src/main/java/com/moez/QKSMS/ui/ThemeManager.java b/QKSMS/src/main/java/com/moez/QKSMS/ui/ThemeManager.java index 2a39bd9d..afabc152 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/ui/ThemeManager.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/ui/ThemeManager.java @@ -17,7 +17,6 @@ import android.preference.PreferenceManager; import android.util.Log; import android.view.Gravity; import android.view.View; -import android.view.Window; import android.view.animation.DecelerateInterpolator; import android.widget.LinearLayout; import com.moez.QKSMS.R; @@ -236,11 +235,7 @@ public class ThemeManager { private static Resources mResources; private static SharedPreferences mPrefs; - private static boolean mStatusTintEnabled = false; - private static boolean mNavigationTintEnabled = false; - private static Context mContext; - private static Window mWindow; public static void init(Context context) { mContext = context; @@ -259,19 +254,6 @@ public class ThemeManager { * of each activity that contains fragments that use ThemeManager */ public static void themeActivity(QKActivity activity) { - - mWindow = activity.getWindow(); - - mStatusTintEnabled = mPrefs.getBoolean(SettingsFragment.STATUS_TINT, true) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; - mNavigationTintEnabled = mPrefs.getBoolean(SettingsFragment.NAVIGATION_TINT, false) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; - - if (mStatusTintEnabled) { - mWindow.setStatusBarColor(ColorUtils.darken(mColor)); - } - if (mNavigationTintEnabled) { - mWindow.setNavigationBarColor(ColorUtils.darken(mColor)); - } - } public static void setTheme(Theme theme) { @@ -551,35 +533,29 @@ public class ThemeManager { } @TargetApi(Build.VERSION_CODES.LOLLIPOP) - public static void setStatusBarTintEnabled(boolean enabled) { - if (mStatusTintEnabled != enabled) { - mStatusTintEnabled = enabled; - int colorFrom = enabled ? mResources.getColor(R.color.black) : mColor; - int colorTo = enabled ? mColor : mResources.getColor(R.color.black); + public static void setStatusBarTintEnabled(QKActivity activity, boolean enabled) { + int colorFrom = enabled ? mResources.getColor(R.color.black) : mColor; + int colorTo = enabled ? mColor : mResources.getColor(R.color.black); - ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); - colorAnimation.setDuration(TRANSITION_LENGTH); - colorAnimation.addUpdateListener(animation -> { - mWindow.setStatusBarColor(ColorUtils.darken((Integer) animation.getAnimatedValue())); - }); - colorAnimation.start(); - } + ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); + colorAnimation.setDuration(TRANSITION_LENGTH); + colorAnimation.addUpdateListener(animation -> { + activity.getWindow().setStatusBarColor(ColorUtils.darken((Integer) animation.getAnimatedValue())); + }); + colorAnimation.start(); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) - public static void setNavigationBarTintEnabled(boolean enabled) { - if (mNavigationTintEnabled != enabled) { - mNavigationTintEnabled = enabled; - int colorFrom = enabled ? mResources.getColor(R.color.black) : mColor; - int colorTo = enabled ? mColor : mResources.getColor(R.color.black); + public static void setNavigationBarTintEnabled(QKActivity activity, boolean enabled) { + int colorFrom = enabled ? mResources.getColor(R.color.black) : mColor; + int colorTo = enabled ? mColor : mResources.getColor(R.color.black); - ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); - colorAnimation.setDuration(TRANSITION_LENGTH); - colorAnimation.addUpdateListener(animation -> { - mWindow.setNavigationBarColor(ColorUtils.darken((Integer) animation.getAnimatedValue())); - }); - colorAnimation.start(); - } + ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); + colorAnimation.setDuration(TRANSITION_LENGTH); + colorAnimation.addUpdateListener(animation -> { + activity.getWindow().setNavigationBarColor(ColorUtils.darken((Integer) animation.getAnimatedValue())); + }); + colorAnimation.start(); } public static String getColorString(int color) { @@ -611,16 +587,7 @@ public class ThemeManager { colorAnimation.setDuration(TRANSITION_LENGTH); colorAnimation.setInterpolator(new DecelerateInterpolator()); colorAnimation.addUpdateListener(animation -> { - mActiveColor = (Integer) animation.getAnimatedValue(); - - if (mStatusTintEnabled) { - mWindow.setStatusBarColor(ColorUtils.darken(mActiveColor)); - } - if (mNavigationTintEnabled) { - mWindow.setNavigationBarColor(ColorUtils.darken(mActiveColor)); - } - - LiveViewManager.refreshViews(QKPreference.THEME); + setActiveColor((Integer) animation.getAnimatedValue()); }); colorAnimation.addListener(new AnimatorListenerAdapter() { @Override @@ -650,16 +617,8 @@ public class ThemeManager { } } - public static void setActiveColor(QKActivity activity, int color) { + public static void setActiveColor(int color) { mActiveColor = color; - - if (mStatusTintEnabled) { - mWindow.setStatusBarColor(ColorUtils.darken(color)); - } - if (mNavigationTintEnabled) { - mWindow.setNavigationBarColor(ColorUtils.darken(color)); - } - LiveViewManager.refreshViews(QKPreference.THEME); } diff --git a/QKSMS/src/main/java/com/moez/QKSMS/ui/base/QKActivity.java b/QKSMS/src/main/java/com/moez/QKSMS/ui/base/QKActivity.java index ad48ffee..1762d164 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/ui/base/QKActivity.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/ui/base/QKActivity.java @@ -25,7 +25,9 @@ import com.moez.QKSMS.QKSMSApp; import com.moez.QKSMS.R; import com.moez.QKSMS.common.LiveViewManager; import com.moez.QKSMS.common.preferences.QKPreference; +import com.moez.QKSMS.common.utils.ColorUtils; import com.moez.QKSMS.ui.ThemeManager; +import com.moez.QKSMS.ui.settings.SettingsFragment; import com.moez.QKSMS.ui.view.QKTextView; import java.util.ArrayList; @@ -42,6 +44,9 @@ public abstract class QKActivity extends AppCompatActivity { protected Resources mRes; protected SharedPreferences mPrefs; + private static boolean mStatusTintEnabled = true; + private static boolean mNavigationTintEnabled = false; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -51,14 +56,24 @@ public abstract class QKActivity extends AppCompatActivity { mProgressDialog = new ProgressDialog(this); mProgressDialog.setIndeterminate(true); mProgressDialog.setCancelable(false); + + LiveViewManager.registerView(QKPreference.TINTED_STATUS, this, key -> { + mStatusTintEnabled = getBoolean(QKPreference.TINTED_STATUS) && + Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; + }); + + LiveViewManager.registerView(QKPreference.TINTED_NAV, this, key -> { + mNavigationTintEnabled = getBoolean(QKPreference.TINTED_NAV) && + Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; + }); } /** * Reloads the toolbar and it's view references. - *

+ *

* This is called every time the content view of the activity is set, since the * toolbar is now a part of the activity layout. - *

+ *

* TODO: If someone ever wants to manage the Toolbar dynamically instead of keeping it in their * TODO layout file, we can add an alternate way of setting the toolbar programmatically. */ @@ -75,6 +90,13 @@ public abstract class QKActivity extends AppCompatActivity { LiveViewManager.registerView(QKPreference.THEME, this, key -> { mToolbar.setBackgroundColor(ThemeManager.getColor()); + + if (mStatusTintEnabled) { + getWindow().setStatusBarColor(ColorUtils.darken(ThemeManager.getColor())); + } + if (mNavigationTintEnabled) { + getWindow().setNavigationBarColor(ColorUtils.darken(ThemeManager.getColor())); + } }); LiveViewManager.registerView(QKPreference.BACKGROUND, this, key -> { @@ -217,7 +239,7 @@ public abstract class QKActivity extends AppCompatActivity { /** * Sets the title of the activity, displayed on the toolbar - *

+ *

* Make sure this is only called AFTER setContentView, or else the Toolbar * is likely not initialized yet and this method will do nothing * diff --git a/QKSMS/src/main/java/com/moez/QKSMS/ui/messagelist/MessageListFragment.java b/QKSMS/src/main/java/com/moez/QKSMS/ui/messagelist/MessageListFragment.java index 98724d37..2bf8667a 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/ui/messagelist/MessageListFragment.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/ui/messagelist/MessageListFragment.java @@ -762,7 +762,7 @@ public class MessageListFragment extends QKContentFragment implements ActivityLa mSensorManager.registerListener(this, mProxSensor, SensorManager.SENSOR_DELAY_NORMAL); } - ThemeManager.setActiveColor(mContext, mConversationPrefs.getColor()); + ThemeManager.setActiveColor(mConversationPrefs.getColor()); } @Override @@ -787,13 +787,13 @@ public class MessageListFragment extends QKContentFragment implements ActivityLa } } - ThemeManager.setActiveColor(mContext, ThemeManager.getThemeColor()); + ThemeManager.setActiveColor(ThemeManager.getThemeColor()); } @Override public void onMenuChanging(float percentOpen) { if (mConversationPrefs != null) { - ThemeManager.setActiveColor(mContext, (int) mArgbEvaluator.evaluate( + ThemeManager.setActiveColor((int) mArgbEvaluator.evaluate( percentOpen, 0xFF000000 | mConversationPrefs.getColor(), 0xFF000000 | ThemeManager.getThemeColor())); } } diff --git a/QKSMS/src/main/java/com/moez/QKSMS/ui/settings/SettingsFragment.java b/QKSMS/src/main/java/com/moez/QKSMS/ui/settings/SettingsFragment.java index 8563586a..e7e84941 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/ui/settings/SettingsFragment.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/ui/settings/SettingsFragment.java @@ -401,10 +401,10 @@ public class SettingsFragment extends PreferenceFragment implements Preference.O ThemeManager.setTheme(ThemeManager.Theme.fromString((String) newValue)); break; case STATUS_TINT: - ThemeManager.setStatusBarTintEnabled((Boolean) newValue); + ThemeManager.setStatusBarTintEnabled(mContext, (Boolean) newValue); break; case NAVIGATION_TINT: - ThemeManager.setNavigationBarTintEnabled((Boolean) newValue); + ThemeManager.setNavigationBarTintEnabled(mContext, (Boolean) newValue); break; case FONT_FAMILY: preference.setSummary(mFontFamilies[Integer.parseInt("" + newValue)]);