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

https://github.com/LordManta/sm_android_frameworks_base · Java · 168 lines · 94 code · 25 blank · 49 comment · 19 complexity · df06fa1a50a23a4783bfc828de7af7c9 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.os.Handler;
  18. import android.net.Uri;
  19. /**
  20. * Receives call backs for changes to content. Must be implemented by objects which are added
  21. * to a {@link ContentObservable}.
  22. */
  23. public abstract class ContentObserver {
  24. private Transport mTransport;
  25. // Protects mTransport
  26. private Object lock = new Object();
  27. /* package */ Handler mHandler;
  28. private final class NotificationRunnable implements Runnable {
  29. private boolean mSelf;
  30. private Uri mUri = null;
  31. public NotificationRunnable(boolean self) {
  32. mSelf = self;
  33. }
  34. public NotificationRunnable(Uri uri, boolean self) {
  35. mSelf = self;
  36. mUri = uri;
  37. }
  38. public void run() {
  39. if(mUri != null) {
  40. ContentObserver.this.onChangeUri(mUri, mSelf);
  41. } else {
  42. ContentObserver.this.onChange(mSelf);
  43. }
  44. }
  45. }
  46. private static final class Transport extends IContentObserver.Stub {
  47. ContentObserver mContentObserver;
  48. public Transport(ContentObserver contentObserver) {
  49. mContentObserver = contentObserver;
  50. }
  51. public boolean deliverSelfNotifications() {
  52. ContentObserver contentObserver = mContentObserver;
  53. if (contentObserver != null) {
  54. return contentObserver.deliverSelfNotifications();
  55. }
  56. return false;
  57. }
  58. public void onChange(boolean selfChange) {
  59. ContentObserver contentObserver = mContentObserver;
  60. if (contentObserver != null) {
  61. contentObserver.dispatchChange(selfChange);
  62. }
  63. }
  64. public void onChangeUri(Uri uri, boolean selfChange) {
  65. ContentObserver contentObserver = mContentObserver;
  66. if (contentObserver != null) {
  67. contentObserver.dispatchChange(uri, selfChange);
  68. }
  69. }
  70. public void releaseContentObserver() {
  71. mContentObserver = null;
  72. }
  73. }
  74. /**
  75. * onChange() will happen on the provider Handler.
  76. *
  77. * @param handler The handler to run {@link #onChange} on.
  78. */
  79. public ContentObserver(Handler handler) {
  80. mHandler = handler;
  81. }
  82. /**
  83. * Gets access to the binder transport object. Not for public consumption.
  84. *
  85. * {@hide}
  86. */
  87. public IContentObserver getContentObserver() {
  88. synchronized(lock) {
  89. if (mTransport == null) {
  90. mTransport = new Transport(this);
  91. }
  92. return mTransport;
  93. }
  94. }
  95. /**
  96. * Gets access to the binder transport object, and unlinks the transport object
  97. * from the ContentObserver. Not for public consumption.
  98. *
  99. * {@hide}
  100. */
  101. public IContentObserver releaseContentObserver() {
  102. synchronized(lock) {
  103. Transport oldTransport = mTransport;
  104. if (oldTransport != null) {
  105. oldTransport.releaseContentObserver();
  106. mTransport = null;
  107. }
  108. return oldTransport;
  109. }
  110. }
  111. /**
  112. * Returns true if this observer is interested in notifications for changes
  113. * made through the cursor the observer is registered with.
  114. */
  115. public boolean deliverSelfNotifications() {
  116. return false;
  117. }
  118. /**
  119. * This method is called when a change occurs to the cursor that
  120. * is being observed.
  121. *
  122. * @param selfChange true if the update was caused by a call to <code>commit</code> on the
  123. * cursor that is being observed.
  124. */
  125. public void onChange(boolean selfChange) {}
  126. /** @hide */
  127. public void onChangeUri(Uri uri, boolean selfChange) {}
  128. public final void dispatchChange(boolean selfChange) {
  129. if (mHandler == null) {
  130. onChange(selfChange);
  131. } else {
  132. mHandler.post(new NotificationRunnable(selfChange));
  133. }
  134. }
  135. /** @hide */
  136. public final void dispatchChange(Uri uri, boolean selfChange) {
  137. if (mHandler == null) {
  138. onChangeUri(uri, selfChange);
  139. } else {
  140. mHandler.post(new NotificationRunnable(uri, selfChange));
  141. }
  142. }
  143. }