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

http://eyes-free.googlecode.com/ · Java · 191 lines · 128 code · 34 blank · 29 comment · 50 complexity · 477a26cdeab4930eba8df7575d9ddb78 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 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.Context;
  18. import android.os.Parcel;
  19. import android.os.Parcelable;
  20. import android.util.Log;
  21. /**
  22. * Type safe enumeration for various notification types.
  23. *
  24. * @author svetoslavganov@google.com (Svetoslav R. Ganov)
  25. */
  26. public enum NotificationType implements Parcelable {
  27. TEXT_MESSAGE(R.string.notification_type_text_message), TEXT_MESSAGE_FAILED(
  28. R.string.notification_type_failed_text_message), MISSED_CALL(
  29. R.string.notification_type_missed_call), USB_CONNECTED(
  30. R.string.notification_type_usb_connected),
  31. MUTE(R.string.notification_type_status_mute), CHAT(R.string.notification_type_status_chat),
  32. ERROR(R.string.notification_type_status_error), MORE(R.string.notification_type_status_more),
  33. SDCARD(R.string.notification_type_status_sdcard), SDCARD_USB(
  34. R.string.notification_type_status_sdcard_usb), SYNC(R.string.notification_status_sync),
  35. SYNC_NOANIM(R.string.notification_type_status_sync_noanim), VOICEMAIL(
  36. R.string.notification_type_status_voicemail), PLAY(R.string.notification_status_play),
  37. EMAIL(R.string.notification_type_new_email);
  38. private int mValue;
  39. private static final int ICON_SMS = loadIcon("com.android.mms", "com.android.mms.R$drawable",
  40. "stat_notify_sms");
  41. private static final int ICON_SMS_FAILED = loadIcon("com.android.mms",
  42. "com.android.mms.R$drawable", "stat_notify_sms_failed");
  43. private static final int ICON_PLAY = loadIcon("com.google.android.music",
  44. "com.android.music.R$drawable", "stat_notify_musicplayer");
  45. private static final int ICON_GMAIL = loadIcon("com.google.android.gm",
  46. "com.google.android.gm.R$drawable", "stat_notify_email");
  47. private static final int ICON_EMAIL = loadIcon("com.google.android.email",
  48. "com.android.email.R$drawable", "stat_notify_email_generic");
  49. // INCLUDED in android.jar (accessible via API)
  50. private static final int ICON_MISSED_CALL = android.R.drawable.stat_notify_missed_call;
  51. private static final int ICON_MUTE = android.R.drawable.stat_notify_call_mute;
  52. private static final int ICON_CHAT = android.R.drawable.stat_notify_chat;
  53. private static final int ICON_ERROR = android.R.drawable.stat_notify_error;
  54. private static final int ICON_MORE = android.R.drawable.stat_notify_more;
  55. private static final int ICON_SDCARD = android.R.drawable.stat_notify_sdcard;
  56. private static final int ICON_SDCARD_USB = android.R.drawable.stat_notify_sdcard_usb;
  57. private static final int ICON_SYNC = android.R.drawable.stat_notify_sync;
  58. private static final int ICON_SYNC_NOANIM = android.R.drawable.stat_notify_sync_noanim;
  59. private static final int ICON_VOICEMAIL = android.R.drawable.stat_notify_voicemail;
  60. // This constant must be public to reference it to screen out phone calls
  61. public static final int ICON_PHONE_CALL = android.R.drawable.stat_sys_phone_call;
  62. private static final int INVALID_ICON = -1;
  63. private NotificationType(int value) {
  64. mValue = value;
  65. }
  66. public int getValue() {
  67. return mValue;
  68. }
  69. @Override
  70. public int describeContents() {
  71. return 0;
  72. }
  73. @Override
  74. public void writeToParcel(Parcel parcel, int flags) {
  75. parcel.writeInt(mValue);
  76. }
  77. /**
  78. * Gets an enumeration member for value.
  79. *
  80. * @param value The value.
  81. * @return The enumeration member.
  82. */
  83. private static NotificationType getMemberForValue(int value) {
  84. for (NotificationType type : values()) {
  85. if (type.mValue == value) {
  86. return type;
  87. }
  88. }
  89. return null;
  90. }
  91. public static final Parcelable.Creator<NotificationType> CREATOR =
  92. new Parcelable.Creator<NotificationType>() {
  93. @Override
  94. public NotificationType createFromParcel(Parcel parcel) {
  95. int value = parcel.readInt();
  96. return getMemberForValue(value);
  97. }
  98. @Override
  99. public NotificationType[] newArray(int size) {
  100. return new NotificationType[size];
  101. }
  102. };
  103. public static NotificationType getNotificationTypeFromIcon(int icon) {
  104. // Can't use switch because not all of the icon fields are constant
  105. if (icon == ICON_SMS) {
  106. return NotificationType.TEXT_MESSAGE;
  107. } else if (icon == ICON_SMS_FAILED) {
  108. return NotificationType.TEXT_MESSAGE_FAILED;
  109. } else if (icon == ICON_MISSED_CALL) {
  110. return NotificationType.MISSED_CALL;
  111. } else if (icon == ICON_MUTE) {
  112. return NotificationType.MUTE;
  113. } else if (icon == ICON_CHAT) {
  114. return NotificationType.CHAT;
  115. } else if (icon == ICON_ERROR) {
  116. return NotificationType.ERROR;
  117. } else if (icon == ICON_MORE) {
  118. return NotificationType.MORE;
  119. } else if (icon == ICON_SDCARD) {
  120. return NotificationType.SDCARD;
  121. } else if (icon == ICON_SDCARD_USB) {
  122. return NotificationType.SDCARD_USB;
  123. } else if (icon == ICON_SYNC) {
  124. return NotificationType.SYNC;
  125. } else if (icon == ICON_SYNC_NOANIM) {
  126. return NotificationType.SYNC_NOANIM;
  127. } else if (icon == ICON_VOICEMAIL) {
  128. return NotificationType.VOICEMAIL;
  129. } else if (icon == ICON_EMAIL) {
  130. return NotificationType.EMAIL;
  131. } else if (icon == ICON_GMAIL) {
  132. return NotificationType.EMAIL;
  133. } else if (icon == ICON_PLAY) {
  134. return NotificationType.PLAY;
  135. } else {
  136. LogUtils.log(NotificationType.class, Log.WARN, "Unknown notification %d", icon);
  137. return null;
  138. }
  139. }
  140. public static int loadIcon(String packageName, String className, String fieldName) {
  141. final ClassLoadingManager clm = ClassLoadingManager.getInstance();
  142. final Context ctx = TalkBackService.asContext();
  143. final Class<?> drawable = clm.loadOrGetCachedClass(ctx, className, packageName);
  144. if (drawable == null) {
  145. LogUtils.log(NotificationType.class, Log.WARN,
  146. "Can't find class drawable in package: %s", packageName);
  147. return INVALID_ICON;
  148. }
  149. try {
  150. return drawable.getDeclaredField(fieldName).getInt(null);
  151. } catch (Exception e) {
  152. LogUtils.log(NotificationType.class, Log.WARN,
  153. "Can't find drawable: %s in package: %s", fieldName, packageName, e.toString());
  154. return INVALID_ICON;
  155. }
  156. }
  157. }