Bug 1351605 - Get rid of color variable in activity r=sebastian

Now we can get toolbar color from intent directly, and the intent will
be stored in `onSavedInstanceState`. Let's get rid of the local
variable.

MozReview-Commit-ID: OsqwgFJctH

--HG--
extra : rebase_source : a5cd688de88de564739481f77fe514bdeffd6c0e
This commit is contained in:
Julian_Chu 2017-04-06 12:54:33 +08:00
Родитель e87fa62937
Коммит e270fdb527
3 изменённых файлов: 44 добавлений и 18 удалений

Просмотреть файл

@ -54,15 +54,9 @@ import org.mozilla.gecko.util.GeckoBundle;
import java.util.List;
import static android.support.customtabs.CustomTabsIntent.EXTRA_TOOLBAR_COLOR;
public class CustomTabsActivity extends GeckoApp implements Tabs.OnTabsChangedListener {
private static final String LOGTAG = "CustomTabsActivity";
private static final String SAVED_START_INTENT = "saved_intent_which_started_this_activity";
private static final String SAVED_TOOLBAR_COLOR = "SavedToolbarColor";
@ColorInt
private static final int DEFAULT_ACTION_BAR_COLOR = 0xFF363b40; // default color to match design
private final SparseArrayCompat<PendingIntent> menuItemsIntent = new SparseArrayCompat<>();
private GeckoPopupMenu popupMenu;
@ -71,9 +65,6 @@ public class CustomTabsActivity extends GeckoApp implements Tabs.OnTabsChangedLi
// A state to indicate whether this activity is finishing with customize animation
private boolean usingCustomAnimation = false;
@ColorInt
private int toolbarColor = DEFAULT_ACTION_BAR_COLOR;
// Bug 1351605 - getIntent() not always returns the intent which started this activity.
// Therefore we make a copy in case of this Activity is re-created.
private Intent startIntent;
@ -84,18 +75,13 @@ public class CustomTabsActivity extends GeckoApp implements Tabs.OnTabsChangedLi
if (savedInstanceState != null) {
startIntent = savedInstanceState.getParcelable(SAVED_START_INTENT);
toolbarColor = savedInstanceState.getInt(SAVED_TOOLBAR_COLOR, DEFAULT_ACTION_BAR_COLOR);
} else {
Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, TelemetryContract.Method.INTENT, "customtab");
startIntent = getIntent();
toolbarColor = getIntent().getIntExtra(EXTRA_TOOLBAR_COLOR, DEFAULT_ACTION_BAR_COLOR);
final String host = getReferrerHost();
recordCustomTabUsage(host);
}
// Translucent color does not make sense for toolbar color. Ensure it is 0xFF.
toolbarColor = 0xFF000000 | toolbarColor;
setThemeFromToolbarColor();
mProgressView = (ProgressBar) findViewById(R.id.page_progress);
@ -106,7 +92,7 @@ public class CustomTabsActivity extends GeckoApp implements Tabs.OnTabsChangedLi
actionBarPresenter = new ActionBarPresenter(actionBar);
actionBarPresenter.displayUrlOnly(startIntent.getDataString());
actionBarPresenter.setBackgroundColor(toolbarColor, getWindow());
actionBarPresenter.setBackgroundColor(IntentUtil.getToolbarColor(startIntent), getWindow());
actionBarPresenter.setTextLongClickListener(new UrlCopyListener());
actionBar.setDisplayHomeAsUpEnabled(true);
@ -125,8 +111,8 @@ public class CustomTabsActivity extends GeckoApp implements Tabs.OnTabsChangedLi
}
private void setThemeFromToolbarColor() {
@StyleRes
int styleRes = (ColorUtil.getReadableTextColor(toolbarColor) == Color.BLACK)
final int color = ColorUtil.getReadableTextColor(IntentUtil.getToolbarColor(startIntent));
@StyleRes final int styleRes = (color == Color.BLACK)
? R.style.GeckoCustomTabs_Light
: R.style.GeckoCustomTabs;
@ -210,7 +196,6 @@ public class CustomTabsActivity extends GeckoApp implements Tabs.OnTabsChangedLi
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(SAVED_START_INTENT, startIntent);
outState.putInt(SAVED_TOOLBAR_COLOR, toolbarColor);
}
@Override

Просмотреть файл

@ -10,7 +10,9 @@ import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.support.customtabs.CustomTabsIntent;
import java.util.ArrayList;
@ -24,6 +26,10 @@ class IntentUtil {
public static final int NO_ANIMATION_RESOURCE = -1;
@VisibleForTesting
@ColorInt
protected static final int DEFAULT_ACTION_BAR_COLOR = 0xFF363b40; // default color to match design
// Hidden constant values from ActivityOptions.java
private static final String PREFIX = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
? "android:activity."
@ -66,6 +72,23 @@ class IntentUtil {
return (bundle == null) ? null : (Bitmap) bundle.getParcelable(CustomTabsIntent.KEY_ICON);
}
/**
* To extract color code from intent for top toolbar.
* It also ensure the color is not translucent.
*
* @param intent which to launch a Custom-Tabs-Activity
* @return color code in integer type.
*/
@ColorInt
static int getToolbarColor(@NonNull Intent intent) {
@ColorInt int toolbarColor = intent.getIntExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR,
DEFAULT_ACTION_BAR_COLOR);
// Translucent color does not make sense for toolbar color. Ensure it is 0xFF.
toolbarColor = 0xFF000000 | toolbarColor;
return toolbarColor;
}
/**
* To extract description from intent for Action-Button. This description is used for
* accessibility.

Просмотреть файл

@ -127,6 +127,24 @@ public class TestIntentUtil {
Assert.assertTrue(Objects.equals(intent2, intents.get(2)));
}
@Test
public void testToolbarColor() {
final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
Assert.assertEquals(IntentUtil.getToolbarColor(builder.build().intent),
IntentUtil.DEFAULT_ACTION_BAR_COLOR);
// Test red color
builder.setToolbarColor(0xFF0000);
Assert.assertEquals(IntentUtil.getToolbarColor(builder.build().intent), 0xFFFF0000);
builder.setToolbarColor(0xFFFF0000);
Assert.assertEquals(IntentUtil.getToolbarColor(builder.build().intent), 0xFFFF0000);
// Test translucent green color, it should force alpha value to be 0xFF
builder.setToolbarColor(0x0000FF00);
Assert.assertEquals(IntentUtil.getToolbarColor(builder.build().intent), 0xFF00FF00);
}
@Test
public void testMenuShareItem() {
final CustomTabsIntent.Builder builderNoShareItem = new CustomTabsIntent.Builder();