NotificationData.java revision d0694b6735a9d91794e6096961231e07364ba3fa
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
73    private final ArrayList<Entry> mEntries = new ArrayList<Entry>();
74    private RankingMap mRanking;
75    private final Comparator<Entry> mRankingComparator = new Comparator<Entry>() {
76        @Override
77        public int compare(Entry a, Entry b) {
78            if (mRanking != null) {
79                Ranking aRanking = mRanking.getRanking(a.key);
80                Ranking bRanking = mRanking.getRanking(b.key);
81                int aRank = aRanking != null ? aRanking.getRank() : -1;
82                int bRank = bRanking != null ? bRanking.getRank() : -1;
83                return aRank - bRank;
84            }
85
86            final StatusBarNotification na = a.notification;
87            final StatusBarNotification nb = b.notification;
88            int d = nb.getScore() - na.getScore();
89            if (a.interruption != b.interruption) {
90                return a.interruption ? -1 : 1;
91            } else if (d != 0) {
92                return d;
93            } else {
94                return (int) (nb.getNotification().when - na.getNotification().when);
95            }
96        }
97    };
98
99    public int size() {
100        return mEntries.size();
101    }
102
103    public Entry get(int i) {
104        return mEntries.get(i);
105    }
106
107    public Entry findByKey(String key) {
108        for (Entry e : mEntries) {
109            if (e.key.equals(key)) {
110                return e;
111            }
112        }
113        return null;
114    }
115
116    public void add(Entry entry, RankingMap ranking) {
117        mEntries.add(entry);
118        updateRankingAndSort(ranking);
119    }
120
121    public Entry remove(String key, RankingMap ranking) {
122        Entry e = findByKey(key);
123        if (e == null) {
124            return null;
125        }
126        mEntries.remove(e);
127        updateRankingAndSort(ranking);
128        return e;
129    }
130
131    public void updateRanking(RankingMap ranking) {
132        updateRankingAndSort(ranking);
133    }
134
135    public boolean isAmbient(String key) {
136        // TODO: Remove when switching to NotificationListener.
137        if (mRanking == null) {
138            for (Entry entry : mEntries) {
139                if (key.equals(entry.key)) {
140                    return entry.notification.getNotification().priority ==
141                            Notification.PRIORITY_MIN;
142                }
143            }
144        } else {
145            Ranking ranking = mRanking.getRanking(key);
146            return ranking != null && ranking.isAmbient();
147        }
148        return false;
149    }
150
151    private void updateRankingAndSort(RankingMap ranking) {
152        if (ranking != null) {
153            mRanking = ranking;
154        }
155        Collections.sort(mEntries, mRankingComparator);
156    }
157
158    /**
159     * Return whether there are any visible items (i.e. items without an error).
160     */
161    public boolean hasVisibleItems() {
162        for (Entry e : mEntries) {
163            if (e.expanded != null) { // the view successfully inflated
164                return true;
165            }
166        }
167        return false;
168    }
169
170    /**
171     * Return whether there are any clearable items (that aren't errors).
172     */
173    public boolean hasClearableItems() {
174        for (Entry e : mEntries) {
175            if (e.expanded != null) { // the view successfully inflated
176                if (e.notification.isClearable()) {
177                    return true;
178                }
179            }
180        }
181        return false;
182    }
183}
184