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