/TalkBack/src/com/google/android/marvin/talkback/BasePackageMonitor.java

http://eyes-free.googlecode.com/ · Java · 129 lines · 53 code · 18 blank · 58 comment · 11 complexity · 1eefae54fbb85962919a1c72e6067b68 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 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 com.google.android.marvin.talkback;
  17. import android.content.BroadcastReceiver;
  18. import android.content.Context;
  19. import android.content.Intent;
  20. import android.content.IntentFilter;
  21. import android.net.Uri;
  22. /**
  23. * Helper class for monitoring packages on the system.
  24. *
  25. * @author svetoslavganov@google.com (Svetoslav R. Ganov)
  26. * @author alanv@google.com (Alan Viverette)
  27. */
  28. abstract class BasePackageMonitor extends BroadcastReceiver {
  29. /**
  30. * The intent filter to match package modifications.
  31. */
  32. private final IntentFilter mPackageFilter;
  33. /**
  34. * The context in which this monitor is registered.
  35. */
  36. private Context mRegisteredContext;
  37. /**
  38. * Creates a new instance.
  39. */
  40. public BasePackageMonitor() {
  41. mPackageFilter = new IntentFilter();
  42. mPackageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
  43. mPackageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
  44. mPackageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
  45. mPackageFilter.addDataScheme("package");
  46. }
  47. /**
  48. * Register this monitor via the given <code>context</code>. Throws an
  49. * {@link IllegalStateException} if this monitor was already registered.
  50. */
  51. public void register(Context context) {
  52. if (mRegisteredContext != null) {
  53. throw new IllegalStateException("Already registered");
  54. }
  55. mRegisteredContext = context;
  56. context.registerReceiver(this, mPackageFilter);
  57. }
  58. /**
  59. * Unregister this monitor. Throws an {@link IllegalStateException} if this
  60. * monitor wasn't registered.
  61. */
  62. public void unregister() {
  63. if (mRegisteredContext == null) {
  64. throw new IllegalStateException("Not registered");
  65. }
  66. mRegisteredContext.unregisterReceiver(this);
  67. mRegisteredContext = null;
  68. }
  69. @Override
  70. public void onReceive(Context context, Intent intent) {
  71. final String packageName = getPackageName(intent);
  72. final String action = intent.getAction();
  73. if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
  74. onPackageAdded(packageName);
  75. } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
  76. onPackageRemoved(packageName);
  77. } else if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
  78. onPackageChanged(packageName);
  79. }
  80. }
  81. /**
  82. * @return The name of the package from an <code>intent</code>.
  83. */
  84. private static String getPackageName(Intent intent) {
  85. final Uri uri = intent.getData();
  86. if (uri == null) {
  87. return null;
  88. }
  89. return uri.getSchemeSpecificPart();
  90. }
  91. /**
  92. * Called when a new application package has been installed on the device.
  93. *
  94. * @param packageName The name of the package that was added.
  95. */
  96. protected abstract void onPackageAdded(String packageName);
  97. /**
  98. * Called when an existing application package has been removed from the
  99. * device.
  100. *
  101. * @param packageName The name of the package that was removed.
  102. */
  103. protected abstract void onPackageRemoved(String packageName);
  104. /**
  105. * Called when an existing application package has been changed (e.g. a
  106. * component has been disabled or enabled).
  107. *
  108. * @param packageName The name of the package that was changed.
  109. */
  110. protected abstract void onPackageChanged(String packageName);
  111. }