Bug 783312 - Set up a backend for pinned sites. r=mfinkle
authorWes Johnston <wjohnston@mozilla.com>
Fri, 28 Dec 2012 13:46:04 -0800
changeset 126290 3ffc0cb25398dab8bba334e213f6345cd86a6f2a
parent 126289 48cd86fc9dfeb9fe123c1620f749fc0c34e39072
child 126291 e74b5611f5d11148128fae4e7df9efe2114d35c9
push id2151
push userlsblakk@mozilla.com
push dateTue, 19 Feb 2013 18:06:57 +0000
treeherdermozilla-beta@4952e88741ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs783312
milestone20.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 783312 - Set up a backend for pinned sites. r=mfinkle
mobile/android/base/db/BrowserContract.java.in
mobile/android/base/db/BrowserDB.java
mobile/android/base/db/BrowserProvider.java.in
mobile/android/base/db/LocalBrowserDB.java
mobile/android/base/locales/en-US/sync_strings.dtd
mobile/android/services/strings.xml.in
--- a/mobile/android/base/db/BrowserContract.java.in
+++ b/mobile/android/base/db/BrowserContract.java.in
@@ -105,25 +105,27 @@ public class BrowserContract {
     }
 
     public static final class Bookmarks implements CommonColumns, URLColumns, FaviconColumns, SyncColumns {
         private Bookmarks() {}
 
         public static final int FIXED_ROOT_ID = 0;
         public static final int FAKE_DESKTOP_FOLDER_ID = -1;
         public static final int FIXED_READING_LIST_ID = -2;
+        public static final int FIXED_PINNED_LIST_ID = -3;
 
         public static final String MOBILE_FOLDER_GUID = "mobile";
         public static final String PLACES_FOLDER_GUID = "places";
         public static final String MENU_FOLDER_GUID = "menu";
         public static final String TAGS_FOLDER_GUID = "tags";
         public static final String TOOLBAR_FOLDER_GUID = "toolbar";
         public static final String UNFILED_FOLDER_GUID = "unfiled";
         public static final String READING_LIST_FOLDER_GUID = "readinglist";
         public static final String FAKE_DESKTOP_FOLDER_GUID = "desktop";
+        public static final String PINNED_FOLDER_GUID = "pinned";
 
         public static final int TYPE_FOLDER = 0;
         public static final int TYPE_BOOKMARK = 1;
         public static final int TYPE_SEPARATOR = 2;
         public static final int TYPE_LIVEMARK = 3;
         public static final int TYPE_QUERY = 4;
 
         public static final Uri CONTENT_URI = Uri.withAppendedPath(AUTHORITY_URI, "bookmarks");
--- a/mobile/android/base/db/BrowserDB.java
+++ b/mobile/android/base/db/BrowserDB.java
@@ -91,16 +91,24 @@ public class BrowserDB {
 
         public void removeThumbnails(ContentResolver cr);
 
         public void registerBookmarkObserver(ContentResolver cr, ContentObserver observer);
 
         public void registerHistoryObserver(ContentResolver cr, ContentObserver observer);
 
         public int getCount(ContentResolver cr, String database);
+
+        public void pinSite(ContentResolver cr, String url, String title, int position);
+
+        public void unpinSite(ContentResolver cr, int position);
+
+        public void unpinAllSites(ContentResolver cr);
+
+        public Cursor getPinnedSites(ContentResolver cr, int limit);
     }
 
     static {
         // Forcing local DB no option to switch to Android DB for now
         sDb = null;
     }
 
     public static void initialize(String profile) {
@@ -240,16 +248,32 @@ public class BrowserDB {
     public static void unregisterContentObserver(ContentResolver cr, ContentObserver observer) {
         cr.unregisterContentObserver(observer);
     }
 
     public static int getCount(ContentResolver cr, String database) {
         return sDb.getCount(cr, database);
     }
 
+    public static void pinSite(ContentResolver cr, String url, String title, int position) {
+        sDb.pinSite(cr, url, title, position);
+    }
+
+    public static void unpinSite(ContentResolver cr, int position) {
+        sDb.unpinSite(cr, position);
+    }
+
+    public static void unpinAllSites(ContentResolver cr) {
+        sDb.unpinAllSites(cr);
+    }
+
+    public static Cursor getPinnedSites(ContentResolver cr, int limit) {
+        return sDb.getPinnedSites(cr, limit);
+    }
+
     /* Cursor wrapper that forces top sites to contain at least
      * mNumberOfTopSites entries. For rows outside the wrapped cursor
      * will return empty strings and zero.
      */
     static public class TopSitesCursorWrapper extends CursorWrapper {
         int mIndex = -1;
         Cursor mCursor = null;
         int mSize = 0;
--- a/mobile/android/base/db/BrowserProvider.java.in
+++ b/mobile/android/base/db/BrowserProvider.java.in
@@ -68,17 +68,17 @@ import android.text.TextUtils;
 import android.util.Log;
 
 public class BrowserProvider extends ContentProvider {
     private static final String LOGTAG = "GeckoBrowserProvider";
     private Context mContext;
 
     static final String DATABASE_NAME = "browser.db";
 
-    static final int DATABASE_VERSION = 13;
+    static final int DATABASE_VERSION = 14;
 
     // Maximum age of deleted records to be cleaned up (20 days in ms)
     static final long MAX_AGE_OF_DELETED_RECORDS = 86400000 * 20;
 
     // Number of records marked as deleted to be removed
     static final long DELETED_RECORDS_PURGE_LIMIT = 5;
 
     // How many records to reposition in a single query.
@@ -1096,29 +1096,33 @@ public class BrowserProvider extends Con
             createOrUpdateSpecialFolder(db, Bookmarks.MENU_FOLDER_GUID,
                 R.string.bookmarks_folder_menu, 2);
             createOrUpdateSpecialFolder(db, Bookmarks.TAGS_FOLDER_GUID,
                 R.string.bookmarks_folder_tags, 3);
             createOrUpdateSpecialFolder(db, Bookmarks.UNFILED_FOLDER_GUID,
                 R.string.bookmarks_folder_unfiled, 4);
             createOrUpdateSpecialFolder(db, Bookmarks.READING_LIST_FOLDER_GUID,
                 R.string.bookmarks_folder_reading_list, 5);
+            createOrUpdateSpecialFolder(db, Bookmarks.PINNED_FOLDER_GUID,
+                R.string.bookmarks_folder_pinned, 6);
         }
 
         private void createOrUpdateSpecialFolder(SQLiteDatabase db,
                 String guid, int titleId, int position) {
             ContentValues values = new ContentValues();
             values.put(Bookmarks.GUID, guid);
             values.put(Bookmarks.TYPE, Bookmarks.TYPE_FOLDER);
             values.put(Bookmarks.POSITION, position);
 
             if (guid.equals(Bookmarks.PLACES_FOLDER_GUID))
                 values.put(Bookmarks._ID, Bookmarks.FIXED_ROOT_ID);
             else if (guid.equals(Bookmarks.READING_LIST_FOLDER_GUID))
                 values.put(Bookmarks._ID, Bookmarks.FIXED_READING_LIST_ID);
+            else if (guid.equals(Bookmarks.PINNED_FOLDER_GUID))
+                values.put(Bookmarks._ID, Bookmarks.FIXED_PINNED_LIST_ID);
 
             // Set the parent to 0, which sync assumes is the root
             values.put(Bookmarks.PARENT, Bookmarks.FIXED_ROOT_ID);
 
             String title = mContext.getResources().getString(titleId);
             values.put(Bookmarks.TITLE, title);
 
             long now = System.currentTimeMillis();
@@ -1562,16 +1566,21 @@ public class BrowserProvider extends Con
 
             createBookmarksWithFaviconsView(db);
             createHistoryWithFaviconsView(db);
             createCombinedViewOn13(db);
 
             db.execSQL("DROP TABLE IF EXISTS " + Obsolete.TABLE_IMAGES);
         }
 
+        private void upgradeDatabaseFrom13to14(SQLiteDatabase db) {
+            createOrUpdateSpecialFolder(db, Bookmarks.PINNED_FOLDER_GUID,
+                R.string.bookmarks_folder_pinned, 6);
+        }
+
         @Override
         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
             debug("Upgrading browser.db: " + db.getPath() + " from " +
                     oldVersion + " to " + newVersion);
 
             db.beginTransaction();
 
             // We have to do incremental upgrades until we reach the current
@@ -1620,16 +1629,20 @@ public class BrowserProvider extends Con
 
                     case 12:
                         upgradeDatabaseFrom11to12(db);
                         break;
 
                     case 13:
                         upgradeDatabaseFrom12to13(db);
                         break;
+
+                    case 14:
+                        upgradeDatabaseFrom13to14(db);
+                        break;
                 }
             }
 
             // If an upgrade after 12->13 fails, the entire upgrade is rolled
             // back, but we can't undo the deletion of favicon_urls.db if we
             // delete this in step 13; therefore, we wait until all steps are
             // complete before removing it.
             if (oldVersion < 13 && newVersion >= 13
--- a/mobile/android/base/db/LocalBrowserDB.java
+++ b/mobile/android/base/db/LocalBrowserDB.java
@@ -439,19 +439,21 @@ public class LocalBrowserDB implements B
 
     public boolean isBookmark(ContentResolver cr, String uri) {
         // This method is about normal bookmarks, not the Reading List
         int count = 0;
         try {
             Cursor c = cr.query(bookmarksUriWithLimit(1),
                                 new String[] { Bookmarks._ID },
                                 Bookmarks.URL + " = ? AND " +
+                                Bookmarks.PARENT + " != ? AND " +
                                 Bookmarks.PARENT + " != ?",
                                 new String[] { uri,
-                                               String.valueOf(Bookmarks.FIXED_READING_LIST_ID) },
+                                               String.valueOf(Bookmarks.FIXED_READING_LIST_ID),
+                                               String.valueOf(Bookmarks.FIXED_PINNED_LIST_ID) },
                                 Bookmarks.URL);
             count = c.getCount();
             c.close();
         } catch (NullPointerException e) {
             Log.e(LOGTAG, "NullPointerException in isBookmark");
         }
 
         return (count > 0);
@@ -1079,9 +1081,70 @@ public class LocalBrowserDB implements B
             return super.getColumnIndex(translateColumnName(columnName));
         }
 
         @Override
         public int getColumnIndexOrThrow(String columnName) {
             return super.getColumnIndexOrThrow(translateColumnName(columnName));
         }
     }
+
+
+    public void pinSite(ContentResolver cr, String url, String title, int position) {
+        ContentValues values = new ContentValues();
+        final long now = System.currentTimeMillis();
+        values.put(Bookmarks.TITLE, title);
+        values.put(Bookmarks.URL, url);
+        values.put(Bookmarks.PARENT, Bookmarks.FIXED_PINNED_LIST_ID);
+        values.put(Bookmarks.DATE_MODIFIED, now);
+        values.put(Bookmarks.POSITION, position);
+        values.put(Bookmarks.IS_DELETED, 0);
+
+        // If this site is already pinned, unpin it
+        cr.delete(mBookmarksUriWithProfile,
+                  Bookmarks.PARENT + " == ? AND " + Bookmarks.URL + " == ?",
+                  new String[] {
+                      String.valueOf(Bookmarks.FIXED_PINNED_LIST_ID),
+                      url
+                  });
+
+        // If something is already pinned in this spot update it
+        int updated = cr.update(mBookmarksUriWithProfile,
+                                values,
+                                Bookmarks.POSITION + " = ? AND " +
+                                Bookmarks.PARENT + " = ?",
+                                new String[] { Integer.toString(position),
+                                               String.valueOf(Bookmarks.FIXED_PINNED_LIST_ID) });
+
+        // Otherwise just insert a new item
+        if (updated == 0) {
+            cr.insert(mBookmarksUriWithProfile, values);
+        }
+    }
+
+    public Cursor getPinnedSites(ContentResolver cr, int limit) {
+        return cr.query(bookmarksUriWithLimit(limit),
+                        new String[] { Bookmarks._ID,
+                                       Bookmarks.URL,
+                                       Bookmarks.TITLE,
+                                       Bookmarks.POSITION },
+                        Bookmarks.PARENT + " == ?",
+                        new String[] { String.valueOf(Bookmarks.FIXED_PINNED_LIST_ID) },
+                        Bookmarks.POSITION + " ASC");
+    }
+
+    public void unpinSite(ContentResolver cr, int position) {
+        cr.delete(mBookmarksUriWithProfile,
+                  Bookmarks.PARENT + " == ? AND " + Bookmarks.POSITION + " = ?",
+                  new String[] {
+                      String.valueOf(Bookmarks.FIXED_PINNED_LIST_ID),
+                      Integer.toString(position)
+                  });
+    }
+
+    public void unpinAllSites(ContentResolver cr) {
+        cr.delete(mBookmarksUriWithProfile,
+                  Bookmarks.PARENT + " == ?",
+                  new String[] {
+                      String.valueOf(Bookmarks.FIXED_PINNED_LIST_ID)
+                  });
+    }
 }
--- a/mobile/android/base/locales/en-US/sync_strings.dtd
+++ b/mobile/android/base/locales/en-US/sync_strings.dtd
@@ -78,16 +78,18 @@
 <!ENTITY bookmarks.folder.menu.label 'Bookmarks Menu'>
 <!ENTITY bookmarks.folder.places.label ''>
 <!ENTITY bookmarks.folder.tags.label 'Tags'>
 <!ENTITY bookmarks.folder.toolbar.label 'Bookmarks Toolbar'>
 <!ENTITY bookmarks.folder.unfiled.label 'Unsorted Bookmarks'>
 <!ENTITY bookmarks.folder.desktop.label 'Desktop Bookmarks'>
 <!ENTITY bookmarks.folder.mobile.label 'Mobile Bookmarks'>
 <!ENTITY bookmarks.folder.readinglist.label 'Reading List'>
+<!-- Pinned sites on about:home. This folder should never be shown to the user, but we have to give it a string name -->
+<!ENTITY bookmarks.folder.pinned.label 'Pinned'>
 
 <!-- Notification strings -->
 <!ENTITY sync.notification.oneaccount.label 'Only one &syncBrand.fullName.label; account is supported.'>
 <!ENTITY sync.notification.configure.saved 'Your selections have been saved.'>
 
 <!-- Incorrect settings and changing credentials. -->
 <!ENTITY sync.invalidcreds.label 'Incorrect account name or password.'>
 <!ENTITY sync.invalidserver.label 'Please enter a valid server URL'>
--- a/mobile/android/services/strings.xml.in
+++ b/mobile/android/services/strings.xml.in
@@ -71,16 +71,17 @@
   <string name="bookmarks_folder_menu">&bookmarks.folder.menu.label;</string>
   <string name="bookmarks_folder_places">&bookmarks.folder.places.label;</string>
   <string name="bookmarks_folder_tags">&bookmarks.folder.tags.label;</string>
   <string name="bookmarks_folder_toolbar">&bookmarks.folder.toolbar.label;</string>
   <string name="bookmarks_folder_unfiled">&bookmarks.folder.unfiled.label;</string>
   <string name="bookmarks_folder_desktop">&bookmarks.folder.desktop.label;</string>
   <string name="bookmarks_folder_mobile">&bookmarks.folder.mobile.label;</string>
   <string name="bookmarks_folder_reading_list">&bookmarks.folder.readinglist.label;</string>
+  <string name="bookmarks_folder_pinned">&bookmarks.folder.pinned.label;</string>
 
   <!-- Notification strings -->
   <string name="sync_notification_oneaccount">&sync.notification.oneaccount.label;</string>
   <string name="sync_notification_savedprefs">&sync.notification.configure.saved;</string>
 
   <!-- Incorrect settings and changing credentials. -->
   <string name="sync_invalidcreds_label">&sync.invalidcreds.label;</string>
   <string name="sync_invalidserver_label">&sync.invalidserver.label;</string>