NotificationData.java revision 22f2ee567dd1b1a42432251229bcb2f05c1c4700
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.service.notification.NotificationListenerService.Ranking;
21import android.service.notification.NotificationListenerService.RankingMap;
22import android.service.notification.StatusBarNotification;
23import android.view.View;
24
25import java.util.ArrayList;
26import java.util.Collections;
27import java.util.Comparator;
28
29/**
30 * The list of currently displaying notifications.
31 *
32 * TODO: Rename to NotificationList.
33 */
34public class NotificationData {
35    public static final class Entry {
36        public String key;
37        public StatusBarNotification notification;
38        public StatusBarIconView icon;
39        public ExpandableNotificationRow row; // the outer expanded view
40        public View expanded; // the inflated RemoteViews
41        public View expandedPublic; // for insecure lockscreens
42        public View expandedBig;
43        private boolean interruption;
44        public boolean autoRedacted; // whether the redacted notification was generated by us
45        public boolean legacy; // whether the notification has a legacy, dark background
46
47        public Entry() {}
48        public Entry(StatusBarNotification n, StatusBarIconView ic) {
49            this.key = n.getKey();
50            this.notification = n;
51            this.icon = ic;
52        }
53        public void setBigContentView(View bigContentView) {
54            this.expandedBig = bigContentView;
55            row.setExpandable(bigContentView != null);
56        }
57        public View getBigContentView() {
58            return expandedBig;
59        }
60        public View getPublicContentView() { return expandedPublic; }
61        /**
62         * Set the flag indicating that this is being touched by the user.
63         */
64        public void setUserLocked(boolean userLocked) {
65            row.setUserLocked(userLocked);
66        }
67
68        public void setInterruption() {
69            interruption = true;
70        }
71
72        public boolean hasInterrupted() {
73            return interruption;
74        }
75
76        /**
77         * Resets the notification entry to be re-used.
78         */
79        public void reset() {
80            // NOTE: Icon needs to be preserved for now.
81            // We should fix this at some point.
82            expanded = null;
83            expandedPublic = null;
84            expandedBig = null;
85            autoRedacted = false;
86            legacy = false;
87            if (row != null) {
88                row.reset();
89            }
90        }
91    }
92
93    private final ArrayList<Entry> mEntries = new ArrayList<Entry>();
94    private RankingMap mRankingMap;
95    private final Ranking mTmpRanking = new Ranking();
96    private final Comparator<Entry> mRankingComparator = new Comparator<Entry>() {
97        private final Ranking mRankingA = new Ranking();
98        private final Ranking mRankingB = new Ranking();
99
100        @Override
101        public int compare(Entry a, Entry b) {
102            if (mRankingMap != null) {
103                mRankingMap.getRanking(a.key, mRankingA);
104                mRankingMap.getRanking(b.key, mRankingB);
105                return mRankingA.getRank() - mRankingB.getRank();
106            }
107
108            final StatusBarNotification na = a.notification;
109            final StatusBarNotification nb = b.notification;
110            int d = nb.getScore() - na.getScore();
111            if (a.interruption != b.interruption) {
112                return a.interruption ? -1 : 1;
113            } else if (d != 0) {
114                return d;
115            } else {
116                return (int) (nb.getNotification().when - na.getNotification().when);
117            }
118        }
119    };
120
121    public int size() {
122        return mEntries.size();
123    }
124
125    public Entry get(int i) {
126        return mEntries.get(i);
127    }
128
129    public Entry findByKey(String key) {
130        for (Entry e : mEntries) {
131            if (e.key.equals(key)) {
132                return e;
133            }
134        }
135        return null;
136    }
137
138    public void add(Entry entry, RankingMap ranking) {
139        mEntries.add(entry);
140        updateRankingAndSort(ranking);
141    }
142
143    public Entry remove(String key, RankingMap ranking) {
144        Entry e = findByKey(key);
145        if (e == null) {
146            return null;
147        }
148        mEntries.remove(e);
149        updateRankingAndSort(ranking);
150        return e;
151    }
152
153    public void updateRanking(RankingMap ranking) {
154        updateRankingAndSort(ranking);
155    }
156
157    public boolean isAmbient(String key) {
158        // TODO: Remove when switching to NotificationListener.
159        if (mRankingMap == null) {
160            for (Entry entry : mEntries) {
161                if (key.equals(entry.key)) {
162                    return entry.notification.getNotification().priority ==
163                            Notification.PRIORITY_MIN;
164                }
165            }
166        } else {
167            mRankingMap.getRanking(key, mTmpRanking);
168            return mTmpRanking.isAmbient();
169        }
170        return false;
171    }
172
173    private void updateRankingAndSort(RankingMap ranking) {
174        if (ranking != null) {
175            mRankingMap = ranking;
176        }
177        Collections.sort(mEntries, mRankingComparator);
178    }
179
180    /**
181     * Return whether there are any visible items (i.e. items without an error).
182     */
183    public boolean hasVisibleItems() {
184        for (Entry e : mEntries) {
185            if (e.expanded != null) { // the view successfully inflated
186                return true;
187            }
188        }
189        return false;
190    }
191
192    /**
193     * Return whether there are any clearable items (that aren't errors).
194     */
195    public boolean hasClearableItems() {
196        for (Entry e : mEntries) {
197            if (e.expanded != null) { // the view successfully inflated
198                if (e.notification.isClearable()) {
199                    return true;
200                }
201            }
202        }
203        return false;
204    }
205}
206