SubscribedFeedsProviderpublic class SubscribedFeedsProvider extends AbstractSyncableContentProvider Manages a list of feeds for which this client is interested in receiving
change notifications. |
Fields Summary |
---|
private static final String | TAG | private static final String | DATABASE_NAME | private static final int | DATABASE_VERSION | private static final int | FEEDS | private static final int | FEED_ID | private static final int | DELETED_FEEDS | private static final int | ACCOUNTS | private static final Map | ACCOUNTS_PROJECTION_MAP | private static final android.content.UriMatcher | sURLMatcher | private static String | sFeedsTable | private static android.net.Uri | sFeedsUrl | private static String | sDeletedFeedsTable | private static android.net.Uri | sDeletedFeedsUrl |
Constructors Summary |
---|
public SubscribedFeedsProvider()
super(DATABASE_NAME, DATABASE_VERSION, sFeedsUrl);
|
Methods Summary |
---|
private static java.lang.String | addIdToWhereClause(java.lang.String id, java.lang.String where)
if (id != null) {
StringBuilder whereSb = new StringBuilder("_id=");
whereSb.append(id);
if (!TextUtils.isEmpty(where)) {
whereSb.append(" AND (");
whereSb.append(where);
whereSb.append(')");
}
return whereSb.toString();
} else {
return where;
}
| protected void | bootstrapDatabase(android.database.sqlite.SQLiteDatabase db)
super.bootstrapDatabase(db);
db.execSQL("CREATE TABLE feeds (" +
"_id INTEGER PRIMARY KEY," +
"_sync_account TEXT," + // From the sync source
"_sync_id TEXT," + // From the sync source
"_sync_time TEXT," + // From the sync source
"_sync_version TEXT," + // From the sync source
"_sync_local_id INTEGER," + // Used while syncing,
// never stored persistently
"_sync_dirty INTEGER," + // if syncable, set if the record
// has local, unsynced, changes
"_sync_mark INTEGER," + // Used to filter out new rows
"feed TEXT," +
"authority TEXT," +
"service TEXT" +
");");
// Trigger to completely remove feeds data when they're deleted
db.execSQL("CREATE TRIGGER feed_cleanup DELETE ON feeds " +
"WHEN old._sync_id is not null " +
"BEGIN " +
"INSERT INTO _deleted_feeds " +
"(_sync_id, _sync_account, _sync_version) " +
"VALUES (old._sync_id, old._sync_account, " +
"old._sync_version);" +
"END");
db.execSQL("CREATE TABLE _deleted_feeds (" +
"_sync_version TEXT," + // From the sync source
"_sync_id TEXT," +
(isTemporary() ? "_sync_local_id INTEGER," : "") + // Used while syncing,
"_sync_account TEXT," +
"_sync_mark INTEGER, " + // Used to filter out new rows
"UNIQUE(_sync_id))");
| public int | deleteInternal(android.net.Uri url, java.lang.String userWhere, java.lang.String[] whereArgs)
final SQLiteDatabase db = getDatabase();
String changedItemId;
switch (sURLMatcher.match(url)) {
case FEEDS:
changedItemId = null;
break;
case FEED_ID:
changedItemId = url.getPathSegments().get(1);
break;
default:
throw new UnsupportedOperationException(
"Cannot delete that URL: " + url);
}
String where = addIdToWhereClause(changedItemId, userWhere);
return db.delete(sFeedsTable, where, whereArgs);
| protected java.lang.Iterable | getMergers()
return Collections.singletonList(new FeedMerger());
| public java.lang.String | getType(android.net.Uri url)
int match = sURLMatcher.match(url);
switch (match) {
case FEEDS:
return SubscribedFeeds.Feeds.CONTENT_TYPE;
case FEED_ID:
return SubscribedFeeds.Feeds.CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unknown URL");
}
| public android.net.Uri | insertInternal(android.net.Uri url, ContentValues initialValues)
final SQLiteDatabase db = getDatabase();
Uri resultUri = null;
long rowID;
int match = sURLMatcher.match(url);
switch (match) {
case FEEDS:
ContentValues values = new ContentValues(initialValues);
values.put(SubscribedFeeds.Feeds._SYNC_DIRTY, 1);
rowID = db.insert(sFeedsTable, "feed", values);
if (rowID > 0) {
resultUri = Uri.parse(
"content://subscribedfeeds/feeds/" + rowID);
}
break;
case DELETED_FEEDS:
if (!isTemporary()) {
throw new UnsupportedOperationException();
}
rowID = db.insert(sDeletedFeedsTable, "_sync_id",
initialValues);
if (rowID > 0) {
resultUri = Uri.parse(
"content://subscribedfeeds/deleted_feeds/" + rowID);
}
break;
default:
throw new UnsupportedOperationException(
"Cannot insert into URL: " + url);
}
return resultUri;
| protected void | onDatabaseOpened(android.database.sqlite.SQLiteDatabase db)
db.markTableSyncable("feeds", "_deleted_feeds");
| public android.database.Cursor | queryInternal(android.net.Uri url, java.lang.String[] projection, java.lang.String selection, java.lang.String[] selectionArgs, java.lang.String sortOrder)
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
// Generate the body of the query
int match = sURLMatcher.match(url);
if (Config.LOGV) Log.v(TAG, "SubscribedFeedsProvider.query: url=" +
url + ", match is " + match);
switch (match) {
case FEEDS:
qb.setTables(sFeedsTable);
break;
case DELETED_FEEDS:
if (!isTemporary()) {
throw new UnsupportedOperationException();
}
qb.setTables(sDeletedFeedsTable);
break;
case ACCOUNTS:
qb.setTables(sFeedsTable);
qb.setDistinct(true);
qb.setProjectionMap(ACCOUNTS_PROJECTION_MAP);
return qb.query(getDatabase(), projection, selection, selectionArgs,
SubscribedFeeds.Feeds._SYNC_ACCOUNT, null, sortOrder);
case FEED_ID:
qb.setTables(sFeedsTable);
qb.appendWhere(sFeedsTable + "._id=");
qb.appendWhere(url.getPathSegments().get(1));
break;
default:
throw new IllegalArgumentException("Unknown URL " + url);
}
// run the query
return qb.query(getDatabase(), projection, selection, selectionArgs,
null, null, sortOrder);
| public int | updateInternal(android.net.Uri url, ContentValues initialValues, java.lang.String userWhere, java.lang.String[] whereArgs)
final SQLiteDatabase db = getDatabase();
ContentValues values = new ContentValues(initialValues);
values.put(SubscribedFeeds.Feeds._SYNC_DIRTY, 1);
String changedItemId;
switch (sURLMatcher.match(url)) {
case FEEDS:
changedItemId = null;
break;
case FEED_ID:
changedItemId = url.getPathSegments().get(1);
break;
default:
throw new UnsupportedOperationException(
"Cannot update URL: " + url);
}
String where = addIdToWhereClause(changedItemId, userWhere);
return db.update(sFeedsTable, values, where, whereArgs);
| protected boolean | upgradeDatabase(android.database.sqlite.SQLiteDatabase db, int oldVersion, int newVersion)
sURLMatcher.addURI("subscribedfeeds", "feeds", FEEDS);
sURLMatcher.addURI("subscribedfeeds", "feeds/#", FEED_ID);
sURLMatcher.addURI("subscribedfeeds", "deleted_feeds", DELETED_FEEDS);
sURLMatcher.addURI("subscribedfeeds", "accounts", ACCOUNTS);
Log.w(TAG, "Upgrading database from version " + oldVersion +
" to " + newVersion +
", which will destroy all old data");
db.execSQL("DROP TRIGGER IF EXISTS feed_cleanup");
db.execSQL("DROP TABLE IF EXISTS _deleted_feeds");
db.execSQL("DROP TABLE IF EXISTS feeds");
bootstrapDatabase(db);
return false; // this was lossy
|
|