Bug 1269001 - Introduce BrowserDB.getBookmarkCountForFolder r=mcomella
authorAndrzej Hunt <andrzej@ahunt.org>
Thu, 05 May 2016 11:10:49 -0700
changeset 336452 95739305c4d0843f72f579cd5437e70281b4aec7
parent 336451 4f0d003a90d1e7bd3b4cdd5320ba80e7c03451de
child 336453 c32981aa0f19bcd2791a27d0dfa693a056126230
push id6249
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 13:59:36 +0000
treeherdermozilla-beta@bad9d4f5bf7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcomella
bugs1269001
milestone49.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 1269001 - Introduce BrowserDB.getBookmarkCountForFolder r=mcomella 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
@@ -877,16 +877,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);
 }