diff options
Diffstat (limited to 'java/com/android/dialer/phonelookup/database/PhoneLookupHistoryContentProvider.java')
-rw-r--r-- | java/com/android/dialer/phonelookup/database/PhoneLookupHistoryContentProvider.java | 90 |
1 files changed, 77 insertions, 13 deletions
diff --git a/java/com/android/dialer/phonelookup/database/PhoneLookupHistoryContentProvider.java b/java/com/android/dialer/phonelookup/database/PhoneLookupHistoryContentProvider.java index e85654e99..5c0c00f81 100644 --- a/java/com/android/dialer/phonelookup/database/PhoneLookupHistoryContentProvider.java +++ b/java/com/android/dialer/phonelookup/database/PhoneLookupHistoryContentProvider.java @@ -17,7 +17,10 @@ package com.android.dialer.phonelookup.database; import android.content.ContentProvider; +import android.content.ContentProviderOperation; +import android.content.ContentProviderResult; import android.content.ContentValues; +import android.content.OperationApplicationException; import android.content.UriMatcher; import android.database.Cursor; import android.database.DatabaseUtils; @@ -31,6 +34,7 @@ import com.android.dialer.common.Assert; import com.android.dialer.common.LogUtil; import com.android.dialer.phonelookup.database.contract.PhoneLookupHistoryContract; import com.android.dialer.phonelookup.database.contract.PhoneLookupHistoryContract.PhoneLookupHistory; +import java.util.ArrayList; /** * {@link ContentProvider} for the PhoneLookupHistory. @@ -50,13 +54,6 @@ import com.android.dialer.phonelookup.database.contract.PhoneLookupHistoryContra */ public class PhoneLookupHistoryContentProvider extends ContentProvider { - /** - * We sometimes run queries where we potentially pass numbers into a where clause using the - * (?,?,?,...) syntax. The maximum number of host parameters is 999, so that's the maximum size - * this table can be. See https://www.sqlite.org/limits.html for more details. - */ - private static final int MAX_ROWS = 999; - // For operations against: content://com.android.dialer.phonelookuphistory/PhoneLookupHistory private static final int PHONE_LOOKUP_HISTORY_TABLE_CODE = 1; // For operations against: content://com.android.dialer.phonelookuphistory/PhoneLookupHistory/+123 @@ -77,9 +74,16 @@ public class PhoneLookupHistoryContentProvider extends ContentProvider { private PhoneLookupHistoryDatabaseHelper databaseHelper; + private final ThreadLocal<Boolean> applyingBatch = new ThreadLocal<>(); + + /** Ensures that only a single notification is generated from {@link #applyBatch(ArrayList)}. */ + private boolean isApplyingBatch() { + return applyingBatch.get() != null && applyingBatch.get(); + } + @Override public boolean onCreate() { - databaseHelper = new PhoneLookupHistoryDatabaseHelper(getContext(), MAX_ROWS); + databaseHelper = new PhoneLookupHistoryDatabaseHelper(getContext()); return true; } @@ -168,7 +172,9 @@ public class PhoneLookupHistoryContentProvider extends ContentProvider { .buildUpon() .appendEncodedPath(values.getAsString(PhoneLookupHistory.NORMALIZED_NUMBER)) .build(); - notifyChange(insertedUri); + if (!isApplyingBatch()) { + notifyChange(insertedUri); + } return insertedUri; } @@ -197,7 +203,9 @@ public class PhoneLookupHistoryContentProvider extends ContentProvider { LogUtil.w("PhoneLookupHistoryContentProvider.delete", "no rows deleted"); return rows; } - notifyChange(uri); + if (!isApplyingBatch()) { + notifyChange(uri); + } return rows; } @@ -206,6 +214,9 @@ public class PhoneLookupHistoryContentProvider extends ContentProvider { * "content://com.android.dialer.phonelookuphistory/PhoneLookupHistory/+123") then the update * operation will actually be a "replace" operation, inserting a new row if one does not already * exist. + * + * <p>All columns in an existing row will be replaced which means you must specify all required + * columns in {@code values} when using this method. */ @Override public int update( @@ -225,7 +236,9 @@ public class PhoneLookupHistoryContentProvider extends ContentProvider { LogUtil.w("PhoneLookupHistoryContentProvider.update", "no rows updated"); return rows; } - notifyChange(uri); + if (!isApplyingBatch()) { + notifyChange(uri); + } return rows; case PHONE_LOOKUP_HISTORY_TABLE_ID_CODE: Assert.checkArgument( @@ -237,14 +250,65 @@ public class PhoneLookupHistoryContentProvider extends ContentProvider { String normalizedNumber = uri.getLastPathSegment(); values.put(PhoneLookupHistory.NORMALIZED_NUMBER, normalizedNumber); - database.replace(PhoneLookupHistory.TABLE, null, values); - notifyChange(uri); + long result = database.replace(PhoneLookupHistory.TABLE, null, values); + Assert.checkArgument(result != -1, "replacing PhoneLookupHistory row failed"); + if (!isApplyingBatch()) { + notifyChange(uri); + } return 1; default: throw new IllegalArgumentException("Unknown uri: " + uri); } } + /** + * {@inheritDoc} + * + * <p>Note: When applyBatch is used with the PhoneLookupHistory, only a single notification for + * the content URI is generated, not individual notifications for each affected URI. + */ + @NonNull + @Override + public ContentProviderResult[] applyBatch(@NonNull ArrayList<ContentProviderOperation> operations) + throws OperationApplicationException { + ContentProviderResult[] results = new ContentProviderResult[operations.size()]; + if (operations.isEmpty()) { + return results; + } + + SQLiteDatabase database = databaseHelper.getWritableDatabase(); + try { + applyingBatch.set(true); + database.beginTransaction(); + for (int i = 0; i < operations.size(); i++) { + ContentProviderOperation operation = operations.get(i); + int match = uriMatcher.match(operation.getUri()); + switch (match) { + case PHONE_LOOKUP_HISTORY_TABLE_CODE: + case PHONE_LOOKUP_HISTORY_TABLE_ID_CODE: + ContentProviderResult result = operation.apply(this, results, i); + if (operation.isInsert()) { + if (result.uri == null) { + throw new OperationApplicationException("error inserting row"); + } + } else if (result.count == 0) { + throw new OperationApplicationException("error applying operation"); + } + results[i] = result; + break; + default: + throw new IllegalArgumentException("Unknown uri: " + operation.getUri()); + } + } + database.setTransactionSuccessful(); + } finally { + applyingBatch.set(false); + database.endTransaction(); + } + notifyChange(PhoneLookupHistory.CONTENT_URI); + return results; + } + private void notifyChange(Uri uri) { getContext().getContentResolver().notifyChange(uri, null); } |