NotificationData.java revision a31e4190cb44eaa51bb8dee7d715fcd72d6c0b03
1/*
2 * Copyright (C) 2008 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.android.systemui.statusbar;
18
19import android.app.Notification;
20import android.os.IBinder;
21import android.view.View;
22import android.widget.ImageView;
23
24import com.android.internal.statusbar.StatusBarNotification;
25
26import java.util.Comparator;
27import java.util.ArrayList;
28
29/**
30 * The list of currently displaying notifications.
31 */
32public class NotificationData {
33    public static final class Entry {
34        public IBinder key;
35        public StatusBarNotification notification;
36        public StatusBarIconView icon;
37        public View row; // the outer expanded view
38        public View content; // takes the click events and sends the PendingIntent
39        public View expanded; // the inflated RemoteViews
40        public ImageView largeIcon;
41        public Entry() {}
42        public Entry(IBinder key, StatusBarNotification n, StatusBarIconView ic) {
43            this.key = key;
44            this.notification = n;
45            this.icon = ic;
46        }
47    }
48    private final ArrayList<Entry> mEntries = new ArrayList<Entry>();
49    private final Comparator<Entry> mEntryCmp = new Comparator<Entry>() {
50        public int compare(Entry a, Entry b) {
51            final StatusBarNotification na = a.notification;
52            final StatusBarNotification nb = b.notification;
53            int priDiff = na.priority - nb.priority;
54            return (priDiff != 0)
55                ? priDiff
56                : (int)(na.notification.when - nb.notification.when);
57        }
58    };
59
60    public int size() {
61        return mEntries.size();
62    }
63
64    public Entry get(int i) {
65        return mEntries.get(i);
66    }
67
68    public Entry findByKey(IBinder key) {
69        for (Entry e : mEntries) {
70            if (e.key == key) {
71                return e;
72            }
73        }
74        return null;
75    }
76
77    public int add(Entry entry) {
78        int i;
79        int N = mEntries.size();
80        for (i=0; i<N; i++) {
81            if (mEntryCmp.compare(mEntries.get(i), entry) > 0) {
82                break;
83            }
84        }
85        mEntries.add(i, entry);
86        return i;
87    }
88
89    public int add(IBinder key, StatusBarNotification notification, View row, View content,
90            View expanded, StatusBarIconView icon) {
91        Entry entry = new Entry();
92        entry.key = key;
93        entry.notification = notification;
94        entry.row = row;
95        entry.content = content;
96        entry.expanded = expanded;
97        entry.icon = icon;
98        entry.largeIcon = null; // TODO add support for large icons
99        return add(entry);
100    }
101
102    public Entry remove(IBinder key) {
103        Entry e = findByKey(key);
104        if (e != null) {
105            mEntries.remove(e);
106        }
107        return e;
108    }
109
110    /**
111     * Return whether there are any visible items (i.e. items without an error).
112     */
113    public boolean hasVisibleItems() {
114        for (Entry e : mEntries) {
115            if (e.expanded != null) { // the view successfully inflated
116                return true;
117            }
118        }
119        return false;
120    }
121
122    /**
123     * Return whether there are any clearable items (that aren't errors).
124     */
125    public boolean hasClearableItems() {
126        for (Entry e : mEntries) {
127            if (e.expanded != null) { // the view successfully inflated
128                if (e.notification.isClearable()) {
129                    return true;
130                }
131            }
132        }
133        return false;
134    }
135}
136