Bug 848254 - Add support of pre-pinned bookmarks on about:home for distributions. r=rnewman
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Mon, 18 Mar 2013 15:39:08 -0700
changeset 125314 d0c34cd5d1226e44cc154dcdb9ce4ff1c9d483eb
parent 125313 545649dc10e7a172599e837f10124ebecd37da6f
child 125315 f49cd7c9b7664296c92d05098d8287a7cfda2240
push idunknown
push userunknown
push dateunknown
reviewersrnewman
bugs848254
milestone22.0a1
Bug 848254 - Add support of pre-pinned bookmarks on about:home for distributions. r=rnewman
mobile/android/base/db/BrowserProvider.java.in
--- a/mobile/android/base/db/BrowserProvider.java.in
+++ b/mobile/android/base/db/BrowserProvider.java.in
@@ -1006,23 +1006,43 @@ public class BrowserProvider extends Con
         private int createDistributionBookmarks(final SQLiteDatabase db) {
             JSONArray bookmarks = Distribution.getBookmarks(mContext);
             if (bookmarks == null) {
                 return 0;
             }
 
             Locale locale = Locale.getDefault();
             int pos = 0;
+            Integer mobileFolderId = getMobileFolderId(db);
+            if (mobileFolderId == null) {
+                Log.e(LOGTAG, "Error creating distribution bookmarks: mobileFolderId is null");
+                return 0;
+            }
+
             for (int i = 0; i < bookmarks.length(); i++) {
                 try {
                     final JSONObject bookmark = bookmarks.getJSONObject(i);
 
                     String title = getLocalizedProperty(bookmark, "title", locale);
                     final String url = getLocalizedProperty(bookmark, "url", locale);
-                    createBookmark(db, title, url, pos);
+                    createBookmark(db, title, url, pos, mobileFolderId);
+
+                    if (bookmark.has("pinned")) {
+                        try {
+                            // Create a fake bookmark in the hidden pinned folder to pin bookmark
+                            // to about:home top sites. Pass pos as the pinned position to pin 
+                            // sites in the order that bookmarks are specified in bookmarks.json.
+                            if (bookmark.getBoolean("pinned")) {
+                                createBookmark(db, title, url, pos, Bookmarks.FIXED_PINNED_LIST_ID);
+                            }
+                        } catch (JSONException e) {
+                            Log.e(LOGTAG, "Error pinning bookmark to top sites", e);
+                        }
+                    }
+
                     pos++;
 
                     // return early if there is no icon for this bookmark
                     if (!bookmark.has("icon")) {
                         continue;
                     }
 
                     // create icons in a separate thread to avoid blocking about:home on startup
@@ -1048,30 +1068,36 @@ public class BrowserProvider extends Con
         }
 
         // Inserts default bookmarks, starting at a specified position
         private void createDefaultBookmarks(final SQLiteDatabase db, int pos) {
             Class<?> stringsClass = R.string.class;
             Field[] fields = stringsClass.getFields();
             Pattern p = Pattern.compile("^bookmarkdefaults_title_");
 
+            Integer mobileFolderId = getMobileFolderId(db);
+            if (mobileFolderId == null) {
+                Log.e(LOGTAG, "Error creating default bookmarks: mobileFolderId is null");
+                return;
+            }
+
             for (int i = 0; i < fields.length; i++) {
                 final String name = fields[i].getName();
                 Matcher m = p.matcher(name);
                 if (!m.find()) {
                     continue;
                 }
                 try {
                     int titleid = fields[i].getInt(null);
                     String title = mContext.getString(titleid);
 
                     Field urlField = stringsClass.getField(name.replace("_title_", "_url_"));
                     int urlId = urlField.getInt(null);
                     final String url = mContext.getString(urlId);
-                    createBookmark(db, title, url, pos);
+                    createBookmark(db, title, url, pos, mobileFolderId);
 
                     // create icons in a separate thread to avoid blocking about:home on startup
                     ThreadUtils.postToBackgroundThread(new Runnable() {
                         @Override
                         public void run() {
                             Bitmap icon = getDefaultFaviconFromPath(name);
                             if (icon == null) {
                                 icon = getDefaultFaviconFromDrawable(name);
@@ -1085,19 +1111,19 @@ public class BrowserProvider extends Con
                 } catch (java.lang.IllegalAccessException ex) {
                     Log.e(LOGTAG, "Can't create bookmark " + name, ex);
                 } catch (java.lang.NoSuchFieldException ex) {
                     Log.e(LOGTAG, "Can't create bookmark " + name, ex);
                 }
             }
         }
 
-        private void createBookmark(SQLiteDatabase db, String title, String url, int pos) {
+        private void createBookmark(SQLiteDatabase db, String title, String url, int pos, int parent) {
             ContentValues bookmarkValues = new ContentValues();
-            bookmarkValues.put(Bookmarks.PARENT, guidToID(db, Bookmarks.MOBILE_FOLDER_GUID));
+            bookmarkValues.put(Bookmarks.PARENT, parent);
 
             long now = System.currentTimeMillis();
             bookmarkValues.put(Bookmarks.DATE_CREATED, now);
             bookmarkValues.put(Bookmarks.DATE_MODIFIED, now);
 
             bookmarkValues.put(Bookmarks.TITLE, title);
             bookmarkValues.put(Bookmarks.URL, url);
             bookmarkValues.put(Bookmarks.GUID, Utils.generateGuid());
@@ -1291,17 +1317,21 @@ public class BrowserProvider extends Con
                     c.close();
             }
 
             // At this point is safe to assume that the mobile folder is
             // in the new table given that we've always created it on
             // database creation time.
             final int nInvalidSpecialEntries = invalidSpecialEntries.size();
             if (nInvalidSpecialEntries > 0) {
-                Long mobileFolderId = guidToID(db, Bookmarks.MOBILE_FOLDER_GUID);
+                Integer mobileFolderId = getMobileFolderId(db);
+                if (mobileFolderId == null) {
+                    Log.e(LOGTAG, "Error migrating invalid special folder entries: mobile folder id is null");
+                    return;
+                }
 
                 debug("Found " + nInvalidSpecialEntries + " invalid special folder entries");
                 for (int i = 0; i < nInvalidSpecialEntries; i++) {
                     ContentValues values = invalidSpecialEntries.get(i);
                     values.put(Bookmarks.PARENT, mobileFolderId);
 
                     db.insert(TABLE_BOOKMARKS, Bookmarks.URL, values);
                 }
@@ -1750,30 +1780,33 @@ public class BrowserProvider extends Con
                 } finally {
                     if (cursor != null)
                         cursor.close();
                 }
             }
         }
     }
 
-    private Long guidToID(SQLiteDatabase db, String guid) {
+    private static final String[] mobileIdColumns = new String[] { Bookmarks._ID };
+    private static final String[] mobileIdSelectionArgs = new String[] { Bookmarks.MOBILE_FOLDER_GUID };
+
+    private Integer getMobileFolderId(SQLiteDatabase db) {
         Cursor c = null;
 
         try {
             c = db.query(TABLE_BOOKMARKS,
-                         new String[] { Bookmarks._ID },
+                         mobileIdColumns,
                          Bookmarks.GUID + " = ?",
-                         new String[] { guid },
+                         mobileIdSelectionArgs,
                          null, null, null);
 
             if (c == null || !c.moveToFirst())
                 return null;
 
-            return c.getLong(c.getColumnIndex(Bookmarks._ID));
+            return c.getInt(c.getColumnIndex(Bookmarks._ID));
         } finally {
             if (c != null)
                 c.close();
         }
     }
 
     private DatabaseHelper getDatabaseHelperForProfile(String profile, boolean isTest) {
         // Each profile has a separate browser.db database. The target