mobile/android/base/db/TopSitesCursorWrapper.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/db/TopSitesCursorWrapper.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,231 @@
     1.4 +package org.mozilla.gecko.db;
     1.5 +
     1.6 +import android.database.Cursor;
     1.7 +import android.database.CursorWrapper;
     1.8 +import android.util.SparseArray;
     1.9 +
    1.10 +import org.mozilla.gecko.db.BrowserContract.Bookmarks;
    1.11 +import org.mozilla.gecko.db.BrowserDB.URLColumns;
    1.12 +
    1.13 +/**
    1.14 + * {@TopSitesCursorWrapper} is a cursor wrapper that merges
    1.15 + * the top and pinned sites cursors into one. It ensures the
    1.16 + * cursor will contain at least a given minimum number of
    1.17 + * entries.
    1.18 + */
    1.19 +public class TopSitesCursorWrapper extends CursorWrapper {
    1.20 +
    1.21 +    private static class PinnedSite {
    1.22 +        public final String title;
    1.23 +        public final String url;
    1.24 +
    1.25 +        public PinnedSite(String title, String url) {
    1.26 +            this.title = (title == null ? "" : title);
    1.27 +            this.url = (url == null ? "" : url);
    1.28 +        }
    1.29 +    }
    1.30 +
    1.31 +    // The cursor for the top sites query
    1.32 +    private final Cursor topCursor;
    1.33 +
    1.34 +    // Associates pinned sites and their respective positions
    1.35 +    private SparseArray<PinnedSite> pinnedSites;
    1.36 +
    1.37 +    // Current position of the cursor
    1.38 +    private int currentPosition = -1;
    1.39 +
    1.40 +    // The size of the cursor wrapper
    1.41 +    private final int count;
    1.42 +
    1.43 +    public TopSitesCursorWrapper(Cursor pinnedCursor, Cursor topCursor, int minSize) {
    1.44 +        super(topCursor);
    1.45 +
    1.46 +        setPinnedSites(pinnedCursor);
    1.47 +        this.topCursor = topCursor;
    1.48 +
    1.49 +        count = Math.max(minSize, pinnedSites.size() + topCursor.getCount());
    1.50 +    }
    1.51 +
    1.52 +    public void setPinnedSites(Cursor c) {
    1.53 +        pinnedSites = new SparseArray<PinnedSite>();
    1.54 +
    1.55 +        if (c == null) {
    1.56 +            return;
    1.57 +        }
    1.58 +
    1.59 +        try {
    1.60 +            if (c.getCount() <= 0) {
    1.61 +                return;
    1.62 +            }
    1.63 +
    1.64 +            c.moveToPosition(0);
    1.65 +            do {
    1.66 +                final int pos = c.getInt(c.getColumnIndex(Bookmarks.POSITION));
    1.67 +                final String url = c.getString(c.getColumnIndex(URLColumns.URL));
    1.68 +                final String title = c.getString(c.getColumnIndex(URLColumns.TITLE));
    1.69 +                pinnedSites.put(pos, new PinnedSite(title, url));
    1.70 +            } while (c.moveToNext());
    1.71 +        } finally {
    1.72 +            c.close();
    1.73 +        }
    1.74 +    }
    1.75 +
    1.76 +    public boolean hasPinnedSites() {
    1.77 +        return (pinnedSites != null && pinnedSites.size() > 0);
    1.78 +    }
    1.79 +
    1.80 +    public PinnedSite getPinnedSite(int position) {
    1.81 +        if (!hasPinnedSites()) {
    1.82 +            return null;
    1.83 +        }
    1.84 +
    1.85 +        return pinnedSites.get(position);
    1.86 +    }
    1.87 +
    1.88 +    public boolean isPinned() {
    1.89 +        return (pinnedSites.get(currentPosition) != null);
    1.90 +    }
    1.91 +
    1.92 +    private int getPinnedBefore(int position) {
    1.93 +        int numFound = 0;
    1.94 +        if (!hasPinnedSites()) {
    1.95 +            return numFound;
    1.96 +        }
    1.97 +
    1.98 +        for (int i = 0; i < position; i++) {
    1.99 +            if (pinnedSites.get(i) != null) {
   1.100 +                numFound++;
   1.101 +            }
   1.102 +        }
   1.103 +
   1.104 +        return numFound;
   1.105 +    }
   1.106 +
   1.107 +    @Override
   1.108 +    public int getPosition() {
   1.109 +        return currentPosition;
   1.110 +    }
   1.111 +
   1.112 +    @Override
   1.113 +    public int getCount() {
   1.114 +        return count;
   1.115 +    }
   1.116 +
   1.117 +    @Override
   1.118 +    public boolean isAfterLast() {
   1.119 +        return (currentPosition >= count);
   1.120 +    }
   1.121 +
   1.122 +    @Override
   1.123 +    public boolean isBeforeFirst() {
   1.124 +        return (currentPosition < 0);
   1.125 +    }
   1.126 +
   1.127 +    @Override
   1.128 +    public boolean isLast() {
   1.129 +        return (currentPosition == count - 1);
   1.130 +    }
   1.131 +
   1.132 +    @Override
   1.133 +    public boolean moveToNext() {
   1.134 +        return moveToPosition(currentPosition + 1);
   1.135 +    }
   1.136 +
   1.137 +    @Override
   1.138 +    public boolean moveToPrevious() {
   1.139 +        return moveToPosition(currentPosition - 1);
   1.140 +    }
   1.141 +
   1.142 +    @Override
   1.143 +    public boolean move(int offset) {
   1.144 +        return moveToPosition(currentPosition + offset);
   1.145 +    }
   1.146 +
   1.147 +    @Override
   1.148 +    public boolean moveToFirst() {
   1.149 +        return moveToPosition(0);
   1.150 +    }
   1.151 +
   1.152 +    @Override
   1.153 +    public boolean moveToLast() {
   1.154 +        return moveToPosition(count - 1);
   1.155 +    }
   1.156 +
   1.157 +    @Override
   1.158 +    public boolean moveToPosition(int position) {
   1.159 +        currentPosition = position;
   1.160 +
   1.161 +        // Move the real cursor as if we were stepping through it to this position.
   1.162 +        // Account for pinned sites, and be careful to update its position to the
   1.163 +        // minimum or maximum position, even if we're moving beyond its bounds.
   1.164 +        final int before = getPinnedBefore(position);
   1.165 +        final int p2 = position - before;
   1.166 +
   1.167 +        if (p2 <= -1) {
   1.168 +            super.moveToPosition(-1);
   1.169 +        } else if (p2 >= topCursor.getCount()) {
   1.170 +            super.moveToPosition(topCursor.getCount());
   1.171 +        } else {
   1.172 +            super.moveToPosition(p2);
   1.173 +        }
   1.174 +
   1.175 +        return (!isBeforeFirst() && !isAfterLast());
   1.176 +    }
   1.177 +
   1.178 +    @Override
   1.179 +    public long getLong(int columnIndex) {
   1.180 +        if (hasPinnedSites()) {
   1.181 +            final PinnedSite site = getPinnedSite(currentPosition);
   1.182 +
   1.183 +            if (site != null) {
   1.184 +                return 0;
   1.185 +            }
   1.186 +        }
   1.187 +
   1.188 +        if (!super.isBeforeFirst() && !super.isAfterLast()) {
   1.189 +            return super.getLong(columnIndex);
   1.190 +        }
   1.191 +
   1.192 +        return 0;
   1.193 +    }
   1.194 +
   1.195 +    @Override
   1.196 +    public int getInt(int columnIndex) {
   1.197 +        if (hasPinnedSites()) {
   1.198 +            final PinnedSite site = getPinnedSite(currentPosition);
   1.199 +
   1.200 +            if (site != null) {
   1.201 +                return 0;
   1.202 +            }
   1.203 +        }
   1.204 +
   1.205 +        if (!super.isBeforeFirst() && !super.isAfterLast()) {
   1.206 +            return super.getInt(columnIndex);
   1.207 +        }
   1.208 +
   1.209 +        return 0;
   1.210 +    }
   1.211 +
   1.212 +    @Override
   1.213 +    public String getString(int columnIndex) {
   1.214 +        if (hasPinnedSites()) {
   1.215 +            final PinnedSite site = getPinnedSite(currentPosition);
   1.216 +
   1.217 +            if (site != null) {
   1.218 +                if (columnIndex == topCursor.getColumnIndex(URLColumns.URL)) {
   1.219 +                    return site.url;
   1.220 +                } else if (columnIndex == topCursor.getColumnIndex(URLColumns.TITLE)) {
   1.221 +                    return site.title;
   1.222 +                }
   1.223 +
   1.224 +                return "";
   1.225 +            }
   1.226 +        }
   1.227 +
   1.228 +        if (!super.isBeforeFirst() && !super.isAfterLast()) {
   1.229 +            return super.getString(columnIndex);
   1.230 +        }
   1.231 +
   1.232 +        return "";
   1.233 +    }
   1.234 +}

mercurial