Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 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 | |
Joe Onorato | 79de0c5 | 2010-05-26 17:03:26 -0400 | [diff] [blame] | 17 | package com.android.systemui.statusbar; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 18 | |
| 19 | import android.os.Handler; |
Joe Onorato | a0c56fe | 2010-05-20 10:21:52 -0700 | [diff] [blame] | 20 | import android.os.IBinder; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 21 | import android.os.Message; |
| 22 | |
| 23 | import com.android.internal.statusbar.IStatusBar; |
| 24 | import com.android.internal.statusbar.StatusBarIcon; |
| 25 | import com.android.internal.statusbar.StatusBarIconList; |
Joe Onorato | a0c56fe | 2010-05-20 10:21:52 -0700 | [diff] [blame] | 26 | import com.android.internal.statusbar.StatusBarNotification; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 27 | |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 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 |
Joe Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame] | 32 | * coalescing these calls so they don't stack up. For the calls |
| 33 | * are coalesced, note that they are all idempotent. |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 34 | */ |
Joe Onorato | 808182d | 2010-07-09 18:52:06 -0400 | [diff] [blame] | 35 | public class CommandQueue extends IStatusBar.Stub { |
Joe Onorato | 514ad663 | 2010-05-13 18:49:00 -0700 | [diff] [blame] | 36 | private static final String TAG = "StatusBar.CommandQueue"; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 37 | |
Daniel Sandler | 1d4d30a | 2011-04-28 12:35:29 -0400 | [diff] [blame] | 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; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 41 | |
Daniel Sandler | 1d4d30a | 2011-04-28 12:35:29 -0400 | [diff] [blame] | 42 | |
| 43 | private static final int MSG_ICON = 1 << MSG_SHIFT; |
| 44 | private static final int OP_SET_ICON = 1; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 45 | private static final int OP_REMOVE_ICON = 2; |
| 46 | |
Daniel Sandler | 1d4d30a | 2011-04-28 12:35:29 -0400 | [diff] [blame] | 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; |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 50 | |
Daniel Sandler | 1d4d30a | 2011-04-28 12:35:29 -0400 | [diff] [blame] | 51 | private static final int MSG_DISABLE = 5 << MSG_SHIFT; |
Joe Onorato | a0c56fe | 2010-05-20 10:21:52 -0700 | [diff] [blame] | 52 | |
Daniel Sandler | 1d4d30a | 2011-04-28 12:35:29 -0400 | [diff] [blame] | 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; |
Joe Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame] | 56 | |
Daniel Sandler | 1d4d30a | 2011-04-28 12:35:29 -0400 | [diff] [blame] | 57 | private static final int MSG_SET_LIGHTS_ON = 7 << MSG_SHIFT; |
Joe Onorato | 9305647 | 2010-09-10 10:30:46 -0400 | [diff] [blame] | 58 | |
Daniel Sandler | 1d4d30a | 2011-04-28 12:35:29 -0400 | [diff] [blame] | 59 | 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 Sandler | e02d808 | 2010-10-08 15:13:22 -0400 | [diff] [blame] | 65 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 66 | private StatusBarIconList mList; |
| 67 | private Callbacks mCallbacks; |
| 68 | private Handler mHandler = new H(); |
| 69 | |
Joe Onorato | a0c56fe | 2010-05-20 10:21:52 -0700 | [diff] [blame] | 70 | private class NotificationQueueEntry { |
| 71 | IBinder key; |
| 72 | StatusBarNotification notification; |
| 73 | } |
| 74 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 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); |
Joe Onorato | a0c56fe | 2010-05-20 10:21:52 -0700 | [diff] [blame] | 83 | public void addNotification(IBinder key, StatusBarNotification notification); |
| 84 | public void updateNotification(IBinder key, StatusBarNotification notification); |
| 85 | public void removeNotification(IBinder key); |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 86 | public void disable(int state); |
Joe Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame] | 87 | public void animateExpand(); |
| 88 | public void animateCollapse(); |
Joe Onorato | 9305647 | 2010-09-10 10:30:46 -0400 | [diff] [blame] | 89 | public void setLightsOn(boolean on); |
Daniel Sandler | e02d808 | 2010-10-08 15:13:22 -0400 | [diff] [blame] | 90 | public void setMenuKeyVisible(boolean visible); |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 91 | public void setImeWindowStatus(IBinder token, int vis, int backDisposition); |
Jeff Brown | 2992ea7 | 2011-01-28 22:04:14 -0800 | [diff] [blame] | 92 | public void setHardKeyboardStatus(boolean available, boolean enabled); |
Daniel Sandler | 1d4d30a | 2011-04-28 12:35:29 -0400 | [diff] [blame] | 93 | public void userActivity(); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 94 | } |
| 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 Onorato | a0c56fe | 2010-05-20 10:21:52 -0700 | [diff] [blame] | 117 | 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 Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 141 | 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 Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame] | 148 | 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 Onorato | 9305647 | 2010-09-10 10:30:46 -0400 | [diff] [blame] | 162 | 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 Sandler | e02d808 | 2010-10-08 15:13:22 -0400 | [diff] [blame] | 169 | 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 Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 176 | public void setImeWindowStatus(IBinder token, int vis, int backDisposition) { |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 177 | synchronized (mList) { |
| 178 | mHandler.removeMessages(MSG_SHOW_IME_BUTTON); |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 179 | mHandler.obtainMessage(MSG_SHOW_IME_BUTTON, vis, backDisposition, token) |
| 180 | .sendToTarget(); |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Jeff Brown | 2992ea7 | 2011-01-28 22:04:14 -0800 | [diff] [blame] | 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 | |
Daniel Sandler | 1d4d30a | 2011-04-28 12:35:29 -0400 | [diff] [blame] | 192 | 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 Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 199 | private final class H extends Handler { |
| 200 | public void handleMessage(Message msg) { |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 201 | final int what = msg.what & MSG_MASK; |
Joe Onorato | 66d7d01 | 2010-05-14 10:05:10 -0700 | [diff] [blame] | 202 | switch (what) { |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 203 | case MSG_ICON: { |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 204 | final int index = msg.what & INDEX_MASK; |
| 205 | final int viewIndex = mList.getViewIndex(index); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 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: |
Joe Onorato | 795f284 | 2010-09-27 11:34:46 -0700 | [diff] [blame] | 221 | if (mList.getIcon(index) != null) { |
| 222 | mList.removeIcon(index); |
| 223 | mCallbacks.removeIcon(mList.getSlot(index), index, viewIndex); |
| 224 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 225 | break; |
| 226 | } |
| 227 | break; |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 228 | } |
Joe Onorato | a0c56fe | 2010-05-20 10:21:52 -0700 | [diff] [blame] | 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 | } |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 243 | case MSG_DISABLE: |
| 244 | mCallbacks.disable(msg.arg1); |
| 245 | break; |
Joe Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame] | 246 | case MSG_SET_VISIBILITY: |
| 247 | if (msg.arg1 == OP_EXPAND) { |
| 248 | mCallbacks.animateExpand(); |
| 249 | } else { |
| 250 | mCallbacks.animateCollapse(); |
| 251 | } |
Joe Onorato | 9305647 | 2010-09-10 10:30:46 -0400 | [diff] [blame] | 252 | break; |
| 253 | case MSG_SET_LIGHTS_ON: |
| 254 | mCallbacks.setLightsOn(msg.arg1 != 0); |
| 255 | break; |
Daniel Sandler | e02d808 | 2010-10-08 15:13:22 -0400 | [diff] [blame] | 256 | case MSG_SHOW_MENU: |
| 257 | mCallbacks.setMenuKeyVisible(msg.arg1 != 0); |
| 258 | break; |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 259 | case MSG_SHOW_IME_BUTTON: |
Joe Onorato | 857fd9b | 2011-01-27 15:08:35 -0800 | [diff] [blame] | 260 | mCallbacks.setImeWindowStatus((IBinder)msg.obj, msg.arg1, msg.arg2); |
satok | 06487a5 | 2010-10-29 11:37:18 +0900 | [diff] [blame] | 261 | break; |
Jeff Brown | 2992ea7 | 2011-01-28 22:04:14 -0800 | [diff] [blame] | 262 | case MSG_SET_HARD_KEYBOARD_STATUS: |
| 263 | mCallbacks.setHardKeyboardStatus(msg.arg1 != 0, msg.arg2 != 0); |
| 264 | break; |
Daniel Sandler | 1d4d30a | 2011-04-28 12:35:29 -0400 | [diff] [blame] | 265 | case MSG_USER_ACTIVITY: |
| 266 | mCallbacks.userActivity(); |
| 267 | break; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |