FileDocCategorySizeDatePackage
SyncStateContentProviderHelper.javaAPI DocAndroid 1.5 API8690Wed May 06 22:41:54 BST 2009android.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.
hide

Fields Summary
final android.database.sqlite.SQLiteOpenHelper
mOpenHelper
private static final String
SYNC_STATE_AUTHORITY
private static final UriMatcher
sURIMatcher
private static final int
STATE
private static final android.net.Uri
CONTENT_URI
private static final String
ACCOUNT_WHERE
private final Provider
mInternalProviderInterface
private static final String
SYNC_STATE_TABLE
private static long
DB_VERSION
private static final String[]
ACCOUNT_PROJECTION
Constructors Summary
public SyncStateContentProviderHelper(android.database.sqlite.SQLiteOpenHelper openHelper)


     
        sURIMatcher.addURI(SYNC_STATE_AUTHORITY, "state", STATE);
    
        mOpenHelper = openHelper;
        mInternalProviderInterface = new Provider();
    
Methods Summary
public ContentProviderasContentProvider()

        return mInternalProviderInterface;
    
public voidcopySyncState(android.database.sqlite.SQLiteDatabase dbSrc, android.database.sqlite.SQLiteDatabase dbDest, java.lang.String account)
Replaces the contents of the _sync_state table in the destination ContentProvider with the row that matches account, if any, in the source ContentProvider.

The ContentProviders must expose the _sync_state table as URI content://syncstate/state.

param
dbSrc the database to read from
param
dbDest the database to write to
param
account the account of the row that should be copied over.

        final String[] whereArgs = new String[]{account};
        Cursor c = dbSrc.query(SYNC_STATE_TABLE, new String[]{"_sync_account", "data"},
                ACCOUNT_WHERE, whereArgs, null, null, null);
        try {
            if (c.moveToNext()) {
                ContentValues values = new ContentValues();
                values.put("_sync_account", c.getString(0));
                values.put("data", c.getBlob(1));
                dbDest.replace(SYNC_STATE_TABLE, "_sync_account", values);
            }
        } finally {
            c.close();
        }
    
public voidcreateDatabase(android.database.sqlite.SQLiteDatabase db)

        db.execSQL("DROP TABLE IF EXISTS _sync_state");
        db.execSQL("CREATE TABLE _sync_state (" +
                   "_id INTEGER PRIMARY KEY," +
                   "_sync_account TEXT," +
                   "data TEXT," +
                   "UNIQUE(_sync_account)" +
                   ");");

        db.execSQL("DROP TABLE IF EXISTS _sync_state_metadata");
        db.execSQL("CREATE TABLE _sync_state_metadata (" +
                    "version INTEGER" +
                    ");");
        ContentValues values = new ContentValues();
        values.put("version", DB_VERSION);
        db.insert("_sync_state_metadata", "version", values);
    
public voiddiscardSyncData(android.database.sqlite.SQLiteDatabase db, java.lang.String account)

        if (account != null) {
            db.delete(SYNC_STATE_TABLE, ACCOUNT_WHERE, new String[]{account});
        } else {
            db.delete(SYNC_STATE_TABLE, null, null);
        }
    
public booleanmatches(android.net.Uri url)
Check if the url matches content that this ContentProvider manages.

param
url the Uri to check
return
true if this ContentProvider can handle that Uri.

        return (SYNC_STATE_AUTHORITY.equals(url.getAuthority()));
    
public voidonAccountsChanged(java.lang.String[] accounts)

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        Cursor c = db.query(SYNC_STATE_TABLE, ACCOUNT_PROJECTION, null, null, null, null, null);
        try {
            while (c.moveToNext()) {
                final String account = c.getString(0);
                if (!ArrayUtils.contains(accounts, account)) {
                    db.delete(SYNC_STATE_TABLE, ACCOUNT_WHERE, new String[]{account});
                }
            }
        } finally {
            c.close();
        }
    
protected voidonDatabaseOpened(android.database.sqlite.SQLiteDatabase db)

        long version = DatabaseUtils.longForQuery(db,
                "select version from _sync_state_metadata", null);
        if (version != DB_VERSION) {
            createDatabase(db);
        }
    
public byte[]readSyncDataBytes(android.database.sqlite.SQLiteDatabase db, java.lang.String account)
Retrieves the SyncData bytes for the given account. The byte array returned may be null.

        Cursor c = db.query(SYNC_STATE_TABLE, null, ACCOUNT_WHERE,
                new String[]{account}, null, null, null);
        try {
            if (c.moveToFirst()) {
                return c.getBlob(c.getColumnIndexOrThrow("data"));
            }
        } finally {
            c.close();
        }
        return null;
    
public voidwriteSyncDataBytes(android.database.sqlite.SQLiteDatabase db, java.lang.String account, byte[] data)
Sets the SyncData bytes for the given account. The bytes array may be null.

        ContentValues values = new ContentValues();
        values.put("data", data);
        db.update(SYNC_STATE_TABLE, values, ACCOUNT_WHERE, new String[]{account});