1616
1717package android .database ;
1818
19+ import android .net .Uri ;
1920import 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
0 commit comments