Browse Source

Fixed ColorMode on the desktop tab UI by caching the backing Bitmap rather than immutable BitmapDrawable

BitmapDrawable turns out is sort of immutable even when using mutate()
so what was happening was that when switching from a tab on the right to
a tab on the left, the foreground drawable was set as the background of
two views for a small instant as the RecyclerView binds views from left
to right and the setColorFilter on the left foreground tab was not
working at all. When you switched from a left to right tab, it worked
fine because the left tab background was changed before the right and
the foreground drawable was only used by one view in that case. The
solution was to not reuse the drawable but instead reuse the backing
bitmap and create a new drawable whenever a tab moved to the foreground.
master
Anthony Restaino 9 years ago
parent
commit
57a25eb9dc
  1. 38
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 8
      app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java

38
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -167,7 +167,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mIsFullScreen = false, mIsFullScreen = false,
mIsImmersive = false, mIsImmersive = false,
mShowTabsInDrawer; mShowTabsInDrawer;
private int mOriginalOrientation, mBackgroundColor, mIdGenerator, mIconColor; private int mOriginalOrientation, mBackgroundColor, mIdGenerator, mIconColor,
mCurrentUiColor;
private String mSearchText, mUntitledTitle, mHomepage, mCameraPhotoPath; private String mSearchText, mUntitledTitle, mHomepage, mCameraPhotoPath;
// Storage // Storage
@ -1307,6 +1308,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
private final CloseTabListener mExitListener; private final CloseTabListener mExitListener;
private final Drawable mBackgroundTabDrawable; private final Drawable mBackgroundTabDrawable;
private final Drawable mForegroundTabDrawable; private final Drawable mForegroundTabDrawable;
private final Bitmap mForegroundTabBitmap;
private final DrawerItemClickListener mClickListener; private final DrawerItemClickListener mClickListener;
private final DrawerItemLongClickListener mLongClickListener; private final DrawerItemLongClickListener mLongClickListener;
private ColorMatrix mColorMatrix; private ColorMatrix mColorMatrix;
@ -1324,6 +1326,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
if (mShowTabsInDrawer) { if (mShowTabsInDrawer) {
mBackgroundTabDrawable = null; mBackgroundTabDrawable = null;
mForegroundTabBitmap = null;
mForegroundTabDrawable = ThemeUtils.getSelectedBackground(context, mDarkTheme); mForegroundTabDrawable = ThemeUtils.getSelectedBackground(context, mDarkTheme);
} else { } else {
int backgroundColor = Utils.mixTwoColors(ThemeUtils.getPrimaryColor(mActivity), Color.BLACK, 0.75f); int backgroundColor = Utils.mixTwoColors(ThemeUtils.getPrimaryColor(mActivity), Color.BLACK, 0.75f);
@ -1332,9 +1335,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mBackgroundTabDrawable = new BitmapDrawable(getResources(), backgroundTabBitmap); mBackgroundTabDrawable = new BitmapDrawable(getResources(), backgroundTabBitmap);
int foregroundColor = ThemeUtils.getPrimaryColor(context); int foregroundColor = ThemeUtils.getPrimaryColor(context);
Bitmap foregroundTabBitmap = Bitmap.createBitmap(Utils.dpToPx(175), Utils.dpToPx(30), Bitmap.Config.ARGB_8888); mForegroundTabBitmap = Bitmap.createBitmap(Utils.dpToPx(175), Utils.dpToPx(30), Bitmap.Config.ARGB_8888);
Utils.drawTrapezoid(new Canvas(foregroundTabBitmap), foregroundColor, false); Utils.drawTrapezoid(new Canvas(mForegroundTabBitmap), foregroundColor, false);
mForegroundTabDrawable = new BitmapDrawable(getResources(), foregroundTabBitmap).mutate(); mForegroundTabDrawable = null;
} }
} }
@ -1364,13 +1367,22 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} else { } else {
holder.txtTitle.setTextAppearance(context, R.style.boldText); holder.txtTitle.setTextAppearance(context, R.style.boldText);
} }
Drawable foregroundDrawable;
if (!mShowTabsInDrawer) {
foregroundDrawable = new BitmapDrawable(getResources(), mForegroundTabBitmap);
if (!isIncognito() && mColorMode) {
foregroundDrawable.setColorFilter(mCurrentUiColor, PorterDuff.Mode.SRC_IN);
}
} else {
foregroundDrawable = mForegroundTabDrawable;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
holder.layout.setBackground(mForegroundTabDrawable); holder.layout.setBackground(foregroundDrawable);
} else { } else {
holder.layout.setBackgroundDrawable(mForegroundTabDrawable); holder.layout.setBackgroundDrawable(foregroundDrawable);
} }
if (!isIncognito() && mColorMode) { if (!isIncognito() && mColorMode) {
changeToolbarBackground(favicon, null /* mForegroundTabDrawable */); changeToolbarBackground(favicon, foregroundDrawable);
} }
holder.favicon.setImageBitmap(favicon); holder.favicon.setImageBitmap(favicon);
} else { } else {
@ -1448,16 +1460,15 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
* @param tabBackground the optional LinearLayout to color * @param tabBackground the optional LinearLayout to color
*/ */
private void changeToolbarBackground(@NonNull Bitmap favicon, @Nullable final Drawable tabBackground) { private void changeToolbarBackground(@NonNull Bitmap favicon, @Nullable final Drawable tabBackground) {
if (!mShowTabsInDrawer) {
// TODO something is messed up and keeping this from working when the tablet tabs are used
return;
}
final int defaultColor; final int defaultColor;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
defaultColor = getResources().getColor(R.color.primary_color); defaultColor = getResources().getColor(R.color.primary_color);
} else { } else {
defaultColor = getColor(R.color.primary_color); defaultColor = getColor(R.color.primary_color);
} }
if (mCurrentUiColor == Color.BLACK) {
mCurrentUiColor = defaultColor;
}
Palette.from(favicon).generate(new Palette.PaletteAsyncListener() { Palette.from(favicon).generate(new Palette.PaletteAsyncListener() {
@Override @Override
public void onGenerated(Palette palette) { public void onGenerated(Palette palette) {
@ -1467,13 +1478,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
int finalColor; // Lighten up the dark color if it is int finalColor; // Lighten up the dark color if it is
// too dark // too dark
if (Utils.isColorTooDark(color)) { if (!mShowTabsInDrawer || Utils.isColorTooDark(color)) {
finalColor = Utils.mixTwoColors(defaultColor, color, 0.25f); finalColor = Utils.mixTwoColors(defaultColor, color, 0.25f);
} else { } else {
finalColor = color; finalColor = color;
} }
ValueAnimator anim = ValueAnimator.ofInt(mBackground.getColor(), finalColor); ValueAnimator anim = ValueAnimator.ofInt(mCurrentUiColor, finalColor);
anim.setEvaluator(new ArgbEvaluator()); anim.setEvaluator(new ArgbEvaluator());
final Window window = getWindow(); final Window window = getWindow();
if (!mShowTabsInDrawer) { if (!mShowTabsInDrawer) {
@ -1490,6 +1501,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} else if (tabBackground != null) { } else if (tabBackground != null) {
tabBackground.setColorFilter(color, PorterDuff.Mode.SRC_IN); tabBackground.setColorFilter(color, PorterDuff.Mode.SRC_IN);
} }
mCurrentUiColor = color;
mToolbarLayout.setBackgroundColor(color); mToolbarLayout.setBackgroundColor(color);
} }

8
app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java

@ -490,9 +490,7 @@ public class GeneralSettingsFragment extends PreferenceFragment implements Prefe
v.setPadding(padding, padding, 0, padding); v.setPadding(padding, padding, 0, padding);
layout.addView(v); layout.addView(v);
layout.addView(getDownload); layout.addView(getDownload);
if (API < Build.VERSION_CODES.JELLY_BEAN) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.edit_text));
} else {
Drawable drawable; Drawable drawable;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable = getResources().getDrawable(android.R.drawable.edit_text, getActivity().getTheme()); drawable = getResources().getDrawable(android.R.drawable.edit_text, getActivity().getTheme());
@ -500,6 +498,8 @@ public class GeneralSettingsFragment extends PreferenceFragment implements Prefe
drawable = getResources().getDrawable(android.R.drawable.edit_text); drawable = getResources().getDrawable(android.R.drawable.edit_text);
} }
layout.setBackground(drawable); layout.setBackground(drawable);
} else {
layout.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.edit_text));
} }
downLocationPicker.setView(layout); downLocationPicker.setView(layout);
downLocationPicker.setPositiveButton(getResources().getString(R.string.action_ok), downLocationPicker.setPositiveButton(getResources().getString(R.string.action_ok),
@ -611,7 +611,7 @@ public class GeneralSettingsFragment extends PreferenceFragment implements Prefe
mPreferences.setGoogleSearchSuggestionsEnabled((Boolean) newValue); mPreferences.setGoogleSearchSuggestionsEnabled((Boolean) newValue);
cbgooglesuggest.setChecked((Boolean) newValue); cbgooglesuggest.setChecked((Boolean) newValue);
return true; return true;
case SETTINGS_DRAWERTABS: case SETTINGS_DRAWERTABS:
mPreferences.setShowTabsInDrawer((Boolean) newValue); mPreferences.setShowTabsInDrawer((Boolean) newValue);
cbDrawerTabs.setChecked((Boolean) newValue); cbDrawerTabs.setChecked((Boolean) newValue);
default: default:

Loading…
Cancel
Save