Migrate more activity theming code from ThemeManager

This commit is contained in:
Moez Bhatti
2016-01-03 20:15:40 -05:00
parent ea5977e168
commit 39d6709abf
4 changed files with 50 additions and 69 deletions
@@ -17,7 +17,6 @@ import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.View; import android.view.View;
import android.view.Window;
import android.view.animation.DecelerateInterpolator; import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import com.moez.QKSMS.R; import com.moez.QKSMS.R;
@@ -236,11 +235,7 @@ public class ThemeManager {
private static Resources mResources; private static Resources mResources;
private static SharedPreferences mPrefs; private static SharedPreferences mPrefs;
private static boolean mStatusTintEnabled = false;
private static boolean mNavigationTintEnabled = false;
private static Context mContext; private static Context mContext;
private static Window mWindow;
public static void init(Context context) { public static void init(Context context) {
mContext = context; mContext = context;
@@ -259,19 +254,6 @@ public class ThemeManager {
* of each activity that contains fragments that use ThemeManager * of each activity that contains fragments that use ThemeManager
*/ */
public static void themeActivity(QKActivity activity) { 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) { public static void setTheme(Theme theme) {
@@ -551,35 +533,29 @@ public class ThemeManager {
} }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void setStatusBarTintEnabled(boolean enabled) { public static void setStatusBarTintEnabled(QKActivity activity, boolean enabled) {
if (mStatusTintEnabled != enabled) { int colorFrom = enabled ? mResources.getColor(R.color.black) : mColor;
mStatusTintEnabled = enabled; int colorTo = enabled ? mColor : mResources.getColor(R.color.black);
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); ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setDuration(TRANSITION_LENGTH); colorAnimation.setDuration(TRANSITION_LENGTH);
colorAnimation.addUpdateListener(animation -> { colorAnimation.addUpdateListener(animation -> {
mWindow.setStatusBarColor(ColorUtils.darken((Integer) animation.getAnimatedValue())); activity.getWindow().setStatusBarColor(ColorUtils.darken((Integer) animation.getAnimatedValue()));
}); });
colorAnimation.start(); colorAnimation.start();
}
} }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void setNavigationBarTintEnabled(boolean enabled) { public static void setNavigationBarTintEnabled(QKActivity activity, boolean enabled) {
if (mNavigationTintEnabled != enabled) { int colorFrom = enabled ? mResources.getColor(R.color.black) : mColor;
mNavigationTintEnabled = enabled; int colorTo = enabled ? mColor : mResources.getColor(R.color.black);
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); ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setDuration(TRANSITION_LENGTH); colorAnimation.setDuration(TRANSITION_LENGTH);
colorAnimation.addUpdateListener(animation -> { colorAnimation.addUpdateListener(animation -> {
mWindow.setNavigationBarColor(ColorUtils.darken((Integer) animation.getAnimatedValue())); activity.getWindow().setNavigationBarColor(ColorUtils.darken((Integer) animation.getAnimatedValue()));
}); });
colorAnimation.start(); colorAnimation.start();
}
} }
public static String getColorString(int color) { public static String getColorString(int color) {
@@ -611,16 +587,7 @@ public class ThemeManager {
colorAnimation.setDuration(TRANSITION_LENGTH); colorAnimation.setDuration(TRANSITION_LENGTH);
colorAnimation.setInterpolator(new DecelerateInterpolator()); colorAnimation.setInterpolator(new DecelerateInterpolator());
colorAnimation.addUpdateListener(animation -> { colorAnimation.addUpdateListener(animation -> {
mActiveColor = (Integer) animation.getAnimatedValue(); setActiveColor((Integer) animation.getAnimatedValue());
if (mStatusTintEnabled) {
mWindow.setStatusBarColor(ColorUtils.darken(mActiveColor));
}
if (mNavigationTintEnabled) {
mWindow.setNavigationBarColor(ColorUtils.darken(mActiveColor));
}
LiveViewManager.refreshViews(QKPreference.THEME);
}); });
colorAnimation.addListener(new AnimatorListenerAdapter() { colorAnimation.addListener(new AnimatorListenerAdapter() {
@Override @Override
@@ -650,16 +617,8 @@ public class ThemeManager {
} }
} }
public static void setActiveColor(QKActivity activity, int color) { public static void setActiveColor(int color) {
mActiveColor = color; mActiveColor = color;
if (mStatusTintEnabled) {
mWindow.setStatusBarColor(ColorUtils.darken(color));
}
if (mNavigationTintEnabled) {
mWindow.setNavigationBarColor(ColorUtils.darken(color));
}
LiveViewManager.refreshViews(QKPreference.THEME); LiveViewManager.refreshViews(QKPreference.THEME);
} }
@@ -25,7 +25,9 @@ import com.moez.QKSMS.QKSMSApp;
import com.moez.QKSMS.R; import com.moez.QKSMS.R;
import com.moez.QKSMS.common.LiveViewManager; import com.moez.QKSMS.common.LiveViewManager;
import com.moez.QKSMS.common.preferences.QKPreference; 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.ThemeManager;
import com.moez.QKSMS.ui.settings.SettingsFragment;
import com.moez.QKSMS.ui.view.QKTextView; import com.moez.QKSMS.ui.view.QKTextView;
import java.util.ArrayList; import java.util.ArrayList;
@@ -42,6 +44,9 @@ public abstract class QKActivity extends AppCompatActivity {
protected Resources mRes; protected Resources mRes;
protected SharedPreferences mPrefs; protected SharedPreferences mPrefs;
private static boolean mStatusTintEnabled = true;
private static boolean mNavigationTintEnabled = false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -51,14 +56,24 @@ public abstract class QKActivity extends AppCompatActivity {
mProgressDialog = new ProgressDialog(this); mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIndeterminate(true); mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false); 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. * Reloads the toolbar and it's view references.
* <p/> * <p>
* This is called every time the content view of the activity is set, since the * This is called every time the content view of the activity is set, since the
* toolbar is now a part of the activity layout. * toolbar is now a part of the activity layout.
* <p/> * <p>
* TODO: If someone ever wants to manage the Toolbar dynamically instead of keeping it in their * 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. * 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 -> { LiveViewManager.registerView(QKPreference.THEME, this, key -> {
mToolbar.setBackgroundColor(ThemeManager.getColor()); 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 -> { 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 * Sets the title of the activity, displayed on the toolbar
* <p/> * <p>
* Make sure this is only called AFTER setContentView, or else 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 * is likely not initialized yet and this method will do nothing
* *
@@ -762,7 +762,7 @@ public class MessageListFragment extends QKContentFragment implements ActivityLa
mSensorManager.registerListener(this, mProxSensor, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener(this, mProxSensor, SensorManager.SENSOR_DELAY_NORMAL);
} }
ThemeManager.setActiveColor(mContext, mConversationPrefs.getColor()); ThemeManager.setActiveColor(mConversationPrefs.getColor());
} }
@Override @Override
@@ -787,13 +787,13 @@ public class MessageListFragment extends QKContentFragment implements ActivityLa
} }
} }
ThemeManager.setActiveColor(mContext, ThemeManager.getThemeColor()); ThemeManager.setActiveColor(ThemeManager.getThemeColor());
} }
@Override @Override
public void onMenuChanging(float percentOpen) { public void onMenuChanging(float percentOpen) {
if (mConversationPrefs != null) { if (mConversationPrefs != null) {
ThemeManager.setActiveColor(mContext, (int) mArgbEvaluator.evaluate( ThemeManager.setActiveColor((int) mArgbEvaluator.evaluate(
percentOpen, 0xFF000000 | mConversationPrefs.getColor(), 0xFF000000 | ThemeManager.getThemeColor())); percentOpen, 0xFF000000 | mConversationPrefs.getColor(), 0xFF000000 | ThemeManager.getThemeColor()));
} }
} }
@@ -401,10 +401,10 @@ public class SettingsFragment extends PreferenceFragment implements Preference.O
ThemeManager.setTheme(ThemeManager.Theme.fromString((String) newValue)); ThemeManager.setTheme(ThemeManager.Theme.fromString((String) newValue));
break; break;
case STATUS_TINT: case STATUS_TINT:
ThemeManager.setStatusBarTintEnabled((Boolean) newValue); ThemeManager.setStatusBarTintEnabled(mContext, (Boolean) newValue);
break; break;
case NAVIGATION_TINT: case NAVIGATION_TINT:
ThemeManager.setNavigationBarTintEnabled((Boolean) newValue); ThemeManager.setNavigationBarTintEnabled(mContext, (Boolean) newValue);
break; break;
case FONT_FAMILY: case FONT_FAMILY:
preference.setSummary(mFontFamilies[Integer.parseInt("" + newValue)]); preference.setSummary(mFontFamilies[Integer.parseInt("" + newValue)]);