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

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