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