PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/core/java/android/database/ContentObserver.java

https://gitlab.com/AvayKumar/android_frameworks_base
Java | 239 lines | 85 code | 22 blank | 132 comment | 9 complexity | dd8f0a9340378f41ba0a749c1d03e3b5 MD5 | raw file
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package android.database;
  17. import android.net.Uri;
  18. import android.os.Handler;
  19. import android.os.UserHandle;
  20. /**
  21. * Receives call backs for changes to content.
  22. * Must be implemented by objects which are added to a {@link ContentObservable}.
  23. */
  24. public abstract class ContentObserver {
  25. private final Object mLock = new Object();
  26. private Transport mTransport; // guarded by mLock
  27. Handler mHandler;
  28. /**
  29. * Creates a content observer.
  30. *
  31. * @param handler The handler to run {@link #onChange} on, or null if none.
  32. */
  33. public ContentObserver(Handler handler) {
  34. mHandler = handler;
  35. }
  36. /**
  37. * Gets access to the binder transport object. Not for public consumption.
  38. *
  39. * {@hide}
  40. */
  41. public IContentObserver getContentObserver() {
  42. synchronized (mLock) {
  43. if (mTransport == null) {
  44. mTransport = new Transport(this);
  45. }
  46. return mTransport;
  47. }
  48. }
  49. /**
  50. * Gets access to the binder transport object, and unlinks the transport object
  51. * from the ContentObserver. Not for public consumption.
  52. *
  53. * {@hide}
  54. */
  55. public IContentObserver releaseContentObserver() {
  56. synchronized (mLock) {
  57. final Transport oldTransport = mTransport;
  58. if (oldTransport != null) {
  59. oldTransport.releaseContentObserver();
  60. mTransport = null;
  61. }
  62. return oldTransport;
  63. }
  64. }
  65. /**
  66. * Returns true if this observer is interested receiving self-change notifications.
  67. *
  68. * Subclasses should override this method to indicate whether the observer
  69. * is interested in receiving notifications for changes that it made to the
  70. * content itself.
  71. *
  72. * @return True if self-change notifications should be delivered to the observer.
  73. */
  74. public boolean deliverSelfNotifications() {
  75. return false;
  76. }
  77. /**
  78. * This method is called when a content change occurs.
  79. * <p>
  80. * Subclasses should override this method to handle content changes.
  81. * </p>
  82. *
  83. * @param selfChange True if this is a self-change notification.
  84. */
  85. public void onChange(boolean selfChange) {
  86. // Do nothing. Subclass should override.
  87. }
  88. /**
  89. * This method is called when a content change occurs.
  90. * Includes the changed content Uri when available.
  91. * <p>
  92. * Subclasses should override this method to handle content changes.
  93. * To ensure correct operation on older versions of the framework that
  94. * did not provide a Uri argument, applications should also implement
  95. * the {@link #onChange(boolean)} overload of this method whenever they
  96. * implement the {@link #onChange(boolean, Uri)} overload.
  97. * </p><p>
  98. * Example implementation:
  99. * <pre><code>
  100. * // Implement the onChange(boolean) method to delegate the change notification to
  101. * // the onChange(boolean, Uri) method to ensure correct operation on older versions
  102. * // of the framework that did not have the onChange(boolean, Uri) method.
  103. * {@literal @Override}
  104. * public void onChange(boolean selfChange) {
  105. * onChange(selfChange, null);
  106. * }
  107. *
  108. * // Implement the onChange(boolean, Uri) method to take advantage of the new Uri argument.
  109. * {@literal @Override}
  110. * public void onChange(boolean selfChange, Uri uri) {
  111. * // Handle change.
  112. * }
  113. * </code></pre>
  114. * </p>
  115. *
  116. * @param selfChange True if this is a self-change notification.
  117. * @param uri The Uri of the changed content, or null if unknown.
  118. */
  119. public void onChange(boolean selfChange, Uri uri) {
  120. onChange(selfChange);
  121. }
  122. /**
  123. * Dispatches a change notification to the observer. Includes the changed
  124. * content Uri when available and also the user whose content changed.
  125. *
  126. * @param selfChange True if this is a self-change notification.
  127. * @param uri The Uri of the changed content, or null if unknown.
  128. * @param userId The user whose content changed. Can be either a specific
  129. * user or {@link UserHandle#USER_ALL}.
  130. *
  131. * @hide
  132. */
  133. public void onChange(boolean selfChange, Uri uri, int userId) {
  134. onChange(selfChange, uri);
  135. }
  136. /**
  137. * Dispatches a change notification to the observer.
  138. * <p>
  139. * If a {@link Handler} was supplied to the {@link ContentObserver} constructor,
  140. * then a call to the {@link #onChange} method is posted to the handler's message queue.
  141. * Otherwise, the {@link #onChange} method is invoked immediately on this thread.
  142. * </p>
  143. *
  144. * @param selfChange True if this is a self-change notification.
  145. *
  146. * @deprecated Use {@link #dispatchChange(boolean, Uri)} instead.
  147. */
  148. @Deprecated
  149. public final void dispatchChange(boolean selfChange) {
  150. dispatchChange(selfChange, null);
  151. }
  152. /**
  153. * Dispatches a change notification to the observer.
  154. * Includes the changed content Uri when available.
  155. * <p>
  156. * If a {@link Handler} was supplied to the {@link ContentObserver} constructor,
  157. * then a call to the {@link #onChange} method is posted to the handler's message queue.
  158. * Otherwise, the {@link #onChange} method is invoked immediately on this thread.
  159. * </p>
  160. *
  161. * @param selfChange True if this is a self-change notification.
  162. * @param uri The Uri of the changed content, or null if unknown.
  163. */
  164. public final void dispatchChange(boolean selfChange, Uri uri) {
  165. dispatchChange(selfChange, uri, UserHandle.getCallingUserId());
  166. }
  167. /**
  168. * Dispatches a change notification to the observer. Includes the changed
  169. * content Uri when available and also the user whose content changed.
  170. * <p>
  171. * If a {@link Handler} was supplied to the {@link ContentObserver} constructor,
  172. * then a call to the {@link #onChange} method is posted to the handler's message queue.
  173. * Otherwise, the {@link #onChange} method is invoked immediately on this thread.
  174. * </p>
  175. *
  176. * @param selfChange True if this is a self-change notification.
  177. * @param uri The Uri of the changed content, or null if unknown.
  178. * @param userId The user whose content changed.
  179. */
  180. private void dispatchChange(boolean selfChange, Uri uri, int userId) {
  181. if (mHandler == null) {
  182. onChange(selfChange, uri, userId);
  183. } else {
  184. mHandler.post(new NotificationRunnable(selfChange, uri, userId));
  185. }
  186. }
  187. private final class NotificationRunnable implements Runnable {
  188. private final boolean mSelfChange;
  189. private final Uri mUri;
  190. private final int mUserId;
  191. public NotificationRunnable(boolean selfChange, Uri uri, int userId) {
  192. mSelfChange = selfChange;
  193. mUri = uri;
  194. mUserId = userId;
  195. }
  196. @Override
  197. public void run() {
  198. ContentObserver.this.onChange(mSelfChange, mUri, mUserId);
  199. }
  200. }
  201. private static final class Transport extends IContentObserver.Stub {
  202. private ContentObserver mContentObserver;
  203. public Transport(ContentObserver contentObserver) {
  204. mContentObserver = contentObserver;
  205. }
  206. @Override
  207. public void onChange(boolean selfChange, Uri uri, int userId) {
  208. ContentObserver contentObserver = mContentObserver;
  209. if (contentObserver != null) {
  210. contentObserver.dispatchChange(selfChange, uri, userId);
  211. }
  212. }
  213. public void releaseContentObserver() {
  214. mContentObserver = null;
  215. }
  216. }
  217. }