PageRenderTime 34ms CodeModel.GetById 15ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

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