Improve content notifications

 Bug: 5465258
 Restrict when we set syncToNetwork and clean up unnecessary
 notifications

Change-Id: I19b6d2b3df6fd70ac3a1b386137ce67d371323e4
diff --git a/src/com/android/browser/provider/BrowserProvider2.java b/src/com/android/browser/provider/BrowserProvider2.java
index 9aa2502..06c8e16 100644
--- a/src/com/android/browser/provider/BrowserProvider2.java
+++ b/src/com/android/browser/provider/BrowserProvider2.java
@@ -379,6 +379,7 @@
     // This is so provider tests can intercept widget updating
     ContentObserver mWidgetObserver = null;
     boolean mUpdateWidgets = false;
+    boolean mSyncToNetwork = true;
 
     final class DatabaseHelper extends SQLiteOpenHelper {
         static final String DATABASE_NAME = "browser2.db";
@@ -804,6 +805,7 @@
             }
             mUpdateWidgets = false;
         }
+        mSyncToNetwork = true;
     }
 
     @Override
@@ -1319,7 +1321,9 @@
         }
         if (deleted > 0) {
             postNotifyUri(uri);
-            postNotifyUri(LEGACY_AUTHORITY_URI);
+            if (shouldNotifyLegacy(uri)) {
+                postNotifyUri(LEGACY_AUTHORITY_URI);
+            }
         }
         return deleted;
     }
@@ -1466,7 +1470,9 @@
 
         if (id >= 0) {
             postNotifyUri(uri);
-            postNotifyUri(LEGACY_AUTHORITY_URI);
+            if (shouldNotifyLegacy(uri)) {
+                postNotifyUri(LEGACY_AUTHORITY_URI);
+            }
             return ContentUris.withAppendedId(uri, id);
         } else {
             return null;
@@ -1678,15 +1684,26 @@
                     db.insertOrThrow(TABLE_IMAGES, Images.FAVICON, values);
                     count = 1;
                 }
+                // Only favicon is exposed in the public API. If we updated
+                // the thumbnail or touch icon don't bother notifying the
+                // legacy authority since it can't read it anyway.
+                boolean updatedLegacy = false;
                 if (getUrlCount(db, TABLE_BOOKMARKS, url) > 0) {
                     postNotifyUri(Bookmarks.CONTENT_URI);
+                    updatedLegacy = values.containsKey(Images.FAVICON);
                     refreshWidgets();
                 }
                 if (getUrlCount(db, TABLE_HISTORY, url) > 0) {
                     postNotifyUri(History.CONTENT_URI);
+                    updatedLegacy = values.containsKey(Images.FAVICON);
                 }
-                postNotifyUri(LEGACY_AUTHORITY_URI);
-                pruneImages();
+                if (pruneImages() > 0 || updatedLegacy) {
+                    postNotifyUri(LEGACY_AUTHORITY_URI);
+                }
+                // Even though we may be calling notifyUri on Bookmarks, don't
+                // sync to network as images aren't synced. Otherwise this
+                // unnecessarily triggers a bookmark sync.
+                mSyncToNetwork = false;
                 return count;
             }
 
@@ -1714,7 +1731,9 @@
         pruneImages();
         if (modified > 0) {
             postNotifyUri(uri);
-            postNotifyUri(LEGACY_AUTHORITY_URI);
+            if (shouldNotifyLegacy(uri)) {
+                postNotifyUri(LEGACY_AUTHORITY_URI);
+            }
         }
         return modified;
     }
@@ -2028,9 +2047,31 @@
         return imageValues;
     }
 
-    void pruneImages() {
+    int pruneImages() {
         final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
-        db.delete(TABLE_IMAGES, IMAGE_PRUNE, null);
+        return db.delete(TABLE_IMAGES, IMAGE_PRUNE, null);
+    }
+
+    boolean shouldNotifyLegacy(Uri uri) {
+        if (uri.getPathSegments().contains("history")
+                || uri.getPathSegments().contains("bookmarks")
+                || uri.getPathSegments().contains("searches")) {
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    protected boolean syncToNetwork(Uri uri) {
+        if (BrowserContract.AUTHORITY.equals(uri.getAuthority())
+                && uri.getPathSegments().contains("bookmarks")) {
+            return mSyncToNetwork;
+        }
+        if (LEGACY_AUTHORITY.equals(uri.getAuthority())) {
+            // Allow for 3rd party sync adapters
+            return true;
+        }
+        return false;
     }
 
     static class SuggestionsCursor extends AbstractCursor {
diff --git a/src/com/android/browser/provider/SQLiteContentProvider.java b/src/com/android/browser/provider/SQLiteContentProvider.java
index 13acd3d..75e298e 100644
--- a/src/com/android/browser/provider/SQLiteContentProvider.java
+++ b/src/com/android/browser/provider/SQLiteContentProvider.java
@@ -239,7 +239,12 @@
         }
         ContentResolver resolver = getContext().getContentResolver();
         for (Uri uri : changed) {
-            resolver.notifyChange(uri, null, !callerIsSyncAdapter);
+            boolean syncToNetwork = !callerIsSyncAdapter && syncToNetwork(uri);
+            resolver.notifyChange(uri, null, syncToNetwork);
         }
     }
+
+    protected boolean syncToNetwork(Uri uri) {
+        return false;
+    }
 }