CommandQueue.java revision e20a177d3f147f3011647c3bdab401f90b2c5d1d
1/*
2 * Copyright (C) 2010 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.Handler;
20import android.os.IBinder;
21import android.os.Message;
22
23import com.android.internal.statusbar.IStatusBar;
24import com.android.internal.statusbar.StatusBarIcon;
25import com.android.internal.statusbar.StatusBarIconList;
26import com.android.internal.statusbar.StatusBarNotification;
27
28/**
29 * This class takes the functions from IStatusBar that come in on
30 * binder pool threads and posts messages to get them onto the main
31 * thread, and calls onto Callbacks.  It also takes care of
32 * coalescing these calls so they don't stack up.  For the calls
33 * are coalesced, note that they are all idempotent.
34 */
35public class CommandQueue extends IStatusBar.Stub {
36    private static final int INDEX_MASK = 0xffff;
37    private static final int MSG_SHIFT  = 16;
38    private static final int MSG_MASK   = 0xffff << MSG_SHIFT;
39
40    private static final int OP_SET_ICON    = 1;
41    private static final int OP_REMOVE_ICON = 2;
42
43    private static final int MSG_ICON                       = 1 << MSG_SHIFT;
44    private static final int MSG_ADD_NOTIFICATION           = 2 << MSG_SHIFT;
45    private static final int MSG_UPDATE_NOTIFICATION        = 3 << MSG_SHIFT;
46    private static final int MSG_REMOVE_NOTIFICATION        = 4 << MSG_SHIFT;
47    private static final int MSG_DISABLE                    = 5 << MSG_SHIFT;
48    private static final int MSG_EXPAND_NOTIFICATIONS       = 6 << MSG_SHIFT;
49    private static final int MSG_COLLAPSE_NOTIFICATIONS     = 7 << MSG_SHIFT;
50    private static final int MSG_EXPAND_QUICK_SETTINGS      = 8 << MSG_SHIFT;
51    private static final int MSG_COLLAPSE_QUICK_SETTINGS    = 9 << MSG_SHIFT;
52    private static final int MSG_SET_SYSTEMUI_VISIBILITY    = 10 << MSG_SHIFT;
53    private static final int MSG_TOP_APP_WINDOW_CHANGED     = 11 << MSG_SHIFT;
54    private static final int MSG_SHOW_IME_BUTTON            = 12 << MSG_SHIFT;
55    private static final int MSG_SET_HARD_KEYBOARD_STATUS   = 13 << MSG_SHIFT;
56    private static final int MSG_TOGGLE_RECENT_APPS         = 14 << MSG_SHIFT;
57    private static final int MSG_PRELOAD_RECENT_APPS        = 15 << MSG_SHIFT;
58    private static final int MSG_CANCEL_PRELOAD_RECENT_APPS = 16 << MSG_SHIFT;
59    private static final int MSG_SET_NAVIGATION_ICON_HINTS  = 17 << MSG_SHIFT;
60
61    public static final int FLAG_EXCLUDE_NONE = 0;
62    public static final int FLAG_EXCLUDE_SEARCH_PANEL = 1 << 0;
63    public static final int FLAG_EXCLUDE_RECENTS_PANEL = 1 << 1;
64    public static final int FLAG_EXCLUDE_NOTIFICATION_PANEL = 1 << 2;
65    public static final int FLAG_EXCLUDE_INPUT_METHODS_PANEL = 1 << 3;
66    public static final int FLAG_EXCLUDE_COMPAT_MODE_PANEL = 1 << 4;
67
68    private StatusBarIconList mList;
69    private Callbacks mCallbacks;
70    private Handler mHandler = new H();
71
72    private class NotificationQueueEntry {
73        IBinder key;
74        StatusBarNotification notification;
75    }
76
77    /**
78     * These methods are called back on the main thread.
79     */
80    public interface Callbacks {
81        public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon);
82        public void updateIcon(String slot, int index, int viewIndex,
83                StatusBarIcon old, StatusBarIcon icon);
84        public void removeIcon(String slot, int index, int viewIndex);
85        public void addNotification(IBinder key, StatusBarNotification notification);
86        public void updateNotification(IBinder key, StatusBarNotification notification);
87        public void removeNotification(IBinder key);
88        public void disable(int state);
89        public void animateExpandNotifications();
90        public void animateCollapseNotifications(int flags);
91        public void animateExpandQuickSettings();
92        public void animateCollapseQuickSettings();
93        public void setSystemUiVisibility(int vis, int mask);
94        public void topAppWindowChanged(boolean visible);
95        public void setImeWindowStatus(IBinder token, int vis, int backDisposition);
96        public void setHardKeyboardStatus(boolean available, boolean enabled);
97        public void toggleRecentApps();
98        public void preloadRecentApps();
99        public void showSearchPanel();
100        public void hideSearchPanel();
101        public void cancelPreloadRecentApps();
102        public void setNavigationIconHints(int hints);
103    }
104
105    public CommandQueue(Callbacks callbacks, StatusBarIconList list) {
106        mCallbacks = callbacks;
107        mList = list;
108    }
109
110    public void setIcon(int index, StatusBarIcon icon) {
111        synchronized (mList) {
112            int what = MSG_ICON | index;
113            mHandler.removeMessages(what);
114            mHandler.obtainMessage(what, OP_SET_ICON, 0, icon.clone()).sendToTarget();
115        }
116    }
117
118    public void removeIcon(int index) {
119        synchronized (mList) {
120            int what = MSG_ICON | index;
121            mHandler.removeMessages(what);
122            mHandler.obtainMessage(what, OP_REMOVE_ICON, 0, null).sendToTarget();
123        }
124    }
125
126    public void addNotification(IBinder key, StatusBarNotification notification) {
127        synchronized (mList) {
128            NotificationQueueEntry ne = new NotificationQueueEntry();
129            ne.key = key;
130            ne.notification = notification;
131            mHandler.obtainMessage(MSG_ADD_NOTIFICATION, 0, 0, ne).sendToTarget();
132        }
133    }
134
135    public void updateNotification(IBinder key, StatusBarNotification notification) {
136        synchronized (mList) {
137            NotificationQueueEntry ne = new NotificationQueueEntry();
138            ne.key = key;
139            ne.notification = notification;
140            mHandler.obtainMessage(MSG_UPDATE_NOTIFICATION, 0, 0, ne).sendToTarget();
141        }
142    }
143
144    public void removeNotification(IBinder key) {
145        synchronized (mList) {
146            mHandler.obtainMessage(MSG_REMOVE_NOTIFICATION, 0, 0, key).sendToTarget();
147        }
148    }
149
150    public void disable(int state) {
151        synchronized (mList) {
152            mHandler.removeMessages(MSG_DISABLE);
153            mHandler.obtainMessage(MSG_DISABLE, state, 0, null).sendToTarget();
154        }
155    }
156
157    public void animateExpandNotifications() {
158        synchronized (mList) {
159            mHandler.removeMessages(MSG_EXPAND_NOTIFICATIONS);
160            mHandler.sendEmptyMessage(MSG_EXPAND_NOTIFICATIONS);
161        }
162    }
163
164    public void animateCollapseNotifications() {
165        synchronized (mList) {
166            mHandler.removeMessages(MSG_COLLAPSE_NOTIFICATIONS);
167            mHandler.sendEmptyMessage(MSG_COLLAPSE_NOTIFICATIONS);
168        }
169    }
170
171    public void animateExpandQuickSettings() {
172        synchronized (mList) {
173            mHandler.removeMessages(MSG_EXPAND_QUICK_SETTINGS);
174            mHandler.sendEmptyMessage(MSG_EXPAND_QUICK_SETTINGS);
175        }
176    }
177
178    public void animateCollapseQuickSettings() {
179        synchronized (mList) {
180            mHandler.removeMessages(MSG_COLLAPSE_QUICK_SETTINGS);
181            mHandler.sendEmptyMessage(MSG_COLLAPSE_QUICK_SETTINGS);
182        }
183    }
184
185    public void setSystemUiVisibility(int vis, int mask) {
186        synchronized (mList) {
187            mHandler.removeMessages(MSG_SET_SYSTEMUI_VISIBILITY);
188            mHandler.obtainMessage(MSG_SET_SYSTEMUI_VISIBILITY, vis, mask, null).sendToTarget();
189        }
190    }
191
192    public void topAppWindowChanged(boolean menuVisible) {
193        synchronized (mList) {
194            mHandler.removeMessages(MSG_TOP_APP_WINDOW_CHANGED);
195            mHandler.obtainMessage(MSG_TOP_APP_WINDOW_CHANGED, menuVisible ? 1 : 0, 0,
196                    null).sendToTarget();
197        }
198    }
199
200    public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
201        synchronized (mList) {
202            mHandler.removeMessages(MSG_SHOW_IME_BUTTON);
203            mHandler.obtainMessage(MSG_SHOW_IME_BUTTON, vis, backDisposition, token)
204                    .sendToTarget();
205        }
206    }
207
208    public void setHardKeyboardStatus(boolean available, boolean enabled) {
209        synchronized (mList) {
210            mHandler.removeMessages(MSG_SET_HARD_KEYBOARD_STATUS);
211            mHandler.obtainMessage(MSG_SET_HARD_KEYBOARD_STATUS,
212                    available ? 1 : 0, enabled ? 1 : 0).sendToTarget();
213        }
214    }
215
216    public void toggleRecentApps() {
217        synchronized (mList) {
218            mHandler.removeMessages(MSG_TOGGLE_RECENT_APPS);
219            mHandler.obtainMessage(MSG_TOGGLE_RECENT_APPS, 0, 0, null).sendToTarget();
220        }
221    }
222
223    public void preloadRecentApps() {
224        synchronized (mList) {
225            mHandler.removeMessages(MSG_PRELOAD_RECENT_APPS);
226            mHandler.obtainMessage(MSG_PRELOAD_RECENT_APPS, 0, 0, null).sendToTarget();
227        }
228    }
229
230    public void cancelPreloadRecentApps() {
231        synchronized (mList) {
232            mHandler.removeMessages(MSG_CANCEL_PRELOAD_RECENT_APPS);
233            mHandler.obtainMessage(MSG_CANCEL_PRELOAD_RECENT_APPS, 0, 0, null).sendToTarget();
234        }
235    }
236
237    public void setNavigationIconHints(int hints) {
238        synchronized (mList) {
239            mHandler.removeMessages(MSG_SET_NAVIGATION_ICON_HINTS);
240            mHandler.obtainMessage(MSG_SET_NAVIGATION_ICON_HINTS, hints, 0, null).sendToTarget();
241        }
242    }
243
244    private final class H extends Handler {
245        public void handleMessage(Message msg) {
246            final int what = msg.what & MSG_MASK;
247            switch (what) {
248                case MSG_ICON: {
249                    final int index = msg.what & INDEX_MASK;
250                    final int viewIndex = mList.getViewIndex(index);
251                    switch (msg.arg1) {
252                        case OP_SET_ICON: {
253                            StatusBarIcon icon = (StatusBarIcon)msg.obj;
254                            StatusBarIcon old = mList.getIcon(index);
255                            if (old == null) {
256                                mList.setIcon(index, icon);
257                                mCallbacks.addIcon(mList.getSlot(index), index, viewIndex, icon);
258                            } else {
259                                mList.setIcon(index, icon);
260                                mCallbacks.updateIcon(mList.getSlot(index), index, viewIndex,
261                                        old, icon);
262                            }
263                            break;
264                        }
265                        case OP_REMOVE_ICON:
266                            if (mList.getIcon(index) != null) {
267                                mList.removeIcon(index);
268                                mCallbacks.removeIcon(mList.getSlot(index), index, viewIndex);
269                            }
270                            break;
271                    }
272                    break;
273                }
274                case MSG_ADD_NOTIFICATION: {
275                    final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj;
276                    mCallbacks.addNotification(ne.key, ne.notification);
277                    break;
278                }
279                case MSG_UPDATE_NOTIFICATION: {
280                    final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj;
281                    mCallbacks.updateNotification(ne.key, ne.notification);
282                    break;
283                }
284                case MSG_REMOVE_NOTIFICATION: {
285                    mCallbacks.removeNotification((IBinder)msg.obj);
286                    break;
287                }
288                case MSG_DISABLE:
289                    mCallbacks.disable(msg.arg1);
290                    break;
291                case MSG_EXPAND_NOTIFICATIONS:
292                    mCallbacks.animateExpandNotifications();
293                    break;
294                case MSG_COLLAPSE_NOTIFICATIONS:
295                    mCallbacks.animateCollapseNotifications(0);
296                    break;
297                case MSG_EXPAND_QUICK_SETTINGS:
298                    mCallbacks.animateExpandQuickSettings();
299                    break;
300                case MSG_COLLAPSE_QUICK_SETTINGS:
301                    mCallbacks.animateCollapseQuickSettings();
302                    break;
303                case MSG_SET_SYSTEMUI_VISIBILITY:
304                    mCallbacks.setSystemUiVisibility(msg.arg1, msg.arg2);
305                    break;
306                case MSG_TOP_APP_WINDOW_CHANGED:
307                    mCallbacks.topAppWindowChanged(msg.arg1 != 0);
308                    break;
309                case MSG_SHOW_IME_BUTTON:
310                    mCallbacks.setImeWindowStatus((IBinder)msg.obj, msg.arg1, msg.arg2);
311                    break;
312                case MSG_SET_HARD_KEYBOARD_STATUS:
313                    mCallbacks.setHardKeyboardStatus(msg.arg1 != 0, msg.arg2 != 0);
314                    break;
315                case MSG_TOGGLE_RECENT_APPS:
316                    mCallbacks.toggleRecentApps();
317                    break;
318                case MSG_PRELOAD_RECENT_APPS:
319                    mCallbacks.preloadRecentApps();
320                    break;
321                case MSG_CANCEL_PRELOAD_RECENT_APPS:
322                    mCallbacks.cancelPreloadRecentApps();
323                    break;
324                case MSG_SET_NAVIGATION_ICON_HINTS:
325                    mCallbacks.setNavigationIconHints(msg.arg1);
326                    break;
327            }
328        }
329    }
330}
331
332