diff options
author | Liefu Liu <liefuliu@google.com> | 2021-04-26 20:48:51 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2021-04-26 20:48:51 +0000 |
commit | 4a016e563ecb0aeed6b05abf625e7e5b6233913f (patch) | |
tree | f776dca3ea182a4879e501a4601a97ca75bdd65d | |
parent | 24e65f11bc947ec31693906550d17bfb219b96d5 (diff) | |
parent | d2e5f7f07469c7a8b72abf1aa68bad9421323d5c (diff) | |
download | ContactsProvider-4a016e563ecb0aeed6b05abf625e7e5b6233913f.tar.gz |
Merge "Set a rate limit to CP2 notifyChange, if the change is not to sync to network." into sc-dev
-rw-r--r-- | src/com/android/providers/contacts/ContactsProvider2.java | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java index befafc05..e29b0fb2 100644 --- a/src/com/android/providers/contacts/ContactsProvider2.java +++ b/src/com/android/providers/contacts/ContactsProvider2.java @@ -20,6 +20,7 @@ import static android.Manifest.permission.INTERACT_ACROSS_USERS; import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL; import static android.content.pm.PackageManager.PERMISSION_GRANTED; +import android.os.Looper; import android.accounts.Account; import android.accounts.AccountManager; import android.accounts.OnAccountsUpdateListener; @@ -62,6 +63,7 @@ import android.os.AsyncTask; import android.os.Binder; import android.os.Bundle; import android.os.CancellationSignal; +import android.os.Handler; import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor.AutoCloseInputStream; import android.os.RemoteException; @@ -258,6 +260,9 @@ public class ContactsProvider2 extends AbstractContactsProvider /** Limit for the maximum number of social stream items to store under a raw contact. */ private static final int MAX_STREAM_ITEMS_PER_RAW_CONTACT = 5; + /** Rate limit (in milliseconds) for notify change. Do it as most once every 5 seconds. */ + private static final int NOTIFY_CHANGE_RATE_LIMIT = 5 * 1000; + /** Rate limit (in milliseconds) for photo cleanup. Do it at most once per day. */ private static final int PHOTO_CLEANUP_RATE_LIMIT = 24 * 60 * 60 * 1000; @@ -1458,6 +1463,8 @@ public class ContactsProvider2 extends AbstractContactsProvider private ContactsTaskScheduler mTaskScheduler; + private long mLastNotifyChange = 0; + private long mLastPhotoCleanup = 0; private FastScrollingIndexCache mFastScrollingIndexCache; @@ -2575,9 +2582,34 @@ public class ContactsProvider2 extends AbstractContactsProvider mSyncToNetwork = false; } - protected void notifyChange(boolean syncToNetwork) { + private final Handler mHandler = new Handler(Looper.getMainLooper()); + private final Runnable mChangeNotifier = () -> { + Log.v(TAG, "Scheduled notifyChange started."); + mLastNotifyChange = System.currentTimeMillis(); getContext().getContentResolver().notifyChange(ContactsContract.AUTHORITY_URI, null, + false); + }; + + protected void notifyChange(boolean syncToNetwork) { + if (syncToNetwork) { + // Changes to sync to network won't be rate limited. + getContext().getContentResolver().notifyChange(ContactsContract.AUTHORITY_URI, null, syncToNetwork); + } else { + // Rate limit the changes which are not to sync to network. + long uptimeMillis = System.currentTimeMillis(); + + mHandler.removeCallbacks(mChangeNotifier); + if (uptimeMillis > mLastNotifyChange + NOTIFY_CHANGE_RATE_LIMIT) { + // Notify change immediately, since it has been a while since last notify. + mLastNotifyChange = uptimeMillis; + getContext().getContentResolver().notifyChange(ContactsContract.AUTHORITY_URI, null, + false); + } else { + // Schedule a deleyed notify, to ensure the very last notifyChange will be executed. + mHandler.postDelayed(mChangeNotifier, NOTIFY_CHANGE_RATE_LIMIT * 2); + } + } } protected void setProviderStatus(int status) { |