bug 695457 - Use android system backend for Awesome Bar data r=dougt
authorBrad Lassey <blassey@mozilla.com>
Wed, 19 Oct 2011 13:39:51 -0400
changeset 83219 208cba10065b54b0edae7935ac9f57d9e39fd132
parent 83218 a8484900da43c8ee07f767c5441928e2e434a370
child 83220 9f0b55203981f91d8a220667712f3a93fc0ee953
push id519
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 00:38:35 +0000
treeherdermozilla-beta@788ea1ef610b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdougt
bugs695457
milestone10.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
bug 695457 - Use android system backend for Awesome Bar data r=dougt
embedding/android/AndroidManifest.xml.in
embedding/android/AwesomeBar.java
embedding/android/DatabaseHelper.java
embedding/android/GeckoApp.java
embedding/android/GeckoBookmarks.java
embedding/android/Makefile.in
embedding/android/Tab.java
--- a/embedding/android/AndroidManifest.xml.in
+++ b/embedding/android/AndroidManifest.xml.in
@@ -19,16 +19,17 @@
     <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
 
     <uses-permission android:name="android.permission.READ_LOGS"/>
     <uses-permission android:name="android.permission.WAKE_LOCK"/>
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
     <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> 
 
     <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/> 
+    <uses-permission android:name="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS"/> 
 
     <uses-feature android:name="android.hardware.location" android:required="false"/>
     <uses-feature android:name="android.hardware.location.gps" android:required="false"/>
     <uses-feature android:name="android.hardware.touchscreen"/>
 
     <uses-permission android:name="android.permission.CAMERA" />
     <uses-feature android:name="android.hardware.camera" android:required="false"/>
     <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
--- a/embedding/android/AwesomeBar.java
+++ b/embedding/android/AwesomeBar.java
@@ -40,65 +40,43 @@
 package org.mozilla.gecko;
 
 import java.io.File;
 
 import android.app.Activity;
 import android.app.ListActivity;
 import android.content.Context;
 import android.content.Intent;
-import android.database.sqlite.SQLiteDatabase;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.text.Editable;
 import android.text.TextWatcher;
 import android.util.Log;
 import android.view.KeyEvent;
 import android.view.View;
 import android.view.Window;
 import android.widget.EditText;
 import android.widget.FilterQueryProvider;
 import android.widget.ListView;
 import android.widget.SimpleCursorAdapter;
+import android.provider.Browser;
+import java.util.*;
 
 public class AwesomeBar extends ListActivity {
     static final String URL_KEY = "url";
     static final String TITLE_KEY = "title";
     static final String CURRENT_URL_KEY = "currenturl";
     static final String TYPE_KEY = "type";
     static enum Type { ADD, EDIT };
 
     private static final String LOG_NAME = "AwesomeBar";
 
     private String mType;
     private Cursor mCursor;
-    private SQLiteDatabase mDb;
-    private AwesomeBarCursorAdapter mAdapter;
-
-    private class AwesomeBarCursorAdapter extends SimpleCursorAdapter {
-        private Cursor mAdapterCursor;
-        private Context mContext;
-
-        public AwesomeBarCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
-            // Using the older, deprecated constructor so we can work on API < 11
-            super(context, layout, c, from, to);
-            mAdapterCursor = c;
-            mContext = context;
-        }
-
-        @Override
-        public void bindView(View view, Context context, Cursor cursor) {
-            super.bindView(view, context, cursor);
-
-            // ImageView imageView = (ImageView) view.findViewById(R.id.favicon);
-            // byte[] raw = cursor.getBlob(cursor.getColumnIndexOrThrow("favicon"));
-            // Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeByteArray(raw, 0, raw.length), 48, 48, true);
-            // imageView.setImageBitmap(bitmap);
-        }
-    }
+    private SimpleCursorAdapter mAdapter;
 
     private String getProfilePath() {
         File home = new File(getFilesDir(), "mozilla");
         if (!home.exists())
             return null;
 
         File profile = null;
         String[] files = home.list();
@@ -124,17 +102,17 @@ public class AwesomeBar extends ListActi
         super.onCreate(savedInstanceState);
 
         Log.d(LOG_NAME, "creating awesomebar");
 
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.awesomebar_search);
 
         // Load the list using a custom adapter so we can create the bitmaps
-        mAdapter = new AwesomeBarCursorAdapter(
+        mAdapter = new SimpleCursorAdapter(
             this,
             R.layout.awesomebar_row,
             null,
             new String[] { TITLE_KEY, URL_KEY },
             new int[] { R.id.title, R.id.url }
         );
         setListAdapter(mAdapter);
 
@@ -177,42 +155,45 @@ public class AwesomeBar extends ListActi
                     finish();
                     return true;
                 } else {
                     return false;
                 }
             }
         });
 
-        DatabaseHelper dbHelper = new DatabaseHelper(this);
-        mDb = dbHelper.getReadableDatabase();
-
+        final Activity activity = this;
         mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
             public Cursor runQuery(CharSequence constraint) {
-                // _id column required for CursorAdapter; provide a dummy here
-                mCursor = mDb.rawQuery(
-                        "SELECT 0 AS _id, title, url "
-                          + "FROM moz_places "
-                          + "WHERE (url LIKE ? OR title LIKE ?) "
-                          + "LIMIT 12",
-                        new String[] {"%" + constraint.toString() + "%", "%" + constraint.toString() + "%",});
+                mCursor = 
+                    activity.managedQuery(Browser.BOOKMARKS_URI,
+                                          null, Browser.BookmarkColumns.URL + " LIKE ? OR title LIKE ?", 
+                                          new String[] {"%" + constraint.toString() + "%", "%" + constraint.toString() + "%",},
+                                          // ORDER BY is number of visits times a multiplier from 1 - 120 of how recently the site 
+                                          // was accessed with a site accessed today getting 120 and a site accessed 119 or more 
+                                          // days ago getting 1
+                                          Browser.BookmarkColumns.VISITS + " * MAX(1, (" + 
+                                          Browser.BookmarkColumns.DATE + " - " + new Date().getTime() + ") / 86400000 + 120) DESC");
+                
+
+                activity.startManagingCursor(mCursor);
+
 
                 return mCursor;
             }
         });
 
         // show unfiltered results initially
         mAdapter.getFilter().filter("");
     }
 
     @Override
     public void onDestroy() {
         super.onDestroy();
         if (mCursor != null) mCursor.close();
-        if (mDb != null) mDb.close();
     }
 
     @Override
     public void onListItemClick(ListView l, View v, int position, long id) {
         Cursor cursor = (Cursor)l.getItemAtPosition(position);
         String url = cursor.getString(cursor.getColumnIndexOrThrow(URL_KEY));
         Intent resultIntent = new Intent();
         resultIntent.putExtra(URL_KEY, url);
deleted file mode 100644
--- a/embedding/android/DatabaseHelper.java
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Version: MPL 1.1/GPL 2.0/LGPL 2.1
- *
- * The contents of this file are subject to the Mozilla Public License Version
- * 1.1 (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.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS" basis,
- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- * for the specific language governing rights and limitations under the
- * License.
- *
- * The Original Code is Places code.
- *
- * The Initial Developer of the Original Code is
- * Mozilla Corporation.
- * Portions created by the Initial Developer are Copyright (C) 2008
- * the Initial Developer. All Rights Reserved.
- *
- * Contributor(s):
- *   Shawn Wilsher <me@shawnwilsher.com> (Original Author)
- *
- * Alternatively, the contents of this file may be used under the terms of
- * either the GNU General Public License Version 2 or later (the "GPL"), or
- * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
- * in which case the provisions of the GPL or the LGPL are applicable instead
- * of those above. If you wish to allow use of your version of this file only
- * under the terms of either the GPL or the LGPL, and not to allow others to
- * use your version of this file under the terms of the MPL, indicate your
- * decision by deleting the provisions above and replace them with the notice
- * and other provisions required by the GPL or the LGPL. If you do not delete
- * the provisions above, a recipient may use your version of this file under
- * the terms of any one of the MPL, the GPL or the LGPL.
- *
- * ***** END LICENSE BLOCK ***** */
-
-package org.mozilla.gecko;
-
-import android.content.Context;
-import android.database.sqlite.SQLiteDatabase;
-import android.database.sqlite.SQLiteOpenHelper;
-import android.util.Log;
-
-class DatabaseHelper extends SQLiteOpenHelper {
-    private static final String LOG_NAME = "DatabaseHelper";
-
-    private static final String CREATE_MOZ_PLACES =
-      "CREATE TABLE moz_places ( " +
-        "  id INTEGER PRIMARY KEY" +
-        ", url LONGVARCHAR UNIQUE" +
-        ", title LONGVARCHAR" +
-        ", rev_host LONGVARCHAR" +
-        ", visit_count INTEGER DEFAULT 0" +
-        ", hidden INTEGER DEFAULT 0 NOT NULL" +
-        ", typed INTEGER DEFAULT 0 NOT NULL" +
-        ", favicon_id INTEGER" +
-        ", frecency INTEGER DEFAULT -1 NOT NULL" +
-        ", last_visit_date INTEGER " +
-        ", guid TEXT" +
-      ")";
-
-    private static final String CREATE_MOZ_HISTORYVISITS  =
-      "CREATE TABLE moz_historyvisits (" +
-        "  id INTEGER PRIMARY KEY" +
-        ", from_visit INTEGER" +
-        ", place_id INTEGER" +
-        ", visit_date INTEGER" +
-        ", visit_type INTEGER" +
-        ", session INTEGER" +
-      ")";
-
-    private static final String CREATE_MOZ_FAVICONS =
-      "CREATE TABLE moz_favicons (" +
-        "  id INTEGER PRIMARY KEY" +
-        ", url LONGVARCHAR UNIQUE" +
-        ", data BLOB" +
-        ", mime_type VARCHAR(32)" +
-        ", expiration LONG" +
-      ")";
-
-    private static final String CREATE_MOZ_BOOKMARKS =
-      "CREATE TABLE moz_bookmarks (" +
-        "  id INTEGER PRIMARY KEY" +
-        ", type INTEGER" +
-        ", fk INTEGER DEFAULT NULL" +
-        ", parent INTEGER" +
-        ", position INTEGER" +
-        ", title LONGVARCHAR" +
-        ", keyword_id INTEGER" +
-        ", folder_type TEXT" +
-        ", dateAdded INTEGER" +
-        ", lastModified INTEGER" +
-        ", guid TEXT" +
-      ")";
-
-    private static final String CREATE_MOZ_BOOKMARKS_ROOTS =
-      "CREATE TABLE moz_bookmarks_roots (" +
-        "  root_name VARCHAR(16) UNIQUE" +
-        ", folder_id INTEGER" +
-      ")";
-
-    private static final String DATABASE_NAME = "places";
-    private static final int DATABASE_VERSION = 1;
-
-    DatabaseHelper(Context context) {
-        super(context, DATABASE_NAME, null, DATABASE_VERSION);
-    }
-
-    @Override
-    public void onCreate(SQLiteDatabase db) {
-        db.execSQL(CREATE_MOZ_PLACES);
-        db.execSQL(CREATE_MOZ_HISTORYVISITS);
-        db.execSQL(CREATE_MOZ_BOOKMARKS);
-        db.execSQL(CREATE_MOZ_BOOKMARKS_ROOTS);
-        db.execSQL(CREATE_MOZ_FAVICONS);
-    }
-
-    @Override
-    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
-        Log.w(LOG_NAME, "Upgrading database from version " + oldVersion + " to "
-                + newVersion + ", which will destroy all old data");
-        db.execSQL("DROP TABLE IF EXISTS places");
-        onCreate(db);
-    }
-}
--- a/embedding/android/GeckoApp.java
+++ b/embedding/android/GeckoApp.java
@@ -84,17 +84,16 @@ abstract public class GeckoApp
     private LinearLayout mMainLayout;
     private RelativeLayout mGeckoLayout;
     public static GeckoSurfaceView surfaceView;
     public static SurfaceView cameraView;
     public static GeckoApp mAppContext;
     public static boolean mFullscreen = false;
     public static File sGREDir = null;
     public Handler mMainHandler;
-    public static DatabaseHelper mDbHelper;
     private IntentFilter mConnectivityFilter;
     private BroadcastReceiver mConnectivityReceiver;
     private BrowserToolbar mBrowserToolbar;
 
     enum LaunchState {Launching, WaitButton,
                       Launched, GeckoRunning, GeckoExiting};
     private static LaunchState sLaunchState = LaunchState.Launching;
     private static boolean sTryCatchAttached = false;
@@ -405,22 +404,16 @@ abstract public class GeckoApp
 
     private void quit() {
         Log.i(LOG_FILE_NAME, "pleaseKillMe");
         if (surfaceView != null)
             surfaceView.saveLast(true);
         System.exit(0);
     }
 
-    public static DatabaseHelper getDatabaseHelper() {
-        if (mDbHelper == null)
-            mDbHelper = new DatabaseHelper(GeckoApp.mAppContext);
-        return mDbHelper;
-    }
-
     void handleLocationChange(final int tabId, final String uri) {
         Tab tab = Tabs.getInstance().getTab(tabId);
         if (tab != null)
             tab.updateURL(uri);
 
         if (!Tabs.getInstance().isSelectedTab(tab))
             return;
 
--- a/embedding/android/GeckoBookmarks.java
+++ b/embedding/android/GeckoBookmarks.java
@@ -47,26 +47,27 @@ import android.util.Log;
 import android.view.View;
 import android.widget.ListAdapter;
 import android.widget.ListView;
 import android.widget.SimpleCursorAdapter;
 
 public class GeckoBookmarks extends ListActivity {
     private static final String LOG_NAME = "GeckoBookmarks";
     private static final String TITLE_KEY = "title";
+    private static final String kBookmarksWhereClause = Browser.BookmarkColumns.BOOKMARK + " = 1";
 
     private Cursor mCursor;
     private Uri mUri;
     private String mTitle;
 
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.bookmarks);
         mCursor = managedQuery(Browser.BOOKMARKS_URI,
-            		null, null, null, null);
+                               null, kBookmarksWhereClause, null, null);
         startManagingCursor(mCursor);
 
         ListAdapter adapter =
           new SimpleCursorAdapter(this, R.layout.bookmark_list_row, mCursor,
         		      new String[] {Browser.BookmarkColumns.TITLE,
         				    Browser.BookmarkColumns.URL},
         		      new int[] {R.id.bookmark_title, R.id.bookmark_url});
         setListAdapter(adapter);
--- a/embedding/android/Makefile.in
+++ b/embedding/android/Makefile.in
@@ -51,17 +51,16 @@ JAVAFILES = \
   GeckoAppShell.java \
   GeckoConnectivityReceiver.java \
   GeckoEvent.java \
   GeckoSurfaceView.java \
   GeckoInputConnection.java \
   AlertNotification.java \
   SurfaceLockInfo.java \
   AwesomeBar.java \
-  DatabaseHelper.java \
   GeckoBookmarks.java \
   Tab.java \
   Tabs.java \
   ShowTabs.java \
   $(NULL)
 
 PROCESSEDJAVAFILES = \
   App.java \
--- a/embedding/android/Tab.java
+++ b/embedding/android/Tab.java
@@ -39,16 +39,17 @@ package org.mozilla.gecko;
 
 import java.util.*;
 
 import android.content.*;
 import android.database.sqlite.SQLiteDatabase;
 import android.os.AsyncTask;
 import android.graphics.drawable.*;
 import android.util.Log;
+import android.provider.Browser;
 
 public class Tab {
 
     private static final String LOG_FILE_NAME = "Tab";
     private int id;
     private String url, title;
     private Drawable favicon, thumbnail;
     private Stack<HistoryEntry> history;
@@ -157,24 +158,13 @@ public class Tab {
         GeckoEvent e = new GeckoEvent("session-back", "");
         GeckoAppShell.sendEventToGecko(e);
         return true;
     }
 
     private class HistoryEntryTask extends AsyncTask<HistoryEntry, Void, Void> {
         protected Void doInBackground(HistoryEntry... entries) {
             HistoryEntry entry = entries[0];
-            Log.d(LOG_FILE_NAME, "adding uri=" + entry.mUri + ", title=" + entry.mTitle + " to history");
-            ContentValues values = new ContentValues();
-            values.put("url", entry.mUri);
-            values.put("title", entry.mTitle);
-
-            DatabaseHelper dbHelper = GeckoApp.getDatabaseHelper();
-            SQLiteDatabase mDb = dbHelper.getWritableDatabase();
-            long id = mDb.insertWithOnConflict("moz_places", null, values, SQLiteDatabase.CONFLICT_REPLACE);
-            values = new ContentValues();
-            values.put("place_id", id);
-            mDb.insertWithOnConflict("moz_historyvisits", null, values, SQLiteDatabase.CONFLICT_REPLACE);
-            mDb.close();
+            Browser.updateVisitedHistory(GeckoApp.mAppContext.getContentResolver(), entry.mUri, true);
             return null;
         }
     }
 }