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