refactor open tab code

Change-Id: Ibcffa5a55b2b7dcf56174f9dd916a565b31506e9
diff --git a/src/com/android/browser/ActiveTabsPage.java b/src/com/android/browser/ActiveTabsPage.java
index 52d943f..23b313a 100644
--- a/src/com/android/browser/ActiveTabsPage.java
+++ b/src/com/android/browser/ActiveTabsPage.java
@@ -75,7 +75,7 @@
         if (v == mNewTab) {
             mController.openTabToHomePage();
         } else if (v == mNewIncognitoTab) {
-            mController.openIncognitoTab();
+            mController.openTab(null, true, true, false);
         }
         mController.removeActiveTabsPage(false);
     }
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index d1e1b0a..0f967ce 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -309,17 +309,15 @@
             // invoked to view the content by another application. In this case,
             // the tab will be close when exit.
             UrlData urlData = mIntentHandler.getUrlDataFromIntent(intent);
-
-            String action = intent.getAction();
-            final Tab t = mTabControl.createNewTab(
-                    (Intent.ACTION_VIEW.equals(action) &&
-                    intent.getData() != null)
-                    || RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS
-                    .equals(action),
-                    intent.getStringExtra(Browser.EXTRA_APPLICATION_ID),
-                    urlData.mUrl, false);
-            addTab(t);
-            setActiveTab(t);
+            Tab t = null;
+            if (urlData.isEmpty()) {
+                t = openTabToHomePage();
+            } else {
+                t = openTab(urlData);
+            }
+            if (t != null) {
+                t.setAppId(intent.getStringExtra(Browser.EXTRA_APPLICATION_ID));
+            }
             WebView webView = t.getWebView();
             if (extra != null) {
                 int scale = extra.getInt(Browser.INITIAL_ZOOM_LEVEL, 0);
@@ -327,15 +325,6 @@
                     webView.setInitialScale(scale);
                 }
             }
-
-            if (urlData.isEmpty()) {
-                loadUrl(webView, mSettings.getHomePage());
-            } else {
-                // monkey protection against delayed start
-                if (t != null) {
-                    loadUrlDataIn(t, urlData);
-                }
-            }
         } else {
             mTabControl.restoreState(icicle, currentTab, restoreIncognitoTabs,
                     mUi.needsRestoreAllTabs());
@@ -491,7 +480,12 @@
                                 break;
                             case R.id.open_newtab_context_menu_id:
                                 final Tab parent = mTabControl.getCurrentTab();
-                                final Tab newTab = openTab(parent, url, false);
+                                final Tab newTab =
+                                        openTab(url,
+                                                (parent != null)
+                                                && parent.isPrivateBrowsingEnabled(),
+                                                !mSettings.openInBackground(),
+                                                true);
                                 if (newTab != null && newTab != parent) {
                                     parent.addChildTab(newTab);
                                 }
@@ -1189,7 +1183,11 @@
         removeComboView();
         if (!TextUtils.isEmpty(url)) {
             if (newTab) {
-                openTab(mTabControl.getCurrentTab(), url, false);
+                final Tab parent = mTabControl.getCurrentTab();
+                openTab(url,
+                        (parent != null) && parent.isPrivateBrowsingEnabled(),
+                        !mSettings.openInBackground(),
+                        true);
             } else {
                 final Tab currentTab = mTabControl.getCurrentTab();
                 dismissSubWindow(currentTab);
@@ -1390,8 +1388,12 @@
                                     @Override
                                     public boolean onMenuItemClick(MenuItem item) {
                                         final Tab parent = mTabControl.getCurrentTab();
-                                        final Tab newTab = openTab(parent,
-                                                extra, false);
+                                        final Tab newTab =
+                                                openTab(extra,
+                                                        (parent != null)
+                                                        && parent.isPrivateBrowsingEnabled(),
+                                                        !mSettings.openInBackground(),
+                                                        true);
                                         if (newTab != parent) {
                                             parent.addChildTab(newTab);
                                         }
@@ -1544,7 +1546,7 @@
                 break;
 
             case R.id.incognito_menu_id:
-                openIncognitoTab();
+                openTab(null, true, true, false);
                 break;
 
             case R.id.goto_menu_id:
@@ -2196,91 +2198,55 @@
         }
     }
 
+    // open a non inconito tab with the given url data
+    // and set as active tab
+    public Tab openTab(UrlData urlData) {
+        Tab tab = createNewTab(false, true, true);
+        if ((tab != null) && !urlData.isEmpty()) {
+            loadUrlDataIn(tab, urlData);
+        }
+        return tab;
+    }
+
     @Override
     public Tab openTabToHomePage() {
-        // check for max tabs
-        if (mTabControl.canCreateNewTab()) {
-            return openTabAndShow(null, new UrlData(mSettings.getHomePage()),
-                    false, null);
-        } else {
-            mUi.showMaxTabsWarning();
-            return null;
-        }
-    }
-
-    protected Tab openTab(Tab parent, String url, boolean forceForeground) {
-        if (mSettings.openInBackground() && !forceForeground) {
-            Tab tab = mTabControl.createNewTab(false, null, null,
-                    (parent != null) && parent.isPrivateBrowsingEnabled());
-            if (tab != null) {
-                addTab(tab);
-                WebView view = tab.getWebView();
-                loadUrl(view, url);
-            }
-            return tab;
-        } else {
-            return openTabAndShow(parent, new UrlData(url), false, null);
-        }
-    }
-
-    // This method does a ton of stuff. It will attempt to create a new tab
-    // if we haven't reached MAX_TABS. Otherwise it uses the current tab. If
-    // url isn't null, it will load the given url.
-    public Tab openTabAndShow(Tab parent, final UrlData urlData,
-            boolean closeOnExit, String appId) {
-        final Tab currentTab = mTabControl.getCurrentTab();
-        if (mTabControl.canCreateNewTab()) {
-            final Tab tab = mTabControl.createNewTab(closeOnExit, appId,
-                    urlData.mUrl,
-                    (parent != null) && parent.isPrivateBrowsingEnabled());
-            WebView webview = tab.getWebView();
-            // We must set the new tab as the current tab to reflect the old
-            // animation behavior.
-            addTab(tab);
-            setActiveTab(tab);
-            if (!urlData.isEmpty()) {
-                loadUrlDataIn(tab, urlData);
-            }
-            return tab;
-        } else {
-            // Get rid of the subwindow if it exists
-            dismissSubWindow(currentTab);
-            if (!urlData.isEmpty()) {
-                // Load the given url.
-                loadUrlDataIn(currentTab, urlData);
-            }
-            return currentTab;
-        }
+        return openTab(mSettings.getHomePage(), false, true, false);
     }
 
     @Override
-    public Tab openIncognitoTab() {
-        if (mTabControl.canCreateNewTab()) {
-            Tab currentTab = mTabControl.getCurrentTab();
-            Tab tab = mTabControl.createNewTab(false, null,
-                    null, true);
-            addTab(tab);
-            setActiveTab(tab);
-            loadUrlDataIn(tab, new UrlData(INCOGNITO_URI));
-            return tab;
-        } else {
-            mUi.showMaxTabsWarning();
-            return null;
-        }
-    }
-
-    @Override
-    public Tab createNewTab(String url, boolean incognito) {
-        if (mTabControl.canCreateNewTab()) {
-            Tab tab = mTabControl.createNewTab(false, null, null, incognito);
+    public Tab openTab(String url, boolean incognito, boolean setActive,
+            boolean useCurrent) {
+        Tab tab = createNewTab(incognito, setActive, useCurrent);
+        if (tab != null) {
             WebView w = tab.getWebView();
-            addTab(tab);
-            loadUrl(w, (incognito ? INCOGNITO_URI : url));
-            return tab;
-        } else {
-            mUi.showMaxTabsWarning();
-            return null;
+            loadUrl(w, ((incognito && url == null) ? INCOGNITO_URI : url));
         }
+        return tab;
+    }
+
+    // this method will attempt to create a new tab
+    // incognito: private browsing tab
+    // setActive: ste tab as current tab
+    // useCurrent: if no new tab can be created, return current tab
+    private Tab createNewTab(boolean incognito, boolean setActive,
+            boolean useCurrent) {
+        Tab tab = null;
+        if (mTabControl.canCreateNewTab()) {
+            tab = mTabControl.createNewTab(incognito);
+            addTab(tab);
+            if (setActive) {
+                setActiveTab(tab);
+            }
+        } else {
+            if (useCurrent) {
+                tab = mTabControl.getCurrentTab();
+                // Get rid of the subwindow if it exists
+                dismissSubWindow(tab);
+            } else {
+                mUi.showMaxTabsWarning();
+            }
+        }
+        return tab;
     }
 
     /**
@@ -2416,12 +2382,6 @@
                 // Now we close the other tab
                 closeTab(current);
             } else {
-                if (current.closeOnExit()) {
-                    // This will finish the activity if there is only one tab
-                    // open or it will switch to the next available tab if
-                    // available.
-                    closeCurrentTab();
-                }
                 /*
                  * Instead of finishing the activity, simply push this to the back
                  * of the stack and let ActivityManager to choose the foreground
@@ -2591,7 +2551,7 @@
                 // exclusive use of a modifier
                 if (event.isCtrlPressed()) {
                     if (event.isShiftPressed()) {
-                        openIncognitoTab();
+                        openTab(null, true, true, false);
                     } else {
                         openTabToHomePage();
                     }
diff --git a/src/com/android/browser/IntentHandler.java b/src/com/android/browser/IntentHandler.java
index 1322d96..40db29f 100644
--- a/src/com/android/browser/IntentHandler.java
+++ b/src/com/android/browser/IntentHandler.java
@@ -134,7 +134,7 @@
             }
 
             if (intent.getBooleanExtra(Browser.EXTRA_CREATE_NEW_TAB, false)) {
-                mController.openTabAndShow(mTabControl.getCurrentTab(), urlData, false, null);
+                mController.openTab(urlData);
                 return;
             }
             final String appId = intent
@@ -151,7 +151,10 @@
                         mController.reuseTab(appTab, appId, urlData);
                         return;
                     } else {
-                        mController.openTabAndShow(null, urlData, false, appId);
+                        Tab tab = mController.openTab(urlData);
+                        if (tab != null) {
+                            tab.setAppId(appId);
+                        }
                     }
                 } else {
                     // No matching application tab, try to find a regular tab
@@ -168,7 +171,10 @@
                         // 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.
-                        mController.openTabAndShow(null, urlData, false, appId);
+                        Tab tab = mController.openTab(urlData);
+                        if (tab != null) {
+                            tab.setAppId(appId);
+                        }
                     }
                 }
             } else {
diff --git a/src/com/android/browser/NavScreen.java b/src/com/android/browser/NavScreen.java
index 848b7b1..4095a96 100644
--- a/src/com/android/browser/NavScreen.java
+++ b/src/com/android/browser/NavScreen.java
@@ -239,14 +239,12 @@
             showMenu();
         } else if (mNewIncognito == v) {
             mUi.hideNavScreen(true);
-            mUiController.openIncognitoTab();
+            mUiController.openTab(null, true, true, false);
         }
     }
 
     private void openNewTab() {
-        Tab tab = mUiController.createNewTab(
-                BrowserSettings.getInstance().getHomePage(),
-                false);
+        Tab tab = mUiController.openTabToHomePage();
         mAdapter.notifyDataSetChanged();
 
         if (tab != null) {
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index 3508005..c217e6e 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -108,8 +108,6 @@
     // Tab that constructed by this Tab. This is used when this Tab is
     // destroyed, it clears all mParentTab values in the children.
     private Vector<Tab> mChildTabs;
-    // If true, the tab will be removed when back out of the first page.
-    private boolean mCloseOnExit;
     // If true, the tab is in the foreground of the current activity.
     private boolean mInForeground;
     // If true, the tab is in page loading state (after onPageStarted,
@@ -191,7 +189,6 @@
     static final String CURRTAB = "currentTab";
     static final String CURRURL = "currentUrl";
     static final String CURRTITLE = "currentTitle";
-    static final String CLOSEONEXIT = "closeonexit";
     static final String PARENTTAB = "parentTab";
     static final String APPID = "appid";
     static final String ORIGINALURL = "originalUrl";
@@ -859,9 +856,9 @@
                 mWebViewController.attachSubWindow(Tab.this);
                 transport.setWebView(mSubView);
             } else {
-                final Tab newTab = mWebViewController.openTabAndShow(
-                        Tab.this,
-                        IntentHandler.EMPTY_URL_DATA, false, null);
+                final Tab newTab = mWebViewController.openTab(null,
+                        Tab.this.isPrivateBrowsingEnabled(),
+                        true, true);
                 if (newTab != Tab.this) {
                     Tab.this.addChildTab(newTab);
                 }
@@ -1290,13 +1287,10 @@
     // remove later
 
     // Construct a new tab
-    Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
-            String url) {
+    Tab(WebViewController wvcontroller, WebView w) {
         mWebViewController = wvcontroller;
         mActivity = mWebViewController.getActivity();
         mSettings = BrowserSettings.getInstance();
-        mCloseOnExit = closeOnExit;
-        mAppId = appId;
         mDataController = DataController.getInstance(mActivity);
         mCurrentState = new PageState(mActivity, w != null
                 ? w.isPrivateBrowsingEnabled() : false);
@@ -1666,15 +1660,6 @@
         return mParentTab;
     }
 
-    /**
-     * Return whether this tab should be closed when it is backing out of the
-     * first page.
-     * @return TRUE if this tab should be closed when exit.
-     */
-    boolean closeOnExit() {
-        return mCloseOnExit;
-    }
-
     private void setLockIconType(LockIcon icon) {
         mCurrentState.mLockIcon = icon;
         mWebViewController.onUpdatedLockIcon(this);
@@ -1741,7 +1726,6 @@
 
         mSavedState.putString(CURRURL, mCurrentState.mUrl);
         mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
-        mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
         if (mAppId != null) {
             mSavedState.putString(APPID, mAppId);
         }
@@ -1766,7 +1750,6 @@
         // Restore the internal state even if the WebView fails to restore.
         // This will maintain the app id, original url and close-on-exit values.
         mSavedState = null;
-        mCloseOnExit = b.getBoolean(CLOSEONEXIT);
         mAppId = b.getString(APPID);
         mScreenshot = b.getParcelable(SCREENSHOT);
 
diff --git a/src/com/android/browser/TabControl.java b/src/com/android/browser/TabControl.java
index 07c9fa5..8be3041 100644
--- a/src/com/android/browser/TabControl.java
+++ b/src/com/android/browser/TabControl.java
@@ -165,8 +165,7 @@
      * @return The newly createTab or null if we have reached the maximum
      *         number of open tabs.
      */
-    Tab createNewTab(boolean closeOnExit, String appId, String url,
-            boolean privateBrowsing) {
+    Tab createNewTab(boolean privateBrowsing) {
         int size = mTabs.size();
         // Return false if we have maxed out on tabs
         if (mMaxTabs == size) {
@@ -175,7 +174,7 @@
         final WebView w = createNewWebView(privateBrowsing);
 
         // Create a new tab and add it to the tab list
-        Tab t = new Tab(mController, w, closeOnExit, appId, url);
+        Tab t = new Tab(mController, w);
         mTabs.add(t);
         // Initially put the tab in the background.
         t.putInBackground();
@@ -187,7 +186,7 @@
      * appId(null), url(null), and privateBrowsing(false).
      */
     Tab createNewTab() {
-        return createNewTab(false, null, null, false);
+        return createNewTab(false);
     }
 
     /**
@@ -361,7 +360,7 @@
             } else {
                 // Create a new tab and don't restore the state yet, add it
                 // to the tab list
-                Tab t = new Tab(mController, null, false, null, null);
+                Tab t = new Tab(mController, null);
                 if (state != null) {
                     t.setSavedState(state);
                     // Need to maintain the app id and original url so we
diff --git a/src/com/android/browser/UiController.java b/src/com/android/browser/UiController.java
index 9b75aca..9f85547 100644
--- a/src/com/android/browser/UiController.java
+++ b/src/com/android/browser/UiController.java
@@ -42,9 +42,8 @@
 
     Tab openTabToHomePage();
 
-    Tab openIncognitoTab();
-
-    Tab createNewTab(String url, boolean incognito);
+    Tab openTab(String url, boolean incognito, boolean setActive,
+            boolean useCurrent);
 
     void setActiveTab(Tab tab);
 
diff --git a/src/com/android/browser/UrlHandler.java b/src/com/android/browser/UrlHandler.java
index fbbc3cf..c76eee8 100644
--- a/src/com/android/browser/UrlHandler.java
+++ b/src/com/android/browser/UrlHandler.java
@@ -214,7 +214,9 @@
     // depressed by opening in a new tab
     boolean handleMenuClick(Tab tab, String url) {
         if (mController.isMenuDown()) {
-            mController.openTab(tab, url, false);
+            mController.openTab(url,
+                    (tab != null) && tab.isPrivateBrowsingEnabled(),
+                    !BrowserSettings.getInstance().openInBackground(), true);
             mActivity.closeOptionsMenu();
             return true;
         }
diff --git a/src/com/android/browser/WebViewController.java b/src/com/android/browser/WebViewController.java
index 93ca410..6028a97 100644
--- a/src/com/android/browser/WebViewController.java
+++ b/src/com/android/browser/WebViewController.java
@@ -16,8 +16,6 @@
 
 package com.android.browser;
 
-import com.android.browser.IntentHandler.UrlData;
-
 import android.app.Activity;
 import android.graphics.Bitmap;
 import android.net.Uri;
@@ -103,8 +101,8 @@
 
     void dismissSubWindow(Tab tab);
 
-    Tab openTabAndShow(Tab parent, UrlData urlData, boolean closeOnExit,
-            String appId);
+    Tab openTab(String url, boolean incognito, boolean setActive,
+            boolean useCurrent);
 
     boolean switchToTab(int tabindex);