diff options
author | Makoto Onuki <omakoto@google.com> | 2017-01-18 17:04:23 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2017-01-18 17:04:26 +0000 |
commit | 9765ceb8b0852d054cac7a12b219b216a535c9b9 (patch) | |
tree | aa96a4464cd1b47b14863f262f403928c7a6cee0 /src | |
parent | e5b6f5b4fed61798be19cbec76688e0d52d0e38a (diff) | |
parent | 5358da6ad10305f9359373ec3cc876a09d8d4e92 (diff) | |
download | ContactsProvider-9765ceb8b0852d054cac7a12b219b216a535c9b9.tar.gz |
Merge "Add ProviderStatus.DATABASE_CREATION_TIMESTAMP"
Diffstat (limited to 'src')
3 files changed, 83 insertions, 13 deletions
diff --git a/src/com/android/providers/contacts/ContactsDatabaseHelper.java b/src/com/android/providers/contacts/ContactsDatabaseHelper.java index f0c53de9..88f8eaf0 100644 --- a/src/com/android/providers/contacts/ContactsDatabaseHelper.java +++ b/src/com/android/providers/contacts/ContactsDatabaseHelper.java @@ -76,6 +76,7 @@ import android.provider.ContactsContract.MetadataSyncState; import android.provider.ContactsContract.PhoneticNameStyle; import android.provider.ContactsContract.PhotoFiles; import android.provider.ContactsContract.PinnedPositions; +import android.provider.ContactsContract.ProviderStatus; import android.provider.ContactsContract.RawContacts; import android.provider.ContactsContract.Settings; import android.provider.ContactsContract.StatusUpdates; @@ -1010,6 +1011,12 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper { private final SyncStateContentProviderHelper mSyncState; private final CountryMonitor mCountryMonitor; + /** + * Time when the DB was created. It's persisted in {@link DbProperties#DATABASE_TIME_CREATED}, + * but loaded into memory so it can be accessed even when the DB is busy. + */ + private long mDatabaseCreationTime; + private MessageDigest mMessageDigest; { try { @@ -1090,6 +1097,40 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper { // were in-memory tables db.execSQL("DELETE FROM " + Tables.PRESENCE + ";"); db.execSQL("DELETE FROM " + Tables.AGGREGATED_PRESENCE + ";"); + + loadDatabaseCreationTime(db); + } + + protected void setDatabaseCreationTime(SQLiteDatabase db) { + // Note we don't do this in the profile DB helper. + mDatabaseCreationTime = System.currentTimeMillis(); + PropertyUtils.setProperty(db, DbProperties.DATABASE_TIME_CREATED, String.valueOf( + mDatabaseCreationTime)); + } + + protected void loadDatabaseCreationTime(SQLiteDatabase db) { + // Note we don't do this in the profile DB helper. + + mDatabaseCreationTime = 0; + final String timestamp = PropertyUtils.getProperty(db, + DbProperties.DATABASE_TIME_CREATED, ""); + if (!TextUtils.isEmpty(timestamp)) { + try { + mDatabaseCreationTime = Long.parseLong(timestamp); + } catch (NumberFormatException e) { + Log.w(TAG, "Failed to parse timestamp: " + timestamp); + } + } + if (AbstractContactsProvider.VERBOSE_LOGGING) { + Log.v(TAG, "Open: creation time=" + mDatabaseCreationTime); + } + if (mDatabaseCreationTime == 0) { + Log.w(TAG, "Unable to load creating time; resetting."); + // Hmm, failed to load the timestamp. Just set the current time then. + mDatabaseCreationTime = System.currentTimeMillis(); + PropertyUtils.setProperty(db, + DbProperties.DATABASE_TIME_CREATED, Long.toString(mDatabaseCreationTime)); + } } private void createPresenceTables(SQLiteDatabase db) { @@ -1183,8 +1224,7 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper { // The create time is needed by BOOT_COMPLETE to send broadcasts. PropertyUtils.createPropertiesTable(db); - PropertyUtils.setProperty(db, DbProperties.DATABASE_TIME_CREATED, String.valueOf( - System.currentTimeMillis())); + setDatabaseCreationTime(db); db.execSQL("CREATE TABLE " + Tables.ACCOUNTS + " (" + AccountsColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + @@ -1613,16 +1653,17 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper { updateSqliteStats(db); } + postOnCreate(); + } + + protected void postOnCreate() { + // Only do this for the main DB, but not for the profile DB. + + notifyProviderStatusChange(mContext); + + // Trigger all sync adapters. ContentResolver.requestSync(null /* all accounts */, ContactsContract.AUTHORITY, new Bundle()); - - // Only send broadcasts for regular contacts db. - if (dbForProfile() == 0) { - final Intent dbCreatedIntent = new Intent( - ContactsContract.Intents.CONTACTS_DATABASE_CREATED); - dbCreatedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); - mContext.sendBroadcast(dbCreatedIntent, android.Manifest.permission.READ_CONTACTS); - } } protected void initializeAutoIncrementSequences(SQLiteDatabase db) { @@ -4923,6 +4964,18 @@ public class ContactsDatabaseHelper extends SQLiteOpenHelper { return metadataSyncInsert.executeInsert(); } + public static void notifyProviderStatusChange(Context context) { + context.getContentResolver().notifyChange(ProviderStatus.CONTENT_URI, + /* observer= */ null, /* syncToNetwork= */ false); + context.getContentResolver().notifyChange( + ProviderStatus.STATUS_CHANGE_NOTIFICATION_CONTENT_URI, + /* observer= */ null, /* syncToNetwork= */ false); + } + + public long getDatabaseCreationTime() { + return mDatabaseCreationTime; + } + private SqlChecker mCachedSqlChecker; private SqlChecker getSqlChecker() { diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java index 423d5d76..f3f85cf8 100644 --- a/src/com/android/providers/contacts/ContactsProvider2.java +++ b/src/com/android/providers/contacts/ContactsProvider2.java @@ -2635,7 +2635,7 @@ public class ContactsProvider2 extends AbstractContactsProvider protected void setProviderStatus(int status) { if (mProviderStatus != status) { mProviderStatus = status; - getContext().getContentResolver().notifyChange(ProviderStatus.CONTENT_URI, null, false); + ContactsDatabaseHelper.notifyProviderStatusChange(getContext()); } } @@ -7069,8 +7069,9 @@ public class ContactsProvider2 extends AbstractContactsProvider providerStatus = ProviderStatus.STATUS_EMPTY; } return buildSingleRowResult(projection, - new String[] {ProviderStatus.STATUS}, - new Object[] {providerStatus}); + new String[] {ProviderStatus.STATUS, + ProviderStatus.DATABASE_CREATION_TIMESTAMP}, + new Object[] {providerStatus, mDbHelper.get().getDatabaseCreationTime()}); } case DIRECTORIES : { @@ -9370,6 +9371,8 @@ public class ContactsProvider2 extends AbstractContactsProvider return StreamItems.StreamItemPhotos.CONTENT_ITEM_TYPE; case STREAM_ITEMS_PHOTOS: throw new UnsupportedOperationException("Not supported for write-only URI " + uri); + case PROVIDER_STATUS: + return ProviderStatus.CONTENT_TYPE; default: waitForAccess(mReadAccessLatch); return mLegacyApiSupport.getType(uri); diff --git a/src/com/android/providers/contacts/ProfileDatabaseHelper.java b/src/com/android/providers/contacts/ProfileDatabaseHelper.java index 4d605f2c..966ee7e8 100644 --- a/src/com/android/providers/contacts/ProfileDatabaseHelper.java +++ b/src/com/android/providers/contacts/ProfileDatabaseHelper.java @@ -74,4 +74,18 @@ public class ProfileDatabaseHelper extends ContactsDatabaseHelper { db.insert(SEQUENCE_TABLE, null, values); } } + + @Override + protected void postOnCreate() { + } + + @Override + protected void setDatabaseCreationTime(SQLiteDatabase db) { + // We don't need the creation time for the profile DB. + } + + @Override + protected void loadDatabaseCreationTime(SQLiteDatabase db) { + // We don't need the creation time for the profile DB. + } } |