New tab flow through Bookmarks/History screen

Change-Id: I0f21279fea33582229f16f37bf813ad1fca15fc2
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index 40ada94..d6f7177 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -1367,7 +1367,7 @@
                 break;
 
             case R.id.bookmarks_menu_id:
-                bookmarksOrHistoryPicker(false);
+                bookmarksOrHistoryPicker(false, false);
                 break;
 
             case R.id.active_tabs_menu_id:
@@ -1451,7 +1451,7 @@
                 break;
 
             case R.id.classic_history_menu_id:
-                bookmarksOrHistoryPicker(true);
+                bookmarksOrHistoryPicker(true, false);
                 break;
 
             case R.id.title_bar_share_page_url:
@@ -1752,7 +1752,7 @@
                             new MenuItem.OnMenuItemClickListener() {
                                 public boolean onMenuItemClick(MenuItem item) {
                                     final Tab parent = mTabControl.getCurrentTab();
-                                    final Tab newTab = openTab(extra);
+                                    final Tab newTab = openTab(extra, false);
                                     if (newTab != parent) {
                                         parent.addChildTab(newTab);
                                     }
@@ -1895,8 +1895,8 @@
         }
     }
 
-    private Tab openTab(String url) {
-        if (mSettings.openInBackground()) {
+    private Tab openTab(String url, boolean forceForeground) {
+        if (mSettings.openInBackground() && !forceForeground) {
             Tab t = mTabControl.createNewTab();
             if (t != null) {
                 WebView view = t.getWebView();
@@ -2239,7 +2239,7 @@
                     return true;
                 } else if (mCustomView == null && mActiveTabsPage == null
                         && event.isLongPress()) {
-                    bookmarksOrHistoryPicker(true);
+                    bookmarksOrHistoryPicker(true, false);
                     return true;
                 }
                 break;
@@ -2789,7 +2789,7 @@
         }
 
         if (mMenuIsDown) {
-            openTab(url);
+            openTab(url, false);
             closeOptionsMenu();
             return true;
         }
@@ -3732,10 +3732,12 @@
                     String data = intent.getAction();
                     Bundle extras = intent.getExtras();
                     if (extras != null && extras.getBoolean("new_window", false)) {
-                        openTab(data);
+                        openTab(data, false);
+                    } else if ((extras != null) &&
+                            extras.getBoolean(CombinedBookmarkHistoryActivity.NEWTAB_MODE)) {
+                        openTab(data, true);
                     } else {
-                        final Tab currentTab =
-                                mTabControl.getCurrentTab();
+                        final Tab currentTab = mTabControl.getCurrentTab();
                         dismissSubWindow(currentTab);
                         if (data != null && data.length() != 0) {
                             loadUrl(getTopWindow(), data);
@@ -3842,7 +3844,7 @@
      * @param startWithHistory If true, open starting on the history tab.
      *                         Otherwise, start with the bookmarks tab.
      */
-    /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory) {
+    /* package */ void bookmarksOrHistoryPicker(boolean startWithHistory, boolean newTabMode) {
         WebView current = mTabControl.getCurrentWebView();
         if (current == null) {
             return;
@@ -3877,6 +3879,9 @@
             intent.putExtra(CombinedBookmarkHistoryActivity.STARTING_TAB,
                     CombinedBookmarkHistoryActivity.HISTORY_TAB);
         }
+        if (newTabMode) {
+            intent.putExtra(CombinedBookmarkHistoryActivity.NEWTAB_MODE, true);
+        }
         startActivityForResult(intent, COMBO_PAGE);
     }
 
diff --git a/src/com/android/browser/CombinedBookmarkHistoryActivity.java b/src/com/android/browser/CombinedBookmarkHistoryActivity.java
index 194956f..64e8673 100644
--- a/src/com/android/browser/CombinedBookmarkHistoryActivity.java
+++ b/src/com/android/browser/CombinedBookmarkHistoryActivity.java
@@ -48,11 +48,18 @@
      */
     private int mResultCode;
 
+    /**
+     * Flag to inform the browser to force the result to open in a new tab.
+     */
+    private boolean mNewTabMode;
+
     /* package */ static String BOOKMARKS_TAB = "bookmark";
     /* package */ static String VISITED_TAB = "visited";
     /* package */ static String HISTORY_TAB = "history";
     /* package */ static String STARTING_TAB = "tab";
 
+    final static String NEWTAB_MODE = "newtab_mode";
+
     static class IconListenerSet implements IconListener {
         // Used to store favicons as we get them from the database
         // FIXME: We use a different method to get the Favicons in
@@ -99,6 +106,10 @@
 
         Bundle extras = getIntent().getExtras();
 
+        if (extras != null) {
+            mNewTabMode = extras.getBoolean(NEWTAB_MODE);
+        }
+
         Intent bookmarksIntent = new Intent(this, BrowserBookmarksPage.class);
         if (extras != null) {
             bookmarksIntent.putExtras(extras);
@@ -186,6 +197,10 @@
             if (mResultData == null) mResultData = new Intent();
             mResultData.putExtra(Intent.EXTRA_TEXT, mExtraData);
         }
+        if (mNewTabMode) {
+            if (mResultData == null) mResultData = new Intent();
+            mResultData.putExtra(NEWTAB_MODE, true);
+        }
         setResult(mResultCode, mResultData);
         super.finish();
     }
diff --git a/src/com/android/browser/TabBar.java b/src/com/android/browser/TabBar.java
index f73185f..da18614 100644
--- a/src/com/android/browser/TabBar.java
+++ b/src/com/android/browser/TabBar.java
@@ -116,7 +116,7 @@
             mBrowserActivity.showFakeTitleBar();
             mUserRequestedUrlbar = true;
         } else if (mNewButton == view) {
-            mBrowserActivity.openTabToHomePage();
+            mBrowserActivity.bookmarksOrHistoryPicker(false, true);
         } else if (mTabs.getSelectedTab() == view) {
             mBrowserActivity.showFakeTitleBar();
             mTitleBar.requestUrlInputFocus();
diff --git a/src/com/android/browser/TitleBarXLarge.java b/src/com/android/browser/TitleBarXLarge.java
index 4e65375..c1fdee3 100644
--- a/src/com/android/browser/TitleBarXLarge.java
+++ b/src/com/android/browser/TitleBarXLarge.java
@@ -104,8 +104,7 @@
         } else if (mMenu == v) {
             mBrowserActivity.openOptionsMenu();
         } else if (mAllButton == v) {
-            // TODO: Show the new bookmarks/windows view.
-            mBrowserActivity.bookmarksOrHistoryPicker(false);
+            mBrowserActivity.bookmarksOrHistoryPicker(false, false);
         } else if (mSearchButton == v) {
             search();
         } else if (mStopButton == v) {