diff --git a/mobile/android/base/AwesomeBar.java b/mobile/android/base/AwesomeBar.java index dff7c832e75b..a10a96d8c349 100644 --- a/mobile/android/base/AwesomeBar.java +++ b/mobile/android/base/AwesomeBar.java @@ -56,6 +56,7 @@ public class AwesomeBar extends GeckoActivity private static final String LOGTAG = "GeckoAwesomeBar"; public static final String URL_KEY = "url"; + public static final String TAB_KEY = "tab"; public static final String CURRENT_URL_KEY = "currenturl"; public static final String TARGET_KEY = "target"; public static final String SEARCH_KEY = "search"; @@ -120,6 +121,13 @@ public class AwesomeBar extends GeckoActivity } }); } + + @Override + public void onSwitchToTab(final int tabId) { + Intent resultIntent = new Intent(); + resultIntent.putExtra(TAB_KEY, Integer.toString(tabId)); + finishWithResult(resultIntent); + } }); mGoButton.setOnClickListener(new Button.OnClickListener() { diff --git a/mobile/android/base/AwesomeBarTabs.java b/mobile/android/base/AwesomeBarTabs.java index 5b40b8edab16..e433aec9045c 100644 --- a/mobile/android/base/AwesomeBarTabs.java +++ b/mobile/android/base/AwesomeBarTabs.java @@ -42,6 +42,7 @@ public class AwesomeBarTabs extends TabHost public void onUrlOpen(String url, String title); public void onSearch(SearchEngine engine, String text); public void onEditSuggestion(String suggestion); + public void onSwitchToTab(final int tabId); } private class AwesomePagerAdapter extends PagerAdapter { diff --git a/mobile/android/base/AwesomebarResultHandler.java b/mobile/android/base/AwesomebarResultHandler.java index e755508eeab9..c91b594d70b0 100644 --- a/mobile/android/base/AwesomebarResultHandler.java +++ b/mobile/android/base/AwesomebarResultHandler.java @@ -7,6 +7,7 @@ package org.mozilla.gecko; import org.mozilla.gecko.util.ActivityResultHandler; import android.content.Intent; +import android.util.Log; class AwesomebarResultHandler implements ActivityResultHandler { private static final String LOGTAG = "GeckoAwesomebarResultHandler"; @@ -14,6 +15,12 @@ class AwesomebarResultHandler implements ActivityResultHandler { @Override public void onActivityResult(int resultCode, Intent data) { if (data != null) { + String tab = data.getStringExtra(AwesomeBar.TAB_KEY); + if (tab != null) { + Tabs.getInstance().selectTab(Integer.parseInt(tab)); + return; + } + String url = data.getStringExtra(AwesomeBar.URL_KEY); AwesomeBar.Target target = AwesomeBar.Target.valueOf(data.getStringExtra(AwesomeBar.TARGET_KEY)); String searchEngine = data.getStringExtra(AwesomeBar.SEARCH_KEY); diff --git a/mobile/android/base/Makefile.in b/mobile/android/base/Makefile.in index 140481ed5acf..c193cd202982 100644 --- a/mobile/android/base/Makefile.in +++ b/mobile/android/base/Makefile.in @@ -578,6 +578,7 @@ RES_DRAWABLE_MDPI = \ res/drawable-mdpi/ic_awesomebar_reader.png \ res/drawable-mdpi/ic_awesomebar_search.png \ res/drawable-mdpi/ic_awesomebar_star.png \ + res/drawable-mdpi/ic_awesomebar_tab.png \ res/drawable-mdpi/ic_menu_addons_filler.png \ res/drawable-mdpi/ic_menu_bookmark_add.png \ res/drawable-mdpi/ic_menu_bookmark_remove.png \ @@ -690,6 +691,7 @@ RES_DRAWABLE_HDPI = \ res/drawable-hdpi/ic_awesomebar_reader.png \ res/drawable-hdpi/ic_awesomebar_search.png \ res/drawable-hdpi/ic_awesomebar_star.png \ + res/drawable-hdpi/ic_awesomebar_tab.png \ res/drawable-hdpi/ic_menu_addons_filler.png \ res/drawable-hdpi/ic_menu_bookmark_add.png \ res/drawable-hdpi/ic_menu_bookmark_remove.png \ @@ -782,6 +784,7 @@ RES_DRAWABLE_XHDPI = \ res/drawable-xhdpi/ic_awesomebar_reader.png \ res/drawable-xhdpi/ic_awesomebar_search.png \ res/drawable-xhdpi/ic_awesomebar_star.png \ + res/drawable-xhdpi/ic_awesomebar_tab.png \ res/drawable-xhdpi/ic_menu_addons_filler.png \ res/drawable-xhdpi/ic_menu_bookmark_add.png \ res/drawable-xhdpi/ic_menu_bookmark_remove.png \ diff --git a/mobile/android/base/Tabs.java b/mobile/android/base/Tabs.java index 6152f5d23be5..12d0f338bf9c 100644 --- a/mobile/android/base/Tabs.java +++ b/mobile/android/base/Tabs.java @@ -155,6 +155,15 @@ public class Tabs implements GeckoEventListener { return count; } + public synchronized int isOpen(String url) { + for (Tab tab : mOrder) { + if (tab.getURL().equals(url)) { + return tab.getId(); + } + } + return -1; + } + // Must be synchronized to avoid racing on mContentObserver. private void lazyRegisterBookmarkObserver() { if (mContentObserver == null) { diff --git a/mobile/android/base/awesomebar/AllPagesTab.java b/mobile/android/base/awesomebar/AllPagesTab.java index fe0c3550e7b9..8cd047996c1d 100644 --- a/mobile/android/base/awesomebar/AllPagesTab.java +++ b/mobile/android/base/awesomebar/AllPagesTab.java @@ -151,6 +151,8 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener { @Override public void destroy() { + super.destroy(); + unregisterEventListener("SearchEngines:Data"); mHandler.removeMessages(MESSAGE_UPDATE_FAVICONS); @@ -321,13 +323,9 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener { @Override public void onClick() { - AwesomeBarTabs.OnUrlOpenListener listener = getUrlListener(); - if (listener == null) - return; - String url = mCursor.getString(mCursor.getColumnIndexOrThrow(URLColumns.URL)); String title = mCursor.getString(mCursor.getColumnIndexOrThrow(URLColumns.TITLE)); - listener.onUrlOpen(url, title); + sendToListener(url, title); } @Override diff --git a/mobile/android/base/awesomebar/AwesomeBarTab.java b/mobile/android/base/awesomebar/AwesomeBarTab.java index 94d7e4d9bb15..a6ad1a50585d 100644 --- a/mobile/android/base/awesomebar/AwesomeBarTab.java +++ b/mobile/android/base/awesomebar/AwesomeBarTab.java @@ -24,10 +24,11 @@ import android.view.inputmethod.InputMethodManager; import android.widget.ImageView; import android.widget.TextView; +import java.util.HashMap; + abstract public class AwesomeBarTab { abstract public String getTag(); abstract public int getTitleStringId(); - abstract public void destroy(); abstract public boolean onBackPressed(); abstract public ContextMenuSubject getSubject(ContextMenu menu, View view, ContextMenuInfo menuInfo); abstract public View getView(); @@ -41,11 +42,16 @@ abstract public class AwesomeBarTab { // FIXME: This value should probably come from a prefs key public static final int MAX_RESULTS = 100; protected Context mContext = null; + public static HashMap sOpenTabs; public AwesomeBarTab(Context context) { mContext = context; } + public void destroy() { + sOpenTabs = null; + } + public void setListTouchListener(View.OnTouchListener listener) { mListListener = listener; if (mView != null) @@ -81,6 +87,17 @@ abstract public class AwesomeBarTab { return mContentResolver; } + private HashMap getOpenTabs() { + if (sOpenTabs == null || sOpenTabs.isEmpty()) { + Iterable tabs = Tabs.getInstance().getTabsInOrder(); + sOpenTabs = new HashMap(); + for (Tab tab : tabs) { + sOpenTabs.put(tab.getURL(), tab.getId()); + } + } + return sOpenTabs; + } + protected Resources getResources() { if (mResources == null) { mResources = mContext.getResources(); @@ -95,22 +112,54 @@ abstract public class AwesomeBarTab { protected void updateTitle(TextView titleView, Cursor cursor) { int titleIndex = cursor.getColumnIndexOrThrow(URLColumns.TITLE); String title = cursor.getString(titleIndex); + String url = ""; // 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 if (TextUtils.isEmpty(title)) { int urlIndex = cursor.getColumnIndexOrThrow(URLColumns.URL); - title = cursor.getString(urlIndex); + url = cursor.getString(urlIndex); } - titleView.setText(title); + updateTitle(titleView, title, url); + } + + protected void updateTitle(TextView titleView, String title, String url) { + if (TextUtils.isEmpty(title)) { + titleView.setText(url); + } else { + titleView.setText(title); + } + } + + public void sendToListener(String url, String title) { + AwesomeBarTabs.OnUrlOpenListener listener = getUrlListener(); + if (listener == null) + return; + + Integer tabId = getOpenTabs().get(url); + if (tabId != null) { + listener.onSwitchToTab(tabId); + } else { + listener.onUrlOpen(url, title); + } } protected void updateUrl(TextView urlView, Cursor cursor) { int urlIndex = cursor.getColumnIndexOrThrow(URLColumns.URL); String url = cursor.getString(urlIndex); + updateUrl(urlView, url); + } - urlView.setText(url); + protected void updateUrl(TextView urlView, String url) { + Integer tabId = getOpenTabs().get(url); + if (tabId != null) { + urlView.setText(R.string.awesomebar_switch_to_tab); + urlView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_awesomebar_tab, 0, 0, 0); + } else { + urlView.setText(url); + urlView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); + } } protected boolean hideSoftInput(View view) { diff --git a/mobile/android/base/awesomebar/BookmarksTab.java b/mobile/android/base/awesomebar/BookmarksTab.java index 13fa17805c27..19691c1c515d 100644 --- a/mobile/android/base/awesomebar/BookmarksTab.java +++ b/mobile/android/base/awesomebar/BookmarksTab.java @@ -95,6 +95,7 @@ public class BookmarksTab extends AwesomeBarTab { @Override public void destroy() { + super.destroy(); // Can't use getters for adapter. It will create one if null. if (mCursorAdapter != null && mView != null) { ListView list = (ListView)mView; @@ -203,18 +204,13 @@ public class BookmarksTab extends AwesomeBarTab { } // Otherwise, just open the URL - AwesomeBarTabs.OnUrlOpenListener listener = getUrlListener(); - if (listener == null) { - return; - } - String url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL)); String title = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE)); long parentId = cursor.getLong(cursor.getColumnIndexOrThrow(Bookmarks.PARENT)); if (parentId == Bookmarks.FIXED_READING_LIST_ID) { url = ReaderModeUtils.getAboutReaderForUrl(url, true); } - listener.onUrlOpen(url, title); + sendToListener(url, title); } private class BookmarksListAdapter extends SimpleCursorAdapter { diff --git a/mobile/android/base/awesomebar/HistoryTab.java b/mobile/android/base/awesomebar/HistoryTab.java index b9623b6bfd05..cd4375b9e4cc 100644 --- a/mobile/android/base/awesomebar/HistoryTab.java +++ b/mobile/android/base/awesomebar/HistoryTab.java @@ -124,6 +124,8 @@ public class HistoryTab extends AwesomeBarTab { @Override public void destroy() { + super.destroy(); + if (mContentObserver != null) BrowserDB.unregisterContentObserver(getContentResolver(), mContentObserver); } @@ -183,11 +185,8 @@ public class HistoryTab extends AwesomeBarTab { String title = (String) historyItem.get(URLColumns.TITLE); String url = (String) historyItem.get(URLColumns.URL); - if (TextUtils.isEmpty(title)) - title = url; - - viewHolder.titleView.setText(title); - viewHolder.urlView.setText(url); + updateTitle(viewHolder.titleView, title, url); + updateUrl(viewHolder.urlView, url); byte[] b = (byte[]) historyItem.get(URLColumns.FAVICON); Bitmap favicon = null; @@ -412,9 +411,7 @@ public class HistoryTab extends AwesomeBarTab { String url = (String) historyItem.get(URLColumns.URL); String title = (String) historyItem.get(URLColumns.TITLE); - AwesomeBarTabs.OnUrlOpenListener listener = getUrlListener(); - if (!TextUtils.isEmpty(url) && listener != null) - listener.onUrlOpen(url, title); + sendToListener(url, title); return true; } diff --git a/mobile/android/base/locales/en-US/android_strings.dtd b/mobile/android/base/locales/en-US/android_strings.dtd index e40dab6cc745..3051bd51ec42 100644 --- a/mobile/android/base/locales/en-US/android_strings.dtd +++ b/mobile/android/base/locales/en-US/android_strings.dtd @@ -12,6 +12,7 @@ + diff --git a/mobile/android/base/resources/drawable-hdpi/ic_awesomebar_tab.png b/mobile/android/base/resources/drawable-hdpi/ic_awesomebar_tab.png new file mode 100644 index 000000000000..cfbabf863c67 Binary files /dev/null and b/mobile/android/base/resources/drawable-hdpi/ic_awesomebar_tab.png differ diff --git a/mobile/android/base/resources/drawable-mdpi/ic_awesomebar_tab.png b/mobile/android/base/resources/drawable-mdpi/ic_awesomebar_tab.png new file mode 100644 index 000000000000..514d02acee7c Binary files /dev/null and b/mobile/android/base/resources/drawable-mdpi/ic_awesomebar_tab.png differ diff --git a/mobile/android/base/resources/drawable-xhdpi/ic_awesomebar_tab.png b/mobile/android/base/resources/drawable-xhdpi/ic_awesomebar_tab.png new file mode 100644 index 000000000000..2d5e6c70236b Binary files /dev/null and b/mobile/android/base/resources/drawable-xhdpi/ic_awesomebar_tab.png differ diff --git a/mobile/android/base/strings.xml.in b/mobile/android/base/strings.xml.in index b74efb606614..84609f35a348 100644 --- a/mobile/android/base/strings.xml.in +++ b/mobile/android/base/strings.xml.in @@ -26,6 +26,7 @@ &awesomebar_all_pages_title; &awesomebar_bookmarks_title; &awesomebar_history_title; + &awesomebar_switch_to_tab; &crash_reporter_title; &crash_message2;