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

SQLiteContentProvider

public abstract class SQLiteContentProvider extends android.content.ContentProvider implements android.database.sqlite.SQLiteTransactionListener
General purpose {@link ContentProvider} base class that uses SQLiteDatabase for storage.

Fields Summary
private static final String
TAG
private android.database.sqlite.SQLiteOpenHelper
mOpenHelper
private volatile boolean
mNotifyChange
protected android.database.sqlite.SQLiteDatabase
mDb
private final ThreadLocal
mApplyingBatch
private static final int
SLEEP_AFTER_YIELD_DELAY
private static final int
MAX_OPERATIONS_PER_YIELD_POINT
Maximum number of operations allowed in a batch between yield points.
Constructors Summary
Methods Summary
public android.content.ContentProviderResult[]applyBatch(java.util.ArrayList operations)

        int ypCount = 0;
        int opCount = 0;
        mDb = mOpenHelper.getWritableDatabase();
        mDb.beginTransactionWithListener(this);
        try {
            mApplyingBatch.set(true);
            final int numOperations = operations.size();
            final ContentProviderResult[] results = new ContentProviderResult[numOperations];
            for (int i = 0; i < numOperations; i++) {
                if (++opCount > getMaxOperationsPerYield()) {
                    throw new OperationApplicationException(
                            "Too many content provider operations between yield points. "
                                    + "The maximum number of operations per yield point is "
                                    + MAX_OPERATIONS_PER_YIELD_POINT, ypCount);
                }
                final ContentProviderOperation operation = operations.get(i);
                if (i > 0 && operation.isYieldAllowed()) {
                    opCount = 0;
                    boolean savedNotifyChange = mNotifyChange;
                    if (mDb.yieldIfContendedSafely(SLEEP_AFTER_YIELD_DELAY)) {
                        mDb = mOpenHelper.getWritableDatabase();
                        mNotifyChange = savedNotifyChange;
                        ypCount++;
                    }
                }

                results[i] = operation.apply(this, results, i);
            }
            mDb.setTransactionSuccessful();
            return results;
        } finally {
            mApplyingBatch.set(false);
            mDb.endTransaction();
            onEndTransaction();
        }
    
private booleanapplyingBatch()

        return mApplyingBatch.get() != null && mApplyingBatch.get();
    
protected voidbeforeTransactionCommit()

    
public intbulkInsert(android.net.Uri uri, android.content.ContentValues[] values)

        int numValues = values.length;
        mDb = mOpenHelper.getWritableDatabase();
        mDb.beginTransactionWithListener(this);
        try {
            for (int i = 0; i < numValues; i++) {
                Uri result = insertInTransaction(uri, values[i]);
                if (result != null) {
                    mNotifyChange = true;
                }
                boolean savedNotifyChange = mNotifyChange;
                SQLiteDatabase savedDb = mDb;
                mDb.yieldIfContendedSafely();
                mDb = savedDb;
                mNotifyChange = savedNotifyChange;
            }
            mDb.setTransactionSuccessful();
        } finally {
            mDb.endTransaction();
        }

        onEndTransaction();
        return numValues;
    
public intdelete(android.net.Uri uri, java.lang.String selection, java.lang.String[] selectionArgs)

        int count = 0;
        boolean applyingBatch = applyingBatch();
        if (!applyingBatch) {
            mDb = mOpenHelper.getWritableDatabase();
            mDb.beginTransactionWithListener(this);
            try {
                count = deleteInTransaction(uri, selection, selectionArgs);
                if (count > 0) {
                    mNotifyChange = true;
                }
                mDb.setTransactionSuccessful();
            } finally {
                mDb.endTransaction();
            }

            onEndTransaction();
        } else {
            count = deleteInTransaction(uri, selection, selectionArgs);
            if (count > 0) {
                mNotifyChange = true;
            }
        }
        return count;
    
protected abstract intdeleteInTransaction(android.net.Uri uri, java.lang.String selection, java.lang.String[] selectionArgs)
The equivalent of the {@link #delete} method, but invoked within a transaction.

protected abstract android.database.sqlite.SQLiteOpenHelpergetDatabaseHelper(android.content.Context context)

public android.database.sqlite.SQLiteOpenHelpergetDatabaseHelper()

        return mOpenHelper;
    
public intgetMaxOperationsPerYield()

return
Number of operations that can be applied at once without a yield point.


                       
       
        return MAX_OPERATIONS_PER_YIELD_POINT;
    
public android.net.Uriinsert(android.net.Uri uri, android.content.ContentValues values)

        Uri result = null;
        boolean applyingBatch = applyingBatch();
        if (!applyingBatch) {
            mDb = mOpenHelper.getWritableDatabase();
            mDb.beginTransactionWithListener(this);
            try {
                result = insertInTransaction(uri, values);
                if (result != null) {
                    mNotifyChange = true;
                }
                mDb.setTransactionSuccessful();
            } finally {
                mDb.endTransaction();
            }

            onEndTransaction();
        } else {
            result = insertInTransaction(uri, values);
            if (result != null) {
                mNotifyChange = true;
            }
        }
        return result;
    
protected abstract android.net.UriinsertInTransaction(android.net.Uri uri, android.content.ContentValues values)
The equivalent of the {@link #insert} method, but invoked within a transaction.

protected abstract voidnotifyChange()

public voidonBegin()

        onBeginTransaction();
    
protected voidonBeginTransaction()

    
public voidonCommit()

        beforeTransactionCommit();
    
public booleanonCreate()

        Context context = getContext();
        mOpenHelper = getDatabaseHelper(context);
        return true;
    
protected voidonEndTransaction()

        if (mNotifyChange) {
            mNotifyChange = false;
            notifyChange();
        }
    
public voidonRollback()

        // not used
    
public intupdate(android.net.Uri uri, android.content.ContentValues values, java.lang.String selection, java.lang.String[] selectionArgs)

        int count = 0;
        boolean applyingBatch = applyingBatch();
        if (!applyingBatch) {
            mDb = mOpenHelper.getWritableDatabase();
            mDb.beginTransactionWithListener(this);
            try {
                count = updateInTransaction(uri, values, selection, selectionArgs);
                if (count > 0) {
                    mNotifyChange = true;
                }
                mDb.setTransactionSuccessful();
            } finally {
                mDb.endTransaction();
            }

            onEndTransaction();
        } else {
            count = updateInTransaction(uri, values, selection, selectionArgs);
            if (count > 0) {
                mNotifyChange = true;
            }
        }

        return count;
    
protected abstract intupdateInTransaction(android.net.Uri uri, android.content.ContentValues values, java.lang.String selection, java.lang.String[] selectionArgs)
The equivalent of the {@link #update} method, but invoked within a transaction.