CommandQueue.java revision e02d808abf370965c3c4e4d38af11bc69110fde2
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 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 45 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; 48 49 private static final int MSG_DISABLE = 0x00050000; 50 51 private static final int MSG_SET_VISIBILITY = 0x00060000; 52 private static final int OP_EXPAND = 1; 53 private static final int OP_COLLAPSE = 2; 54 55 private static final int MSG_SET_LIGHTS_ON = 0x00070000; 56 57 private static final int MSG_SHOW_MENU = 0x00080000; 58 59 private StatusBarIconList mList; 60 private Callbacks mCallbacks; 61 private Handler mHandler = new H(); 62 63 private class NotificationQueueEntry { 64 IBinder key; 65 StatusBarNotification notification; 66 } 67 68 /** 69 * These methods are called back on the main thread. 70 */ 71 public interface Callbacks { 72 public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon); 73 public void updateIcon(String slot, int index, int viewIndex, 74 StatusBarIcon old, StatusBarIcon icon); 75 public void removeIcon(String slot, int index, int viewIndex); 76 public void addNotification(IBinder key, StatusBarNotification notification); 77 public void updateNotification(IBinder key, StatusBarNotification notification); 78 public void removeNotification(IBinder key); 79 public void disable(int state); 80 public void animateExpand(); 81 public void animateCollapse(); 82 public void setLightsOn(boolean on); 83 public void setMenuKeyVisible(boolean visible); 84 } 85 86 public CommandQueue(Callbacks callbacks, StatusBarIconList list) { 87 mCallbacks = callbacks; 88 mList = list; 89 } 90 91 public void setIcon(int index, StatusBarIcon icon) { 92 synchronized (mList) { 93 int what = MSG_ICON | index; 94 mHandler.removeMessages(what); 95 mHandler.obtainMessage(what, OP_SET_ICON, 0, icon.clone()).sendToTarget(); 96 } 97 } 98 99 public void removeIcon(int index) { 100 synchronized (mList) { 101 int what = MSG_ICON | index; 102 mHandler.removeMessages(what); 103 mHandler.obtainMessage(what, OP_REMOVE_ICON, 0, null).sendToTarget(); 104 } 105 } 106 107 public void addNotification(IBinder key, StatusBarNotification notification) { 108 synchronized (mList) { 109 NotificationQueueEntry ne = new NotificationQueueEntry(); 110 ne.key = key; 111 ne.notification = notification; 112 mHandler.obtainMessage(MSG_ADD_NOTIFICATION, 0, 0, ne).sendToTarget(); 113 } 114 } 115 116 public void updateNotification(IBinder key, StatusBarNotification notification) { 117 synchronized (mList) { 118 NotificationQueueEntry ne = new NotificationQueueEntry(); 119 ne.key = key; 120 ne.notification = notification; 121 mHandler.obtainMessage(MSG_UPDATE_NOTIFICATION, 0, 0, ne).sendToTarget(); 122 } 123 } 124 125 public void removeNotification(IBinder key) { 126 synchronized (mList) { 127 mHandler.obtainMessage(MSG_REMOVE_NOTIFICATION, 0, 0, key).sendToTarget(); 128 } 129 } 130 131 public void disable(int state) { 132 synchronized (mList) { 133 mHandler.removeMessages(MSG_DISABLE); 134 mHandler.obtainMessage(MSG_DISABLE, state, 0, null).sendToTarget(); 135 } 136 } 137 138 public void animateExpand() { 139 synchronized (mList) { 140 mHandler.removeMessages(MSG_SET_VISIBILITY); 141 mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_EXPAND, 0, null).sendToTarget(); 142 } 143 } 144 145 public void animateCollapse() { 146 synchronized (mList) { 147 mHandler.removeMessages(MSG_SET_VISIBILITY); 148 mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_COLLAPSE, 0, null).sendToTarget(); 149 } 150 } 151 152 public void setLightsOn(boolean on) { 153 synchronized (mList) { 154 mHandler.removeMessages(MSG_SET_LIGHTS_ON); 155 mHandler.obtainMessage(MSG_SET_LIGHTS_ON, on ? 1 : 0, 0, null).sendToTarget(); 156 } 157 } 158 159 public void setMenuKeyVisible(boolean visible) { 160 synchronized (mList) { 161 mHandler.removeMessages(MSG_SHOW_MENU); 162 mHandler.obtainMessage(MSG_SHOW_MENU, visible ? 1 : 0, 0, null).sendToTarget(); 163 } 164 } 165 166 private final class H extends Handler { 167 public void handleMessage(Message msg) { 168 final int what = msg.what & MSG_MASK; 169 switch (what) { 170 case MSG_ICON: { 171 final int index = msg.what & INDEX_MASK; 172 final int viewIndex = mList.getViewIndex(index); 173 switch (msg.arg1) { 174 case OP_SET_ICON: { 175 StatusBarIcon icon = (StatusBarIcon)msg.obj; 176 StatusBarIcon old = mList.getIcon(index); 177 if (old == null) { 178 mList.setIcon(index, icon); 179 mCallbacks.addIcon(mList.getSlot(index), index, viewIndex, icon); 180 } else { 181 mList.setIcon(index, icon); 182 mCallbacks.updateIcon(mList.getSlot(index), index, viewIndex, 183 old, icon); 184 } 185 break; 186 } 187 case OP_REMOVE_ICON: 188 if (mList.getIcon(index) != null) { 189 mList.removeIcon(index); 190 mCallbacks.removeIcon(mList.getSlot(index), index, viewIndex); 191 } 192 break; 193 } 194 break; 195 } 196 case MSG_ADD_NOTIFICATION: { 197 final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj; 198 mCallbacks.addNotification(ne.key, ne.notification); 199 break; 200 } 201 case MSG_UPDATE_NOTIFICATION: { 202 final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj; 203 mCallbacks.updateNotification(ne.key, ne.notification); 204 break; 205 } 206 case MSG_REMOVE_NOTIFICATION: { 207 mCallbacks.removeNotification((IBinder)msg.obj); 208 break; 209 } 210 case MSG_DISABLE: 211 mCallbacks.disable(msg.arg1); 212 break; 213 case MSG_SET_VISIBILITY: 214 if (msg.arg1 == OP_EXPAND) { 215 mCallbacks.animateExpand(); 216 } else { 217 mCallbacks.animateCollapse(); 218 } 219 break; 220 case MSG_SET_LIGHTS_ON: 221 mCallbacks.setLightsOn(msg.arg1 != 0); 222 break; 223 case MSG_SHOW_MENU: 224 mCallbacks.setMenuKeyVisible(msg.arg1 != 0); 225 break; 226 } 227 } 228 } 229} 230 231