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