Bug 1315348 - Handle orientation changes in CustomTabsActivity without losing toolbar color or title. r=sebastian
authorDylan Roeh <droeh@mozilla.com>
Tue, 08 Nov 2016 11:03:41 -0600
changeset 348402 0d7eda3bfe639fbb69048e7f9ff8e0eca676c080
parent 348401 d2baab775c7ee6b8f6f38d835ea10f6bdf323ffa
child 348403 d6a3f82cfc8107fc5cba91a89e067e88773ef027
push id10298
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:33:03 +0000
treeherdermozilla-aurora@7e29173b1641 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1315348
milestone52.0a1
Bug 1315348 - Handle orientation changes in CustomTabsActivity without losing toolbar color or title. r=sebastian
mobile/android/base/java/org/mozilla/gecko/customtabs/CustomTabsActivity.java
--- a/mobile/android/base/java/org/mozilla/gecko/customtabs/CustomTabsActivity.java
+++ b/mobile/android/base/java/org/mozilla/gecko/customtabs/CustomTabsActivity.java
@@ -30,42 +30,57 @@ import org.mozilla.gecko.util.NativeJSOb
 import org.mozilla.gecko.util.ThreadUtils;
 
 import java.lang.reflect.Field;
 
 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_TOOLBAR_COLOR = "SavedToolbarColor";
+    private static final String SAVED_TOOLBAR_TITLE = "SavedToolbarTitle";
     private static final int NO_COLOR = -1;
     private Toolbar toolbar;
 
     private ActionBar actionBar;
     private int tabId = -1;
     private boolean useDomainTitle = true;
 
+    private int toolbarColor;
+    private String toolbarTitle;
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+
+        if (savedInstanceState != null) {
+            toolbarColor = savedInstanceState.getInt(SAVED_TOOLBAR_COLOR, NO_COLOR);
+            toolbarTitle = savedInstanceState.getString(SAVED_TOOLBAR_TITLE, AppConstants.MOZ_APP_BASENAME);
+        } else {
+            toolbarColor = NO_COLOR;
+            toolbarTitle = AppConstants.MOZ_APP_BASENAME;
+        }
+
         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
         updateActionBarWithToolbar(toolbar);
         try {
             // Since we don't create the Toolbar's TextView ourselves, this seems
             // to be the only way of changing the ellipsize setting.
             Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
             f.setAccessible(true);
             TextView textView = (TextView) f.get(toolbar);
             textView.setEllipsize(TextUtils.TruncateAt.START);
         } catch (Exception e) {
             // If we can't ellipsize at the start of the title, we shouldn't display the host
             // so as to avoid displaying a misleadingly truncated host.
             Log.w(LOGTAG, "Failed to get Toolbar TextView, using default title.");
             useDomainTitle = false;
         }
         actionBar = getSupportActionBar();
+        actionBar.setTitle(toolbarTitle);
         updateToolbarColor(toolbar);
 
         toolbar.setNavigationOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 onBackPressed();
             }
         });
@@ -102,23 +117,32 @@ public class CustomTabsActivity extends 
         if (msg == Tabs.TabEvents.LOCATION_CHANGE) {
             tabId = tab.getId();
             final Uri uri = Uri.parse(tab.getURL());
             String title = null;
             if (uri != null) {
                 title = uri.getHost();
             }
             if (!useDomainTitle || title == null || title.isEmpty()) {
-                actionBar.setTitle(AppConstants.MOZ_APP_BASENAME);
+                toolbarTitle = AppConstants.MOZ_APP_BASENAME;
             } else {
-                actionBar.setTitle(title);
+                toolbarTitle = title;
             }
+            actionBar.setTitle(toolbarTitle);
         }
     }
 
+    @Override
+    protected void onSaveInstanceState(Bundle outState) {
+        super.onSaveInstanceState(outState);
+
+        outState.putInt(SAVED_TOOLBAR_COLOR, toolbarColor);
+        outState.putString(SAVED_TOOLBAR_TITLE, toolbarTitle);
+    }
+
     public boolean onOptionsItemSelected(MenuItem item) {
         switch (item.getItemId()) {
             case android.R.id.home:
                 finish();
                 return true;
         }
         return super.onOptionsItemSelected(item);
     }
@@ -127,20 +151,24 @@ public class CustomTabsActivity extends 
         setSupportActionBar(toolbar);
         final ActionBar ab = getSupportActionBar();
         if (ab != null) {
             ab.setDisplayHomeAsUpEnabled(true);
         }
     }
 
     private void updateToolbarColor(final Toolbar toolbar) {
-        final int color = getIntent().getIntExtra(EXTRA_TOOLBAR_COLOR, NO_COLOR);
-        if (color == NO_COLOR) {
-            return;
+        if (toolbarColor == NO_COLOR) {
+            final int color = getIntent().getIntExtra(EXTRA_TOOLBAR_COLOR, NO_COLOR);
+            if (color == NO_COLOR) {
+                return;
+            }
+            toolbarColor = color;
         }
-        toolbar.setBackgroundColor(color);
+
+        toolbar.setBackgroundColor(toolbarColor);
         final Window window = getWindow();
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
-            window.setStatusBarColor(ColorUtil.darken(color, 0.25));
+            window.setStatusBarColor(ColorUtil.darken(toolbarColor, 0.25));
         }
     }
 }