Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.db; |
michael@0 | 6 | |
michael@0 | 7 | import org.mozilla.gecko.db.BrowserContract.ReadingListItems; |
michael@0 | 8 | import org.mozilla.gecko.sync.Utils; |
michael@0 | 9 | |
michael@0 | 10 | import android.content.ContentUris; |
michael@0 | 11 | import android.content.ContentValues; |
michael@0 | 12 | import android.content.UriMatcher; |
michael@0 | 13 | import android.database.Cursor; |
michael@0 | 14 | import android.database.sqlite.SQLiteDatabase; |
michael@0 | 15 | import android.database.sqlite.SQLiteQueryBuilder; |
michael@0 | 16 | import android.net.Uri; |
michael@0 | 17 | import android.text.TextUtils; |
michael@0 | 18 | |
michael@0 | 19 | public class ReadingListProvider extends SharedBrowserDatabaseProvider { |
michael@0 | 20 | static final String TABLE_READING_LIST = ReadingListItems.TABLE_NAME; |
michael@0 | 21 | |
michael@0 | 22 | static final int ITEMS = 101; |
michael@0 | 23 | static final int ITEMS_ID = 102; |
michael@0 | 24 | static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH); |
michael@0 | 25 | |
michael@0 | 26 | static { |
michael@0 | 27 | URI_MATCHER.addURI(BrowserContract.READING_LIST_AUTHORITY, "items", ITEMS); |
michael@0 | 28 | URI_MATCHER.addURI(BrowserContract.READING_LIST_AUTHORITY, "items/#", ITEMS_ID); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | /** |
michael@0 | 32 | * Updates items that match the selection criteria. If no such items is found |
michael@0 | 33 | * one is inserted with the attributes passed in. Returns 0 if no item updated. |
michael@0 | 34 | * |
michael@0 | 35 | * @return Number of items updated or inserted |
michael@0 | 36 | */ |
michael@0 | 37 | public int updateOrInsertItem(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
michael@0 | 38 | int updated = updateItems(uri, values, selection, selectionArgs); |
michael@0 | 39 | if (updated <= 0) { |
michael@0 | 40 | updated = insertItem(uri, values) != -1 ? 1 : 0; |
michael@0 | 41 | } |
michael@0 | 42 | return updated; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | /** |
michael@0 | 46 | * Updates items that match the selection criteria. |
michael@0 | 47 | * |
michael@0 | 48 | * @return Number of items updated or inserted |
michael@0 | 49 | */ |
michael@0 | 50 | public int updateItems(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
michael@0 | 51 | trace("Updating ReadingListItems on URI: " + uri); |
michael@0 | 52 | final SQLiteDatabase db = getWritableDatabase(uri); |
michael@0 | 53 | if (!values.containsKey(ReadingListItems.DATE_MODIFIED)) { |
michael@0 | 54 | values.put(ReadingListItems.DATE_MODIFIED, System.currentTimeMillis()); |
michael@0 | 55 | } |
michael@0 | 56 | return db.update(TABLE_READING_LIST, values, selection, selectionArgs); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | /** |
michael@0 | 60 | * Inserts a new item into the DB. DATE_CREATED, DATE_MODIFIED |
michael@0 | 61 | * and GUID fields are generated if they are not specified. |
michael@0 | 62 | * |
michael@0 | 63 | * @return ID of the newly inserted item |
michael@0 | 64 | */ |
michael@0 | 65 | long insertItem(Uri uri, ContentValues values) { |
michael@0 | 66 | long now = System.currentTimeMillis(); |
michael@0 | 67 | if (!values.containsKey(ReadingListItems.DATE_CREATED)) { |
michael@0 | 68 | values.put(ReadingListItems.DATE_CREATED, now); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | if (!values.containsKey(ReadingListItems.DATE_MODIFIED)) { |
michael@0 | 72 | values.put(ReadingListItems.DATE_MODIFIED, now); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | if (!values.containsKey(ReadingListItems.GUID)) { |
michael@0 | 76 | values.put(ReadingListItems.GUID, Utils.generateGuid()); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | String url = values.getAsString(ReadingListItems.URL); |
michael@0 | 80 | debug("Inserting item in database with URL: " + url); |
michael@0 | 81 | return getWritableDatabase(uri) |
michael@0 | 82 | .insertOrThrow(TABLE_READING_LIST, null, values); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | /** |
michael@0 | 86 | * Deletes items. Item is marked as 'deleted' so that sync can |
michael@0 | 87 | * detect the change. |
michael@0 | 88 | * |
michael@0 | 89 | * @return Number of deleted items |
michael@0 | 90 | */ |
michael@0 | 91 | int deleteItems(Uri uri, String selection, String[] selectionArgs) { |
michael@0 | 92 | debug("Deleting item entry for URI: " + uri); |
michael@0 | 93 | final SQLiteDatabase db = getWritableDatabase(uri); |
michael@0 | 94 | |
michael@0 | 95 | if (isCallerSync(uri)) { |
michael@0 | 96 | return db.delete(TABLE_READING_LIST, selection, selectionArgs); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | debug("Marking item entry as deleted for URI: " + uri); |
michael@0 | 100 | ContentValues values = new ContentValues(); |
michael@0 | 101 | values.put(ReadingListItems.IS_DELETED, 1); |
michael@0 | 102 | |
michael@0 | 103 | cleanUpSomeDeletedRecords(uri, TABLE_READING_LIST); |
michael@0 | 104 | return updateItems(uri, values, selection, selectionArgs); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | @Override |
michael@0 | 108 | @SuppressWarnings("fallthrough") |
michael@0 | 109 | public int updateInTransaction(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
michael@0 | 110 | trace("Calling update in transaction on URI: " + uri); |
michael@0 | 111 | |
michael@0 | 112 | int updated = 0; |
michael@0 | 113 | int match = URI_MATCHER.match(uri); |
michael@0 | 114 | |
michael@0 | 115 | switch (match) { |
michael@0 | 116 | case ITEMS_ID: |
michael@0 | 117 | debug("Update on ITEMS_ID: " + uri); |
michael@0 | 118 | selection = DBUtils.concatenateWhere(selection, TABLE_READING_LIST + "._id = ?"); |
michael@0 | 119 | selectionArgs = DBUtils.appendSelectionArgs(selectionArgs, |
michael@0 | 120 | new String[] { Long.toString(ContentUris.parseId(uri)) }); |
michael@0 | 121 | |
michael@0 | 122 | case ITEMS: { |
michael@0 | 123 | debug("Updating ITEMS: " + uri); |
michael@0 | 124 | updated = shouldUpdateOrInsert(uri) ? |
michael@0 | 125 | updateOrInsertItem(uri, values, selection, selectionArgs) : |
michael@0 | 126 | updateItems(uri, values, selection, selectionArgs); |
michael@0 | 127 | break; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | default: |
michael@0 | 131 | throw new UnsupportedOperationException("Unknown update URI " + uri); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | debug("Updated " + updated + " rows for URI: " + uri); |
michael@0 | 135 | return updated; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | |
michael@0 | 139 | @Override |
michael@0 | 140 | @SuppressWarnings("fallthrough") |
michael@0 | 141 | public int deleteInTransaction(Uri uri, String selection, String[] selectionArgs) { |
michael@0 | 142 | trace("Calling delete in transaction on URI: " + uri); |
michael@0 | 143 | |
michael@0 | 144 | int numDeleted = 0; |
michael@0 | 145 | int match = URI_MATCHER.match(uri); |
michael@0 | 146 | |
michael@0 | 147 | switch (match) { |
michael@0 | 148 | case ITEMS_ID: |
michael@0 | 149 | debug("Deleting on ITEMS_ID: " + uri); |
michael@0 | 150 | selection = DBUtils.concatenateWhere(selection, TABLE_READING_LIST + "._id = ?"); |
michael@0 | 151 | selectionArgs = DBUtils.appendSelectionArgs(selectionArgs, |
michael@0 | 152 | new String[] { Long.toString(ContentUris.parseId(uri)) }); |
michael@0 | 153 | |
michael@0 | 154 | case ITEMS: |
michael@0 | 155 | debug("Deleting ITEMS: " + uri); |
michael@0 | 156 | numDeleted = deleteItems(uri, selection, selectionArgs); |
michael@0 | 157 | break; |
michael@0 | 158 | |
michael@0 | 159 | default: |
michael@0 | 160 | throw new UnsupportedOperationException("Unknown update URI " + uri); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | debug("Deleted " + numDeleted + " rows for URI: " + uri); |
michael@0 | 164 | return numDeleted; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | @Override |
michael@0 | 168 | public Uri insertInTransaction(Uri uri, ContentValues values) { |
michael@0 | 169 | trace("Calling insert in transaction on URI: " + uri); |
michael@0 | 170 | long id = -1; |
michael@0 | 171 | int match = URI_MATCHER.match(uri); |
michael@0 | 172 | |
michael@0 | 173 | switch (match) { |
michael@0 | 174 | case ITEMS: |
michael@0 | 175 | trace("Insert on ITEMS: " + uri); |
michael@0 | 176 | id = insertItem(uri, values); |
michael@0 | 177 | break; |
michael@0 | 178 | |
michael@0 | 179 | default: |
michael@0 | 180 | throw new UnsupportedOperationException("Unknown insert URI " + uri); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | debug("Inserted ID in database: " + id); |
michael@0 | 184 | |
michael@0 | 185 | if (id >= 0) { |
michael@0 | 186 | return ContentUris.withAppendedId(uri, id); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | return null; |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | @Override |
michael@0 | 193 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { |
michael@0 | 194 | String groupBy = null; |
michael@0 | 195 | SQLiteDatabase db = getReadableDatabase(uri); |
michael@0 | 196 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
michael@0 | 197 | String limit = uri.getQueryParameter(BrowserContract.PARAM_LIMIT); |
michael@0 | 198 | |
michael@0 | 199 | final int match = URI_MATCHER.match(uri); |
michael@0 | 200 | switch (match) { |
michael@0 | 201 | case ITEMS_ID: |
michael@0 | 202 | trace("Query on ITEMS_ID: " + uri); |
michael@0 | 203 | selection = DBUtils.concatenateWhere(selection, ReadingListItems._ID + " = ?"); |
michael@0 | 204 | selectionArgs = DBUtils.appendSelectionArgs(selectionArgs, |
michael@0 | 205 | new String[] { Long.toString(ContentUris.parseId(uri)) }); |
michael@0 | 206 | |
michael@0 | 207 | case ITEMS: |
michael@0 | 208 | trace("Query on ITEMS: " + uri); |
michael@0 | 209 | if (!shouldShowDeleted(uri)) |
michael@0 | 210 | selection = DBUtils.concatenateWhere(ReadingListItems.IS_DELETED + " = 0", selection); |
michael@0 | 211 | break; |
michael@0 | 212 | |
michael@0 | 213 | default: |
michael@0 | 214 | throw new UnsupportedOperationException("Unknown query URI " + uri); |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | if (TextUtils.isEmpty(sortOrder)) { |
michael@0 | 218 | sortOrder = ReadingListItems.DEFAULT_SORT_ORDER; |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | trace("Running built query."); |
michael@0 | 222 | qb.setTables(TABLE_READING_LIST); |
michael@0 | 223 | Cursor cursor = qb.query(db, projection, selection, selectionArgs, groupBy, null, sortOrder, limit); |
michael@0 | 224 | cursor.setNotificationUri(getContext().getContentResolver(), uri); |
michael@0 | 225 | |
michael@0 | 226 | return cursor; |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | @Override |
michael@0 | 230 | public String getType(Uri uri) { |
michael@0 | 231 | trace("Getting URI type: " + uri); |
michael@0 | 232 | |
michael@0 | 233 | final int match = URI_MATCHER.match(uri); |
michael@0 | 234 | switch (match) { |
michael@0 | 235 | case ITEMS: |
michael@0 | 236 | trace("URI is ITEMS: " + uri); |
michael@0 | 237 | return ReadingListItems.CONTENT_TYPE; |
michael@0 | 238 | |
michael@0 | 239 | case ITEMS_ID: |
michael@0 | 240 | trace("URI is ITEMS_ID: " + uri); |
michael@0 | 241 | return ReadingListItems.CONTENT_ITEM_TYPE; |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | debug("URI has unrecognized type: " + uri); |
michael@0 | 245 | return null; |
michael@0 | 246 | } |
michael@0 | 247 | } |