Merge "Fix BP1to2UpgradeTests"
diff --git a/res/menu/title_context.xml b/res/menu/title_context.xml
deleted file mode 100644
index a60f102..0000000
--- a/res/menu/title_context.xml
+++ /dev/null
@@ -1,26 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2009 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-
-<menu xmlns:android="http://schemas.android.com/apk/res/android">
-    <!-- These are for the context menu for the title bar.  Note that we need a
-            different item from share_page_menu_id, which looks at the top
-            window (possibly a subwindow), because this should be the main
-            window.-->
-    <item android:id="@+id/title_bar_share_page_url"
-        android:title="@string/share_page"/>
-    <item android:id="@+id/title_bar_copy_page_url"
-        android:title="@string/copy_page_url"/>
-</menu>
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 92f448c..fc4c35c 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -1636,7 +1636,6 @@
                 bookmarksOrHistoryPicker(true);
                 break;
 
-            case R.id.title_bar_share_page_url:
             case R.id.share_page_menu_id:
                 Tab currentTab = mTabControl.getCurrentTab();
                 if (null == currentTab) {
@@ -1703,20 +1702,6 @@
         int id = item.getItemId();
         boolean result = true;
         switch (id) {
-            // For the context menu from the title bar
-            case R.id.title_bar_copy_page_url:
-                Tab currentTab = mTabControl.getCurrentTab();
-                if (null == currentTab) {
-                    result = false;
-                    break;
-                }
-                WebView mainView = currentTab.getWebView();
-                if (null == mainView) {
-                    result = false;
-                    break;
-                }
-                copy(mainView.getUrl());
-                break;
             // -- Browser context menu
             case R.id.open_context_menu_id:
             case R.id.save_link_context_menu_id:
diff --git a/src/com/android/browser/NavigationBarBase.java b/src/com/android/browser/NavigationBarBase.java
index a059352..724dcc8 100644
--- a/src/com/android/browser/NavigationBarBase.java
+++ b/src/com/android/browser/NavigationBarBase.java
@@ -74,7 +74,6 @@
         mFavicon = (ImageView) findViewById(R.id.favicon);
         mUrlInput = (UrlInputView) findViewById(R.id.url);
         mUrlInput.setUrlInputListener(this);
-        mUrlInput.setController(mUiController);
         mUrlInput.setOnFocusChangeListener(this);
         mUrlInput.setSelectAllOnFocus(true);
         mUrlInput.addQueryTextWatcher(this);
@@ -84,6 +83,7 @@
         mTitleBar = titleBar;
         mBaseUi = mTitleBar.getUi();
         mUiController = mTitleBar.getUiController();
+        mUrlInput.setController(mUiController);
     }
 
     public void setLock(Drawable d) {
diff --git a/src/com/android/browser/NavigationBarPhone.java b/src/com/android/browser/NavigationBarPhone.java
index 400b695..388c262 100644
--- a/src/com/android/browser/NavigationBarPhone.java
+++ b/src/com/android/browser/NavigationBarPhone.java
@@ -86,14 +86,6 @@
     }
 
     @Override
-    public void createContextMenu(ContextMenu menu) {
-        Activity activity = mBaseUi.getActivity();
-        MenuInflater inflater = activity.getMenuInflater();
-        inflater.inflate(R.menu.title_context, menu);
-        activity.onCreateContextMenu(menu, this, null);
-    }
-
-    @Override
     protected void setSearchMode(boolean voiceSearchEnabled) {
         boolean showvoicebutton = voiceSearchEnabled &&
                 mUiController.supportsVoiceSearch();
diff --git a/src/com/android/browser/SuggestionsAdapter.java b/src/com/android/browser/SuggestionsAdapter.java
index 9a099cb..30b4738 100644
--- a/src/com/android/browser/SuggestionsAdapter.java
+++ b/src/com/android/browser/SuggestionsAdapter.java
@@ -17,6 +17,7 @@
 package com.android.browser;
 
 import com.android.browser.provider.BrowserProvider2;
+import com.android.browser.provider.BrowserProvider2.OmniboxSuggestions;
 import com.android.browser.search.SearchEngine;
 
 import android.app.SearchManager;
@@ -53,9 +54,12 @@
     public static final int TYPE_SUGGEST = 4;
     public static final int TYPE_VOICE_SEARCH = 5;
 
-    private static final String[] COMBINED_PROJECTION =
-            {BrowserContract.Combined._ID, BrowserContract.Combined.TITLE,
-                    BrowserContract.Combined.URL, BrowserContract.Combined.IS_BOOKMARK};
+    private static final String[] COMBINED_PROJECTION = {
+            OmniboxSuggestions._ID,
+            OmniboxSuggestions.TITLE,
+            OmniboxSuggestions.URL,
+            OmniboxSuggestions.IS_BOOKMARK
+            };
 
     private static final String COMBINED_SELECTION =
             "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR title LIKE ?)";
@@ -470,18 +474,14 @@
                 args[4] = like;
                 selection = COMBINED_SELECTION;
             }
-            Uri.Builder ub = BrowserContract.Combined.CONTENT_URI.buildUpon();
+            Uri.Builder ub = OmniboxSuggestions.CONTENT_URI.buildUpon();
             ub.appendQueryParameter(BrowserContract.PARAM_LIMIT,
                     Integer.toString(Math.max(mLinesLandscape, mLinesPortrait)));
             ub.appendQueryParameter(BrowserProvider2.PARAM_GROUP_BY,
-                    BrowserContract.Combined.URL);
+                    OmniboxSuggestions.URL);
             mCursor =
                     mContext.getContentResolver().query(ub.build(), COMBINED_PROJECTION,
-                            selection,
-                            (constraint != null) ? args : null,
-                            BrowserContract.Combined.IS_BOOKMARK + " DESC, " +
-                            BrowserContract.Combined.VISITS + " DESC, " +
-                            BrowserContract.Combined.DATE_LAST_VISITED + " DESC");
+                            selection, (constraint != null) ? args : null, null);
             if (mCursor != null) {
                 mCursor.moveToFirst();
             }
diff --git a/src/com/android/browser/TabBar.java b/src/com/android/browser/TabBar.java
index b2c2af8..2b4dd63 100644
--- a/src/com/android/browser/TabBar.java
+++ b/src/com/android/browser/TabBar.java
@@ -231,13 +231,6 @@
         mUi.showTitleBar();
     }
 
-    @Override
-    public void createContextMenu(ContextMenu menu) {
-        MenuInflater inflater = mActivity.getMenuInflater();
-        inflater.inflate(R.menu.title_context, menu);
-        mActivity.onCreateContextMenu(menu, this, null);
-    }
-
     private TabView buildTabView(Tab tab) {
         TabView tabview = new TabView(mActivity, tab);
         mTabMap.put(tab, tabview);
diff --git a/src/com/android/browser/XLargeUi.java b/src/com/android/browser/XLargeUi.java
index aeba7cf..301e09f 100644
--- a/src/com/android/browser/XLargeUi.java
+++ b/src/com/android/browser/XLargeUi.java
@@ -253,7 +253,7 @@
     @Override
     public void onActionModeStarted(ActionMode mode) {
         if (!mTitleBar.isEditingUrl()) {
-            // hide the fake title bar when CAB is shown
+            // hide the title bar when CAB is shown
             hideTitleBar();
         }
     }
diff --git a/src/com/android/browser/provider/BrowserProvider2.java b/src/com/android/browser/provider/BrowserProvider2.java
index 36064a5..1821b50 100644
--- a/src/com/android/browser/provider/BrowserProvider2.java
+++ b/src/com/android/browser/provider/BrowserProvider2.java
@@ -81,6 +81,15 @@
         public static final String THUMBNAIL = "thumbnail";
     }
 
+    public static interface OmniboxSuggestions {
+        public static final Uri CONTENT_URI = Uri.withAppendedPath(
+                BrowserContract.AUTHORITY_URI, "omnibox_suggestions");
+        public static final String _ID = "_id";
+        public static final String URL = "url";
+        public static final String TITLE = "title";
+        public static final String IS_BOOKMARK = "bookmark";
+    }
+
     static final String TABLE_BOOKMARKS = "bookmarks";
     static final String TABLE_HISTORY = "history";
     static final String TABLE_IMAGES = "images";
@@ -97,6 +106,7 @@
 
     static final String VIEW_ACCOUNTS = "v_accounts";
     static final String VIEW_SNAPSHOTS_COMBINED = "v_snapshots_combined";
+    static final String VIEW_OMNIBOX_SUGGESTIONS = "v_omnibox_suggestions";
 
     static final String FORMAT_COMBINED_JOIN_SUBQUERY_JOIN_IMAGES =
             "history LEFT OUTER JOIN (%s) bookmarks " +
@@ -121,6 +131,7 @@
 
     static final int THUMBNAILS = 10;
     static final int THUMBNAILS_ID = 11;
+    static final int OMNIBOX_SUGGESTIONS = 20;
 
     static final int BOOKMARKS = 1000;
     static final int BOOKMARKS_ID = 1001;
@@ -200,6 +211,7 @@
         matcher.addURI(authority, "settings", SETTINGS);
         matcher.addURI(authority, "thumbnails", THUMBNAILS);
         matcher.addURI(authority, "thumbnails/#", THUMBNAILS_ID);
+        matcher.addURI(authority, "omnibox_suggestions", OMNIBOX_SUGGESTIONS);
 
         // Legacy
         matcher.addURI(LEGACY_AUTHORITY, "searches", SEARCHES);
@@ -346,7 +358,7 @@
 
     final class DatabaseHelper extends SQLiteOpenHelper {
         static final String DATABASE_NAME = "browser2.db";
-        static final int DATABASE_VERSION = 31;
+        static final int DATABASE_VERSION = 32;
         public DatabaseHelper(Context context) {
             super(context, DATABASE_NAME, null, DATABASE_VERSION);
         }
@@ -418,6 +430,11 @@
             }
 
             enableSync(db);
+            createOmniboxSuggestions(db);
+        }
+
+        void createOmniboxSuggestions(SQLiteDatabase db) {
+            db.execSQL(SQL_CREATE_VIEW_OMNIBOX_SUGGESTIONS);
         }
 
         void createThumbnails(SQLiteDatabase db) {
@@ -545,6 +562,9 @@
 
         @Override
         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+            if (oldVersion < 32) {
+                createOmniboxSuggestions(db);
+            }
             if (oldVersion < 31) {
                 createThumbnails(db);
             }
@@ -1034,6 +1054,11 @@
                 break;
             }
 
+            case OMNIBOX_SUGGESTIONS: {
+                qb.setTables(VIEW_OMNIBOX_SUGGESTIONS);
+                break;
+            }
+
             default: {
                 throw new UnsupportedOperationException("Unknown URL " + uri.toString());
             }
@@ -2062,4 +2087,21 @@
             return mSource.moveToPosition(newPosition);
         }
     }
+
+    // ---------------------------------------------------
+    //  SQL below, be warned
+    // ---------------------------------------------------
+
+    private static final String SQL_CREATE_VIEW_OMNIBOX_SUGGESTIONS =
+            "CREATE VIEW IF NOT EXISTS v_omnibox_suggestions "
+            + " AS "
+            + "  SELECT _id, url, title, 1 AS bookmark, 0 AS visits, 0 AS date"
+            + "  FROM bookmarks "
+            + "  WHERE deleted = 0 AND folder = 0 "
+            + "  UNION ALL "
+            + "  SELECT _id, url, title, 0 AS bookmark, visits, date "
+            + "  FROM history "
+            + "  WHERE url NOT IN (SELECT url FROM bookmarks"
+            + "    WHERE deleted = 0 AND folder = 0) "
+            + "  ORDER BY bookmark DESC, visits DESC, date DESC ";
 }