CommandQueue.java revision 3b1fc47d004f6b29af8f40d181baa3460b1e3b15
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_LIGHTS_ON          = 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_USER_ACTIVITY          = 11 << MSG_SHIFT;
64    private static final int MSG_TOGGLE_RECENT_APPS       = 12 << MSG_SHIFT;
65
66    private StatusBarIconList mList;
67    private Callbacks mCallbacks;
68    private Handler mHandler = new H();
69
70    private class NotificationQueueEntry {
71        IBinder key;
72        StatusBarNotification notification;
73    }
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(IBinder key, StatusBarNotification notification);
84        public void updateNotification(IBinder key, StatusBarNotification notification);
85        public void removeNotification(IBinder key);
86        public void disable(int state);
87        public void animateExpand();
88        public void animateCollapse();
89        public void setLightsOn(boolean on);
90        public void topAppWindowChanged(boolean visible);
91        public void setImeWindowStatus(IBinder token, int vis, int backDisposition);
92        public void setHardKeyboardStatus(boolean available, boolean enabled);
93        public void userActivity();
94        public void toggleRecentApps();
95    }
96
97    public CommandQueue(Callbacks callbacks, StatusBarIconList list) {
98        mCallbacks = callbacks;
99        mList = list;
100    }
101
102    public void setIcon(int index, StatusBarIcon icon) {
103        synchronized (mList) {
104            int what = MSG_ICON | index;
105            mHandler.removeMessages(what);
106            mHandler.obtainMessage(what, OP_SET_ICON, 0, icon.clone()).sendToTarget();
107        }
108    }
109
110    public void removeIcon(int index) {
111        synchronized (mList) {
112            int what = MSG_ICON | index;
113            mHandler.removeMessages(what);
114            mHandler.obtainMessage(what, OP_REMOVE_ICON, 0, null).sendToTarget();
115        }
116    }
117
118    public void addNotification(IBinder key, StatusBarNotification notification) {
119        synchronized (mList) {
120            NotificationQueueEntry ne = new NotificationQueueEntry();
121            ne.key = key;
122            ne.notification = notification;
123            mHandler.obtainMessage(MSG_ADD_NOTIFICATION, 0, 0, ne).sendToTarget();
124        }
125    }
126
127    public void updateNotification(IBinder key, StatusBarNotification notification) {
128        synchronized (mList) {
129            NotificationQueueEntry ne = new NotificationQueueEntry();
130            ne.key = key;
131            ne.notification = notification;
132            mHandler.obtainMessage(MSG_UPDATE_NOTIFICATION, 0, 0, ne).sendToTarget();
133        }
134    }
135
136    public void removeNotification(IBinder key) {
137        synchronized (mList) {
138            mHandler.obtainMessage(MSG_REMOVE_NOTIFICATION, 0, 0, key).sendToTarget();
139        }
140    }
141
142    public void disable(int state) {
143        synchronized (mList) {
144            mHandler.removeMessages(MSG_DISABLE);
145            mHandler.obtainMessage(MSG_DISABLE, state, 0, null).sendToTarget();
146        }
147    }
148
149    public void animateExpand() {
150        synchronized (mList) {
151            mHandler.removeMessages(MSG_SET_VISIBILITY);
152            mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_EXPAND, 0, null).sendToTarget();
153        }
154    }
155
156    public void animateCollapse() {
157        synchronized (mList) {
158            mHandler.removeMessages(MSG_SET_VISIBILITY);
159            mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_COLLAPSE, 0, null).sendToTarget();
160        }
161    }
162
163    public void setLightsOn(boolean on) {
164        synchronized (mList) {
165            mHandler.removeMessages(MSG_SET_LIGHTS_ON);
166            mHandler.obtainMessage(MSG_SET_LIGHTS_ON, on ? 1 : 0, 0, null).sendToTarget();
167        }
168    }
169
170    public void topAppWindowChanged(boolean menuVisible) {
171        synchronized (mList) {
172            mHandler.removeMessages(MSG_TOP_APP_WINDOW_CHANGED);
173            mHandler.obtainMessage(MSG_TOP_APP_WINDOW_CHANGED, menuVisible ? 1 : 0, 0,
174                    null).sendToTarget();
175        }
176    }
177
178    public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
179        synchronized (mList) {
180            mHandler.removeMessages(MSG_SHOW_IME_BUTTON);
181            mHandler.obtainMessage(MSG_SHOW_IME_BUTTON, vis, backDisposition, token)
182                    .sendToTarget();
183        }
184    }
185
186    public void setHardKeyboardStatus(boolean available, boolean enabled) {
187        synchronized (mList) {
188            mHandler.removeMessages(MSG_SET_HARD_KEYBOARD_STATUS);
189            mHandler.obtainMessage(MSG_SET_HARD_KEYBOARD_STATUS,
190                    available ? 1 : 0, enabled ? 1 : 0).sendToTarget();
191        }
192    }
193
194    public void userActivity() {
195        synchronized (mList) {
196            mHandler.removeMessages(MSG_USER_ACTIVITY);
197            mHandler.obtainMessage(MSG_USER_ACTIVITY, 0, 0, null).sendToTarget();
198        }
199    }
200
201    public void toggleRecentApps() {
202        synchronized (mList) {
203            mHandler.removeMessages(MSG_TOGGLE_RECENT_APPS);
204            mHandler.obtainMessage(MSG_TOGGLE_RECENT_APPS, 0, 0, null).sendToTarget();
205        }
206    }
207
208    private final class H extends Handler {
209        public void handleMessage(Message msg) {
210            final int what = msg.what & MSG_MASK;
211            switch (what) {
212                case MSG_ICON: {
213                    final int index = msg.what & INDEX_MASK;
214                    final int viewIndex = mList.getViewIndex(index);
215                    switch (msg.arg1) {
216                        case OP_SET_ICON: {
217                            StatusBarIcon icon = (StatusBarIcon)msg.obj;
218                            StatusBarIcon old = mList.getIcon(index);
219                            if (old == null) {
220                                mList.setIcon(index, icon);
221                                mCallbacks.addIcon(mList.getSlot(index), index, viewIndex, icon);
222                            } else {
223                                mList.setIcon(index, icon);
224                                mCallbacks.updateIcon(mList.getSlot(index), index, viewIndex,
225                                        old, icon);
226                            }
227                            break;
228                        }
229                        case OP_REMOVE_ICON:
230                            if (mList.getIcon(index) != null) {
231                                mList.removeIcon(index);
232                                mCallbacks.removeIcon(mList.getSlot(index), index, viewIndex);
233                            }
234                            break;
235                    }
236                    break;
237                }
238                case MSG_ADD_NOTIFICATION: {
239                    final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj;
240                    mCallbacks.addNotification(ne.key, ne.notification);
241                    break;
242                }
243                case MSG_UPDATE_NOTIFICATION: {
244                    final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj;
245                    mCallbacks.updateNotification(ne.key, ne.notification);
246                    break;
247                }
248                case MSG_REMOVE_NOTIFICATION: {
249                    mCallbacks.removeNotification((IBinder)msg.obj);
250                    break;
251                }
252                case MSG_DISABLE:
253                    mCallbacks.disable(msg.arg1);
254                    break;
255                case MSG_SET_VISIBILITY:
256                    if (msg.arg1 == OP_EXPAND) {
257                        mCallbacks.animateExpand();
258                    } else {
259                        mCallbacks.animateCollapse();
260                    }
261                    break;
262                case MSG_SET_LIGHTS_ON:
263                    mCallbacks.setLightsOn(msg.arg1 != 0);
264                    break;
265                case MSG_TOP_APP_WINDOW_CHANGED:
266                    mCallbacks.topAppWindowChanged(msg.arg1 != 0);
267                    break;
268                case MSG_SHOW_IME_BUTTON:
269                    mCallbacks.setImeWindowStatus((IBinder)msg.obj, msg.arg1, msg.arg2);
270                    break;
271                case MSG_SET_HARD_KEYBOARD_STATUS:
272                    mCallbacks.setHardKeyboardStatus(msg.arg1 != 0, msg.arg2 != 0);
273                    break;
274                case MSG_USER_ACTIVITY:
275                    mCallbacks.userActivity();
276                    break;
277                case MSG_TOGGLE_RECENT_APPS:
278                    mCallbacks.toggleRecentApps();
279                    break;
280            }
281        }
282    }
283}
284
285