Preventing unnecessary update of contact visibility at boot time

Bug: 2511409
Change-Id: I5191cc424adbaa97648db9cedee6573a16b0232c
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java
index e0e15e9..9fcca32 100644
--- a/src/com/android/providers/contacts/ContactsProvider2.java
+++ b/src/com/android/providers/contacts/ContactsProvider2.java
@@ -4167,6 +4167,7 @@
 
     public void onAccountsUpdated(Account[] accounts) {
         // TODO : Check the unit test.
+        boolean accountsChanged = false;
         HashSet<Account> existingAccounts = new HashSet<Account>();
         mDb.beginTransaction();
         try {
@@ -4175,6 +4176,7 @@
             // Add a row to the ACCOUNTS table for each new account
             for (Account account : accounts) {
                 if (!existingAccounts.contains(account)) {
+                    accountsChanged = true;
                     mDb.execSQL("INSERT INTO " + Tables.ACCOUNTS + " (" + RawContacts.ACCOUNT_NAME
                             + ", " + RawContacts.ACCOUNT_TYPE + ") VALUES (?, ?)",
                             new String[] {account.name, account.type});
@@ -4188,38 +4190,39 @@
                 accountsToDelete.remove(account);
             }
 
-            for (Account account : accountsToDelete) {
-                Log.d(TAG, "removing data for removed account " + account);
-                String[] params = new String[] {account.name, account.type};
-                mDb.execSQL(
-                        "DELETE FROM " + Tables.GROUPS +
-                        " WHERE " + Groups.ACCOUNT_NAME + " = ?" +
-                                " AND " + Groups.ACCOUNT_TYPE + " = ?", params);
-                mDb.execSQL(
-                        "DELETE FROM " + Tables.PRESENCE +
-                        " WHERE " + PresenceColumns.RAW_CONTACT_ID + " IN (" +
-                                "SELECT " + RawContacts._ID +
-                                " FROM " + Tables.RAW_CONTACTS +
-                                " WHERE " + RawContacts.ACCOUNT_NAME + " = ?" +
-                                " AND " + RawContacts.ACCOUNT_TYPE + " = ?)", params);
-                mDb.execSQL(
-                        "DELETE FROM " + Tables.RAW_CONTACTS +
-                        " WHERE " + RawContacts.ACCOUNT_NAME + " = ?" +
-                        " AND " + RawContacts.ACCOUNT_TYPE + " = ?", params);
-                mDb.execSQL(
-                        "DELETE FROM " + Tables.SETTINGS +
-                        " WHERE " + Settings.ACCOUNT_NAME + " = ?" +
-                        " AND " + Settings.ACCOUNT_TYPE + " = ?", params);
-                mDb.execSQL(
-                        "DELETE FROM " + Tables.ACCOUNTS +
-                        " WHERE " + RawContacts.ACCOUNT_NAME + "=?" +
-                        " AND " + RawContacts.ACCOUNT_TYPE + "=?", params);
-            }
-
             if (!accountsToDelete.isEmpty()) {
+                accountsChanged = true;
+                for (Account account : accountsToDelete) {
+                    Log.d(TAG, "removing data for removed account " + account);
+                    String[] params = new String[] {account.name, account.type};
+                    mDb.execSQL(
+                            "DELETE FROM " + Tables.GROUPS +
+                            " WHERE " + Groups.ACCOUNT_NAME + " = ?" +
+                                    " AND " + Groups.ACCOUNT_TYPE + " = ?", params);
+                    mDb.execSQL(
+                            "DELETE FROM " + Tables.PRESENCE +
+                            " WHERE " + PresenceColumns.RAW_CONTACT_ID + " IN (" +
+                                    "SELECT " + RawContacts._ID +
+                                    " FROM " + Tables.RAW_CONTACTS +
+                                    " WHERE " + RawContacts.ACCOUNT_NAME + " = ?" +
+                                    " AND " + RawContacts.ACCOUNT_TYPE + " = ?)", params);
+                    mDb.execSQL(
+                            "DELETE FROM " + Tables.RAW_CONTACTS +
+                            " WHERE " + RawContacts.ACCOUNT_NAME + " = ?" +
+                            " AND " + RawContacts.ACCOUNT_TYPE + " = ?", params);
+                    mDb.execSQL(
+                            "DELETE FROM " + Tables.SETTINGS +
+                            " WHERE " + Settings.ACCOUNT_NAME + " = ?" +
+                            " AND " + Settings.ACCOUNT_TYPE + " = ?", params);
+                    mDb.execSQL(
+                            "DELETE FROM " + Tables.ACCOUNTS +
+                            " WHERE " + RawContacts.ACCOUNT_NAME + "=?" +
+                            " AND " + RawContacts.ACCOUNT_TYPE + "=?", params);
+                }
+
                 // Find all aggregated contacts that used to contain the raw contacts
                 // we have just deleted and see if they are still referencing the deleted
-                // names of photos.  If so, fix up those contacts.
+                // names or photos.  If so, fix up those contacts.
                 HashSet<Long> orphanContactIds = Sets.newHashSet();
                 Cursor cursor = mDb.rawQuery("SELECT " + Contacts._ID +
                         " FROM " + Tables.CONTACTS +
@@ -4242,11 +4245,12 @@
                 for (Long contactId : orphanContactIds) {
                     mContactAggregator.updateAggregateData(contactId);
                 }
+                mDbHelper.updateAllVisible();
             }
 
-            mDbHelper.updateAllVisible();
-
-            mDbHelper.getSyncState().onAccountsChanged(mDb, accounts);
+            if (accountsChanged) {
+                mDbHelper.getSyncState().onAccountsChanged(mDb, accounts);
+            }
             mDb.setTransactionSuccessful();
         } finally {
             mDb.endTransaction();
diff --git a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
index 74f600b..f6b4708 100644
--- a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
+++ b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
@@ -2629,8 +2629,7 @@
         assertNoRowsAndClose(queryGroupMemberships(mAccount));
         assertNoRowsAndClose(queryGroupMemberships(mAccountTwo));
 
-        Uri contactUri = findContactUriByRawContactUri(
-                ContentUris.withAppendedId(RawContacts.CONTENT_URI, r1));
+        Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, queryContactId(r1));
         assertNotNull(contactUri);
 
         // mark r1 as starred via its contact lookup uri
@@ -2656,18 +2655,6 @@
         assertNoRowsAndClose(queryGroupMemberships(mAccountTwo));
     }
 
-    private Uri findContactUriByRawContactUri(Uri uri) {
-        Cursor c = mResolver.query(uri,
-                new String[]{RawContacts.CONTACT_ID},
-                null,
-                null,
-                null);
-        if (c.moveToNext()) {
-            return ContentUris.withAppendedId(Contacts.CONTENT_URI, c.getLong(0));
-        }
-        return null;
-    }
-
     public void testStarChangedAfterGroupMembershipChange() {
         long g1 = createGroup(mAccount, "g1", "t1", 0, false /* autoAdd */, true /* favorite */);
         long g2 = createGroup(mAccount, "g2", "t2", 0, false /* autoAdd */, false/* favorite */);