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

http://eyes-free.googlecode.com/ · Java · 194 lines · 80 code · 30 blank · 84 comment · 13 complexity · 099feae1785eee63232ef69e626bf2e8 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 java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. /**
  23. * This class is a cache for notifications. It supports adding, reading,
  24. * removing of notifications as well as formatting these notification for
  25. * presentation to the user.
  26. *
  27. * @author svetoslavganov@google.com (Svetoslav R. Ganov)
  28. */
  29. class NotificationCache {
  30. /** Map of notification types to cached messages. */
  31. private Map<NotificationType, List<String>> mTypeToMessageMap =
  32. new HashMap<NotificationType, List<String>>();
  33. /**
  34. * Creates a new instance.
  35. */
  36. public NotificationCache() {
  37. /* do nothing */
  38. }
  39. /**
  40. * Adds a notifications for a given {@link NotificationType}.
  41. *
  42. * @param type The {@link NotificationType}.
  43. * @param notification The notification message.
  44. * @return True if the notification was added , false otherwise.
  45. */
  46. public boolean addNotification(NotificationType type, String notification) {
  47. List<String> notifications = mTypeToMessageMap.get(type);
  48. if (notifications == null) {
  49. notifications = new ArrayList<String>();
  50. mTypeToMessageMap.put(type, notifications);
  51. }
  52. return notifications.add(notification);
  53. }
  54. /**
  55. * Removes a notification from a given type.
  56. *
  57. * @param type The {@link NotificationType}.
  58. * @param notification The notification message.
  59. * @return True if removed, false otherwise.
  60. */
  61. public boolean removeNotification(NotificationType type, String notification) {
  62. return getNotificationsForType(type).remove(notification);
  63. }
  64. /**
  65. * Gets all notifications form a given {@link NotificationType}.
  66. *
  67. * @param type The {@link NotificationType}.
  68. * @return The notifications as a list. If no notifications exist an empty
  69. * list is returned.
  70. */
  71. public List<String> getNotificationsForType(NotificationType type) {
  72. List<String> notifications = mTypeToMessageMap.get(type);
  73. if (notifications == null) {
  74. notifications = Collections.emptyList();
  75. }
  76. return notifications;
  77. }
  78. /**
  79. * Remove all notifications of a given type.
  80. *
  81. * @param type The {@link NotificationType}.
  82. * @return True if notifications are removed, false otherwise.
  83. */
  84. public boolean removeNotificationsForType(NotificationType type) {
  85. return (mTypeToMessageMap.remove(type) != null);
  86. }
  87. /**
  88. * Returns all notifications.
  89. *
  90. * @return All notifications.
  91. */
  92. public List<String> getNotificationsAll() {
  93. List<String> notifications = new ArrayList<String>();
  94. for (NotificationType type : mTypeToMessageMap.keySet()) {
  95. notifications.addAll(getNotificationsForType(type));
  96. }
  97. return notifications;
  98. }
  99. /**
  100. * Removes all notifications from the cache.
  101. */
  102. public void removeNotificationsAll() {
  103. mTypeToMessageMap.clear();
  104. }
  105. /**
  106. * Returns all notifications for a given type.
  107. *
  108. * @param type The {@link NotificationType}.
  109. * @return The formatted string.
  110. */
  111. public String getFormattedForType(NotificationType type) {
  112. final StringBuilder formatted = new StringBuilder();
  113. final List<String> notifications = getNotificationsForType(type);
  114. for (String notification : notifications) {
  115. formatted.append(getStringForResourceId(type.getValue()));
  116. formatted.append(" ");
  117. formatted.append(notification);
  118. formatted.append(" ");
  119. }
  120. return formatted.toString();
  121. }
  122. /**
  123. * Returns all notifications formatted for presentation.
  124. *
  125. * @return The formatted string.
  126. */
  127. public String getFormattedAll() {
  128. final StringBuilder formatted = new StringBuilder();
  129. for (NotificationType type : mTypeToMessageMap.keySet()) {
  130. formatted.append(getFormattedForType(type));
  131. }
  132. return formatted.toString();
  133. }
  134. /**
  135. * Returns a summary of the notifications.
  136. *
  137. * @return The summary.
  138. */
  139. public String getFormattedSummary() {
  140. final StringBuilder summary = new StringBuilder();
  141. for (Map.Entry<NotificationType, List<String>> entry : mTypeToMessageMap.entrySet()) {
  142. final int count = entry.getValue().size();
  143. if (count > 0 && entry.getKey() != null) {
  144. summary.append(count);
  145. summary.append(" ");
  146. summary.append(getStringForResourceId(entry.getKey().getValue()));
  147. if (count > 1) {
  148. summary.append("s");
  149. }
  150. summary.append("\n");
  151. }
  152. }
  153. return summary.toString();
  154. }
  155. /**
  156. * Returns a string for a resource id.
  157. *
  158. * @param resourceId The resource id.
  159. * @return the string.
  160. */
  161. private String getStringForResourceId(int resourceId) {
  162. return TalkBackService.asContext().getResources().getString(resourceId);
  163. }
  164. }