Try to reuse tabs with matching urls to avoid loading a new page.
If there is no matching application tab, try to find a regular tab (created in
the browser) with a matching url. This avoids opening a new tab and loading a
new page for a page that has already been loaded.
TODO: There is a ton of duplicate code around animations to/from the tab
overview. It is a massive amount of cruft and I really want to rewrite it all in
a separate change.
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index 585b8ff..1bccd41 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -891,7 +891,7 @@
(flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
final String appId =
intent.getStringExtra(Browser.EXTRA_APPLICATION_ID);
- final TabControl.Tab appTab = mTabControl.getTabFromId(appId);
+ TabControl.Tab appTab = mTabControl.getTabFromId(appId);
if (appTab != null) {
Log.i(LOGTAG, "Reusing tab for " + appId);
// Dismiss the subwindow if applicable.
@@ -922,12 +922,32 @@
}
}
return;
+ } else {
+ // No matching application tab, try to find a regular tab
+ // with a matching url.
+ appTab = mTabControl.findUnusedTabWithUrl(urlData.mUrl);
+ if (appTab != null) {
+ if (current != appTab) {
+ // Use EMPTY_URL_DATA so we do not reload the page
+ showTab(appTab, EMPTY_URL_DATA);
+ } else {
+ if (mTabOverview != null && mAnimationCount == 0) {
+ sendAnimateFromOverview(appTab, false,
+ EMPTY_URL_DATA, TAB_OVERVIEW_DELAY,
+ null);
+ }
+ // Don't do anything here since we are on the
+ // correct page.
+ }
+ } else {
+ // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
+ // will be opened in a new tab unless we have reached
+ // MAX_TABS. Then the url will be opened in the current
+ // tab. If a new tab is created, it will have "true" for
+ // exit on close.
+ openTabAndShow(urlData, null, true, appId);
+ }
}
- // if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url will be
- // opened in a new tab unless we have reached MAX_TABS. Then the
- // url will be opened in the current tab. If a new tab is
- // created, it will have "true" for exit on close.
- openTabAndShow(urlData, null, true, appId);
} else {
if ("about:debug".equals(urlData.mUrl)) {
mSettings.toggleDebugSettings();
diff --git a/src/com/android/browser/TabControl.java b/src/com/android/browser/TabControl.java
index 274598a..1c8b7c8 100644
--- a/src/com/android/browser/TabControl.java
+++ b/src/com/android/browser/TabControl.java
@@ -804,6 +804,45 @@
return null;
}
+ // This method checks if a non-app tab (one created within the browser)
+ // matches the given url.
+ private boolean tabMatchesUrl(Tab t, String url) {
+ if (t.mAppId != null) {
+ return false;
+ } else if (t.mMainView == null) {
+ return false;
+ } else if (url.equals(t.mMainView.getUrl()) ||
+ url.equals(t.mMainView.getOriginalUrl())) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Return the tab that has no app id associated with it and the url of the
+ * tab matches the given url.
+ * @param url The url to search for.
+ */
+ Tab findUnusedTabWithUrl(String url) {
+ if (url == null) {
+ return null;
+ }
+ // Check the current tab first.
+ Tab t = getCurrentTab();
+ if (t != null && tabMatchesUrl(t, url)) {
+ return t;
+ }
+ // Now check all the rest.
+ final int size = getTabCount();
+ for (int i = 0; i < size; i++) {
+ t = getTab(i);
+ if (tabMatchesUrl(t, url)) {
+ return t;
+ }
+ }
+ return null;
+ }
+
/**
* Recreate the main WebView of the given tab. Returns true if the WebView
* was deleted.