FileDocCategorySizeDatePackage
SyncStateContentProviderHelper.javaAPI DocAndroid 5.1 API6007Thu Mar 12 22:22:48 GMT 2015com.android.common.content

SyncStateContentProviderHelper

public 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
Constructors Summary
Methods Summary
private static booleancontains(T[] array, T value)
Checks that value is present as at least one of the elements of the array.

param
array the array to check in
param
value the value to check for
return
true if the value is present in 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 voidcreateDatabase(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 intdelete(android.database.sqlite.SQLiteDatabase db, java.lang.String userWhere, java.lang.String[] whereArgs)

        return db.delete(SYNC_STATE_TABLE, userWhere, whereArgs);
    
public longinsert(android.database.sqlite.SQLiteDatabase db, android.content.ContentValues values)

        return db.replace(SYNC_STATE_TABLE, SyncStateContract.Columns.ACCOUNT_NAME, values);
    
public voidonAccountsChanged(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 voidonDatabaseOpened(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.Cursorquery(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 intupdate(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 intupdate(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;