Bug 993970 - Factor out TopSitesCursorWrapper into separate file (r=wesj)
authorLucas Rocha <lucasr@mozilla.com>
Tue, 15 Apr 2014 16:31:55 +0100
changeset 197064 1ceb041e811ea87ba41d33782a3d4ee94f5a5790
parent 197063 f6e8cdd02ef78723d1bd369551c5ca157b7d2e37
child 197065 2dea8051f1451d30cc72fcce7c54cfc1d9350a8b
push id3624
push userasasaki@mozilla.com
push dateMon, 09 Jun 2014 21:49:01 +0000
treeherdermozilla-beta@b1a5da15899a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswesj
bugs993970
milestone31.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 993970 - Factor out TopSitesCursorWrapper into separate file (r=wesj)
mobile/android/base/db/BrowserDB.java
mobile/android/base/db/TopSitesCursorWrapper.java
mobile/android/base/home/TopSitesGridView.java
mobile/android/base/home/TopSitesPanel.java
mobile/android/base/moz.build
--- a/mobile/android/base/db/BrowserDB.java
+++ b/mobile/android/base/db/BrowserDB.java
@@ -2,28 +2,25 @@
  * 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.List;
 
-import org.mozilla.gecko.db.BrowserContract.Bookmarks;
 import org.mozilla.gecko.db.BrowserContract.ExpirePriority;
 import org.mozilla.gecko.favicons.decoders.LoadFaviconResult;
 import org.mozilla.gecko.mozglue.RobocopTarget;
 
 import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.database.ContentObserver;
 import android.database.Cursor;
-import android.database.CursorWrapper;
 import android.graphics.drawable.BitmapDrawable;
-import android.util.SparseArray;
 
 public class BrowserDB {
     private static boolean sAreContentProvidersEnabled = true;
 
     public static interface URLColumns {
         public static String URL = "url";
         public static String TITLE = "title";
         public static String FAVICON = "favicon";
@@ -353,188 +350,9 @@ public class BrowserDB {
 
     public static boolean areContentProvidersDisabled() {
         return sAreContentProvidersEnabled;
     }
 
     public static void setEnableContentProviders(boolean enableContentProviders) {
         sAreContentProvidersEnabled = enableContentProviders;
     }
-
-    public static class PinnedSite {
-        public String title = "";
-        public String url = "";
-
-        public PinnedSite(String aTitle, String aUrl) {
-            title = aTitle;
-            url = aUrl;
-        }
-    }
-
-    /* Cursor wrapper that forces top sites to contain at least
-     * mNumberOfTopSites entries. For rows outside the wrapped cursor
-     * will return empty strings and zero.
-     */
-    public static class TopSitesCursorWrapper extends CursorWrapper {
-        int mIndex = -1; // Current position of the cursor
-        Cursor mCursor = null;
-        int mSize = 0;
-        private SparseArray<PinnedSite> mPinnedSites = null;
-
-        public TopSitesCursorWrapper(Cursor pinnedCursor, Cursor normalCursor, int minSize) {
-            super(normalCursor);
-
-            setPinnedSites(pinnedCursor);
-            mCursor = normalCursor;
-            mSize = Math.max(minSize, mPinnedSites.size() + mCursor.getCount());
-        }
-
-        public void setPinnedSites(Cursor c) {
-            mPinnedSites = new SparseArray<PinnedSite>();
-
-            if (c == null) {
-                return;
-            }
-
-            try {
-                if (c.getCount() <= 0) {
-                    return;
-                }
-                c.moveToPosition(0);
-                do {
-                    int pos = c.getInt(c.getColumnIndex(Bookmarks.POSITION));
-                    String url = c.getString(c.getColumnIndex(URLColumns.URL));
-                    String title = c.getString(c.getColumnIndex(URLColumns.TITLE));
-                    mPinnedSites.put(pos, new PinnedSite(title, url));
-                } while (c.moveToNext());
-            } finally {
-                c.close();
-            }
-        }
-
-        public boolean hasPinnedSites() {
-            return mPinnedSites != null && mPinnedSites.size() > 0;
-        }
-
-        public PinnedSite getPinnedSite(int position) {
-            if (!hasPinnedSites()) {
-                return null;
-            }
-            return mPinnedSites.get(position);
-        }
-
-        public boolean isPinned() {
-            return mPinnedSites.get(mIndex) != null;
-        }
-
-        private int getPinnedBefore(int position) {
-            int numFound = 0;
-            if (!hasPinnedSites()) {
-                return numFound;
-            }
-
-            for (int i = 0; i < position; i++) {
-                if (mPinnedSites.get(i) != null) {
-                    numFound++;
-                }
-            }
-
-            return numFound;
-        }
-
-        @Override
-        public int getPosition() { return mIndex; }
-        @Override
-        public int getCount() { return mSize; }
-        @Override
-        public boolean isAfterLast() { return mIndex >= mSize; }
-        @Override
-        public boolean isBeforeFirst() { return mIndex < 0; }
-        @Override
-        public boolean isLast() { return mIndex == mSize - 1; }
-        @Override
-        public boolean moveToNext() { return moveToPosition(mIndex + 1); }
-        @Override
-        public boolean moveToPrevious() { return moveToPosition(mIndex - 1); }
-
-        @Override
-        public boolean moveToPosition(int position) {
-            mIndex = position;
-
-            // Move the real cursor as if we were stepping through it to this position.
-            // Account for pinned sites, and be careful to update its position to the
-            // minimum or maximum position, even if we're moving beyond its bounds.
-            int before = getPinnedBefore(position);
-            int p2 = position - before;
-            if (p2 <= -1) {
-                super.moveToPosition(-1);
-            } else if (p2 >= mCursor.getCount()) {
-                super.moveToPosition(mCursor.getCount());
-            } else {
-                super.moveToPosition(p2);
-            }
-
-            return !(isBeforeFirst() || isAfterLast());
-        }
-
-        @Override
-        public long getLong(int columnIndex) {
-            if (hasPinnedSites()) {
-                PinnedSite site = getPinnedSite(mIndex);
-                if (site != null) {
-                    return 0;
-                }
-            }
-
-            if (!super.isBeforeFirst() && !super.isAfterLast())
-                return super.getLong(columnIndex);
-            return 0;
-        }
-
-        @Override
-        public int getInt(int columnIndex) {
-            if (hasPinnedSites()) {
-                PinnedSite site = getPinnedSite(mIndex);
-                if (site != null) {
-                    return 0;
-                }
-            }
-
-            if (!super.isBeforeFirst() && !super.isAfterLast())
-                return super.getInt(columnIndex);
-            return 0;
-        }
-
-        @Override
-        public String getString(int columnIndex) {
-            if (hasPinnedSites()) {
-                PinnedSite site = getPinnedSite(mIndex);
-                if (site != null) {
-                    if (columnIndex == mCursor.getColumnIndex(URLColumns.URL)) {
-                        return site.url;
-                    } else if (columnIndex == mCursor.getColumnIndex(URLColumns.TITLE)) {
-                        return site.title;
-                    }
-                    return "";
-                }
-            }
-
-            if (!super.isBeforeFirst() && !super.isAfterLast())
-                return super.getString(columnIndex);
-            return "";
-        }
-
-        @Override
-        public boolean move(int offset) {
-            return moveToPosition(mIndex + offset);
-        }
-
-        @Override
-        public boolean moveToFirst() {
-            return moveToPosition(0);
-        }
-
-        @Override
-        public boolean moveToLast() {
-            return moveToPosition(mSize-1);
-        }
-    }
 }
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/db/TopSitesCursorWrapper.java
@@ -0,0 +1,187 @@
+package org.mozilla.gecko.db;
+
+import android.database.Cursor;
+import android.database.CursorWrapper;
+import android.util.SparseArray;
+
+import org.mozilla.gecko.db.BrowserContract.Bookmarks;
+import org.mozilla.gecko.db.BrowserDB.URLColumns;
+
+/* Cursor wrapper that forces top sites to contain at least
+ * mNumberOfTopSites entries. For rows outside the wrapped cursor
+ * will return empty strings and zero.
+ */
+public class TopSitesCursorWrapper extends CursorWrapper {
+    public static class PinnedSite {
+        public String title = "";
+        public String url = "";
+
+        public PinnedSite(String aTitle, String aUrl) {
+            title = aTitle;
+            url = aUrl;
+        }
+    }
+
+    int mIndex = -1; // Current position of the cursor
+    Cursor mCursor = null;
+    int mSize = 0;
+    private SparseArray<PinnedSite> mPinnedSites = null;
+
+    public TopSitesCursorWrapper(Cursor pinnedCursor, Cursor normalCursor, int minSize) {
+        super(normalCursor);
+
+        setPinnedSites(pinnedCursor);
+        mCursor = normalCursor;
+        mSize = Math.max(minSize, mPinnedSites.size() + mCursor.getCount());
+    }
+
+    public void setPinnedSites(Cursor c) {
+        mPinnedSites = new SparseArray<PinnedSite>();
+
+        if (c == null) {
+            return;
+        }
+
+        try {
+            if (c.getCount() <= 0) {
+                return;
+            }
+            c.moveToPosition(0);
+            do {
+                int pos = c.getInt(c.getColumnIndex(Bookmarks.POSITION));
+                String url = c.getString(c.getColumnIndex(URLColumns.URL));
+                String title = c.getString(c.getColumnIndex(URLColumns.TITLE));
+                mPinnedSites.put(pos, new PinnedSite(title, url));
+            } while (c.moveToNext());
+        } finally {
+            c.close();
+        }
+    }
+
+    public boolean hasPinnedSites() {
+        return mPinnedSites != null && mPinnedSites.size() > 0;
+    }
+
+    public PinnedSite getPinnedSite(int position) {
+        if (!hasPinnedSites()) {
+            return null;
+        }
+        return mPinnedSites.get(position);
+    }
+
+    public boolean isPinned() {
+        return mPinnedSites.get(mIndex) != null;
+    }
+
+    private int getPinnedBefore(int position) {
+        int numFound = 0;
+        if (!hasPinnedSites()) {
+            return numFound;
+        }
+
+        for (int i = 0; i < position; i++) {
+            if (mPinnedSites.get(i) != null) {
+                numFound++;
+            }
+        }
+
+        return numFound;
+    }
+
+    @Override
+    public int getPosition() { return mIndex; }
+    @Override
+    public int getCount() { return mSize; }
+    @Override
+    public boolean isAfterLast() { return mIndex >= mSize; }
+    @Override
+    public boolean isBeforeFirst() { return mIndex < 0; }
+    @Override
+    public boolean isLast() { return mIndex == mSize - 1; }
+    @Override
+    public boolean moveToNext() { return moveToPosition(mIndex + 1); }
+    @Override
+    public boolean moveToPrevious() { return moveToPosition(mIndex - 1); }
+
+    @Override
+    public boolean moveToPosition(int position) {
+        mIndex = position;
+
+        // Move the real cursor as if we were stepping through it to this position.
+        // Account for pinned sites, and be careful to update its position to the
+        // minimum or maximum position, even if we're moving beyond its bounds.
+        int before = getPinnedBefore(position);
+        int p2 = position - before;
+        if (p2 <= -1) {
+            super.moveToPosition(-1);
+        } else if (p2 >= mCursor.getCount()) {
+            super.moveToPosition(mCursor.getCount());
+        } else {
+            super.moveToPosition(p2);
+        }
+
+        return !(isBeforeFirst() || isAfterLast());
+    }
+
+    @Override
+    public long getLong(int columnIndex) {
+        if (hasPinnedSites()) {
+            PinnedSite site = getPinnedSite(mIndex);
+            if (site != null) {
+                return 0;
+            }
+        }
+
+        if (!super.isBeforeFirst() && !super.isAfterLast())
+            return super.getLong(columnIndex);
+        return 0;
+    }
+
+    @Override
+    public int getInt(int columnIndex) {
+        if (hasPinnedSites()) {
+            PinnedSite site = getPinnedSite(mIndex);
+            if (site != null) {
+                return 0;
+            }
+        }
+
+        if (!super.isBeforeFirst() && !super.isAfterLast())
+            return super.getInt(columnIndex);
+        return 0;
+    }
+
+    @Override
+    public String getString(int columnIndex) {
+        if (hasPinnedSites()) {
+            PinnedSite site = getPinnedSite(mIndex);
+            if (site != null) {
+                if (columnIndex == mCursor.getColumnIndex(URLColumns.URL)) {
+                    return site.url;
+                } else if (columnIndex == mCursor.getColumnIndex(URLColumns.TITLE)) {
+                    return site.title;
+                }
+                return "";
+            }
+        }
+
+        if (!super.isBeforeFirst() && !super.isAfterLast())
+            return super.getString(columnIndex);
+        return "";
+    }
+
+    @Override
+    public boolean move(int offset) {
+        return moveToPosition(mIndex + offset);
+    }
+
+    @Override
+    public boolean moveToFirst() {
+        return moveToPosition(0);
+    }
+
+    @Override
+    public boolean moveToLast() {
+        return moveToPosition(mSize-1);
+    }
+}
--- a/mobile/android/base/home/TopSitesGridView.java
+++ b/mobile/android/base/home/TopSitesGridView.java
@@ -4,18 +4,18 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.home;
 
 import java.util.EnumSet;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.ThumbnailHelper;
-import org.mozilla.gecko.db.BrowserDB.TopSitesCursorWrapper;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
+import org.mozilla.gecko.db.TopSitesCursorWrapper;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
 
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.database.Cursor;
 import android.graphics.Rect;
 import android.text.TextUtils;
 import android.util.AttributeSet;
--- a/mobile/android/base/home/TopSitesPanel.java
+++ b/mobile/android/base/home/TopSitesPanel.java
@@ -11,18 +11,18 @@ import java.util.HashMap;
 import java.util.Map;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.db.BrowserContract.Combined;
 import org.mozilla.gecko.db.BrowserContract.Thumbnails;
 import org.mozilla.gecko.db.BrowserDB;
-import org.mozilla.gecko.db.BrowserDB.TopSitesCursorWrapper;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
+import org.mozilla.gecko.db.TopSitesCursorWrapper;
 import org.mozilla.gecko.favicons.Favicons;
 import org.mozilla.gecko.favicons.OnFaviconLoadedListener;
 import org.mozilla.gecko.gfx.BitmapUtils;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
 import org.mozilla.gecko.home.PinSiteDialog.OnSiteSelectedListener;
 import org.mozilla.gecko.home.TopSitesGridView.OnEditPinnedSiteListener;
 import org.mozilla.gecko.home.TopSitesGridView.TopSitesGridContextMenuInfo;
 import org.mozilla.gecko.util.ThreadUtils;
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -134,16 +134,17 @@ gbjar.sources += [
     'db/LocalBrowserDB.java',
     'db/PasswordsProvider.java',
     'db/PerProfileDatabaseProvider.java',
     'db/PerProfileDatabases.java',
     'db/ReadingListProvider.java',
     'db/SharedBrowserDatabaseProvider.java',
     'db/SQLiteBridgeContentProvider.java',
     'db/TabsProvider.java',
+    'db/TopSitesCursorWrapper.java',
     'Distribution.java',
     'DoorHangerPopup.java',
     'DynamicToolbar.java',
     'EditBookmarkDialog.java',
     'EventDispatcher.java',
     'favicons/cache/FaviconCache.java',
     'favicons/cache/FaviconCacheElement.java',
     'favicons/cache/FaviconsForURL.java',