mobile/android/base/db/HomeProvider.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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 java.io.IOException;
michael@0 8
michael@0 9 import org.json.JSONArray;
michael@0 10 import org.json.JSONException;
michael@0 11 import org.json.JSONObject;
michael@0 12 import org.mozilla.gecko.R;
michael@0 13 import org.mozilla.gecko.db.BrowserContract.HomeItems;
michael@0 14 import org.mozilla.gecko.db.DBUtils;
michael@0 15 import org.mozilla.gecko.sqlite.SQLiteBridge;
michael@0 16 import org.mozilla.gecko.util.RawResource;
michael@0 17
michael@0 18 import android.content.ContentResolver;
michael@0 19 import android.content.ContentValues;
michael@0 20 import android.content.UriMatcher;
michael@0 21 import android.database.Cursor;
michael@0 22 import android.database.MatrixCursor;
michael@0 23 import android.net.Uri;
michael@0 24 import android.util.Log;
michael@0 25
michael@0 26 public class HomeProvider extends SQLiteBridgeContentProvider {
michael@0 27 private static final String LOGTAG = "GeckoHomeProvider";
michael@0 28
michael@0 29 // This should be kept in sync with the db version in mobile/android/modules/HomeProvider.jsm
michael@0 30 private static int DB_VERSION = 2;
michael@0 31 private static String DB_FILENAME = "home.sqlite";
michael@0 32 private static final String TELEMETRY_TAG = "SQLITEBRIDGE_PROVIDER_HOME";
michael@0 33
michael@0 34 private static final String TABLE_ITEMS = "items";
michael@0 35
michael@0 36 // Endpoint to return static fake data.
michael@0 37 static final int ITEMS_FAKE = 100;
michael@0 38 static final int ITEMS = 101;
michael@0 39 static final int ITEMS_ID = 102;
michael@0 40
michael@0 41 static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
michael@0 42
michael@0 43 static {
michael@0 44 URI_MATCHER.addURI(BrowserContract.HOME_AUTHORITY, "items/fake", ITEMS_FAKE);
michael@0 45 URI_MATCHER.addURI(BrowserContract.HOME_AUTHORITY, "items", ITEMS);
michael@0 46 URI_MATCHER.addURI(BrowserContract.HOME_AUTHORITY, "items/#", ITEMS_ID);
michael@0 47 }
michael@0 48
michael@0 49 public HomeProvider() {
michael@0 50 super(LOGTAG);
michael@0 51 }
michael@0 52
michael@0 53 @Override
michael@0 54 public String getType(Uri uri) {
michael@0 55 final int match = URI_MATCHER.match(uri);
michael@0 56
michael@0 57 switch (match) {
michael@0 58 case ITEMS_FAKE: {
michael@0 59 return HomeItems.CONTENT_TYPE;
michael@0 60 }
michael@0 61 case ITEMS: {
michael@0 62 return HomeItems.CONTENT_TYPE;
michael@0 63 }
michael@0 64 default: {
michael@0 65 throw new UnsupportedOperationException("Unknown type " + uri);
michael@0 66 }
michael@0 67 }
michael@0 68 }
michael@0 69
michael@0 70 @Override
michael@0 71 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
michael@0 72 final int match = URI_MATCHER.match(uri);
michael@0 73
michael@0 74 // If we're querying the fake items, don't try to get the database.
michael@0 75 if (match == ITEMS_FAKE) {
michael@0 76 return queryFakeItems(uri, projection, selection, selectionArgs, sortOrder);
michael@0 77 }
michael@0 78
michael@0 79 final String datasetId = uri.getQueryParameter(BrowserContract.PARAM_DATASET_ID);
michael@0 80 if (datasetId == null) {
michael@0 81 throw new IllegalArgumentException("All queries should contain a dataset ID parameter");
michael@0 82 }
michael@0 83
michael@0 84 selection = DBUtils.concatenateWhere(selection, HomeItems.DATASET_ID + " = ?");
michael@0 85 selectionArgs = DBUtils.appendSelectionArgs(selectionArgs,
michael@0 86 new String[] { datasetId });
michael@0 87
michael@0 88 // Otherwise, let the SQLiteContentProvider implementation take care of this query for us!
michael@0 89 Cursor c = super.query(uri, projection, selection, selectionArgs, sortOrder);
michael@0 90
michael@0 91 // SQLiteBridgeContentProvider may return a null Cursor if the database hasn't been created yet.
michael@0 92 // However, we need a non-null cursor in order to listen for notifications.
michael@0 93 if (c == null) {
michael@0 94 c = new MatrixCursor(projection != null ? projection : HomeItems.DEFAULT_PROJECTION);
michael@0 95 }
michael@0 96
michael@0 97 final ContentResolver cr = getContext().getContentResolver();
michael@0 98 c.setNotificationUri(cr, getDatasetNotificationUri(datasetId));
michael@0 99
michael@0 100 return c;
michael@0 101 }
michael@0 102
michael@0 103 /**
michael@0 104 * Returns a cursor populated with static fake data.
michael@0 105 */
michael@0 106 private Cursor queryFakeItems(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
michael@0 107 JSONArray items = null;
michael@0 108 try {
michael@0 109 final String jsonString = RawResource.getAsString(getContext(), R.raw.fake_home_items);
michael@0 110 items = new JSONArray(jsonString);
michael@0 111 } catch (IOException e) {
michael@0 112 Log.e(LOGTAG, "Error getting fake home items", e);
michael@0 113 return null;
michael@0 114 } catch (JSONException e) {
michael@0 115 Log.e(LOGTAG, "Error parsing fake_home_items.json", e);
michael@0 116 return null;
michael@0 117 }
michael@0 118
michael@0 119 final MatrixCursor c = new MatrixCursor(HomeItems.DEFAULT_PROJECTION);
michael@0 120 for (int i = 0; i < items.length(); i++) {
michael@0 121 try {
michael@0 122 final JSONObject item = items.getJSONObject(i);
michael@0 123 c.addRow(new Object[] {
michael@0 124 item.getInt("id"),
michael@0 125 item.getString("dataset_id"),
michael@0 126 item.getString("url"),
michael@0 127 item.getString("title"),
michael@0 128 item.getString("description"),
michael@0 129 item.getString("image_url"),
michael@0 130 item.getString("filter")
michael@0 131 });
michael@0 132 } catch (JSONException e) {
michael@0 133 Log.e(LOGTAG, "Error creating cursor row for fake home item", e);
michael@0 134 }
michael@0 135 }
michael@0 136 return c;
michael@0 137 }
michael@0 138
michael@0 139 /**
michael@0 140 * SQLiteBridgeContentProvider implementation
michael@0 141 */
michael@0 142
michael@0 143 @Override
michael@0 144 protected String getDBName(){
michael@0 145 return DB_FILENAME;
michael@0 146 }
michael@0 147
michael@0 148 @Override
michael@0 149 protected String getTelemetryPrefix() {
michael@0 150 return TELEMETRY_TAG;
michael@0 151 }
michael@0 152
michael@0 153 @Override
michael@0 154 protected int getDBVersion(){
michael@0 155 return DB_VERSION;
michael@0 156 }
michael@0 157
michael@0 158 @Override
michael@0 159 public String getTable(Uri uri) {
michael@0 160 final int match = URI_MATCHER.match(uri);
michael@0 161 switch (match) {
michael@0 162 case ITEMS: {
michael@0 163 return TABLE_ITEMS;
michael@0 164 }
michael@0 165 default: {
michael@0 166 throw new UnsupportedOperationException("Unknown table " + uri);
michael@0 167 }
michael@0 168 }
michael@0 169 }
michael@0 170
michael@0 171 @Override
michael@0 172 public String getSortOrder(Uri uri, String aRequested) {
michael@0 173 return null;
michael@0 174 }
michael@0 175
michael@0 176 @Override
michael@0 177 public void setupDefaults(Uri uri, ContentValues values) { }
michael@0 178
michael@0 179 @Override
michael@0 180 public void initGecko() { }
michael@0 181
michael@0 182 @Override
michael@0 183 public void onPreInsert(ContentValues values, Uri uri, SQLiteBridge db) { }
michael@0 184
michael@0 185 @Override
michael@0 186 public void onPreUpdate(ContentValues values, Uri uri, SQLiteBridge db) { }
michael@0 187
michael@0 188 @Override
michael@0 189 public void onPostQuery(Cursor cursor, Uri uri, SQLiteBridge db) { }
michael@0 190
michael@0 191 public static Uri getDatasetNotificationUri(String datasetId) {
michael@0 192 return Uri.withAppendedPath(HomeItems.CONTENT_URI, datasetId);
michael@0 193 }
michael@0 194 }

mercurial