Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | package org.mozilla.gecko.db; |
michael@0 | 2 | |
michael@0 | 3 | import android.database.Cursor; |
michael@0 | 4 | import android.database.CursorWrapper; |
michael@0 | 5 | import android.util.SparseArray; |
michael@0 | 6 | |
michael@0 | 7 | import org.mozilla.gecko.db.BrowserContract.Bookmarks; |
michael@0 | 8 | import org.mozilla.gecko.db.BrowserDB.URLColumns; |
michael@0 | 9 | |
michael@0 | 10 | /** |
michael@0 | 11 | * {@TopSitesCursorWrapper} is a cursor wrapper that merges |
michael@0 | 12 | * the top and pinned sites cursors into one. It ensures the |
michael@0 | 13 | * cursor will contain at least a given minimum number of |
michael@0 | 14 | * entries. |
michael@0 | 15 | */ |
michael@0 | 16 | public class TopSitesCursorWrapper extends CursorWrapper { |
michael@0 | 17 | |
michael@0 | 18 | private static class PinnedSite { |
michael@0 | 19 | public final String title; |
michael@0 | 20 | public final String url; |
michael@0 | 21 | |
michael@0 | 22 | public PinnedSite(String title, String url) { |
michael@0 | 23 | this.title = (title == null ? "" : title); |
michael@0 | 24 | this.url = (url == null ? "" : url); |
michael@0 | 25 | } |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | // The cursor for the top sites query |
michael@0 | 29 | private final Cursor topCursor; |
michael@0 | 30 | |
michael@0 | 31 | // Associates pinned sites and their respective positions |
michael@0 | 32 | private SparseArray<PinnedSite> pinnedSites; |
michael@0 | 33 | |
michael@0 | 34 | // Current position of the cursor |
michael@0 | 35 | private int currentPosition = -1; |
michael@0 | 36 | |
michael@0 | 37 | // The size of the cursor wrapper |
michael@0 | 38 | private final int count; |
michael@0 | 39 | |
michael@0 | 40 | public TopSitesCursorWrapper(Cursor pinnedCursor, Cursor topCursor, int minSize) { |
michael@0 | 41 | super(topCursor); |
michael@0 | 42 | |
michael@0 | 43 | setPinnedSites(pinnedCursor); |
michael@0 | 44 | this.topCursor = topCursor; |
michael@0 | 45 | |
michael@0 | 46 | count = Math.max(minSize, pinnedSites.size() + topCursor.getCount()); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | public void setPinnedSites(Cursor c) { |
michael@0 | 50 | pinnedSites = new SparseArray<PinnedSite>(); |
michael@0 | 51 | |
michael@0 | 52 | if (c == null) { |
michael@0 | 53 | return; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | try { |
michael@0 | 57 | if (c.getCount() <= 0) { |
michael@0 | 58 | return; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | c.moveToPosition(0); |
michael@0 | 62 | do { |
michael@0 | 63 | final int pos = c.getInt(c.getColumnIndex(Bookmarks.POSITION)); |
michael@0 | 64 | final String url = c.getString(c.getColumnIndex(URLColumns.URL)); |
michael@0 | 65 | final String title = c.getString(c.getColumnIndex(URLColumns.TITLE)); |
michael@0 | 66 | pinnedSites.put(pos, new PinnedSite(title, url)); |
michael@0 | 67 | } while (c.moveToNext()); |
michael@0 | 68 | } finally { |
michael@0 | 69 | c.close(); |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | public boolean hasPinnedSites() { |
michael@0 | 74 | return (pinnedSites != null && pinnedSites.size() > 0); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | public PinnedSite getPinnedSite(int position) { |
michael@0 | 78 | if (!hasPinnedSites()) { |
michael@0 | 79 | return null; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | return pinnedSites.get(position); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | public boolean isPinned() { |
michael@0 | 86 | return (pinnedSites.get(currentPosition) != null); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | private int getPinnedBefore(int position) { |
michael@0 | 90 | int numFound = 0; |
michael@0 | 91 | if (!hasPinnedSites()) { |
michael@0 | 92 | return numFound; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | for (int i = 0; i < position; i++) { |
michael@0 | 96 | if (pinnedSites.get(i) != null) { |
michael@0 | 97 | numFound++; |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | return numFound; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | @Override |
michael@0 | 105 | public int getPosition() { |
michael@0 | 106 | return currentPosition; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | @Override |
michael@0 | 110 | public int getCount() { |
michael@0 | 111 | return count; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | @Override |
michael@0 | 115 | public boolean isAfterLast() { |
michael@0 | 116 | return (currentPosition >= count); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | @Override |
michael@0 | 120 | public boolean isBeforeFirst() { |
michael@0 | 121 | return (currentPosition < 0); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | @Override |
michael@0 | 125 | public boolean isLast() { |
michael@0 | 126 | return (currentPosition == count - 1); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | @Override |
michael@0 | 130 | public boolean moveToNext() { |
michael@0 | 131 | return moveToPosition(currentPosition + 1); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | @Override |
michael@0 | 135 | public boolean moveToPrevious() { |
michael@0 | 136 | return moveToPosition(currentPosition - 1); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | @Override |
michael@0 | 140 | public boolean move(int offset) { |
michael@0 | 141 | return moveToPosition(currentPosition + offset); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | @Override |
michael@0 | 145 | public boolean moveToFirst() { |
michael@0 | 146 | return moveToPosition(0); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | @Override |
michael@0 | 150 | public boolean moveToLast() { |
michael@0 | 151 | return moveToPosition(count - 1); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | @Override |
michael@0 | 155 | public boolean moveToPosition(int position) { |
michael@0 | 156 | currentPosition = position; |
michael@0 | 157 | |
michael@0 | 158 | // Move the real cursor as if we were stepping through it to this position. |
michael@0 | 159 | // Account for pinned sites, and be careful to update its position to the |
michael@0 | 160 | // minimum or maximum position, even if we're moving beyond its bounds. |
michael@0 | 161 | final int before = getPinnedBefore(position); |
michael@0 | 162 | final int p2 = position - before; |
michael@0 | 163 | |
michael@0 | 164 | if (p2 <= -1) { |
michael@0 | 165 | super.moveToPosition(-1); |
michael@0 | 166 | } else if (p2 >= topCursor.getCount()) { |
michael@0 | 167 | super.moveToPosition(topCursor.getCount()); |
michael@0 | 168 | } else { |
michael@0 | 169 | super.moveToPosition(p2); |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | return (!isBeforeFirst() && !isAfterLast()); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | @Override |
michael@0 | 176 | public long getLong(int columnIndex) { |
michael@0 | 177 | if (hasPinnedSites()) { |
michael@0 | 178 | final PinnedSite site = getPinnedSite(currentPosition); |
michael@0 | 179 | |
michael@0 | 180 | if (site != null) { |
michael@0 | 181 | return 0; |
michael@0 | 182 | } |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | if (!super.isBeforeFirst() && !super.isAfterLast()) { |
michael@0 | 186 | return super.getLong(columnIndex); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | return 0; |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | @Override |
michael@0 | 193 | public int getInt(int columnIndex) { |
michael@0 | 194 | if (hasPinnedSites()) { |
michael@0 | 195 | final PinnedSite site = getPinnedSite(currentPosition); |
michael@0 | 196 | |
michael@0 | 197 | if (site != null) { |
michael@0 | 198 | return 0; |
michael@0 | 199 | } |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | if (!super.isBeforeFirst() && !super.isAfterLast()) { |
michael@0 | 203 | return super.getInt(columnIndex); |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | return 0; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | @Override |
michael@0 | 210 | public String getString(int columnIndex) { |
michael@0 | 211 | if (hasPinnedSites()) { |
michael@0 | 212 | final PinnedSite site = getPinnedSite(currentPosition); |
michael@0 | 213 | |
michael@0 | 214 | if (site != null) { |
michael@0 | 215 | if (columnIndex == topCursor.getColumnIndex(URLColumns.URL)) { |
michael@0 | 216 | return site.url; |
michael@0 | 217 | } else if (columnIndex == topCursor.getColumnIndex(URLColumns.TITLE)) { |
michael@0 | 218 | return site.title; |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | return ""; |
michael@0 | 222 | } |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | if (!super.isBeforeFirst() && !super.isAfterLast()) { |
michael@0 | 226 | return super.getString(columnIndex); |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | return ""; |
michael@0 | 230 | } |
michael@0 | 231 | } |