Color the search bar appropriately for the various theme/color mode, fixed bug when restarting activity

This commit is contained in:
Anthony Restaino 2016-03-19 13:16:53 -04:00
parent 1685a13df3
commit f90ab177d5
5 changed files with 62 additions and 26 deletions

View File

@ -147,6 +147,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
// Toolbar Views // Toolbar Views
private View mSearchBackground;
private Toolbar mToolbar; private Toolbar mToolbar;
private AutoCompleteTextView mSearch; private AutoCompleteTextView mSearch;
private ImageView mArrowImage; private ImageView mArrowImage;
@ -262,7 +263,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mShowTabsInDrawer = mPreferences.getShowTabsInDrawer(!isTablet()); mShowTabsInDrawer = mPreferences.getShowTabsInDrawer(!isTablet());
// initialize background ColorDrawable // initialize background ColorDrawable
mBackground.setColor(((ColorDrawable) mToolbarLayout.getBackground()).getColor()); int primaryColor = ThemeUtils.getPrimaryColor(this);
mBackground.setColor(primaryColor);
// Drawer stutters otherwise // Drawer stutters otherwise
mDrawerLeft.setLayerType(View.LAYER_TYPE_NONE, null); mDrawerLeft.setLayerType(View.LAYER_TYPE_NONE, null);
@ -353,11 +355,18 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
// create the search EditText in the ToolBar // create the search EditText in the ToolBar
mSearch = (AutoCompleteTextView) customView.findViewById(R.id.search); mSearch = (AutoCompleteTextView) customView.findViewById(R.id.search);
mSearchBackground = customView.findViewById(R.id.search_container);
// initialize search background color
mSearchBackground.getBackground().setColorFilter(getSearchBarColor(primaryColor, primaryColor), PorterDuff.Mode.SRC_IN);
mSearch.setHintTextColor(ThemeUtils.getThemedTextHintColor(mDarkTheme));
mSearch.setTextColor(mDarkTheme ? Color.WHITE : Color.BLACK);
mUntitledTitle = getString(R.string.untitled); mUntitledTitle = getString(R.string.untitled);
mBackgroundColor = ContextCompat.getColor(this, R.color.primary_color); mBackgroundColor = ThemeUtils.getPrimaryColor(this);
mDeleteIcon = ThemeUtils.getLightThemedDrawable(this, R.drawable.ic_action_delete); mDeleteIcon = ThemeUtils.getThemedDrawable(this, R.drawable.ic_action_delete, mDarkTheme);
mRefreshIcon = ThemeUtils.getLightThemedDrawable(this, R.drawable.ic_action_refresh); mRefreshIcon = ThemeUtils.getThemedDrawable(this, R.drawable.ic_action_refresh, mDarkTheme);
mClearIcon = ThemeUtils.getLightThemedDrawable(this, R.drawable.ic_action_delete); mClearIcon = ThemeUtils.getThemedDrawable(this, R.drawable.ic_action_delete, mDarkTheme);
int iconBounds = Utils.dpToPx(30); int iconBounds = Utils.dpToPx(30);
mDeleteIcon.setBounds(0, 0, iconBounds, iconBounds); mDeleteIcon.setBounds(0, 0, iconBounds, iconBounds);
@ -1232,7 +1241,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
// OR with opaque black to remove transparency glitches // OR with opaque black to remove transparency glitches
int color = 0xff000000 | palette.getVibrantColor(defaultColor); int color = 0xff000000 | palette.getVibrantColor(defaultColor);
int finalColor; // Lighten up the dark color if it is final int finalColor; // Lighten up the dark color if it is
// too dark // too dark
if (!mShowTabsInDrawer || Utils.isColorTooDark(color)) { if (!mShowTabsInDrawer || Utils.isColorTooDark(color)) {
finalColor = Utils.mixTwoColors(defaultColor, color, 0.25f); finalColor = Utils.mixTwoColors(defaultColor, color, 0.25f);
@ -1240,17 +1249,19 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
finalColor = color; finalColor = color;
} }
ValueAnimator anim = ValueAnimator.ofInt(mCurrentUiColor, finalColor);
anim.setEvaluator(new ArgbEvaluator());
final Window window = getWindow(); final Window window = getWindow();
if (!mShowTabsInDrawer) { if (!mShowTabsInDrawer) {
window.setBackgroundDrawable(new ColorDrawable(Color.BLACK)); window.setBackgroundDrawable(new ColorDrawable(Color.BLACK));
} }
anim.addUpdateListener(new AnimatorUpdateListener() {
final int startSearchColor = getSearchBarColor(mCurrentUiColor, defaultColor);
final int finalSearchColor = getSearchBarColor(finalColor, defaultColor);
Animation animation = new Animation() {
@Override @Override
public void onAnimationUpdate(ValueAnimator animation) { protected void applyTransformation(float interpolatedTime, Transformation t) {
final int color = (Integer) animation.getAnimatedValue(); final int color = DrawableUtils.mixColor(interpolatedTime, mCurrentUiColor, finalColor);
if (mShowTabsInDrawer) { if (mShowTabsInDrawer) {
mBackground.setColor(color); mBackground.setColor(color);
window.setBackgroundDrawable(mBackground); window.setBackgroundDrawable(mBackground);
@ -1259,15 +1270,24 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} }
mCurrentUiColor = color; mCurrentUiColor = color;
mToolbarLayout.setBackgroundColor(color); mToolbarLayout.setBackgroundColor(color);
mSearchBackground.getBackground().setColorFilter(DrawableUtils.mixColor(interpolatedTime,
startSearchColor, finalSearchColor), PorterDuff.Mode.SRC_IN);
} }
};
}); animation.setDuration(300);
anim.setDuration(300); mToolbarLayout.startAnimation(animation);
anim.start();
} }
}); });
} }
private int getSearchBarColor(int requestedColor, int defaultColor) {
if (requestedColor == defaultColor) {
return mDarkTheme ? DrawableUtils.mixColor(0.25f, defaultColor, Color.WHITE): Color.WHITE;
} else {
return DrawableUtils.mixColor(0.5f, requestedColor, Color.WHITE);
}
}
@Override @Override
public boolean getUseDarkTheme() { public boolean getUseDarkTheme() {
return mDarkTheme; return mDarkTheme;

View File

@ -48,8 +48,7 @@ public abstract class ThemableBrowserActivity extends AppCompatActivity {
} }
private void restart() { private void restart() {
Intent intent = getIntent();
finish(); finish();
startActivity(intent); startActivity(new Intent(this, getClass()));
} }
} }

View File

@ -51,4 +51,24 @@ public class DrawableUtils {
return image; return image;
} }
public static int mixColor(float fraction, int startValue, int endValue) {
int startInt = startValue;
int startA = (startInt >> 24) & 0xff;
int startR = (startInt >> 16) & 0xff;
int startG = (startInt >> 8) & 0xff;
int startB = startInt & 0xff;
int endInt = endValue;
int endA = (endInt >> 24) & 0xff;
int endR = (endInt >> 16) & 0xff;
int endG = (endInt >> 8) & 0xff;
int endB = endInt & 0xff;
return (startA + (int)(fraction * (endA - startA))) << 24 |
(startR + (int)(fraction * (endR - startR))) << 16 |
(startG + (int)(fraction * (endG - startG))) << 8 |
(startB + (int)(fraction * (endB - startB)));
}
} }

View File

@ -5,6 +5,7 @@ import android.content.res.TypedArray;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter; import android.graphics.ColorFilter;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
@ -87,14 +88,6 @@ public class ThemeUtils {
return drawable; return drawable;
} }
@NonNull
public static Drawable getLightThemedDrawable(@NonNull Context context, @DrawableRes int res) {
final Drawable drawable = ContextCompat.getDrawable(context, res);
drawable.mutate();
drawable.setColorFilter(getIconLightThemeColor(context), PorterDuff.Mode.SRC_IN);
return drawable;
}
@NonNull @NonNull
public static ColorDrawable getSelectedBackground(@NonNull Context context, boolean dark) { public static ColorDrawable getSelectedBackground(@NonNull Context context, boolean dark) {
@ColorInt final int color = (dark) ? ContextCompat.getColor(context, R.color.selected_dark) : @ColorInt final int color = (dark) ? ContextCompat.getColor(context, R.color.selected_dark) :
@ -102,6 +95,10 @@ public class ThemeUtils {
return new ColorDrawable(color); return new ColorDrawable(color);
} }
public static int getThemedTextHintColor(boolean dark){
return 0x80ffffff & (dark ? Color.WHITE : Color.BLACK);
}
public static int getTextColor(@NonNull Context context) { public static int getTextColor(@NonNull Context context) {
return getColor(context, android.R.attr.editTextColor); return getColor(context, android.R.attr.editTextColor);
} }

View File

@ -105,7 +105,7 @@
<style name="Theme.BlackTheme" parent="Theme.AppCompat"> <style name="Theme.BlackTheme" parent="Theme.AppCompat">
<!-- customize the color palette --> <!-- customize the color palette -->
<item name="searchBackground">@drawable/card_bg_elevate</item> <item name="searchBackground">@drawable/card_bg</item>
<item name="colorPrimary">@color/black</item> <item name="colorPrimary">@color/black</item>
<item name="colorPrimaryDark">@color/black</item> <item name="colorPrimaryDark">@color/black</item>
<item name="colorAccent">@color/accent_color</item> <item name="colorAccent">@color/accent_color</item>