public abstract class SQLiteContentProvider extends ContentProvider implements SQLiteTransactionListener
ContentProvider
base class that uses SQLiteDatabase for storage.ContentProvider.PipeDataWriter<T>
Modifier and Type | Field and Description |
---|---|
protected SQLiteDatabase |
mDb |
TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, TRIM_MEMORY_RUNNING_MODERATE, TRIM_MEMORY_UI_HIDDEN
Constructor and Description |
---|
SQLiteContentProvider() |
Modifier and Type | Method and Description |
---|---|
ContentProviderResult[] |
applyBatch(ArrayList<ContentProviderOperation> operations)
Override this to handle requests to perform a batch of operations, or the
default implementation will iterate over the operations and call
ContentProviderOperation.apply(android.content.ContentProvider, android.content.ContentProviderResult[], int) on each of them. |
protected void |
beforeTransactionCommit() |
int |
bulkInsert(Uri uri,
ContentValues[] values)
Override this to handle requests to insert a set of new rows, or the
default implementation will iterate over the values and call
ContentProvider.insert(android.net.Uri, android.content.ContentValues) on each of them. |
int |
delete(Uri uri,
String selection,
String[] selectionArgs)
Implement this to handle requests to delete one or more rows.
|
protected abstract int |
deleteInTransaction(Uri uri,
String selection,
String[] selectionArgs)
The equivalent of the
delete(android.net.Uri, java.lang.String, java.lang.String[]) method, but invoked within a transaction. |
SQLiteOpenHelper |
getDatabaseHelper() |
protected abstract SQLiteOpenHelper |
getDatabaseHelper(Context context) |
int |
getMaxOperationsPerYield() |
Uri |
insert(Uri uri,
ContentValues values)
Implement this to handle requests to insert a new row.
|
protected abstract Uri |
insertInTransaction(Uri uri,
ContentValues values)
The equivalent of the
insert(android.net.Uri, android.content.ContentValues) method, but invoked within a transaction. |
protected abstract void |
notifyChange() |
void |
onBegin()
Called immediately after the transaction begins.
|
protected void |
onBeginTransaction() |
void |
onCommit()
Called immediately before commiting the transaction.
|
boolean |
onCreate()
Implement this to initialize your content provider on startup.
|
protected void |
onEndTransaction() |
void |
onRollback()
Called if the transaction is about to be rolled back.
|
int |
update(Uri uri,
ContentValues values,
String selection,
String[] selectionArgs)
Implement this to handle requests to update one or more rows.
|
protected abstract int |
updateInTransaction(Uri uri,
ContentValues values,
String selection,
String[] selectionArgs)
The equivalent of the
update(android.net.Uri, android.content.ContentValues, java.lang.String, java.lang.String[]) method, but invoked within a transaction. |
attachInfo, attachInfoForTesting, call, canonicalize, coerceToLocalContentProvider, dump, enforceReadPermissionInner, enforceWritePermissionInner, getAppOpsManager, getAuthorityWithoutUserId, getCallingPackage, getContext, getIContentProvider, getPathPermissions, getReadPermission, getStreamTypes, getType, getUriWithoutUserId, getUserIdFromAuthority, getUserIdFromAuthority, getUserIdFromUri, getUserIdFromUri, getWritePermission, isTemporary, matchesOurAuthorities, maybeAddUserId, onConfigurationChanged, onLowMemory, onTrimMemory, openAssetFile, openAssetFile, openFile, openFile, openFileHelper, openPipeHelper, openTypedAssetFile, openTypedAssetFile, query, query, rejectInsert, setAppOps, setAuthorities, setPathPermissions, setReadPermission, setWritePermission, shutdown, uncanonicalize, uriHasUserId
protected SQLiteDatabase mDb
public int getMaxOperationsPerYield()
public boolean onCreate()
ContentProvider
You should defer nontrivial initialization (such as opening,
upgrading, and scanning databases) until the content provider is used
(via ContentProvider.query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)
, ContentProvider.insert(android.net.Uri, android.content.ContentValues)
, etc). Deferred initialization
keeps application startup fast, avoids unnecessary work if the provider
turns out not to be needed, and stops database errors (such as a full
disk) from halting application launch.
If you use SQLite, SQLiteOpenHelper
is a helpful utility class that makes it easy to manage databases,
and will automatically defer opening until first use. If you do use
SQLiteOpenHelper, make sure to avoid calling
SQLiteOpenHelper.getReadableDatabase()
or
SQLiteOpenHelper.getWritableDatabase()
from this method. (Instead, override
SQLiteOpenHelper.onOpen(android.database.sqlite.SQLiteDatabase)
to initialize the
database when it is first opened.)
onCreate
in class ContentProvider
protected abstract SQLiteOpenHelper getDatabaseHelper(Context context)
protected abstract Uri insertInTransaction(Uri uri, ContentValues values)
insert(android.net.Uri, android.content.ContentValues)
method, but invoked within a transaction.protected abstract int updateInTransaction(Uri uri, ContentValues values, String selection, String[] selectionArgs)
update(android.net.Uri, android.content.ContentValues, java.lang.String, java.lang.String[])
method, but invoked within a transaction.protected abstract int deleteInTransaction(Uri uri, String selection, String[] selectionArgs)
delete(android.net.Uri, java.lang.String, java.lang.String[])
method, but invoked within a transaction.protected abstract void notifyChange()
public SQLiteOpenHelper getDatabaseHelper()
public Uri insert(Uri uri, ContentValues values)
ContentProvider
notifyChange()
after inserting.
This method can be called from multiple threads, as described in
Processes
and Threads.insert
in class ContentProvider
uri
- The content:// URI of the insertion request. This must not be null
.values
- A set of column_name/value pairs to add to the database.
This must not be null
.public int bulkInsert(Uri uri, ContentValues[] values)
ContentProvider
ContentProvider.insert(android.net.Uri, android.content.ContentValues)
on each of them.
As a courtesy, call notifyChange()
after inserting.
This method can be called from multiple threads, as described in
Processes
and Threads.bulkInsert
in class ContentProvider
uri
- The content:// URI of the insertion request.values
- An array of sets of column_name/value pairs to add to the database.
This must not be null
.public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
ContentProvider
notifyChange()
after updating.
This method can be called from multiple threads, as described in
Processes
and Threads.update
in class ContentProvider
uri
- The URI to query. This can potentially have a record ID if this
is an update request for a specific record.values
- A set of column_name/value pairs to update in the database.
This must not be null
.selection
- An optional filter to match rows to update.public int delete(Uri uri, String selection, String[] selectionArgs)
ContentProvider
notifyChange()
after deleting.
This method can be called from multiple threads, as described in
Processes
and Threads.
The implementation is responsible for parsing out a row ID at the end
of the URI, if a specific row is being deleted. That is, the client would
pass in content://contacts/people/22
and the implementation is
responsible for parsing the record number (22) when creating a SQL statement.
delete
in class ContentProvider
uri
- The full URI to query, including a row ID (if a specific record is requested).selection
- An optional restriction to apply to rows when deleting.public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException
ContentProvider
ContentProviderOperation.apply(android.content.ContentProvider, android.content.ContentProviderResult[], int)
on each of them.
If all calls to ContentProviderOperation.apply(android.content.ContentProvider, android.content.ContentProviderResult[], int)
succeed
then a ContentProviderResult
array with as many
elements as there were operations will be returned. If any of the calls
fail, it is up to the implementation how many of the others take effect.
This method can be called from multiple threads, as described in
Processes
and Threads.applyBatch
in class ContentProvider
operations
- the operations to applyOperationApplicationException
- thrown if any operation fails.ContentProviderOperation.apply(android.content.ContentProvider, android.content.ContentProviderResult[], int)
public void onBegin()
SQLiteTransactionListener
onBegin
in interface SQLiteTransactionListener
public void onCommit()
SQLiteTransactionListener
onCommit
in interface SQLiteTransactionListener
public void onRollback()
SQLiteTransactionListener
onRollback
in interface SQLiteTransactionListener
protected void onBeginTransaction()
protected void beforeTransactionCommit()
protected void onEndTransaction()