Bug 897247 - Update "Switch to tab" UI when background tabs change. r=wesj
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Tue, 06 Aug 2013 16:19:42 -0700
changeset 143500 847f396b0dded3e3f807f050985fbe74ff8897b9
parent 143499 e5b2a760d5d079b031b4f9ec9a37947390d19555
child 143501 e979b2ee0d42b888050a4793d5501ac31493cef2
push id25130
push userlrocha@mozilla.com
push dateWed, 21 Aug 2013 09:41:27 +0000
treeherdermozilla-central@b2486721572e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswesj
bugs897247
milestone25.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 897247 - Update "Switch to tab" UI when background tabs change. r=wesj
mobile/android/base/home/TwoLinePageRow.java
--- a/mobile/android/base/home/TwoLinePageRow.java
+++ b/mobile/android/base/home/TwoLinePageRow.java
@@ -2,43 +2,49 @@
  * 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.home;
 
 import org.mozilla.gecko.Favicons;
 import org.mozilla.gecko.R;
+import org.mozilla.gecko.Tab;
 import org.mozilla.gecko.Tabs;
 import org.mozilla.gecko.db.BrowserContract.Combined;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
 import org.mozilla.gecko.gfx.BitmapUtils;
+import org.mozilla.gecko.util.ThreadUtils;
 import org.mozilla.gecko.widget.FaviconView;
 
 import android.content.Context;
 import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.view.Gravity;
 import android.view.LayoutInflater;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
-public class TwoLinePageRow extends LinearLayout {
+public class TwoLinePageRow extends LinearLayout
+                            implements Tabs.OnTabsChangedListener {
     private static final int NO_ICON = 0;
 
     private final TextView mTitle;
     private final TextView mUrl;
     private final FaviconView mFavicon;
 
     private int mUrlIconId;
     private int mBookmarkIconId;
     private boolean mShowIcons;
 
+    // The URL for the page corresponding to this view.
+    private String mPageUrl;
+
     public TwoLinePageRow(Context context) {
         this(context, null);
     }
 
     public TwoLinePageRow(Context context, AttributeSet attrs) {
         super(context, attrs);
 
         setGravity(Gravity.CENTER_VERTICAL);
@@ -48,16 +54,44 @@ public class TwoLinePageRow extends Line
         mShowIcons = true;
 
         LayoutInflater.from(context).inflate(R.layout.two_line_page_row, this);
         mTitle = (TextView) findViewById(R.id.title);
         mUrl = (TextView) findViewById(R.id.url);
         mFavicon = (FaviconView) findViewById(R.id.favicon);
     }
 
+    @Override
+    protected void onAttachedToWindow() {
+        Tabs.registerOnTabsChangedListener(this);
+    }
+
+    @Override
+    protected void onDetachedFromWindow() {
+        // Delay removing the listener to avoid modifying mTabsChangedListeners
+        // while notifyListeners is iterating through the array.
+        ThreadUtils.postToUiThread(new Runnable() {
+            @Override
+            public void run() {
+                Tabs.unregisterOnTabsChangedListener(TwoLinePageRow.this);
+            }
+        });
+    }
+
+    @Override
+    public void onTabChanged(final Tab tab, final Tabs.TabEvents msg, final Object data) {
+        switch(msg) {
+            case ADDED:
+            case CLOSED:
+            case LOCATION_CHANGE:
+                updateDisplayedUrl();
+                break;
+        }
+    }
+
     private void setTitle(String title) {
         mTitle.setText(title);
     }
 
     private void setUrl(String url) {
         mUrl.setText(url);
     }
 
@@ -82,16 +116,38 @@ public class TwoLinePageRow extends Line
         if (mBookmarkIconId == bookmarkIconId) {
             return;
         }
 
         mBookmarkIconId = bookmarkIconId;
         mUrl.setCompoundDrawablesWithIntrinsicBounds(mUrlIconId, 0, mBookmarkIconId, 0);
     }
 
+    /**
+     * Stores the page URL, so that we can use it to replace "Switch to tab" if the open
+     * tab changes or is closed.
+     */
+    private void updateDisplayedUrl(String url) {
+        mPageUrl = url;
+        updateDisplayedUrl();
+    }
+
+    /**
+     * Replaces the page URL with "Switch to tab" if there is already a tab open with that URL.
+     */
+    private void updateDisplayedUrl() {
+        if (!mShowIcons || !Tabs.getInstance().hasUrl(mPageUrl)) {
+            setUrl(mPageUrl);
+            setUrlIcon(NO_ICON);
+        } else {
+            setUrl(R.string.switch_to_tab);
+            setUrlIcon(R.drawable.ic_url_bar_tab);
+        }
+    }
+
     public void setShowIcons(boolean showIcons) {
         mShowIcons = showIcons;
     }
 
     public void updateFromCursor(Cursor cursor) {
         if (cursor == null) {
             return;
         }
@@ -101,24 +157,17 @@ public class TwoLinePageRow extends Line
 
         int urlIndex = cursor.getColumnIndexOrThrow(URLColumns.URL);
         final String url = cursor.getString(urlIndex);
 
         // Use the URL instead of an empty title for consistency with the normal URL
         // bar view - this is the equivalent of getDisplayTitle() in Tab.java
         setTitle(TextUtils.isEmpty(title) ? url : title);
 
-        // Update the url with "Switch to tab" if needed.
-        if (!mShowIcons || !Tabs.getInstance().hasUrl(url)) {
-            setUrl(url);
-            setUrlIcon(NO_ICON);
-        } else {
-            setUrl(R.string.switch_to_tab);
-            setUrlIcon(R.drawable.ic_url_bar_tab);
-        }
+        updateDisplayedUrl(url);
 
         int faviconIndex = cursor.getColumnIndex(URLColumns.FAVICON);
         if (faviconIndex != -1) {
             byte[] b = cursor.getBlob(faviconIndex);
 
             Bitmap favicon = null;
             if (b != null) {
                 Bitmap bitmap = BitmapUtils.decodeByteArray(b);