summaryrefslogtreecommitdiffstats
path: root/core/java/android/database/ContentObserver.java
diff options
context:
space:
mode:
authorJeff Brown <jeffbrown@google.com>2012-01-23 13:01:18 -0800
committerJeff Brown <jeffbrown@google.com>2012-01-23 13:01:18 -0800
commit86de0590b94bcce27e3038c27464bed510bb564a (patch)
tree0f192948c6ed5b80d4efd0219bd6c6b74b12ced9 /core/java/android/database/ContentObserver.java
parentbd4c9f13022e875c8b420248214482a5f5b46618 (diff)
downloadframeworks_base-86de0590b94bcce27e3038c27464bed510bb564a.zip
frameworks_base-86de0590b94bcce27e3038c27464bed510bb564a.tar.gz
frameworks_base-86de0590b94bcce27e3038c27464bed510bb564a.tar.bz2
Clean up content observer code.
Improved the documentation a little bit. Fixed a bug in ContentService wherein if a ContentObserver was passed as an argument and its deliverSelfNotifications() method returned true, then notifyChange would tell all observers that the change was a self-change even though it was only a self-change from the perspective of the provided observer. Deprecated ContentObservable.notifyChange since it is never used and in general it shouldn't be because we want the notification to be posted to the handler. Change-Id: Idde49eb40777e011a068f2adae8a32f779dfb923
Diffstat (limited to 'core/java/android/database/ContentObserver.java')
-rw-r--r--core/java/android/database/ContentObserver.java126
1 files changed, 65 insertions, 61 deletions
diff --git a/core/java/android/database/ContentObserver.java b/core/java/android/database/ContentObserver.java
index 3b829a3..4c20262 100644
--- a/core/java/android/database/ContentObserver.java
+++ b/core/java/android/database/ContentObserver.java
@@ -19,62 +19,19 @@ package android.database;
import android.os.Handler;
/**
- * Receives call backs for changes to content. Must be implemented by objects which are added
- * to a {@link ContentObservable}.
+ * Receives call backs for changes to content.
+ * Must be implemented by objects which are added to a {@link ContentObservable}.
*/
public abstract class ContentObserver {
+ private final Object mLock = new Object();
+ private Transport mTransport; // guarded by mLock
- private Transport mTransport;
-
- // Protects mTransport
- private Object lock = new Object();
-
- /* package */ Handler mHandler;
-
- private final class NotificationRunnable implements Runnable {
-
- private boolean mSelf;
-
- public NotificationRunnable(boolean self) {
- mSelf = self;
- }
-
- public void run() {
- ContentObserver.this.onChange(mSelf);
- }
- }
-
- private static final class Transport extends IContentObserver.Stub {
- ContentObserver mContentObserver;
-
- public Transport(ContentObserver contentObserver) {
- mContentObserver = contentObserver;
- }
-
- public boolean deliverSelfNotifications() {
- ContentObserver contentObserver = mContentObserver;
- if (contentObserver != null) {
- return contentObserver.deliverSelfNotifications();
- }
- return false;
- }
-
- public void onChange(boolean selfChange) {
- ContentObserver contentObserver = mContentObserver;
- if (contentObserver != null) {
- contentObserver.dispatchChange(selfChange);
- }
- }
-
- public void releaseContentObserver() {
- mContentObserver = null;
- }
- }
+ Handler mHandler;
/**
- * onChange() will happen on the provider Handler.
+ * Creates a content observer.
*
- * @param handler The handler to run {@link #onChange} on.
+ * @param handler The handler to run {@link #onChange} on, or null if none.
*/
public ContentObserver(Handler handler) {
mHandler = handler;
@@ -86,7 +43,7 @@ public abstract class ContentObserver {
* {@hide}
*/
public IContentObserver getContentObserver() {
- synchronized(lock) {
+ synchronized (mLock) {
if (mTransport == null) {
mTransport = new Transport(this);
}
@@ -101,8 +58,8 @@ public abstract class ContentObserver {
* {@hide}
*/
public IContentObserver releaseContentObserver() {
- synchronized(lock) {
- Transport oldTransport = mTransport;
+ synchronized (mLock) {
+ final Transport oldTransport = mTransport;
if (oldTransport != null) {
oldTransport.releaseContentObserver();
mTransport = null;
@@ -112,22 +69,36 @@ public abstract class ContentObserver {
}
/**
- * Returns true if this observer is interested in notifications for changes
- * made through the cursor the observer is registered with.
+ * Returns true if this observer is interested receiving self-change notifications.
+ *
+ * Subclasses should override this method to indicate whether the observer
+ * is interested in receiving notifications for changes that it made to the
+ * content itself.
+ *
+ * @return True if self-change notifications should be delivered to the observer.
*/
public boolean deliverSelfNotifications() {
return false;
}
/**
- * This method is called when a change occurs to the cursor that
- * is being observed.
- *
- * @param selfChange true if the update was caused by a call to <code>commit</code> on the
- * cursor that is being observed.
+ * This method is called when a content change occurs.
+ *
+ * @param selfChange True if this is a self-change notification.
*/
- public void onChange(boolean selfChange) {}
+ public void onChange(boolean selfChange) {
+ // Do nothing. Subclass should override.
+ }
+ /**
+ * Dispatches a change notification to the observer.
+ *
+ * If a {@link Handler} was supplied to the {@link ContentObserver} constructor,
+ * then a call to the {@link #onChange} method is posted to the handler's message queue.
+ * Otherwise, the {@link #onChange} method is invoked immediately on this thread.
+ *
+ * @param selfChange True if this is a self-change notification.
+ */
public final void dispatchChange(boolean selfChange) {
if (mHandler == null) {
onChange(selfChange);
@@ -135,4 +106,37 @@ public abstract class ContentObserver {
mHandler.post(new NotificationRunnable(selfChange));
}
}
+
+ private final class NotificationRunnable implements Runnable {
+ private final boolean mSelf;
+
+ public NotificationRunnable(boolean self) {
+ mSelf = self;
+ }
+
+ @Override
+ public void run() {
+ ContentObserver.this.onChange(mSelf);
+ }
+ }
+
+ private static final class Transport extends IContentObserver.Stub {
+ private ContentObserver mContentObserver;
+
+ public Transport(ContentObserver contentObserver) {
+ mContentObserver = contentObserver;
+ }
+
+ @Override
+ public void onChange(boolean selfChange) {
+ ContentObserver contentObserver = mContentObserver;
+ if (contentObserver != null) {
+ contentObserver.dispatchChange(selfChange);
+ }
+ }
+
+ public void releaseContentObserver() {
+ mContentObserver = null;
+ }
+ }
}