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

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