Bug 1351605 - Get rid of color variable in activity
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
--- a/mobile/android/base/java/org/mozilla/gecko/customtabs/CustomTabsActivity.java
+++ b/mobile/android/base/java/org/mozilla/gecko/customtabs/CustomTabsActivity.java
@@ -49,69 +49,55 @@ import org.mozilla.gecko.menu.GeckoMenuI
import org.mozilla.gecko.util.Clipboard;
import org.mozilla.gecko.util.ColorUtil;
import org.mozilla.gecko.util.IntentUtils;
import org.mozilla.gecko.widget.GeckoPopupMenu;
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;
private ActionBarPresenter actionBarPresenter;
private ProgressBar mProgressView;
// 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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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);
final Toolbar toolbar = (Toolbar) findViewById(R.id.actionbar);
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
bindNavigationCallback(toolbar);
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);
Tabs.registerOnTabsChangedListener(this);
}
private void recordCustomTabUsage(final String host) {
final GeckoBundle data = new GeckoBundle(1);
@@ -120,18 +106,18 @@ public class CustomTabsActivity extends
} else {
data.putString("client", "unknown");
}
// Pass a message to Gecko to send Telemetry data
EventDispatcher.getInstance().dispatch("Telemetry:CustomTabsPing", data);
}
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;
setTheme(styleRes);
}
// Bug 1329145: 3rd party app could specify customized exit-animation to this activity.
// Activity.overridePendingTransition will invoke getPackageName to retrieve that animation resource.
@@ -205,17 +191,16 @@ public class CustomTabsActivity extends
updateMenuItemForward();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(SAVED_START_INTENT, startIntent);
- outState.putInt(SAVED_TOOLBAR_COLOR, toolbarColor);
}
@Override
public void onResume() {
if (lastSelectedTabId >= 0) {
final Tabs tabs = Tabs.getInstance();
final Tab tab = tabs.getTab(lastSelectedTabId);
if (tab == null) {
--- a/mobile/android/base/java/org/mozilla/gecko/customtabs/IntentUtil.java
+++ b/mobile/android/base/java/org/mozilla/gecko/customtabs/IntentUtil.java
@@ -5,30 +5,36 @@
package org.mozilla.gecko.customtabs;
import android.app.PendingIntent;
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;
import java.util.List;
/**
* A utility class for CustomTabsActivity to extract information from intent.
* For example, this class helps to extract exit-animation resource id.
*/
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."
: "android:";
private static final String KEY_PACKAGE_NAME = PREFIX + "packageName";
private static final String KEY_ANIM_ENTER_RES_ID = PREFIX + "animEnterRes";
private static final String KEY_ANIM_EXIT_RES_ID = PREFIX + "animExitRes";
@@ -62,16 +68,33 @@ class IntentUtil {
* @return bitmap icon, if any. Otherwise, null.
*/
static Bitmap getActionButtonIcon(@NonNull Intent intent) {
final Bundle bundle = getActionButtonBundle(intent);
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.
*
* @param intent which to launch a Custom-Tabs-Activity
* @return description, if any. Otherwise, null.
*/
static String getActionButtonDescription(@NonNull Intent intent) {
final Bundle bundle = getActionButtonBundle(intent);
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/customtabs/TestIntentUtil.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/customtabs/TestIntentUtil.java
@@ -123,16 +123,34 @@ public class TestIntentUtil {
Assert.assertEquals("Label 1", titles.get(1));
Assert.assertEquals("Label 2", titles.get(2));
Assert.assertTrue(Objects.equals(intent0, intents.get(0)));
Assert.assertTrue(Objects.equals(intent1, intents.get(1)));
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();
Assert.assertFalse(IntentUtil.hasShareItem(builderNoShareItem.build().intent));
final CustomTabsIntent.Builder builderHasShareItem = new CustomTabsIntent.Builder();
builderHasShareItem.addDefaultShareMenuItem();
Assert.assertTrue(IntentUtil.hasShareItem(builderHasShareItem.build().intent));
}