Skip to content

Commit d4e34d6

Browse files
Jeff BrownAndroid (Google) Code Review
authored andcommitted
Merge "Inform ContentObservers about the changed content Uri."
2 parents b934a82 + 655e66b commit d4e34d6

File tree

7 files changed

+110
-20
lines changed

7 files changed

+110
-20
lines changed

api/current.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6791,16 +6791,19 @@ package android.database {
67916791

67926792
public class ContentObservable extends android.database.Observable {
67936793
ctor public ContentObservable();
6794-
method public void dispatchChange(boolean);
6794+
method public deprecated void dispatchChange(boolean);
6795+
method public void dispatchChange(boolean, android.net.Uri);
67956796
method public deprecated void notifyChange(boolean);
67966797
method public void registerObserver(android.database.ContentObserver);
67976798
}
67986799

67996800
public abstract class ContentObserver {
68006801
ctor public ContentObserver(android.os.Handler);
68016802
method public boolean deliverSelfNotifications();
6802-
method public final void dispatchChange(boolean);
6803+
method public final deprecated void dispatchChange(boolean);
6804+
method public final void dispatchChange(boolean, android.net.Uri);
68036805
method public void onChange(boolean);
6806+
method public void onChange(boolean, android.net.Uri);
68046807
}
68056808

68066809
public abstract interface CrossProcessCursor implements android.database.Cursor {

core/java/android/content/ContentService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public void notifyChange(Uri uri, IContentObserver observer,
176176
for (int i=0; i<numCalls; i++) {
177177
ObserverCall oc = calls.get(i);
178178
try {
179-
oc.mObserver.onChange(oc.mSelfChange);
179+
oc.mObserver.onChange(oc.mSelfChange, uri);
180180
if (Log.isLoggable(TAG, Log.VERBOSE)) {
181181
Log.v(TAG, "Notified " + oc.mObserver + " of " + "update at " + uri);
182182
}

core/java/android/database/AbstractCursor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public void unregisterDataSetObserver(DataSetObserver observer) {
300300
*/
301301
protected void onChange(boolean selfChange) {
302302
synchronized (mSelfObserverLock) {
303-
mContentObservable.dispatchChange(selfChange);
303+
mContentObservable.dispatchChange(selfChange, null);
304304
if (mNotifyUri != null && selfChange) {
305305
mContentResolver.notifyChange(mNotifyUri, mSelfObserver);
306306
}

core/java/android/database/ContentObservable.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package android.database;
1818

19+
import android.net.Uri;
20+
1921
/**
2022
* A specialization of {@link Observable} for {@link ContentObserver}
2123
* that provides methods for sending notifications to a list of
@@ -31,20 +33,41 @@ public void registerObserver(ContentObserver observer) {
3133
}
3234

3335
/**
34-
* Invokes {@link ContentObserver#dispatchChange} on each observer.
35-
*
36+
* Invokes {@link ContentObserver#dispatchChange(boolean)} on each observer.
37+
* <p>
3638
* If <code>selfChange</code> is true, only delivers the notification
3739
* to the observer if it has indicated that it wants to receive self-change
3840
* notifications by implementing {@link ContentObserver#deliverSelfNotifications}
3941
* to return true.
42+
* </p>
4043
*
4144
* @param selfChange True if this is a self-change notification.
45+
*
46+
* @deprecated Use {@link #dispatchChange(boolean, Uri)} instead.
4247
*/
48+
@Deprecated
4349
public void dispatchChange(boolean selfChange) {
50+
dispatchChange(selfChange, null);
51+
}
52+
53+
/**
54+
* Invokes {@link ContentObserver#dispatchChange(boolean, Uri)} on each observer.
55+
* Includes the changed content Uri when available.
56+
* <p>
57+
* If <code>selfChange</code> is true, only delivers the notification
58+
* to the observer if it has indicated that it wants to receive self-change
59+
* notifications by implementing {@link ContentObserver#deliverSelfNotifications}
60+
* to return true.
61+
* </p>
62+
*
63+
* @param selfChange True if this is a self-change notification.
64+
* @param uri The Uri of the changed content, or null if unknown.
65+
*/
66+
public void dispatchChange(boolean selfChange, Uri uri) {
4467
synchronized(mObservers) {
4568
for (ContentObserver observer : mObservers) {
4669
if (!selfChange || observer.deliverSelfNotifications()) {
47-
observer.dispatchChange(selfChange);
70+
observer.dispatchChange(selfChange, uri);
4871
}
4972
}
5073
}
@@ -61,7 +84,7 @@ public void dispatchChange(boolean selfChange) {
6184
public void notifyChange(boolean selfChange) {
6285
synchronized(mObservers) {
6386
for (ContentObserver observer : mObservers) {
64-
observer.onChange(selfChange);
87+
observer.onChange(selfChange, null);
6588
}
6689
}
6790
}

core/java/android/database/ContentObserver.java

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package android.database;
1818

19+
import android.net.Uri;
1920
import android.os.Handler;
2021

2122
/**
@@ -83,6 +84,9 @@ public boolean deliverSelfNotifications() {
8384

8485
/**
8586
* This method is called when a content change occurs.
87+
* <p>
88+
* Subclasses should override this method to handle content changes.
89+
* </p>
8690
*
8791
* @param selfChange True if this is a self-change notification.
8892
*/
@@ -91,32 +95,89 @@ public void onChange(boolean selfChange) {
9195
}
9296

9397
/**
94-
* Dispatches a change notification to the observer.
98+
* This method is called when a content change occurs.
99+
* Includes the changed content Uri when available.
100+
* <p>
101+
* Subclasses should override this method to handle content changes.
102+
* To ensure correct operation on older versions of the framework that
103+
* did not provide a Uri argument, applications should also implement
104+
* the {@link #onChange(boolean)} overload of this method whenever they
105+
* implement the {@link #onChange(boolean, Uri)} overload.
106+
* </p><p>
107+
* Example implementation:
108+
* <pre><code>
109+
* // Implement the onChange(boolean) method to delegate the change notification to
110+
* // the onChange(boolean, Uri) method to ensure correct operation on older versions
111+
* // of the framework that did not have the onChange(boolean, Uri) method.
112+
* {@literal @Override}
113+
* public void onChange(boolean selfChange) {
114+
* onChange(selfChange, null);
115+
* }
116+
*
117+
* // Implement the onChange(boolean, Uri) method to take advantage of the new Uri argument.
118+
* {@literal @Override}
119+
* public void onChange(boolean selfChange, Uri uri) {
120+
* // Handle change.
121+
* }
122+
* </code></pre>
123+
* </p>
95124
*
125+
* @param selfChange True if this is a self-change notification.
126+
* @param uri The Uri of the changed content, or null if unknown.
127+
*/
128+
public void onChange(boolean selfChange, Uri uri) {
129+
onChange(selfChange);
130+
}
131+
132+
/**
133+
* Dispatches a change notification to the observer.
134+
* <p>
96135
* If a {@link Handler} was supplied to the {@link ContentObserver} constructor,
97136
* then a call to the {@link #onChange} method is posted to the handler's message queue.
98137
* Otherwise, the {@link #onChange} method is invoked immediately on this thread.
138+
* </p>
99139
*
100140
* @param selfChange True if this is a self-change notification.
141+
*
142+
* @deprecated Use {@link #dispatchChange(boolean, Uri)} instead.
101143
*/
144+
@Deprecated
102145
public final void dispatchChange(boolean selfChange) {
146+
dispatchChange(selfChange, null);
147+
}
148+
149+
/**
150+
* Dispatches a change notification to the observer.
151+
* Includes the changed content Uri when available.
152+
* <p>
153+
* If a {@link Handler} was supplied to the {@link ContentObserver} constructor,
154+
* then a call to the {@link #onChange} method is posted to the handler's message queue.
155+
* Otherwise, the {@link #onChange} method is invoked immediately on this thread.
156+
* </p>
157+
*
158+
* @param selfChange True if this is a self-change notification.
159+
* @param uri The Uri of the changed content, or null if unknown.
160+
*/
161+
public final void dispatchChange(boolean selfChange, Uri uri) {
103162
if (mHandler == null) {
104-
onChange(selfChange);
163+
onChange(selfChange, uri);
105164
} else {
106-
mHandler.post(new NotificationRunnable(selfChange));
165+
mHandler.post(new NotificationRunnable(selfChange, uri));
107166
}
108167
}
109168

110169
private final class NotificationRunnable implements Runnable {
111-
private final boolean mSelf;
170+
private final boolean mSelfChange;
171+
private final Uri mUri;
112172

113-
public NotificationRunnable(boolean self) {
114-
mSelf = self;
173+
public NotificationRunnable(boolean selfChange, Uri uri) {
174+
mSelfChange = selfChange;
175+
mUri = uri;
115176
}
116177

117178
@Override
118179
public void run() {
119-
ContentObserver.this.onChange(mSelf);
180+
ContentObserver.this.onChange(mSelfChange, mUri);
120181
}
121182
}
122183

@@ -128,10 +189,10 @@ public Transport(ContentObserver contentObserver) {
128189
}
129190

130191
@Override
131-
public void onChange(boolean selfChange) {
192+
public void onChange(boolean selfChange, Uri uri) {
132193
ContentObserver contentObserver = mContentObserver;
133194
if (contentObserver != null) {
134-
contentObserver.dispatchChange(selfChange);
195+
contentObserver.dispatchChange(selfChange, uri);
135196
}
136197
}
137198

core/java/android/database/CursorToBulkCursorAdaptor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package android.database;
1818

19+
import android.net.Uri;
1920
import android.os.Bundle;
2021
import android.os.IBinder;
2122
import android.os.RemoteException;
@@ -78,9 +79,9 @@ public boolean deliverSelfNotifications() {
7879
}
7980

8081
@Override
81-
public void onChange(boolean selfChange) {
82+
public void onChange(boolean selfChange, Uri uri) {
8283
try {
83-
mRemote.onChange(selfChange);
84+
mRemote.onChange(selfChange, uri);
8485
} catch (RemoteException ex) {
8586
// Do nothing, the far side is dead
8687
}

core/java/android/database/IContentObserver.aidl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package android.database;
1919

20+
import android.net.Uri;
21+
2022
/**
2123
* @hide
2224
*/
@@ -27,5 +29,5 @@ interface IContentObserver
2729
* observed. selfUpdate is true if the update was caused by a call to
2830
* commit on the cursor that is being observed.
2931
*/
30-
oneway void onChange(boolean selfUpdate);
32+
oneway void onChange(boolean selfUpdate, in Uri uri);
3133
}

0 commit comments

Comments
 (0)