CommandQueue.java revision df89e65bf0fcc651d20b208c8d8d0b848fb43418
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 String TAG = "StatusBar.CommandQueue";
37
38    private static final int INDEX_MASK = 0xffff;
39    private static final int MSG_SHIFT  = 16;
40    private static final int MSG_MASK   = 0xffff << MSG_SHIFT;
41
42
43    private static final int MSG_ICON                   = 1 << MSG_SHIFT;
44    private static final int OP_SET_ICON    = 1;
45    private static final int OP_REMOVE_ICON = 2;
46
47    private static final int MSG_ADD_NOTIFICATION       = 2 << MSG_SHIFT;
48    private static final int MSG_UPDATE_NOTIFICATION    = 3 << MSG_SHIFT;
49    private static final int MSG_REMOVE_NOTIFICATION    = 4 << MSG_SHIFT;
50
51    private static final int MSG_DISABLE                = 5 << MSG_SHIFT;
52
53    private static final int MSG_SET_VISIBILITY         = 6 << MSG_SHIFT;
54    private static final int OP_EXPAND      = 1;
55    private static final int OP_COLLAPSE    = 2;
56
57    private static final int MSG_SET_SYSTEMUI_VISIBILITY          = 7 << MSG_SHIFT;
58
59    private static final int MSG_TOP_APP_WINDOW_CHANGED = 8 << MSG_SHIFT;
60    private static final int MSG_SHOW_IME_BUTTON        = 9 << MSG_SHIFT;
61    private static final int MSG_SET_HARD_KEYBOARD_STATUS = 10 << MSG_SHIFT;
62
63    private static final int MSG_TOGGLE_RECENT_APPS       = 11 << MSG_SHIFT;
64
65    private StatusBarIconList mList;
66    private Callbacks mCallbacks;
67    private Handler mHandler = new H();
68
69    private class NotificationQueueEntry {
70        IBinder key;
71        StatusBarNotification notification;
72    }
73
74    /**
75     * These methods are called back on the main thread.
76     */
77    public interface Callbacks {
78        public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon);
79        public void updateIcon(String slot, int index, int viewIndex,
80                StatusBarIcon old, StatusBarIcon icon);
81        public void removeIcon(String slot, int index, int viewIndex);
82        public void addNotification(IBinder key, StatusBarNotification notification);
83        public void updateNotification(IBinder key, StatusBarNotification notification);
84        public void removeNotification(IBinder key);
85        public void disable(int state);
86        public void animateExpand();
87        public void animateCollapse();
88        public void setSystemUiVisibility(int vis);
89        public void topAppWindowChanged(boolean visible);
90        public void setImeWindowStatus(IBinder token, int vis, int backDisposition);
91        public void setHardKeyboardStatus(boolean available, boolean enabled);
92        public void toggleRecentApps();
93    }
94
95    public CommandQueue(Callbacks callbacks, StatusBarIconList list) {
96        mCallbacks = callbacks;
97        mList = list;
98    }
99
100    public void setIcon(int index, StatusBarIcon icon) {
101        synchronized (mList) {
102            int what = MSG_ICON | index;
103            mHandler.removeMessages(what);
104            mHandler.obtainMessage(what, OP_SET_ICON, 0, icon.clone()).sendToTarget();
105        }
106    }
107
108    public void removeIcon(int index) {
109        synchronized (mList) {
110            int what = MSG_ICON | index;
111            mHandler.removeMessages(what);
112            mHandler.obtainMessage(what, OP_REMOVE_ICON, 0, null).sendToTarget();
113        }
114    }
115
116    public void addNotification(IBinder key, StatusBarNotification notification) {
117        synchronized (mList) {
118            NotificationQueueEntry ne = new NotificationQueueEntry();
119            ne.key = key;
120            ne.notification = notification;
121            mHandler.obtainMessage(MSG_ADD_NOTIFICATION, 0, 0, ne).sendToTarget();
122        }
123    }
124
125    public void updateNotification(IBinder key, StatusBarNotification notification) {
126        synchronized (mList) {
127            NotificationQueueEntry ne = new NotificationQueueEntry();
128            ne.key = key;
129            ne.notification = notification;
130            mHandler.obtainMessage(MSG_UPDATE_NOTIFICATION, 0, 0, ne).sendToTarget();
131        }
132    }
133
134    public void removeNotification(IBinder key) {
135        synchronized (mList) {
136            mHandler.obtainMessage(MSG_REMOVE_NOTIFICATION, 0, 0, key).sendToTarget();
137        }
138    }
139
140    public void disable(int state) {
141        synchronized (mList) {
142            mHandler.removeMessages(MSG_DISABLE);
143            mHandler.obtainMessage(MSG_DISABLE, state, 0, null).sendToTarget();
144        }
145    }
146
147    public void animateExpand() {
148        synchronized (mList) {
149            mHandler.removeMessages(MSG_SET_VISIBILITY);
150            mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_EXPAND, 0, null).sendToTarget();
151        }
152    }
153
154    public void animateCollapse() {
155        synchronized (mList) {
156            mHandler.removeMessages(MSG_SET_VISIBILITY);
157            mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_COLLAPSE, 0, null).sendToTarget();
158        }
159    }
160
161    public void setSystemUiVisibility(int vis) {
162        synchronized (mList) {
163            mHandler.removeMessages(MSG_SET_SYSTEMUI_VISIBILITY);
164            mHandler.obtainMessage(MSG_SET_SYSTEMUI_VISIBILITY, vis, 0, null).sendToTarget();
165        }
166    }
167
168    public void topAppWindowChanged(boolean menuVisible) {
169        synchronized (mList) {
170            mHandler.removeMessages(MSG_TOP_APP_WINDOW_CHANGED);
171            mHandler.obtainMessage(MSG_TOP_APP_WINDOW_CHANGED, menuVisible ? 1 : 0, 0,
172                    null).sendToTarget();
173        }
174    }
175
176    public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
177        synchronized (mList) {
178            mHandler.removeMessages(MSG_SHOW_IME_BUTTON);
179            mHandler.obtainMessage(MSG_SHOW_IME_BUTTON, vis, backDisposition, token)
180                    .sendToTarget();
181        }
182    }
183
184    public void setHardKeyboardStatus(boolean available, boolean enabled) {
185        synchronized (mList) {
186            mHandler.removeMessages(MSG_SET_HARD_KEYBOARD_STATUS);
187            mHandler.obtainMessage(MSG_SET_HARD_KEYBOARD_STATUS,
188                    available ? 1 : 0, enabled ? 1 : 0).sendToTarget();
189        }
190    }
191
192    public void toggleRecentApps() {
193        synchronized (mList) {
194            mHandler.removeMessages(MSG_TOGGLE_RECENT_APPS);
195            mHandler.obtainMessage(MSG_TOGGLE_RECENT_APPS, 0, 0, null).sendToTarget();
196        }
197    }
198
199    private final class H extends Handler {
200        public void handleMessage(Message msg) {
201            final int what = msg.what & MSG_MASK;
202            switch (what) {
203                case MSG_ICON: {
204                    final int index = msg.what & INDEX_MASK;
205                    final int viewIndex = mList.getViewIndex(index);
206                    switch (msg.arg1) {
207                        case OP_SET_ICON: {
208                            StatusBarIcon icon = (StatusBarIcon)msg.obj;
209                            StatusBarIcon old = mList.getIcon(index);
210                            if (old == null) {
211                                mList.setIcon(index, icon);
212                                mCallbacks.addIcon(mList.getSlot(index), index, viewIndex, icon);
213                            } else {
214                                mList.setIcon(index, icon);
215                                mCallbacks.updateIcon(mList.getSlot(index), index, viewIndex,
216                                        old, icon);
217                            }
218                            break;
219                        }
220                        case OP_REMOVE_ICON:
221                            if (mList.getIcon(index) != null) {
222                                mList.removeIcon(index);
223                                mCallbacks.removeIcon(mList.getSlot(index), index, viewIndex);
224                            }
225                            break;
226                    }
227                    break;
228                }
229                case MSG_ADD_NOTIFICATION: {
230                    final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj;
231                    mCallbacks.addNotification(ne.key, ne.notification);
232                    break;
233                }
234                case MSG_UPDATE_NOTIFICATION: {
235                    final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj;
236                    mCallbacks.updateNotification(ne.key, ne.notification);
237                    break;
238                }
239                case MSG_REMOVE_NOTIFICATION: {
240                    mCallbacks.removeNotification((IBinder)msg.obj);
241                    break;
242                }
243                case MSG_DISABLE:
244                    mCallbacks.disable(msg.arg1);
245                    break;
246                case MSG_SET_VISIBILITY:
247                    if (msg.arg1 == OP_EXPAND) {
248                        mCallbacks.animateExpand();
249                    } else {
250                        mCallbacks.animateCollapse();
251                    }
252                    break;
253                case MSG_SET_SYSTEMUI_VISIBILITY:
254                    mCallbacks.setSystemUiVisibility(msg.arg1);
255                    break;
256                case MSG_TOP_APP_WINDOW_CHANGED:
257                    mCallbacks.topAppWindowChanged(msg.arg1 != 0);
258                    break;
259                case MSG_SHOW_IME_BUTTON:
260                    mCallbacks.setImeWindowStatus((IBinder)msg.obj, msg.arg1, msg.arg2);
261                    break;
262                case MSG_SET_HARD_KEYBOARD_STATUS:
263                    mCallbacks.setHardKeyboardStatus(msg.arg1 != 0, msg.arg2 != 0);
264                    break;
265                case MSG_TOGGLE_RECENT_APPS:
266                    mCallbacks.toggleRecentApps();
267                    break;
268            }
269        }
270    }
271}
272
273