Bug 997888/1008210 - Exclude pinned/top sites from suggested sites (r=mfinkle)
authorLucas Rocha <lucasr@mozilla.com>
Tue, 13 May 2014 16:33:19 +0100
changeset 182891 6fb2da864ddf4fae702d9a2f44af2699cd9f9125
parent 182890 c5a13dbf31544a161d53803affc30872da4eb802
child 182892 e9f6b0bee21daa9c7892ef0fb0ff284e77e4bacc
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersmfinkle
bugs997888, 1008210
milestone32.0a1
Bug 997888/1008210 - Exclude pinned/top sites from suggested sites (r=mfinkle)
mobile/android/base/db/BrowserDB.java
--- a/mobile/android/base/db/BrowserDB.java
+++ b/mobile/android/base/db/BrowserDB.java
@@ -1,15 +1,16 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.db;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import org.mozilla.gecko.db.BrowserContract.ExpirePriority;
 import org.mozilla.gecko.db.SuggestedSites;
 import org.mozilla.gecko.favicons.decoders.LoadFaviconResult;
 import org.mozilla.gecko.mozglue.RobocopTarget;
 
 import android.content.ContentResolver;
@@ -155,29 +156,41 @@ public class BrowserDB {
         sDb.invalidateCachedState();
     }
 
     @RobocopTarget
     public static Cursor filter(ContentResolver cr, CharSequence constraint, int limit) {
         return sDb.filter(cr, constraint, limit);
     }
 
+    private static void appendUrlsFromCursor(List<String> urls, Cursor c) {
+        c.moveToPosition(-1);
+        while (c.moveToNext()) {
+            urls.add(c.getString(c.getColumnIndex(URLColumns.URL)));
+        };
+    }
+
     public static Cursor getTopSites(ContentResolver cr, int minLimit, int maxLimit) {
         // Note this is not a single query anymore, but actually returns a mixture
         // of two queries, one for topSites and one for pinned sites.
         Cursor pinnedSites = sDb.getPinnedSites(cr, minLimit);
 
         int pinnedCount = pinnedSites.getCount();
         Cursor topSites = sDb.getTopSites(cr, maxLimit - pinnedCount);
+        int topCount = topSites.getCount();
 
         Cursor suggestedSites = null;
         if (sSuggestedSites != null) {
-            final int count = minLimit - pinnedCount - topSites.getCount();
+            final int count = minLimit - pinnedCount - topCount;
             if (count > 0) {
-                suggestedSites = sSuggestedSites.get(count);
+                final List<String> excludeUrls = new ArrayList<String>(pinnedCount + topCount);
+                appendUrlsFromCursor(excludeUrls, pinnedSites);
+                appendUrlsFromCursor(excludeUrls, topSites);
+
+                suggestedSites = sSuggestedSites.get(count, excludeUrls);
             }
         }
 
         return new TopSitesCursorWrapper(pinnedSites, topSites, suggestedSites, minLimit);
     }
 
     public static void updateVisitedHistory(ContentResolver cr, String uri) {
         if (sAreContentProvidersEnabled) {