NotificationData.java revision 51c7510e493680b4aca1ed7695b35c52d2cd63ff
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.os.IBinder;
20import android.service.notification.StatusBarNotification;
21import android.view.View;
22import android.widget.ImageView;
23
24import com.android.systemui.R;
25
26import java.util.ArrayList;
27import java.util.Comparator;
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 ExpandableNotificationRow 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        private View expandedBig;
42        public Entry() {}
43        public Entry(IBinder key, StatusBarNotification n, StatusBarIconView ic) {
44            this.key = key;
45            this.notification = n;
46            this.icon = ic;
47        }
48        public void setBigContentView(View bigContentView) {
49            this.expandedBig = bigContentView;
50            row.setExpandable(bigContentView != null);
51        }
52        public View getBigContentView() {
53            return expandedBig;
54        }
55        /**
56         * Set the flag indicating that this is being touched by the user.
57         */
58        public void setUserLocked(boolean userLocked) {
59            row.setUserLocked(userLocked);
60        }
61    }
62    private final ArrayList<Entry> mEntries = new ArrayList<Entry>();
63    private final Comparator<Entry> mEntryCmp = new Comparator<Entry>() {
64        // sort first by score, then by when
65        public int compare(Entry a, Entry b) {
66            final StatusBarNotification na = a.notification;
67            final StatusBarNotification nb = b.notification;
68            int d = na.getScore() - nb.getScore();
69            return (d != 0)
70                ? d
71                : (int)(na.getNotification().when - nb.getNotification().when);
72        }
73    };
74
75    public int size() {
76        return mEntries.size();
77    }
78
79    public Entry get(int i) {
80        return mEntries.get(i);
81    }
82
83    public Entry findByKey(IBinder key) {
84        for (Entry e : mEntries) {
85            if (e.key == key) {
86                return e;
87            }
88        }
89        return null;
90    }
91
92    public int add(Entry entry) {
93        int i;
94        int N = mEntries.size();
95        for (i=0; i<N; i++) {
96            if (mEntryCmp.compare(mEntries.get(i), entry) > 0) {
97                break;
98            }
99        }
100        mEntries.add(i, entry);
101        return i;
102    }
103
104    public int add(IBinder key, StatusBarNotification notification, ExpandableNotificationRow row,
105            View content, View expanded, StatusBarIconView icon) {
106        Entry entry = new Entry();
107        entry.key = key;
108        entry.notification = notification;
109        entry.row = row;
110        entry.content = content;
111        entry.expanded = expanded;
112        entry.icon = icon;
113        entry.largeIcon = null; // TODO add support for large icons
114        return add(entry);
115    }
116
117    public Entry remove(IBinder key) {
118        Entry e = findByKey(key);
119        if (e != null) {
120            mEntries.remove(e);
121        }
122        return e;
123    }
124
125    /**
126     * Return whether there are any visible items (i.e. items without an error).
127     */
128    public boolean hasVisibleItems() {
129        for (Entry e : mEntries) {
130            if (e.expanded != null) { // the view successfully inflated
131                return true;
132            }
133        }
134        return false;
135    }
136
137    /**
138     * Return whether there are any clearable items (that aren't errors).
139     */
140    public boolean hasClearableItems() {
141        for (Entry e : mEntries) {
142            if (e.expanded != null) { // the view successfully inflated
143                if (e.notification.isClearable()) {
144                    return true;
145                }
146            }
147        }
148        return false;
149    }
150}
151