SyncStateContentProviderHelperpublic class SyncStateContentProviderHelper extends Object Extends the schema of a ContentProvider to include the _sync_state table
and implements query/insert/update/delete to access that table using the
authority "syncstate". This can be used to store the sync state for a
set of accounts. |
Fields Summary |
---|
private static final String | SELECT_BY_ACCOUNT | private static final String | SYNC_STATE_TABLE | private static final String | SYNC_STATE_META_TABLE | private static final String | SYNC_STATE_META_VERSION_COLUMN | private static long | DB_VERSION | private static final String[] | ACCOUNT_PROJECTION | public static final String | PATH | private static final String | QUERY_COUNT_SYNC_STATE_ROWS |
Methods Summary |
---|
private static boolean | contains(T[] array, T value)Checks that value is present as at least one of the elements of the array.
for (T element : array) {
if (element == null) {
if (value == null) return true;
} else {
if (value != null && element.equals(value)) return true;
}
}
return false;
| public void | createDatabase(android.database.sqlite.SQLiteDatabase db)
db.execSQL("DROP TABLE IF EXISTS " + SYNC_STATE_TABLE);
db.execSQL("CREATE TABLE " + SYNC_STATE_TABLE + " ("
+ SyncStateContract.Columns._ID + " INTEGER PRIMARY KEY,"
+ SyncStateContract.Columns.ACCOUNT_NAME + " TEXT NOT NULL,"
+ SyncStateContract.Columns.ACCOUNT_TYPE + " TEXT NOT NULL,"
+ SyncStateContract.Columns.DATA + " TEXT,"
+ "UNIQUE(" + SyncStateContract.Columns.ACCOUNT_NAME + ", "
+ SyncStateContract.Columns.ACCOUNT_TYPE + "));");
db.execSQL("DROP TABLE IF EXISTS " + SYNC_STATE_META_TABLE);
db.execSQL("CREATE TABLE " + SYNC_STATE_META_TABLE + " ("
+ SYNC_STATE_META_VERSION_COLUMN + " INTEGER);");
ContentValues values = new ContentValues();
values.put(SYNC_STATE_META_VERSION_COLUMN, DB_VERSION);
db.insert(SYNC_STATE_META_TABLE, SYNC_STATE_META_VERSION_COLUMN, values);
| public int | delete(android.database.sqlite.SQLiteDatabase db, java.lang.String userWhere, java.lang.String[] whereArgs)
return db.delete(SYNC_STATE_TABLE, userWhere, whereArgs);
| public long | insert(android.database.sqlite.SQLiteDatabase db, android.content.ContentValues values)
return db.replace(SYNC_STATE_TABLE, SyncStateContract.Columns.ACCOUNT_NAME, values);
| public void | onAccountsChanged(android.database.sqlite.SQLiteDatabase db, android.accounts.Account[] accounts)
Cursor c = db.query(SYNC_STATE_TABLE, ACCOUNT_PROJECTION, null, null, null, null, null);
try {
while (c.moveToNext()) {
final String accountName = c.getString(0);
final String accountType = c.getString(1);
Account account = new Account(accountName, accountType);
if (!contains(accounts, account)) {
db.delete(SYNC_STATE_TABLE, SELECT_BY_ACCOUNT,
new String[]{accountName, accountType});
}
}
} finally {
c.close();
}
| public void | onDatabaseOpened(android.database.sqlite.SQLiteDatabase db)
long version = DatabaseUtils.longForQuery(db,
"SELECT " + SYNC_STATE_META_VERSION_COLUMN + " FROM " + SYNC_STATE_META_TABLE,
null);
if (version != DB_VERSION) {
createDatabase(db);
}
| public android.database.Cursor | query(android.database.sqlite.SQLiteDatabase db, java.lang.String[] projection, java.lang.String selection, java.lang.String[] selectionArgs, java.lang.String sortOrder)
return db.query(SYNC_STATE_TABLE, projection, selection, selectionArgs,
null, null, sortOrder);
| public int | update(android.database.sqlite.SQLiteDatabase db, android.content.ContentValues values, java.lang.String selection, java.lang.String[] selectionArgs)
return db.update(SYNC_STATE_TABLE, values, selection, selectionArgs);
| public int | update(android.database.sqlite.SQLiteDatabase db, long rowId, java.lang.Object data)
if (DatabaseUtils.longForQuery(db, QUERY_COUNT_SYNC_STATE_ROWS,
new String[]{Long.toString(rowId)}) < 1) {
return 0;
}
db.execSQL("UPDATE " + SYNC_STATE_TABLE
+ " SET " + SyncStateContract.Columns.DATA + "=?"
+ " WHERE " + SyncStateContract.Columns._ID + "=" + rowId,
new Object[]{data});
// assume a row was modified since we know it exists
return 1;
|
|