Bug 1279278 - Use domain name as a Toolbar title and behave correctly when Toolbar navigation is used. r=sebastian
authorDylan Roeh <droeh@mozilla.com>
Mon, 07 Nov 2016 09:21:23 -0600
changeset 348131 1c529127c7a4f0e69143110bb631f23cfc027895
parent 348130 d18a718eb96f8418438556121661c0005135e511
child 348132 4fcbd5540284da08fbee7031ac7c1717bf9c715b
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
bugs1279278
milestone52.0a1
Bug 1279278 - Use domain name as a Toolbar title and behave correctly when Toolbar navigation is used. 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
@@ -1,58 +1,124 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.customtabs;
 
-import android.graphics.Color;
+import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
 import android.support.v7.app.ActionBar;
 import android.support.v7.widget.Toolbar;
+import android.text.TextUtils;
+import android.util.Log;
 import android.view.MenuItem;
+import android.view.View;
 import android.view.Window;
 import android.view.WindowManager;
+import android.widget.TextView;
 
+import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.GeckoApp;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Tab;
 import org.mozilla.gecko.Tabs;
 import org.mozilla.gecko.util.ColorUtil;
 import org.mozilla.gecko.util.GeckoRequest;
 import org.mozilla.gecko.util.NativeJSObject;
 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 {
+public class CustomTabsActivity extends GeckoApp implements Tabs.OnTabsChangedListener {
+    private static final String LOGTAG = "CustomTabsActivity";
     private static final int NO_COLOR = -1;
     private Toolbar toolbar;
 
+    private ActionBar actionBar;
+    private int tabId = -1;
+    private boolean useDomainTitle = true;
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        toolbar = (Toolbar) findViewById(R.id.toolbar);
+        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
         updateActionBarWithToolbar(toolbar);
-        updateToolbarColor();
+        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();
+        updateToolbarColor(toolbar);
+
+        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                onBackPressed();
+            }
+        });
+
+        Tabs.registerOnTabsChangedListener(this);
+    }
+
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        Tabs.unregisterOnTabsChangedListener(this);
     }
 
     @Override
     public int getLayout() {
         return R.layout.customtabs_activity;
     }
 
     @Override
     protected void onDone() {
         finish();
     }
 
+    @Override
+    public void onTabChanged(Tab tab, Tabs.TabEvents msg, String data) {
+        if (tab == null) {
+            return;
+        }
+
+        if (tabId >= 0 && tab.getId() != tabId) {
+            return;
+        }
+
+        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);
+            } else {
+                actionBar.setTitle(title);
+            }
+        }
+    }
+
     public boolean onOptionsItemSelected(MenuItem item) {
         switch (item.getItemId()) {
             case android.R.id.home:
                 finish();
                 return true;
         }
         return super.onOptionsItemSelected(item);
     }
@@ -60,17 +126,17 @@ public class CustomTabsActivity extends 
     private void updateActionBarWithToolbar(final Toolbar toolbar) {
         setSupportActionBar(toolbar);
         final ActionBar ab = getSupportActionBar();
         if (ab != null) {
             ab.setDisplayHomeAsUpEnabled(true);
         }
     }
 
-    private void updateToolbarColor() {
+    private void updateToolbarColor(final Toolbar toolbar) {
         final int color = getIntent().getIntExtra(EXTRA_TOOLBAR_COLOR, NO_COLOR);
         if (color == NO_COLOR) {
             return;
         }
         toolbar.setBackgroundColor(color);
         final Window window = getWindow();
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);