blob: da1e1c5e72c53b91fca36b2e014cccd6fe78ad35 [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 Sandler1d4d30a2011-04-28 12:35:29 -040057 private static final int MSG_SET_LIGHTS_ON = 7 << MSG_SHIFT;
Joe Onorato93056472010-09-10 10:30:46 -040058
Daniel Sandler1d4d30a2011-04-28 12:35:29 -040059 private static final int MSG_SHOW_MENU = 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
Daniel Sandlere02d8082010-10-08 15:13:22 -040065
Joe Onorato0cbda992010-05-02 16:28:15 -070066 private StatusBarIconList mList;
67 private Callbacks mCallbacks;
68 private Handler mHandler = new H();
69
Joe Onoratoa0c56fe2010-05-20 10:21:52 -070070 private class NotificationQueueEntry {
71 IBinder key;
72 StatusBarNotification notification;
73 }
74
Joe Onorato0cbda992010-05-02 16:28:15 -070075 /**
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);
Joe Onoratoa0c56fe2010-05-20 10:21:52 -070083 public void addNotification(IBinder key, StatusBarNotification notification);
84 public void updateNotification(IBinder key, StatusBarNotification notification);
85 public void removeNotification(IBinder key);
Joe Onoratof3f0e052010-05-14 18:49:29 -070086 public void disable(int state);
Joe Onorato4762c2d2010-05-17 15:42:59 -070087 public void animateExpand();
88 public void animateCollapse();
Joe Onorato93056472010-09-10 10:30:46 -040089 public void setLightsOn(boolean on);
Daniel Sandlere02d8082010-10-08 15:13:22 -040090 public void setMenuKeyVisible(boolean visible);
Joe Onorato857fd9b2011-01-27 15:08:35 -080091 public void setImeWindowStatus(IBinder token, int vis, int backDisposition);
Jeff Brown2992ea72011-01-28 22:04:14 -080092 public void setHardKeyboardStatus(boolean available, boolean enabled);
Daniel Sandler1d4d30a2011-04-28 12:35:29 -040093 public void userActivity();
Joe Onorato0cbda992010-05-02 16:28:15 -070094 }
95
96 public CommandQueue(Callbacks callbacks, StatusBarIconList list) {
97 mCallbacks = callbacks;
98 mList = list;
99 }
100
101 public void setIcon(int index, StatusBarIcon icon) {
102 synchronized (mList) {
103 int what = MSG_ICON | index;
104 mHandler.removeMessages(what);
105 mHandler.obtainMessage(what, OP_SET_ICON, 0, icon.clone()).sendToTarget();
106 }
107 }
108
109 public void removeIcon(int index) {
110 synchronized (mList) {
111 int what = MSG_ICON | index;
112 mHandler.removeMessages(what);
113 mHandler.obtainMessage(what, OP_REMOVE_ICON, 0, null).sendToTarget();
114 }
115 }
116
Joe Onoratoa0c56fe2010-05-20 10:21:52 -0700117 public void addNotification(IBinder key, StatusBarNotification notification) {
118 synchronized (mList) {
119 NotificationQueueEntry ne = new NotificationQueueEntry();
120 ne.key = key;
121 ne.notification = notification;
122 mHandler.obtainMessage(MSG_ADD_NOTIFICATION, 0, 0, ne).sendToTarget();
123 }
124 }
125
126 public void updateNotification(IBinder key, StatusBarNotification notification) {
127 synchronized (mList) {
128 NotificationQueueEntry ne = new NotificationQueueEntry();
129 ne.key = key;
130 ne.notification = notification;
131 mHandler.obtainMessage(MSG_UPDATE_NOTIFICATION, 0, 0, ne).sendToTarget();
132 }
133 }
134
135 public void removeNotification(IBinder key) {
136 synchronized (mList) {
137 mHandler.obtainMessage(MSG_REMOVE_NOTIFICATION, 0, 0, key).sendToTarget();
138 }
139 }
140
Joe Onoratof3f0e052010-05-14 18:49:29 -0700141 public void disable(int state) {
142 synchronized (mList) {
143 mHandler.removeMessages(MSG_DISABLE);
144 mHandler.obtainMessage(MSG_DISABLE, state, 0, null).sendToTarget();
145 }
146 }
147
Joe Onorato4762c2d2010-05-17 15:42:59 -0700148 public void animateExpand() {
149 synchronized (mList) {
150 mHandler.removeMessages(MSG_SET_VISIBILITY);
151 mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_EXPAND, 0, null).sendToTarget();
152 }
153 }
154
155 public void animateCollapse() {
156 synchronized (mList) {
157 mHandler.removeMessages(MSG_SET_VISIBILITY);
158 mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_COLLAPSE, 0, null).sendToTarget();
159 }
160 }
161
Joe Onorato93056472010-09-10 10:30:46 -0400162 public void setLightsOn(boolean on) {
163 synchronized (mList) {
164 mHandler.removeMessages(MSG_SET_LIGHTS_ON);
165 mHandler.obtainMessage(MSG_SET_LIGHTS_ON, on ? 1 : 0, 0, null).sendToTarget();
166 }
167 }
168
Daniel Sandlere02d8082010-10-08 15:13:22 -0400169 public void setMenuKeyVisible(boolean visible) {
170 synchronized (mList) {
171 mHandler.removeMessages(MSG_SHOW_MENU);
172 mHandler.obtainMessage(MSG_SHOW_MENU, visible ? 1 : 0, 0, null).sendToTarget();
173 }
174 }
175
Joe Onorato857fd9b2011-01-27 15:08:35 -0800176 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +0900177 synchronized (mList) {
178 mHandler.removeMessages(MSG_SHOW_IME_BUTTON);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800179 mHandler.obtainMessage(MSG_SHOW_IME_BUTTON, vis, backDisposition, token)
180 .sendToTarget();
satok06487a52010-10-29 11:37:18 +0900181 }
182 }
183
Jeff Brown2992ea72011-01-28 22:04:14 -0800184 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
Daniel Sandler1d4d30a2011-04-28 12:35:29 -0400192 public void userActivity() {
193 synchronized (mList) {
194 mHandler.removeMessages(MSG_USER_ACTIVITY);
195 mHandler.obtainMessage(MSG_USER_ACTIVITY, 0, 0, null).sendToTarget();
196 }
197 }
198
Joe Onorato0cbda992010-05-02 16:28:15 -0700199 private final class H extends Handler {
200 public void handleMessage(Message msg) {
Joe Onoratof3f0e052010-05-14 18:49:29 -0700201 final int what = msg.what & MSG_MASK;
Joe Onorato66d7d012010-05-14 10:05:10 -0700202 switch (what) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700203 case MSG_ICON: {
Joe Onoratof3f0e052010-05-14 18:49:29 -0700204 final int index = msg.what & INDEX_MASK;
205 final int viewIndex = mList.getViewIndex(index);
Joe Onorato0cbda992010-05-02 16:28:15 -0700206 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:
Joe Onorato795f2842010-09-27 11:34:46 -0700221 if (mList.getIcon(index) != null) {
222 mList.removeIcon(index);
223 mCallbacks.removeIcon(mList.getSlot(index), index, viewIndex);
224 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700225 break;
226 }
227 break;
Joe Onoratof3f0e052010-05-14 18:49:29 -0700228 }
Joe Onoratoa0c56fe2010-05-20 10:21:52 -0700229 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 }
Joe Onoratof3f0e052010-05-14 18:49:29 -0700243 case MSG_DISABLE:
244 mCallbacks.disable(msg.arg1);
245 break;
Joe Onorato4762c2d2010-05-17 15:42:59 -0700246 case MSG_SET_VISIBILITY:
247 if (msg.arg1 == OP_EXPAND) {
248 mCallbacks.animateExpand();
249 } else {
250 mCallbacks.animateCollapse();
251 }
Joe Onorato93056472010-09-10 10:30:46 -0400252 break;
253 case MSG_SET_LIGHTS_ON:
254 mCallbacks.setLightsOn(msg.arg1 != 0);
255 break;
Daniel Sandlere02d8082010-10-08 15:13:22 -0400256 case MSG_SHOW_MENU:
257 mCallbacks.setMenuKeyVisible(msg.arg1 != 0);
258 break;
satok06487a52010-10-29 11:37:18 +0900259 case MSG_SHOW_IME_BUTTON:
Joe Onorato857fd9b2011-01-27 15:08:35 -0800260 mCallbacks.setImeWindowStatus((IBinder)msg.obj, msg.arg1, msg.arg2);
satok06487a52010-10-29 11:37:18 +0900261 break;
Jeff Brown2992ea72011-01-28 22:04:14 -0800262 case MSG_SET_HARD_KEYBOARD_STATUS:
263 mCallbacks.setHardKeyboardStatus(msg.arg1 != 0, msg.arg2 != 0);
264 break;
Daniel Sandler1d4d30a2011-04-28 12:35:29 -0400265 case MSG_USER_ACTIVITY:
266 mCallbacks.userActivity();
267 break;
Joe Onorato0cbda992010-05-02 16:28:15 -0700268 }
269 }
270 }
271}
272