CommandQueue.java revision 71f18fd1b64071e486bafff237b1f87a56d4aead
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;
22import android.service.notification.StatusBarNotification;
23
24import com.android.internal.statusbar.IStatusBar;
25import com.android.internal.statusbar.StatusBarIcon;
26import com.android.internal.statusbar.StatusBarIconList;
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_PANELS            = 7 << MSG_SHIFT;
50    private static final int MSG_EXPAND_SETTINGS            = 8 << MSG_SHIFT;
51    private static final int MSG_SET_SYSTEMUI_VISIBILITY    = 9 << MSG_SHIFT;
52    private static final int MSG_TOP_APP_WINDOW_CHANGED     = 10 << MSG_SHIFT;
53    private static final int MSG_SHOW_IME_BUTTON            = 11 << MSG_SHIFT;
54    private static final int MSG_SET_HARD_KEYBOARD_STATUS   = 12 << MSG_SHIFT;
55    private static final int MSG_TOGGLE_RECENT_APPS         = 13 << MSG_SHIFT;
56    private static final int MSG_PRELOAD_RECENT_APPS        = 14 << MSG_SHIFT;
57    private static final int MSG_CANCEL_PRELOAD_RECENT_APPS = 15 << MSG_SHIFT;
58    private static final int MSG_SET_WINDOW_STATE           = 16 << MSG_SHIFT;
59    private static final int MSG_SHOW_RECENT_APPS           = 17 << MSG_SHIFT;
60    private static final int MSG_HIDE_RECENT_APPS           = 18 << MSG_SHIFT;
61
62    public static final int FLAG_EXCLUDE_NONE = 0;
63    public static final int FLAG_EXCLUDE_SEARCH_PANEL = 1 << 0;
64    public static final int FLAG_EXCLUDE_RECENTS_PANEL = 1 << 1;
65    public static final int FLAG_EXCLUDE_NOTIFICATION_PANEL = 1 << 2;
66    public static final int FLAG_EXCLUDE_INPUT_METHODS_PANEL = 1 << 3;
67    public static final int FLAG_EXCLUDE_COMPAT_MODE_PANEL = 1 << 4;
68
69    private static final String SHOW_IME_SWITCHER_KEY = "showImeSwitcherKey";
70
71    private StatusBarIconList mList;
72    private Callbacks mCallbacks;
73    private Handler mHandler = new H();
74
75    /**
76     * These methods are called back on the main thread.
77     */
78    public interface Callbacks {
79        public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon);
80        public void updateIcon(String slot, int index, int viewIndex,
81                StatusBarIcon old, StatusBarIcon icon);
82        public void removeIcon(String slot, int index, int viewIndex);
83        public void addNotification(StatusBarNotification notification);
84        public void updateNotification(StatusBarNotification notification);
85        public void removeNotification(String key);
86        public void disable(int state);
87        public void animateExpandNotificationsPanel();
88        public void animateCollapsePanels(int flags);
89        public void animateExpandSettingsPanel();
90        public void setSystemUiVisibility(int vis, int mask);
91        public void topAppWindowChanged(boolean visible);
92        public void setImeWindowStatus(IBinder token, int vis, int backDisposition,
93                boolean showImeSwitcher);
94        public void setHardKeyboardStatus(boolean available, boolean enabled);
95        public void showRecentApps(boolean triggeredFromAltTab);
96        public void hideRecentApps(boolean triggeredFromAltTab);
97        public void toggleRecentApps();
98        public void preloadRecentApps();
99        public void cancelPreloadRecentApps();
100        public void showSearchPanel();
101        public void hideSearchPanel();
102        public void setWindowState(int window, int state);
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    @Override
127    public void addNotification(StatusBarNotification notification) {
128        synchronized (mList) {
129            mHandler.obtainMessage(MSG_ADD_NOTIFICATION, 0, 0, notification).sendToTarget();
130        }
131    }
132
133    @Override
134    public void updateNotification(StatusBarNotification notification) {
135        synchronized (mList) {
136            mHandler.obtainMessage(MSG_UPDATE_NOTIFICATION, 0, 0, notification).sendToTarget();
137        }
138    }
139
140    public void removeNotification(String key) {
141        synchronized (mList) {
142            mHandler.obtainMessage(MSG_REMOVE_NOTIFICATION, 0, 0, key).sendToTarget();
143        }
144    }
145
146    public void disable(int state) {
147        synchronized (mList) {
148            mHandler.removeMessages(MSG_DISABLE);
149            mHandler.obtainMessage(MSG_DISABLE, state, 0, null).sendToTarget();
150        }
151    }
152
153    public void animateExpandNotificationsPanel() {
154        synchronized (mList) {
155            mHandler.removeMessages(MSG_EXPAND_NOTIFICATIONS);
156            mHandler.sendEmptyMessage(MSG_EXPAND_NOTIFICATIONS);
157        }
158    }
159
160    public void animateCollapsePanels() {
161        synchronized (mList) {
162            mHandler.removeMessages(MSG_COLLAPSE_PANELS);
163            mHandler.sendEmptyMessage(MSG_COLLAPSE_PANELS);
164        }
165    }
166
167    public void animateExpandSettingsPanel() {
168        synchronized (mList) {
169            mHandler.removeMessages(MSG_EXPAND_SETTINGS);
170            mHandler.sendEmptyMessage(MSG_EXPAND_SETTINGS);
171        }
172    }
173
174    public void setSystemUiVisibility(int vis, int mask) {
175        synchronized (mList) {
176            mHandler.removeMessages(MSG_SET_SYSTEMUI_VISIBILITY);
177            mHandler.obtainMessage(MSG_SET_SYSTEMUI_VISIBILITY, vis, mask, null).sendToTarget();
178        }
179    }
180
181    public void topAppWindowChanged(boolean menuVisible) {
182        synchronized (mList) {
183            mHandler.removeMessages(MSG_TOP_APP_WINDOW_CHANGED);
184            mHandler.obtainMessage(MSG_TOP_APP_WINDOW_CHANGED, menuVisible ? 1 : 0, 0,
185                    null).sendToTarget();
186        }
187    }
188
189    public void setImeWindowStatus(IBinder token, int vis, int backDisposition,
190            boolean showImeSwitcher) {
191        synchronized (mList) {
192            mHandler.removeMessages(MSG_SHOW_IME_BUTTON);
193            Message m = mHandler.obtainMessage(MSG_SHOW_IME_BUTTON, vis, backDisposition, token);
194            m.getData().putBoolean(SHOW_IME_SWITCHER_KEY, showImeSwitcher);
195            m.sendToTarget();
196        }
197    }
198
199    public void setHardKeyboardStatus(boolean available, boolean enabled) {
200        synchronized (mList) {
201            mHandler.removeMessages(MSG_SET_HARD_KEYBOARD_STATUS);
202            mHandler.obtainMessage(MSG_SET_HARD_KEYBOARD_STATUS,
203                    available ? 1 : 0, enabled ? 1 : 0).sendToTarget();
204        }
205    }
206
207    public void showRecentApps(boolean triggeredFromAltTab) {
208        synchronized (mList) {
209            mHandler.removeMessages(MSG_SHOW_RECENT_APPS);
210            mHandler.obtainMessage(MSG_SHOW_RECENT_APPS,
211                    triggeredFromAltTab ? 1 : 0, 0, null).sendToTarget();
212        }
213    }
214
215    public void hideRecentApps(boolean triggeredFromAltTab) {
216        synchronized (mList) {
217            mHandler.removeMessages(MSG_HIDE_RECENT_APPS);
218            mHandler.obtainMessage(MSG_HIDE_RECENT_APPS,
219                    triggeredFromAltTab ? 1 : 0, 0, null).sendToTarget();
220        }
221    }
222
223    public void toggleRecentApps() {
224        synchronized (mList) {
225            mHandler.removeMessages(MSG_TOGGLE_RECENT_APPS);
226            mHandler.obtainMessage(MSG_TOGGLE_RECENT_APPS, 0, 0, null).sendToTarget();
227        }
228    }
229
230    public void preloadRecentApps() {
231        synchronized (mList) {
232            mHandler.removeMessages(MSG_PRELOAD_RECENT_APPS);
233            mHandler.obtainMessage(MSG_PRELOAD_RECENT_APPS, 0, 0, null).sendToTarget();
234        }
235    }
236
237    public void cancelPreloadRecentApps() {
238        synchronized (mList) {
239            mHandler.removeMessages(MSG_CANCEL_PRELOAD_RECENT_APPS);
240            mHandler.obtainMessage(MSG_CANCEL_PRELOAD_RECENT_APPS, 0, 0, null).sendToTarget();
241        }
242    }
243
244    public void setWindowState(int window, int state) {
245        synchronized (mList) {
246            // don't coalesce these
247            mHandler.obtainMessage(MSG_SET_WINDOW_STATE, window, state, null).sendToTarget();
248        }
249    }
250
251
252    private final class H extends Handler {
253        public void handleMessage(Message msg) {
254            final int what = msg.what & MSG_MASK;
255            switch (what) {
256                case MSG_ICON: {
257                    final int index = msg.what & INDEX_MASK;
258                    final int viewIndex = mList.getViewIndex(index);
259                    switch (msg.arg1) {
260                        case OP_SET_ICON: {
261                            StatusBarIcon icon = (StatusBarIcon)msg.obj;
262                            StatusBarIcon old = mList.getIcon(index);
263                            if (old == null) {
264                                mList.setIcon(index, icon);
265                                mCallbacks.addIcon(mList.getSlot(index), index, viewIndex, icon);
266                            } else {
267                                mList.setIcon(index, icon);
268                                mCallbacks.updateIcon(mList.getSlot(index), index, viewIndex,
269                                        old, icon);
270                            }
271                            break;
272                        }
273                        case OP_REMOVE_ICON:
274                            if (mList.getIcon(index) != null) {
275                                mList.removeIcon(index);
276                                mCallbacks.removeIcon(mList.getSlot(index), index, viewIndex);
277                            }
278                            break;
279                    }
280                    break;
281                }
282                case MSG_ADD_NOTIFICATION: {
283                    mCallbacks.addNotification((StatusBarNotification) msg.obj);
284                    break;
285                }
286                case MSG_UPDATE_NOTIFICATION: {
287                    mCallbacks.updateNotification((StatusBarNotification) msg.obj);
288                    break;
289                }
290                case MSG_REMOVE_NOTIFICATION: {
291                    mCallbacks.removeNotification((String) msg.obj);
292                    break;
293                }
294                case MSG_DISABLE:
295                    mCallbacks.disable(msg.arg1);
296                    break;
297                case MSG_EXPAND_NOTIFICATIONS:
298                    mCallbacks.animateExpandNotificationsPanel();
299                    break;
300                case MSG_COLLAPSE_PANELS:
301                    mCallbacks.animateCollapsePanels(0);
302                    break;
303                case MSG_EXPAND_SETTINGS:
304                    mCallbacks.animateExpandSettingsPanel();
305                    break;
306                case MSG_SET_SYSTEMUI_VISIBILITY:
307                    mCallbacks.setSystemUiVisibility(msg.arg1, msg.arg2);
308                    break;
309                case MSG_TOP_APP_WINDOW_CHANGED:
310                    mCallbacks.topAppWindowChanged(msg.arg1 != 0);
311                    break;
312                case MSG_SHOW_IME_BUTTON:
313                    mCallbacks.setImeWindowStatus((IBinder) msg.obj, msg.arg1, msg.arg2,
314                            msg.getData().getBoolean(SHOW_IME_SWITCHER_KEY, false));
315                    break;
316                case MSG_SET_HARD_KEYBOARD_STATUS:
317                    mCallbacks.setHardKeyboardStatus(msg.arg1 != 0, msg.arg2 != 0);
318                    break;
319                case MSG_SHOW_RECENT_APPS:
320                    mCallbacks.showRecentApps(msg.arg1 != 0);
321                    break;
322                case MSG_HIDE_RECENT_APPS:
323                    mCallbacks.hideRecentApps(msg.arg1 != 0);
324                    break;
325                case MSG_TOGGLE_RECENT_APPS:
326                    mCallbacks.toggleRecentApps();
327                    break;
328                case MSG_PRELOAD_RECENT_APPS:
329                    mCallbacks.preloadRecentApps();
330                    break;
331                case MSG_CANCEL_PRELOAD_RECENT_APPS:
332                    mCallbacks.cancelPreloadRecentApps();
333                    break;
334                case MSG_SET_WINDOW_STATE:
335                    mCallbacks.setWindowState(msg.arg1, msg.arg2);
336                    break;
337
338            }
339        }
340    }
341}
342
343