blob: f8dfa8fcf6b0f2a40191b406a7a884ec4323798a [file] [log] [blame]
Joe Onorato0cbda992010-05-02 16:28:15 -07001/*
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
Joe Onorato79de0c52010-05-26 17:03:26 -040017package com.android.systemui.statusbar;
Joe Onorato0cbda992010-05-02 16:28:15 -070018
19import android.os.Handler;
Joe Onoratoa0c56fe2010-05-20 10:21:52 -070020import android.os.IBinder;
Joe Onorato0cbda992010-05-02 16:28:15 -070021import android.os.Message;
22
23import com.android.internal.statusbar.IStatusBar;
24import com.android.internal.statusbar.StatusBarIcon;
25import com.android.internal.statusbar.StatusBarIconList;
Joe Onoratoa0c56fe2010-05-20 10:21:52 -070026import com.android.internal.statusbar.StatusBarNotification;
Joe Onorato0cbda992010-05-02 16:28:15 -070027
Joe Onoratof3f0e052010-05-14 18:49:29 -070028/**
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
Joe Onorato4762c2d2010-05-17 15:42:59 -070032 * coalescing these calls so they don't stack up. For the calls
33 * are coalesced, note that they are all idempotent.
Joe Onoratof3f0e052010-05-14 18:49:29 -070034 */
Joe Onorato808182d2010-07-09 18:52:06 -040035public class CommandQueue extends IStatusBar.Stub {
Joe Onorato514ad6632010-05-13 18:49:00 -070036 private static final String TAG = "StatusBar.CommandQueue";
Joe Onorato0cbda992010-05-02 16:28:15 -070037
Daniel Sandler1d4d30a2011-04-28 12:35:29 -040038 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;
Joe Onorato0cbda992010-05-02 16:28:15 -070041
Daniel Sandler1d4d30a2011-04-28 12:35:29 -040042
43 private static final int MSG_ICON = 1 << MSG_SHIFT;
44 private static final int OP_SET_ICON = 1;
Joe Onorato0cbda992010-05-02 16:28:15 -070045 private static final int OP_REMOVE_ICON = 2;
46
Daniel Sandler1d4d30a2011-04-28 12:35:29 -040047 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;
Joe Onoratof3f0e052010-05-14 18:49:29 -070050
Daniel Sandler1d4d30a2011-04-28 12:35:29 -040051 private static final int MSG_DISABLE = 5 << MSG_SHIFT;
Joe Onoratoa0c56fe2010-05-20 10:21:52 -070052
Daniel Sandler1d4d30a2011-04-28 12:35:29 -040053 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;
Joe Onorato4762c2d2010-05-17 15:42:59 -070056
Daniel Sandler60ee2562011-07-22 12:34:33 -040057 private static final int MSG_SET_SYSTEMUI_VISIBILITY = 7 << MSG_SHIFT;
Joe Onorato93056472010-09-10 10:30:46 -040058
Dianne Hackborn3fe9cc52011-06-14 16:13:26 -070059 private static final int MSG_TOP_APP_WINDOW_CHANGED = 8 << MSG_SHIFT;
Daniel Sandler1d4d30a2011-04-28 12:35:29 -040060 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
Dianne Hackborndf89e652011-10-06 22:35:11 -070063 private static final int MSG_TOGGLE_RECENT_APPS = 11 << MSG_SHIFT;
Daniel Sandler1d4d30a2011-04-28 12:35:29 -040064
Daniel Sandler328310c2011-09-23 15:56:52 -040065 private static final int MSG_SET_NAVIGATION_ICON_HINTS = 13 << MSG_SHIFT;
66
Joe Onorato0cbda992010-05-02 16:28:15 -070067 private StatusBarIconList mList;
68 private Callbacks mCallbacks;
69 private Handler mHandler = new H();
70
Joe Onoratoa0c56fe2010-05-20 10:21:52 -070071 private class NotificationQueueEntry {
72 IBinder key;
73 StatusBarNotification notification;
74 }
75
Joe Onorato0cbda992010-05-02 16:28:15 -070076 /**
77 * These methods are called back on the main thread.
78 */
79 public interface Callbacks {
80 public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon);
81 public void updateIcon(String slot, int index, int viewIndex,
82 StatusBarIcon old, StatusBarIcon icon);
83 public void removeIcon(String slot, int index, int viewIndex);
Joe Onoratoa0c56fe2010-05-20 10:21:52 -070084 public void addNotification(IBinder key, StatusBarNotification notification);
85 public void updateNotification(IBinder key, StatusBarNotification notification);
86 public void removeNotification(IBinder key);
Joe Onoratof3f0e052010-05-14 18:49:29 -070087 public void disable(int state);
Joe Onorato4762c2d2010-05-17 15:42:59 -070088 public void animateExpand();
89 public void animateCollapse();
Daniel Sandler60ee2562011-07-22 12:34:33 -040090 public void setSystemUiVisibility(int vis);
Dianne Hackborn7d049322011-06-14 15:00:32 -070091 public void topAppWindowChanged(boolean visible);
Joe Onorato857fd9b2011-01-27 15:08:35 -080092 public void setImeWindowStatus(IBinder token, int vis, int backDisposition);
Jeff Brown2992ea72011-01-28 22:04:14 -080093 public void setHardKeyboardStatus(boolean available, boolean enabled);
Michael Jurka3b1fc472011-06-13 10:54:40 -070094 public void toggleRecentApps();
Daniel Sandler328310c2011-09-23 15:56:52 -040095 public void setNavigationIconHints(int hints);
Joe Onorato0cbda992010-05-02 16:28:15 -070096 }
97
98 public CommandQueue(Callbacks callbacks, StatusBarIconList list) {
99 mCallbacks = callbacks;
100 mList = list;
101 }
102
103 public void setIcon(int index, StatusBarIcon icon) {
104 synchronized (mList) {
105 int what = MSG_ICON | index;
106 mHandler.removeMessages(what);
107 mHandler.obtainMessage(what, OP_SET_ICON, 0, icon.clone()).sendToTarget();
108 }
109 }
110
111 public void removeIcon(int index) {
112 synchronized (mList) {
113 int what = MSG_ICON | index;
114 mHandler.removeMessages(what);
115 mHandler.obtainMessage(what, OP_REMOVE_ICON, 0, null).sendToTarget();
116 }
117 }
118
Joe Onoratoa0c56fe2010-05-20 10:21:52 -0700119 public void addNotification(IBinder key, StatusBarNotification notification) {
120 synchronized (mList) {
121 NotificationQueueEntry ne = new NotificationQueueEntry();
122 ne.key = key;
123 ne.notification = notification;
124 mHandler.obtainMessage(MSG_ADD_NOTIFICATION, 0, 0, ne).sendToTarget();
125 }
126 }
127
128 public void updateNotification(IBinder key, StatusBarNotification notification) {
129 synchronized (mList) {
130 NotificationQueueEntry ne = new NotificationQueueEntry();
131 ne.key = key;
132 ne.notification = notification;
133 mHandler.obtainMessage(MSG_UPDATE_NOTIFICATION, 0, 0, ne).sendToTarget();
134 }
135 }
136
137 public void removeNotification(IBinder key) {
138 synchronized (mList) {
139 mHandler.obtainMessage(MSG_REMOVE_NOTIFICATION, 0, 0, key).sendToTarget();
140 }
141 }
142
Joe Onoratof3f0e052010-05-14 18:49:29 -0700143 public void disable(int state) {
144 synchronized (mList) {
145 mHandler.removeMessages(MSG_DISABLE);
146 mHandler.obtainMessage(MSG_DISABLE, state, 0, null).sendToTarget();
147 }
148 }
149
Joe Onorato4762c2d2010-05-17 15:42:59 -0700150 public void animateExpand() {
151 synchronized (mList) {
152 mHandler.removeMessages(MSG_SET_VISIBILITY);
153 mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_EXPAND, 0, null).sendToTarget();
154 }
155 }
156
157 public void animateCollapse() {
158 synchronized (mList) {
159 mHandler.removeMessages(MSG_SET_VISIBILITY);
160 mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_COLLAPSE, 0, null).sendToTarget();
161 }
162 }
163
Daniel Sandler60ee2562011-07-22 12:34:33 -0400164 public void setSystemUiVisibility(int vis) {
Joe Onorato93056472010-09-10 10:30:46 -0400165 synchronized (mList) {
Daniel Sandler60ee2562011-07-22 12:34:33 -0400166 mHandler.removeMessages(MSG_SET_SYSTEMUI_VISIBILITY);
167 mHandler.obtainMessage(MSG_SET_SYSTEMUI_VISIBILITY, vis, 0, null).sendToTarget();
Joe Onorato93056472010-09-10 10:30:46 -0400168 }
169 }
170
Dianne Hackborn7d049322011-06-14 15:00:32 -0700171 public void topAppWindowChanged(boolean menuVisible) {
Daniel Sandlere02d8082010-10-08 15:13:22 -0400172 synchronized (mList) {
Dianne Hackborn7d049322011-06-14 15:00:32 -0700173 mHandler.removeMessages(MSG_TOP_APP_WINDOW_CHANGED);
174 mHandler.obtainMessage(MSG_TOP_APP_WINDOW_CHANGED, menuVisible ? 1 : 0, 0,
175 null).sendToTarget();
Daniel Sandlere02d8082010-10-08 15:13:22 -0400176 }
177 }
178
Joe Onorato857fd9b2011-01-27 15:08:35 -0800179 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +0900180 synchronized (mList) {
181 mHandler.removeMessages(MSG_SHOW_IME_BUTTON);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800182 mHandler.obtainMessage(MSG_SHOW_IME_BUTTON, vis, backDisposition, token)
183 .sendToTarget();
satok06487a52010-10-29 11:37:18 +0900184 }
185 }
186
Jeff Brown2992ea72011-01-28 22:04:14 -0800187 public void setHardKeyboardStatus(boolean available, boolean enabled) {
188 synchronized (mList) {
189 mHandler.removeMessages(MSG_SET_HARD_KEYBOARD_STATUS);
190 mHandler.obtainMessage(MSG_SET_HARD_KEYBOARD_STATUS,
191 available ? 1 : 0, enabled ? 1 : 0).sendToTarget();
192 }
193 }
194
Michael Jurka3b1fc472011-06-13 10:54:40 -0700195 public void toggleRecentApps() {
196 synchronized (mList) {
197 mHandler.removeMessages(MSG_TOGGLE_RECENT_APPS);
198 mHandler.obtainMessage(MSG_TOGGLE_RECENT_APPS, 0, 0, null).sendToTarget();
199 }
200 }
201
Daniel Sandler328310c2011-09-23 15:56:52 -0400202 public void setNavigationIconHints(int hints) {
203 synchronized (mList) {
204 mHandler.removeMessages(MSG_SET_NAVIGATION_ICON_HINTS);
205 mHandler.obtainMessage(MSG_SET_NAVIGATION_ICON_HINTS, hints, 0, null).sendToTarget();
206 }
207 }
208
Joe Onorato0cbda992010-05-02 16:28:15 -0700209 private final class H extends Handler {
210 public void handleMessage(Message msg) {
Joe Onoratof3f0e052010-05-14 18:49:29 -0700211 final int what = msg.what & MSG_MASK;
Joe Onorato66d7d012010-05-14 10:05:10 -0700212 switch (what) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700213 case MSG_ICON: {
Joe Onoratof3f0e052010-05-14 18:49:29 -0700214 final int index = msg.what & INDEX_MASK;
215 final int viewIndex = mList.getViewIndex(index);
Joe Onorato0cbda992010-05-02 16:28:15 -0700216 switch (msg.arg1) {
217 case OP_SET_ICON: {
218 StatusBarIcon icon = (StatusBarIcon)msg.obj;
219 StatusBarIcon old = mList.getIcon(index);
220 if (old == null) {
221 mList.setIcon(index, icon);
222 mCallbacks.addIcon(mList.getSlot(index), index, viewIndex, icon);
223 } else {
224 mList.setIcon(index, icon);
225 mCallbacks.updateIcon(mList.getSlot(index), index, viewIndex,
226 old, icon);
227 }
228 break;
229 }
230 case OP_REMOVE_ICON:
Joe Onorato795f2842010-09-27 11:34:46 -0700231 if (mList.getIcon(index) != null) {
232 mList.removeIcon(index);
233 mCallbacks.removeIcon(mList.getSlot(index), index, viewIndex);
234 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700235 break;
236 }
237 break;
Joe Onoratof3f0e052010-05-14 18:49:29 -0700238 }
Joe Onoratoa0c56fe2010-05-20 10:21:52 -0700239 case MSG_ADD_NOTIFICATION: {
240 final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj;
241 mCallbacks.addNotification(ne.key, ne.notification);
242 break;
243 }
244 case MSG_UPDATE_NOTIFICATION: {
245 final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj;
246 mCallbacks.updateNotification(ne.key, ne.notification);
247 break;
248 }
249 case MSG_REMOVE_NOTIFICATION: {
250 mCallbacks.removeNotification((IBinder)msg.obj);
251 break;
252 }
Joe Onoratof3f0e052010-05-14 18:49:29 -0700253 case MSG_DISABLE:
254 mCallbacks.disable(msg.arg1);
255 break;
Joe Onorato4762c2d2010-05-17 15:42:59 -0700256 case MSG_SET_VISIBILITY:
257 if (msg.arg1 == OP_EXPAND) {
258 mCallbacks.animateExpand();
259 } else {
260 mCallbacks.animateCollapse();
261 }
Joe Onorato93056472010-09-10 10:30:46 -0400262 break;
Daniel Sandler60ee2562011-07-22 12:34:33 -0400263 case MSG_SET_SYSTEMUI_VISIBILITY:
264 mCallbacks.setSystemUiVisibility(msg.arg1);
Joe Onorato93056472010-09-10 10:30:46 -0400265 break;
Dianne Hackborn7d049322011-06-14 15:00:32 -0700266 case MSG_TOP_APP_WINDOW_CHANGED:
267 mCallbacks.topAppWindowChanged(msg.arg1 != 0);
Daniel Sandlere02d8082010-10-08 15:13:22 -0400268 break;
satok06487a52010-10-29 11:37:18 +0900269 case MSG_SHOW_IME_BUTTON:
Joe Onorato857fd9b2011-01-27 15:08:35 -0800270 mCallbacks.setImeWindowStatus((IBinder)msg.obj, msg.arg1, msg.arg2);
satok06487a52010-10-29 11:37:18 +0900271 break;
Jeff Brown2992ea72011-01-28 22:04:14 -0800272 case MSG_SET_HARD_KEYBOARD_STATUS:
273 mCallbacks.setHardKeyboardStatus(msg.arg1 != 0, msg.arg2 != 0);
274 break;
Michael Jurka3b1fc472011-06-13 10:54:40 -0700275 case MSG_TOGGLE_RECENT_APPS:
276 mCallbacks.toggleRecentApps();
277 break;
Daniel Sandler328310c2011-09-23 15:56:52 -0400278 case MSG_SET_NAVIGATION_ICON_HINTS:
279 mCallbacks.setNavigationIconHints(msg.arg1);
280 break;
Joe Onorato0cbda992010-05-02 16:28:15 -0700281 }
282 }
283 }
284}
285