NotificationData.java revision 2561b0b10a55841a08e0e1d467e73e10b1bf256d
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        // sort first by score, then by when
51        public int compare(Entry a, Entry b) {
52            final StatusBarNotification na = a.notification;
53            final StatusBarNotification nb = b.notification;
54            int d = na.score - nb.score;
55            return (d != 0)
56                ? d
57                : (int)(na.notification.when - nb.notification.when);
58        }
59    };
60
61    public int size() {
62        return mEntries.size();
63    }
64
65    public Entry get(int i) {
66        return mEntries.get(i);
67    }
68
69    public Entry findByKey(IBinder key) {
70        for (Entry e : mEntries) {
71            if (e.key == key) {
72                return e;
73            }
74        }
75        return null;
76    }
77
78    public int add(Entry entry) {
79        int i;
80        int N = mEntries.size();
81        for (i=0; i<N; i++) {
82            if (mEntryCmp.compare(mEntries.get(i), entry) > 0) {
83                break;
84            }
85        }
86        mEntries.add(i, entry);
87        return i;
88    }
89
90    public int add(IBinder key, StatusBarNotification notification, View row, View content,
91            View expanded, StatusBarIconView icon) {
92        Entry entry = new Entry();
93        entry.key = key;
94        entry.notification = notification;
95        entry.row = row;
96        entry.content = content;
97        entry.expanded = expanded;
98        entry.icon = icon;
99        entry.largeIcon = null; // TODO add support for large icons
100        return add(entry);
101    }
102
103    public Entry remove(IBinder key) {
104        Entry e = findByKey(key);
105        if (e != null) {
106            mEntries.remove(e);
107        }
108        return e;
109    }
110
111    /**
112     * Return whether there are any visible items (i.e. items without an error).
113     */
114    public boolean hasVisibleItems() {
115        for (Entry e : mEntries) {
116            if (e.expanded != null) { // the view successfully inflated
117                return true;
118            }
119        }
120        return false;
121    }
122
123    /**
124     * Return whether there are any clearable items (that aren't errors).
125     */
126    public boolean hasClearableItems() {
127        for (Entry e : mEntries) {
128            if (e.expanded != null) { // the view successfully inflated
129                if (e.notification.isClearable()) {
130                    return true;
131                }
132            }
133        }
134        return false;
135    }
136}
137