Bug 1269001 - Introduce BrowserDB.getBookmarkCountForFolder r=mcomella a=sylvestre
authorAndrzej Hunt <andrzej@ahunt.org>
Thu, 05 May 2016 11:10:49 -0700
changeset 333037 683a20539adac5be187179e8c0f32a503f9e91bf
parent 333036 30919b955abda9c7c081e75b15a5ebd4a32c15d3
child 333038 69d24df550f760a2a03368914d0cf4dc99903263
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcomella, sylvestre
bugs1269001
milestone48.0a2
Bug 1269001 - Introduce BrowserDB.getBookmarkCountForFolder r=mcomella a=sylvestre For now we only need to support the bookmarks smartfolder, however we might want to extend this to support "recent bookmarks" in future. MozReview-Commit-ID: CvDNyfycWRl
mobile/android/base/java/org/mozilla/gecko/db/BrowserDB.java
mobile/android/base/java/org/mozilla/gecko/db/LocalBrowserDB.java
mobile/android/base/java/org/mozilla/gecko/db/LocalUrlAnnotations.java
mobile/android/base/java/org/mozilla/gecko/db/StubBrowserDB.java
mobile/android/base/java/org/mozilla/gecko/db/UrlAnnotations.java
--- a/mobile/android/base/java/org/mozilla/gecko/db/BrowserDB.java
+++ b/mobile/android/base/java/org/mozilla/gecko/db/BrowserDB.java
@@ -117,16 +117,18 @@ public interface BrowserDB {
     public abstract void updateBookmark(ContentResolver cr, int id, String uri, String title, String keyword);
     public abstract boolean hasBookmarkWithGuid(ContentResolver cr, String guid);
 
     /**
      * Can return <code>null</code>.
      */
     public abstract Cursor getBookmarksInFolder(ContentResolver cr, long folderId);
 
+    public abstract int getBookmarkCountForFolder(ContentResolver cr, long folderId);
+
     /**
      * Get the favicon from the database, if any, associated with the given favicon URL. (That is,
      * the URL of the actual favicon image, not the URL of the page with which the favicon is associated.)
      * @param cr The ContentResolver to use.
      * @param faviconURL The URL of the favicon to fetch from the database.
      * @return The decoded Bitmap from the database, if any. null if none is stored.
      */
     public abstract LoadFaviconResult getFaviconForUrl(ContentResolver cr, String faviconURL);
--- a/mobile/android/base/java/org/mozilla/gecko/db/LocalBrowserDB.java
+++ b/mobile/android/base/java/org/mozilla/gecko/db/LocalBrowserDB.java
@@ -881,16 +881,25 @@ public class LocalBrowserDB implements B
             cursorsToMerge.add(c);
             final Cursor[] arr = (Cursor[]) Array.newInstance(Cursor.class, cursorsToMerge.size());
             return new MergeCursor(cursorsToMerge.toArray(arr));
         } else {
             return c;
         }
     }
 
+    @Override
+    public int getBookmarkCountForFolder(ContentResolver cr, long folderID) {
+        if (folderID == Bookmarks.FAKE_READINGLIST_SMARTFOLDER_ID) {
+            return getUrlAnnotations().getAnnotationCount(cr, BrowserContract.UrlAnnotations.Key.READER_VIEW);
+        } else {
+            throw new IllegalArgumentException("Retrieving bookmark count for folder with ID=" + folderID + " not supported yet");
+        }
+    }
+
     @CheckResult
     private ArrayList<Cursor> getSpecialFoldersCursorList(final boolean addDesktopFolder,
             final boolean addScreenshotsFolder, final boolean addReadingListFolder) {
         if (addDesktopFolder || addScreenshotsFolder || addReadingListFolder) {
             // Avoid calling this twice.
             assertDefaultBookmarkColumnOrdering();
         }
 
--- a/mobile/android/base/java/org/mozilla/gecko/db/LocalUrlAnnotations.java
+++ b/mobile/android/base/java/org/mozilla/gecko/db/LocalUrlAnnotations.java
@@ -223,9 +223,31 @@ public class LocalUrlAnnotations impleme
     public void insertReaderViewUrl(final ContentResolver cr, final String pageUrl) {
         insertAnnotation(cr, pageUrl, Key.READER_VIEW.getDbValue(), BrowserContract.UrlAnnotations.READER_VIEW_SAVED_VALUE);
     }
 
     @Override
     public void deleteReaderViewUrl(ContentResolver cr, String pageURL) {
         deleteAnnotation(cr, pageURL, Key.READER_VIEW);
     }
+
+    public int getAnnotationCount(ContentResolver cr, Key key) {
+        final String countColumnname = "count";
+        final Cursor c = queryByKey(cr,
+                key,
+                new String[] {
+                        "COUNT(*) AS " + countColumnname
+                },
+                null);
+
+        try {
+            if (c != null && c.moveToFirst()) {
+                return c.getInt(c.getColumnIndexOrThrow(countColumnname));
+            } else {
+                return 0;
+            }
+        } finally {
+            if (c != null) {
+                c.close();
+            }
+        }
+    }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/db/StubBrowserDB.java
+++ b/mobile/android/base/java/org/mozilla/gecko/db/StubBrowserDB.java
@@ -136,16 +136,21 @@ class StubUrlAnnotations implements UrlA
     @Override
     public void deleteReaderViewUrl(ContentResolver cr, String pageURL) {}
 
     @Override
     public boolean hasAcceptedOrDeclinedHomeScreenShortcut(ContentResolver cr, String url) { return false; }
 
     @Override
     public void insertHomeScreenShortcut(ContentResolver cr, String url, boolean hasCreatedShortCut) {}
+
+    @Override
+    public int getAnnotationCount(ContentResolver cr, BrowserContract.UrlAnnotations.Key key) {
+        return 0;
+    }
 }
 
 /*
  * This base implementation just stubs all methods. For the
  * real implementations, see LocalBrowserDB.java.
  */
 public class StubBrowserDB implements BrowserDB {
     private final StubSearches searches = new StubSearches();
@@ -237,16 +242,18 @@ public class StubBrowserDB implements Br
     public void clearHistory(ContentResolver cr, boolean clearSearchHistory) {
     }
 
     @RobocopTarget
     public Cursor getBookmarksInFolder(ContentResolver cr, long folderId) {
         return null;
     }
 
+    public int getBookmarkCountForFolder(ContentResolver cr, long folderId) { return 0; }
+
     @RobocopTarget
     public boolean isBookmark(ContentResolver cr, String uri) {
         return false;
     }
 
     public String getUrlForKeyword(ContentResolver cr, String keyword) {
         return null;
     }
--- a/mobile/android/base/java/org/mozilla/gecko/db/UrlAnnotations.java
+++ b/mobile/android/base/java/org/mozilla/gecko/db/UrlAnnotations.java
@@ -41,9 +41,11 @@ public interface UrlAnnotations {
     /**
      * Insert an indication that the user has interacted with this URL in regards to home screen
      * shortcuts.
      *
      * @param hasCreatedShortCut True if a home screen shortcut has been created for this URL. False
      *                           if the user has actively declined to create a shortcut for this URL.
      */
     void insertHomeScreenShortcut(ContentResolver cr, String url, boolean hasCreatedShortCut);
+
+    int getAnnotationCount(ContentResolver cr, BrowserContract.UrlAnnotations.Key key);
 }