blob: ed2ed1c83bc41744b35f5b817a91fc35144c1da4 [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
38 private static final int MSG_MASK = 0xffff0000;
39 private static final int INDEX_MASK = 0x0000ffff;
40
41 private static final int MSG_ICON = 0x00010000;
42 private static final int OP_SET_ICON = 1;
43 private static final int OP_REMOVE_ICON = 2;
44
Joe Onoratoa0c56fe2010-05-20 10:21:52 -070045 private static final int MSG_ADD_NOTIFICATION = 0x00020000;
46 private static final int MSG_UPDATE_NOTIFICATION = 0x00030000;
47 private static final int MSG_REMOVE_NOTIFICATION = 0x00040000;
Joe Onoratof3f0e052010-05-14 18:49:29 -070048
Joe Onoratoa0c56fe2010-05-20 10:21:52 -070049 private static final int MSG_DISABLE = 0x00050000;
50
51 private static final int MSG_SET_VISIBILITY = 0x00060000;
Joe Onorato4762c2d2010-05-17 15:42:59 -070052 private static final int OP_EXPAND = 1;
53 private static final int OP_COLLAPSE = 2;
54
Joe Onorato93056472010-09-10 10:30:46 -040055 private static final int MSG_SET_LIGHTS_ON = 0x00070000;
56
Daniel Sandlere02d8082010-10-08 15:13:22 -040057 private static final int MSG_SHOW_MENU = 0x00080000;
satok06487a52010-10-29 11:37:18 +090058 private static final int MSG_SHOW_IME_BUTTON = 0x00090000;
Daniel Sandlere02d8082010-10-08 15:13:22 -040059
Joe Onorato0cbda992010-05-02 16:28:15 -070060 private StatusBarIconList mList;
61 private Callbacks mCallbacks;
62 private Handler mHandler = new H();
63
Joe Onoratoa0c56fe2010-05-20 10:21:52 -070064 private class NotificationQueueEntry {
65 IBinder key;
66 StatusBarNotification notification;
67 }
68
Joe Onorato0cbda992010-05-02 16:28:15 -070069 /**
70 * These methods are called back on the main thread.
71 */
72 public interface Callbacks {
73 public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon);
74 public void updateIcon(String slot, int index, int viewIndex,
75 StatusBarIcon old, StatusBarIcon icon);
76 public void removeIcon(String slot, int index, int viewIndex);
Joe Onoratoa0c56fe2010-05-20 10:21:52 -070077 public void addNotification(IBinder key, StatusBarNotification notification);
78 public void updateNotification(IBinder key, StatusBarNotification notification);
79 public void removeNotification(IBinder key);
Joe Onoratof3f0e052010-05-14 18:49:29 -070080 public void disable(int state);
Joe Onorato4762c2d2010-05-17 15:42:59 -070081 public void animateExpand();
82 public void animateCollapse();
Joe Onorato93056472010-09-10 10:30:46 -040083 public void setLightsOn(boolean on);
Daniel Sandlere02d8082010-10-08 15:13:22 -040084 public void setMenuKeyVisible(boolean visible);
satok06487a52010-10-29 11:37:18 +090085 public void setIMEButtonVisible(boolean visible);
Joe Onorato0cbda992010-05-02 16:28:15 -070086 }
87
88 public CommandQueue(Callbacks callbacks, StatusBarIconList list) {
89 mCallbacks = callbacks;
90 mList = list;
91 }
92
93 public void setIcon(int index, StatusBarIcon icon) {
94 synchronized (mList) {
95 int what = MSG_ICON | index;
96 mHandler.removeMessages(what);
97 mHandler.obtainMessage(what, OP_SET_ICON, 0, icon.clone()).sendToTarget();
98 }
99 }
100
101 public void removeIcon(int index) {
102 synchronized (mList) {
103 int what = MSG_ICON | index;
104 mHandler.removeMessages(what);
105 mHandler.obtainMessage(what, OP_REMOVE_ICON, 0, null).sendToTarget();
106 }
107 }
108
Joe Onoratoa0c56fe2010-05-20 10:21:52 -0700109 public void addNotification(IBinder key, StatusBarNotification notification) {
110 synchronized (mList) {
111 NotificationQueueEntry ne = new NotificationQueueEntry();
112 ne.key = key;
113 ne.notification = notification;
114 mHandler.obtainMessage(MSG_ADD_NOTIFICATION, 0, 0, ne).sendToTarget();
115 }
116 }
117
118 public void updateNotification(IBinder key, StatusBarNotification notification) {
119 synchronized (mList) {
120 NotificationQueueEntry ne = new NotificationQueueEntry();
121 ne.key = key;
122 ne.notification = notification;
123 mHandler.obtainMessage(MSG_UPDATE_NOTIFICATION, 0, 0, ne).sendToTarget();
124 }
125 }
126
127 public void removeNotification(IBinder key) {
128 synchronized (mList) {
129 mHandler.obtainMessage(MSG_REMOVE_NOTIFICATION, 0, 0, key).sendToTarget();
130 }
131 }
132
Joe Onoratof3f0e052010-05-14 18:49:29 -0700133 public void disable(int state) {
134 synchronized (mList) {
135 mHandler.removeMessages(MSG_DISABLE);
136 mHandler.obtainMessage(MSG_DISABLE, state, 0, null).sendToTarget();
137 }
138 }
139
Joe Onorato4762c2d2010-05-17 15:42:59 -0700140 public void animateExpand() {
141 synchronized (mList) {
142 mHandler.removeMessages(MSG_SET_VISIBILITY);
143 mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_EXPAND, 0, null).sendToTarget();
144 }
145 }
146
147 public void animateCollapse() {
148 synchronized (mList) {
149 mHandler.removeMessages(MSG_SET_VISIBILITY);
150 mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_COLLAPSE, 0, null).sendToTarget();
151 }
152 }
153
Joe Onorato93056472010-09-10 10:30:46 -0400154 public void setLightsOn(boolean on) {
155 synchronized (mList) {
156 mHandler.removeMessages(MSG_SET_LIGHTS_ON);
157 mHandler.obtainMessage(MSG_SET_LIGHTS_ON, on ? 1 : 0, 0, null).sendToTarget();
158 }
159 }
160
Daniel Sandlere02d8082010-10-08 15:13:22 -0400161 public void setMenuKeyVisible(boolean visible) {
162 synchronized (mList) {
163 mHandler.removeMessages(MSG_SHOW_MENU);
164 mHandler.obtainMessage(MSG_SHOW_MENU, visible ? 1 : 0, 0, null).sendToTarget();
165 }
166 }
167
satok06487a52010-10-29 11:37:18 +0900168 public void setIMEButtonVisible(boolean visible) {
169 synchronized (mList) {
170 mHandler.removeMessages(MSG_SHOW_IME_BUTTON);
171 mHandler.obtainMessage(MSG_SHOW_IME_BUTTON, visible ? 1 : 0, 0, null).sendToTarget();
172 }
173 }
174
Joe Onorato0cbda992010-05-02 16:28:15 -0700175 private final class H extends Handler {
176 public void handleMessage(Message msg) {
Joe Onoratof3f0e052010-05-14 18:49:29 -0700177 final int what = msg.what & MSG_MASK;
Joe Onorato66d7d012010-05-14 10:05:10 -0700178 switch (what) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700179 case MSG_ICON: {
Joe Onoratof3f0e052010-05-14 18:49:29 -0700180 final int index = msg.what & INDEX_MASK;
181 final int viewIndex = mList.getViewIndex(index);
Joe Onorato0cbda992010-05-02 16:28:15 -0700182 switch (msg.arg1) {
183 case OP_SET_ICON: {
184 StatusBarIcon icon = (StatusBarIcon)msg.obj;
185 StatusBarIcon old = mList.getIcon(index);
186 if (old == null) {
187 mList.setIcon(index, icon);
188 mCallbacks.addIcon(mList.getSlot(index), index, viewIndex, icon);
189 } else {
190 mList.setIcon(index, icon);
191 mCallbacks.updateIcon(mList.getSlot(index), index, viewIndex,
192 old, icon);
193 }
194 break;
195 }
196 case OP_REMOVE_ICON:
Joe Onorato795f2842010-09-27 11:34:46 -0700197 if (mList.getIcon(index) != null) {
198 mList.removeIcon(index);
199 mCallbacks.removeIcon(mList.getSlot(index), index, viewIndex);
200 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700201 break;
202 }
203 break;
Joe Onoratof3f0e052010-05-14 18:49:29 -0700204 }
Joe Onoratoa0c56fe2010-05-20 10:21:52 -0700205 case MSG_ADD_NOTIFICATION: {
206 final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj;
207 mCallbacks.addNotification(ne.key, ne.notification);
208 break;
209 }
210 case MSG_UPDATE_NOTIFICATION: {
211 final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj;
212 mCallbacks.updateNotification(ne.key, ne.notification);
213 break;
214 }
215 case MSG_REMOVE_NOTIFICATION: {
216 mCallbacks.removeNotification((IBinder)msg.obj);
217 break;
218 }
Joe Onoratof3f0e052010-05-14 18:49:29 -0700219 case MSG_DISABLE:
220 mCallbacks.disable(msg.arg1);
221 break;
Joe Onorato4762c2d2010-05-17 15:42:59 -0700222 case MSG_SET_VISIBILITY:
223 if (msg.arg1 == OP_EXPAND) {
224 mCallbacks.animateExpand();
225 } else {
226 mCallbacks.animateCollapse();
227 }
Joe Onorato93056472010-09-10 10:30:46 -0400228 break;
229 case MSG_SET_LIGHTS_ON:
230 mCallbacks.setLightsOn(msg.arg1 != 0);
231 break;
Daniel Sandlere02d8082010-10-08 15:13:22 -0400232 case MSG_SHOW_MENU:
233 mCallbacks.setMenuKeyVisible(msg.arg1 != 0);
234 break;
satok06487a52010-10-29 11:37:18 +0900235 case MSG_SHOW_IME_BUTTON:
236 mCallbacks.setIMEButtonVisible(msg.arg1 != 0);
237 break;
Joe Onorato0cbda992010-05-02 16:28:15 -0700238 }
239 }
240 }
241}
242