Merge "If contacts observer is running, return early."

main
Dan Zivkovic 2015-02-19 22:16:32 +00:00 committed by Android (Google) Code Review
commit 7041313f71
1 changed files with 13 additions and 5 deletions

View File

@ -27,6 +27,7 @@ import com.android.inputmethod.latin.ContactsManager.ContactsChangedListener;
import com.android.inputmethod.latin.utils.ExecutorUtils; import com.android.inputmethod.latin.utils.ExecutorUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* A content observer that listens to updates to content provider {@link Contacts#CONTENT_URI}. * A content observer that listens to updates to content provider {@link Contacts#CONTENT_URI}.
@ -35,13 +36,14 @@ import java.util.ArrayList;
public class ContactsContentObserver implements Runnable { public class ContactsContentObserver implements Runnable {
private static final String TAG = ContactsContentObserver.class.getSimpleName(); private static final String TAG = ContactsContentObserver.class.getSimpleName();
private static final boolean DEBUG = false; private static final boolean DEBUG = false;
private static AtomicBoolean sRunning = new AtomicBoolean(false);
private ContentObserver mContentObserver;
private ContactsChangedListener mContactsChangedListener;
private final Context mContext; private final Context mContext;
private final ContactsManager mManager; private final ContactsManager mManager;
private ContentObserver mContentObserver;
private ContactsChangedListener mContactsChangedListener;
public ContactsContentObserver(final ContactsManager manager, final Context context) { public ContactsContentObserver(final ContactsManager manager, final Context context) {
mManager = manager; mManager = manager;
mContext = context; mContext = context;
@ -55,7 +57,6 @@ public class ContactsContentObserver implements Runnable {
mContentObserver = new ContentObserver(null /* handler */) { mContentObserver = new ContentObserver(null /* handler */) {
@Override @Override
public void onChange(boolean self) { public void onChange(boolean self) {
// TODO(zivkovic): Limit the queue to 1 instance of ContactsContentObserver.
ExecutorUtils.getExecutorForDynamicLanguageModelUpdate() ExecutorUtils.getExecutorForDynamicLanguageModelUpdate()
.execute(ContactsContentObserver.this); .execute(ContactsContentObserver.this);
} }
@ -66,12 +67,19 @@ public class ContactsContentObserver implements Runnable {
@Override @Override
public void run() { public void run() {
if (!sRunning.compareAndSet(false /* expect */, true /* update */)) {
if (DEBUG) {
Log.d(TAG, "run() : Already running. Don't waste time checking again.");
}
return;
}
if (haveContentsChanged()) { if (haveContentsChanged()) {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "Contacts have changed; notifying listeners"); Log.d(TAG, "run() : Contacts have changed. Notifying listeners.");
} }
mContactsChangedListener.onContactsChange(); mContactsChangedListener.onContactsChange();
} }
sRunning.set(false);
} }
private boolean haveContentsChanged() { private boolean haveContentsChanged() {