diff --git a/mobile/android/base/RemoteTabsExpandableListAdapter.java b/mobile/android/base/RemoteTabsExpandableListAdapter.java index 8a117e558ef..799c4603762 100644 --- a/mobile/android/base/RemoteTabsExpandableListAdapter.java +++ b/mobile/android/base/RemoteTabsExpandableListAdapter.java @@ -9,6 +9,7 @@ import java.util.List; import org.mozilla.gecko.TabsAccessor.RemoteClient; import org.mozilla.gecko.TabsAccessor.RemoteTab; +import org.mozilla.gecko.home.TwoLinePageRow; import android.content.Context; import android.text.TextUtils; @@ -151,11 +152,18 @@ public class RemoteTabsExpandableListAdapter extends BaseExpandableListAdapter { final RemoteClient client = clients.get(groupPosition); final RemoteTab tab = client.tabs.get(childPosition); - final TextView titleView = (TextView) view.findViewById(R.id.title); - titleView.setText(TextUtils.isEmpty(tab.title) ? tab.url : tab.title); + // The view is a TwoLinePageRow only for some of our child views: it's + // present for the home panel children and not for the tabs tray + // children. Therefore, we must handle one case manually. + if (view instanceof TwoLinePageRow) { + ((TwoLinePageRow) view).update(tab.title, tab.url); + } else { + final TextView titleView = (TextView) view.findViewById(R.id.title); + titleView.setText(TextUtils.isEmpty(tab.title) ? tab.url : tab.title); - final TextView urlView = (TextView) view.findViewById(R.id.url); - urlView.setText(tab.url); + final TextView urlView = (TextView) view.findViewById(R.id.url); + urlView.setText(tab.url); + } return view; } diff --git a/mobile/android/base/home/TwoLinePageRow.java b/mobile/android/base/home/TwoLinePageRow.java index 07014a51e06..7434e5c75cb 100644 --- a/mobile/android/base/home/TwoLinePageRow.java +++ b/mobile/android/base/home/TwoLinePageRow.java @@ -50,6 +50,11 @@ public class TwoLinePageRow extends LinearLayout this.view = new WeakReference(view); } + /** + * Update this row's favicon. + *

+ * This method is always invoked on the UI thread. + */ @Override public void onFaviconLoaded(String url, String faviconURL, Bitmap favicon) { FaviconView v = view.get(); @@ -96,24 +101,49 @@ public class TwoLinePageRow extends LinearLayout @Override protected void onAttachedToWindow() { + super.onAttachedToWindow(); + Tabs.registerOnTabsChangedListener(this); } @Override protected void onDetachedFromWindow() { + super.onDetachedFromWindow(); + // Tabs' listener array is safe to modify during use: its // iteration pattern is based on snapshots. Tabs.unregisterOnTabsChangedListener(this); } + /** + * Update the row in response to a tab change event. + *

+ * This method is always invoked on the UI thread. + */ @Override public void onTabChanged(final Tab tab, final Tabs.TabEvents msg, final Object data) { - switch(msg) { + // Carefully check if this tab event is relevant to this row. + final String pageUrl = mPageUrl; + if (pageUrl == null) { + return; + } + final String tabUrl; + if (tab == null) { + return; + } + tabUrl = tab.getURL(); + if (!pageUrl.equals(tabUrl)) { + return; + } + + switch (msg) { case ADDED: case CLOSED: case LOCATION_CHANGE: updateDisplayedUrl(); break; + default: + break; } } @@ -181,32 +211,29 @@ public class TwoLinePageRow extends LinearLayout mShowIcons = showIcons; } - public void updateFromCursor(Cursor cursor) { - if (cursor == null) { - return; - } - - int titleIndex = cursor.getColumnIndexOrThrow(URLColumns.TITLE); - final String title = cursor.getString(titleIndex); - - int urlIndex = cursor.getColumnIndexOrThrow(URLColumns.URL); - final String url = cursor.getString(urlIndex); + /** + * Update the data displayed by this row. + *

+ * This method must be invoked on the UI thread. + * + * @param title to display. + * @param url to display. + */ + public void update(String title, String url) { + update(title, url, 0); + } + protected void update(String title, String url, long bookmarkId) { if (mShowIcons) { - final int bookmarkIdIndex = cursor.getColumnIndex(Combined.BOOKMARK_ID); - if (bookmarkIdIndex != -1) { - final long bookmarkId = cursor.getLong(bookmarkIdIndex); - - // The bookmark id will be 0 (null in database) when the url - // is not a bookmark. - if (bookmarkId == 0) { - setPageTypeIcon(NO_ICON); - } else { - setPageTypeIcon(R.drawable.ic_url_bar_star); - } - } else { + // The bookmark id will be 0 (null in database) when the url + // is not a bookmark. + if (bookmarkId == 0) { setPageTypeIcon(NO_ICON); + } else { + setPageTypeIcon(R.drawable.ic_url_bar_star); } + } else { + setPageTypeIcon(NO_ICON); } // Use the URL instead of an empty title for consistency with the normal URL @@ -220,8 +247,38 @@ public class TwoLinePageRow extends LinearLayout // Blank the Favicon, so we don't show the wrong Favicon if we scroll and miss DB. mFavicon.clearImage(); + Favicons.cancelFaviconLoad(mLoadFaviconJobId); mLoadFaviconJobId = Favicons.getSizedFaviconForPageFromLocal(getContext(), url, mFaviconListener); updateDisplayedUrl(url); } + + /** + * Update the data displayed by this row. + *

+ * This method must be invoked on the UI thread. + * + * @param cursor to extract data from. + */ + public void updateFromCursor(Cursor cursor) { + if (cursor == null) { + return; + } + + int titleIndex = cursor.getColumnIndexOrThrow(URLColumns.TITLE); + final String title = cursor.getString(titleIndex); + + int urlIndex = cursor.getColumnIndexOrThrow(URLColumns.URL); + final String url = cursor.getString(urlIndex); + + final long bookmarkId; + final int bookmarkIdIndex = cursor.getColumnIndex(Combined.BOOKMARK_ID); + if (bookmarkIdIndex != -1) { + bookmarkId = cursor.getLong(bookmarkIdIndex); + } else { + bookmarkId = 0; + } + + update(title, url, bookmarkId); + } }