2013-12-19 22:51:09 +00:00
|
|
|
/* 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;
|
|
|
|
|
2014-02-23 03:53:00 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2013-12-19 22:51:09 +00:00
|
|
|
|
2014-01-24 23:11:15 +00:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2014-02-23 03:53:00 +00:00
|
|
|
import org.mozilla.gecko.R;
|
|
|
|
import org.mozilla.gecko.db.BrowserContract.HomeItems;
|
|
|
|
import org.mozilla.gecko.sqlite.SQLiteBridge;
|
2014-01-24 23:11:15 +00:00
|
|
|
|
2013-12-19 22:51:09 +00:00
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.UriMatcher;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.MatrixCursor;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2014-01-27 21:49:34 +00:00
|
|
|
public class HomeProvider extends SQLiteBridgeContentProvider {
|
|
|
|
private static final String LOGTAG = "GeckoHomeProvider";
|
2013-12-19 22:51:09 +00:00
|
|
|
|
2014-01-24 23:16:17 +00:00
|
|
|
// This should be kept in sync with the db version in mobile/android/modules/HomeProvider.jsm
|
2014-03-04 16:57:43 +00:00
|
|
|
private static int DB_VERSION = 2;
|
2014-01-24 23:16:17 +00:00
|
|
|
private static String DB_FILENAME = "home.sqlite";
|
2013-12-19 22:51:09 +00:00
|
|
|
|
2014-01-24 23:16:17 +00:00
|
|
|
private static final String TABLE_ITEMS = "items";
|
2013-12-19 22:51:09 +00:00
|
|
|
|
|
|
|
// Endpoint to return static fake data.
|
|
|
|
static final int ITEMS_FAKE = 100;
|
|
|
|
static final int ITEMS = 101;
|
|
|
|
static final int ITEMS_ID = 102;
|
|
|
|
|
|
|
|
static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
|
|
|
|
static {
|
2014-01-27 21:50:56 +00:00
|
|
|
URI_MATCHER.addURI(BrowserContract.HOME_AUTHORITY, "items/fake", ITEMS_FAKE);
|
|
|
|
URI_MATCHER.addURI(BrowserContract.HOME_AUTHORITY, "items", ITEMS);
|
|
|
|
URI_MATCHER.addURI(BrowserContract.HOME_AUTHORITY, "items/#", ITEMS_ID);
|
2013-12-19 22:51:09 +00:00
|
|
|
}
|
|
|
|
|
2014-01-27 21:49:34 +00:00
|
|
|
public HomeProvider() {
|
2014-01-24 23:16:17 +00:00
|
|
|
super(LOGTAG);
|
2013-12-19 22:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
final int match = URI_MATCHER.match(uri);
|
2014-01-24 23:16:17 +00:00
|
|
|
|
2013-12-19 22:51:09 +00:00
|
|
|
switch (match) {
|
|
|
|
case ITEMS_FAKE: {
|
2014-01-27 21:50:56 +00:00
|
|
|
return HomeItems.CONTENT_TYPE;
|
2013-12-19 22:51:09 +00:00
|
|
|
}
|
|
|
|
case ITEMS: {
|
2014-01-27 21:50:56 +00:00
|
|
|
return HomeItems.CONTENT_TYPE;
|
2013-12-19 22:51:09 +00:00
|
|
|
}
|
2014-01-24 23:16:17 +00:00
|
|
|
default: {
|
|
|
|
throw new UnsupportedOperationException("Unknown type " + uri);
|
2013-12-19 22:51:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
|
|
|
|
final int match = URI_MATCHER.match(uri);
|
|
|
|
|
|
|
|
// If we're querying the fake items, don't try to get the database.
|
|
|
|
if (match == ITEMS_FAKE) {
|
|
|
|
return queryFakeItems(uri, projection, selection, selectionArgs, sortOrder);
|
|
|
|
}
|
|
|
|
|
2014-01-24 23:16:17 +00:00
|
|
|
// Otherwise, let the SQLiteContentProvider implementation take care of this query for us!
|
|
|
|
return super.query(uri, projection, selection, selectionArgs, sortOrder);
|
2013-12-19 22:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a cursor populated with static fake data.
|
|
|
|
*/
|
|
|
|
private Cursor queryFakeItems(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
|
2014-01-24 23:11:15 +00:00
|
|
|
JSONArray items = null;
|
|
|
|
try {
|
|
|
|
items = new JSONArray(getRawFakeItems());
|
|
|
|
} catch (IOException e) {
|
2014-01-27 21:50:56 +00:00
|
|
|
Log.e(LOGTAG, "Error getting fake home items", e);
|
2014-01-24 23:11:15 +00:00
|
|
|
return null;
|
|
|
|
} catch (JSONException e) {
|
2014-01-27 21:50:56 +00:00
|
|
|
Log.e(LOGTAG, "Error parsing fake_home_items.json", e);
|
2014-01-24 23:11:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-12-19 22:51:09 +00:00
|
|
|
final String[] itemsColumns = new String[] {
|
2014-01-27 21:50:56 +00:00
|
|
|
HomeItems._ID,
|
|
|
|
HomeItems.DATASET_ID,
|
|
|
|
HomeItems.URL,
|
|
|
|
HomeItems.TITLE,
|
2014-02-06 21:06:39 +00:00
|
|
|
HomeItems.DESCRIPTION,
|
2014-02-05 19:00:06 +00:00
|
|
|
HomeItems.IMAGE_URL,
|
|
|
|
HomeItems.FILTER
|
2013-12-19 22:51:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
final MatrixCursor c = new MatrixCursor(itemsColumns);
|
2014-01-24 23:11:15 +00:00
|
|
|
for (int i = 0; i < items.length(); i++) {
|
|
|
|
try {
|
|
|
|
final JSONObject item = items.getJSONObject(i);
|
|
|
|
c.addRow(new Object[] {
|
|
|
|
item.getInt("id"),
|
2014-01-27 21:50:56 +00:00
|
|
|
item.getString("dataset_id"),
|
2014-01-24 23:11:15 +00:00
|
|
|
item.getString("url"),
|
2014-01-27 21:50:56 +00:00
|
|
|
item.getString("title"),
|
2014-02-06 21:06:39 +00:00
|
|
|
item.getString("description"),
|
2014-02-05 19:00:06 +00:00
|
|
|
item.getString("image_url"),
|
|
|
|
item.getString("filter")
|
2014-01-24 23:11:15 +00:00
|
|
|
});
|
|
|
|
} catch (JSONException e) {
|
2014-01-27 21:50:56 +00:00
|
|
|
Log.e(LOGTAG, "Error creating cursor row for fake home item", e);
|
2014-01-24 23:11:15 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-19 22:51:09 +00:00
|
|
|
return c;
|
|
|
|
}
|
2014-01-24 23:11:15 +00:00
|
|
|
|
|
|
|
private String getRawFakeItems() throws IOException {
|
2014-01-27 21:50:56 +00:00
|
|
|
final InputStream inputStream = getContext().getResources().openRawResource(R.raw.fake_home_items);
|
2014-01-24 23:11:15 +00:00
|
|
|
final byte[] buffer = new byte[1024];
|
|
|
|
StringBuilder s = new StringBuilder();
|
|
|
|
int count;
|
|
|
|
|
|
|
|
while ((count = inputStream.read(buffer)) != -1) {
|
|
|
|
s.append(new String(buffer, 0, count));
|
|
|
|
}
|
|
|
|
return s.toString();
|
|
|
|
}
|
2014-01-24 23:16:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SQLiteBridgeContentProvider implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected String getDBName(){
|
|
|
|
return DB_FILENAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected int getDBVersion(){
|
|
|
|
return DB_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getTable(Uri uri) {
|
|
|
|
final int match = URI_MATCHER.match(uri);
|
|
|
|
switch (match) {
|
|
|
|
case ITEMS: {
|
|
|
|
return TABLE_ITEMS;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new UnsupportedOperationException("Unknown table " + uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getSortOrder(Uri uri, String aRequested) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setupDefaults(Uri uri, ContentValues values) { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void initGecko() { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPreInsert(ContentValues values, Uri uri, SQLiteBridge db) { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPreUpdate(ContentValues values, Uri uri, SQLiteBridge db) { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPostQuery(Cursor cursor, Uri uri, SQLiteBridge db) { }
|
|
|
|
|
|
|
|
|
2013-12-19 22:51:09 +00:00
|
|
|
}
|