Bug 737896 - Put desktop bookmark folders under a "Desktop Bookmarks" folder. r=lucasr
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Mon, 02 Apr 2012 11:56:53 -0700
changeset 90858 dc634206d156893059c17882a9afa47e60da2059
parent 90857 823ab3b9d814478813d49e273ec63a0d5da4309c
child 90859 7ee9b08c2785463b447f25b271fb83276da20f2f
push idunknown
push userunknown
push dateunknown
reviewerslucasr
bugs737896
milestone14.0a1
Bug 737896 - Put desktop bookmark folders under a "Desktop Bookmarks" folder. r=lucasr
mobile/android/base/AwesomeBarTabs.java
mobile/android/base/db/BrowserContract.java.in
mobile/android/base/db/LocalBrowserDB.java
--- a/mobile/android/base/AwesomeBarTabs.java
+++ b/mobile/android/base/AwesomeBarTabs.java
@@ -250,18 +250,18 @@ public class AwesomeBarTabs extends TabH
 
             String guid = c.getString(c.getColumnIndexOrThrow(Bookmarks.GUID));
 
             // If we don't have a special GUID, just return the folder title from the DB.
             if (guid == null || guid.length() == 12)
                 return c.getString(c.getColumnIndexOrThrow(Bookmarks.TITLE));
 
             // Use localized strings for special folder names.
-            if (guid.equals(Bookmarks.MOBILE_FOLDER_GUID))
-                return mResources.getString(R.string.bookmarks_folder_mobile);
+            if (guid.equals(Bookmarks.FAKE_DESKTOP_FOLDER_GUID))
+                return mResources.getString(R.string.bookmarks_folder_desktop);
             else if (guid.equals(Bookmarks.MENU_FOLDER_GUID))
                 return mResources.getString(R.string.bookmarks_folder_menu);
             else if (guid.equals(Bookmarks.TOOLBAR_FOLDER_GUID))
                 return mResources.getString(R.string.bookmarks_folder_toolbar);
             else if (guid.equals(Bookmarks.UNFILED_FOLDER_GUID))
                 return mResources.getString(R.string.bookmarks_folder_unfiled);
 
             // If for some reason we have a folder with a special GUID, but it's not one of
--- a/mobile/android/base/db/BrowserContract.java.in
+++ b/mobile/android/base/db/BrowserContract.java.in
@@ -100,23 +100,25 @@ public class BrowserContract {
         public static final String URL = "url_key";
         public static final String FAVICON_URL = "favicon_url";
     }
 
     public static final class Bookmarks implements CommonColumns, URLColumns, ImageColumns, SyncColumns {
         private Bookmarks() {}
 
         public static final int FIXED_ROOT_ID = 0;
+        public static final int FAKE_DESKTOP_FOLDER_ID = -1;
 
         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 FAKE_DESKTOP_FOLDER_GUID = "desktop";
 
         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/LocalBrowserDB.java
+++ b/mobile/android/base/db/LocalBrowserDB.java
@@ -347,57 +347,59 @@ public class LocalBrowserDB implements B
     public int getMaxHistoryCount() {
         return MAX_HISTORY_COUNT;
     }
 
     public void clearHistory(ContentResolver cr) {
         cr.delete(mHistoryUriWithProfile, null, null);
     }
 
-    // This method filters out the root folder and the tags folder, since we
-    // don't want to see those in the UI
     public Cursor getBookmarksInFolder(ContentResolver cr, long folderId) {
         Cursor c = null;
+        boolean addDesktopFolder = false;
 
-        // If there are no desktop bookmarks, use the mobile bookmarks folder
-        // for the root folder view
-        if (folderId == Bookmarks.FIXED_ROOT_ID && !desktopBookmarksExist(cr))
+        // We always want to show mobile bookmarks in the root view.
+        if (folderId == Bookmarks.FIXED_ROOT_ID) {
             folderId = getMobileBookmarksFolderId(cr);
 
-        if (folderId == Bookmarks.FIXED_ROOT_ID) {
-            // Because of sync, we can end up with some additional records under
-            // the root node that we don't want to see. Since sync doesn't 
-            // want to run into problems deleting these, we can just ignore them
-            // by querying specifically for only the folders we care about.
+            // We'll add a fake "Desktop Bookmarks" folder to the root view if desktop 
+            // bookmarks exist, so that the user can still access non-mobile bookmarks.
+            addDesktopFolder = desktopBookmarksExist(cr);
+        }
+
+        if (folderId == Bookmarks.FAKE_DESKTOP_FOLDER_ID) {
+            // Since the "Desktop Bookmarks" folder doesn't actually exist, we
+            // just fake it by querying specifically certain known desktop folders.
             c = cr.query(mBookmarksUriWithProfile,
                          DEFAULT_BOOKMARK_COLUMNS,
-                         Bookmarks.PARENT + " = ? AND (" +
-                         Bookmarks.GUID + " = ? OR " +
                          Bookmarks.GUID + " = ? OR " +
                          Bookmarks.GUID + " = ? OR " +
-                         Bookmarks.GUID + " = ?)",
-                         new String[] { String.valueOf(folderId),
-                                        Bookmarks.MOBILE_FOLDER_GUID,
-                                        Bookmarks.TOOLBAR_FOLDER_GUID,
+                         Bookmarks.GUID + " = ?",
+                         new String[] { Bookmarks.TOOLBAR_FOLDER_GUID,
                                         Bookmarks.MENU_FOLDER_GUID,
                                         Bookmarks.UNFILED_FOLDER_GUID },
                          null);
         } else {
             // Right now, we only support showing folder and bookmark type of
             // entries. We should add support for other types though (bug 737024)
             c = cr.query(mBookmarksUriWithProfile,
                          DEFAULT_BOOKMARK_COLUMNS,
                          Bookmarks.PARENT + " = ? AND " +
                          "(" + Bookmarks.TYPE + " = ? OR " + Bookmarks.TYPE + " = ?)",
                          new String[] { String.valueOf(folderId),
                                         String.valueOf(Bookmarks.TYPE_BOOKMARK),
                                         String.valueOf(Bookmarks.TYPE_FOLDER) },
                          null);
         }
 
+        if (addDesktopFolder) {
+            // Wrap cursor to add fake desktop bookmarks folder
+            c = new DesktopBookmarksCursorWrapper(c);
+        }
+
         return new LocalDBCursor(c);
     }
 
     // Returns true if any desktop bookmarks exist, which will be true if the user
     // has set up sync at one point, or done a profile migration from XUL fennec.
     private boolean desktopBookmarksExist(ContentResolver cr) {
         if (mDesktopBookmarksExist != null)
             return mDesktopBookmarksExist;
@@ -659,16 +661,77 @@ public class LocalBrowserDB implements B
         int thumbnailIndex = c.getColumnIndexOrThrow(Images.THUMBNAIL);
 
         byte[] b = c.getBlob(thumbnailIndex);
         c.close();
 
         return b;
     }
 
+    // This wrapper adds a fake "Desktop Bookmarks" folder entry to the
+    // beginning of the cursor's data set.
+    private static class DesktopBookmarksCursorWrapper extends CursorWrapper {
+        private boolean mAtDesktopBookmarksPosition = false;
+
+        public DesktopBookmarksCursorWrapper(Cursor c) {
+            super(c);
+        }
+
+        @Override
+        public int getCount() {
+            return super.getCount() + 1;
+        }
+
+        @Override
+        public boolean moveToPosition(int position) {
+            if (position == 0) {
+                mAtDesktopBookmarksPosition = true;
+                return true;
+            }
+
+            mAtDesktopBookmarksPosition = false;
+            return super.moveToPosition(position - 1);
+        }
+
+        @Override
+        public long getLong(int columnIndex) {
+            if (!mAtDesktopBookmarksPosition)
+                return super.getLong(columnIndex);
+
+            if (columnIndex == getColumnIndex(Bookmarks._ID))
+                return Bookmarks.FAKE_DESKTOP_FOLDER_ID;
+            if (columnIndex == getColumnIndex(Bookmarks.PARENT))
+                return Bookmarks.FIXED_ROOT_ID;
+
+            return -1;
+        }
+
+        @Override
+        public int getInt(int columnIndex) {
+            if (!mAtDesktopBookmarksPosition)
+                return super.getInt(columnIndex);
+
+            if (columnIndex == getColumnIndex(Bookmarks.TYPE))
+                return Bookmarks.TYPE_FOLDER;
+
+            return -1;
+        }
+
+        @Override
+        public String getString(int columnIndex) {
+            if (!mAtDesktopBookmarksPosition)
+                return super.getString(columnIndex);
+
+            if (columnIndex == getColumnIndex(Bookmarks.GUID))
+                return Bookmarks.FAKE_DESKTOP_FOLDER_GUID;
+
+            return "";
+        }
+    }
+
     private static class LocalDBCursor extends CursorWrapper {
         public LocalDBCursor(Cursor c) {
             super(c);
         }
 
         private String translateColumnName(String columnName) {
             if (columnName.equals(BrowserDB.URLColumns.URL)) {
                 columnName = URLColumns.URL;