CommandQueue.java revision 79de0c550037a5328bbc7f4fddaf02f192a5c283
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; 22import android.util.Slog; 23 24import com.android.internal.statusbar.IStatusBar; 25import com.android.internal.statusbar.StatusBarIcon; 26import com.android.internal.statusbar.StatusBarIconList; 27import com.android.internal.statusbar.StatusBarNotification; 28import com.android.internal.statusbar.StatusBarNotificationList; 29 30/** 31 * This class takes the functions from IStatusBar that come in on 32 * binder pool threads and posts messages to get them onto the main 33 * thread, and calls onto Callbacks. It also takes care of 34 * coalescing these calls so they don't stack up. For the calls 35 * are coalesced, note that they are all idempotent. 36 */ 37class CommandQueue extends IStatusBar.Stub { 38 private static final String TAG = "StatusBar.CommandQueue"; 39 40 private static final int MSG_MASK = 0xffff0000; 41 private static final int INDEX_MASK = 0x0000ffff; 42 43 private static final int MSG_ICON = 0x00010000; 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 = 0x00020000; 48 private static final int MSG_UPDATE_NOTIFICATION = 0x00030000; 49 private static final int MSG_REMOVE_NOTIFICATION = 0x00040000; 50 51 private static final int MSG_DISABLE = 0x00050000; 52 53 private static final int MSG_SET_VISIBILITY = 0x00060000; 54 private static final int OP_EXPAND = 1; 55 private static final int OP_COLLAPSE = 2; 56 57 private StatusBarIconList mList; 58 private Callbacks mCallbacks; 59 private Handler mHandler = new H(); 60 61 private class NotificationQueueEntry { 62 IBinder key; 63 StatusBarNotification notification; 64 } 65 66 /** 67 * These methods are called back on the main thread. 68 */ 69 public interface Callbacks { 70 public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon); 71 public void updateIcon(String slot, int index, int viewIndex, 72 StatusBarIcon old, StatusBarIcon icon); 73 public void removeIcon(String slot, int index, int viewIndex); 74 public void addNotification(IBinder key, StatusBarNotification notification); 75 public void updateNotification(IBinder key, StatusBarNotification notification); 76 public void removeNotification(IBinder key); 77 public void disable(int state); 78 public void animateExpand(); 79 public void animateCollapse(); 80 } 81 82 public CommandQueue(Callbacks callbacks, StatusBarIconList list) { 83 mCallbacks = callbacks; 84 mList = list; 85 } 86 87 public void setIcon(int index, StatusBarIcon icon) { 88 synchronized (mList) { 89 int what = MSG_ICON | index; 90 mHandler.removeMessages(what); 91 mHandler.obtainMessage(what, OP_SET_ICON, 0, icon.clone()).sendToTarget(); 92 } 93 } 94 95 public void removeIcon(int index) { 96 synchronized (mList) { 97 int what = MSG_ICON | index; 98 mHandler.removeMessages(what); 99 mHandler.obtainMessage(what, OP_REMOVE_ICON, 0, null).sendToTarget(); 100 } 101 } 102 103 public void addNotification(IBinder key, StatusBarNotification notification) { 104 synchronized (mList) { 105 NotificationQueueEntry ne = new NotificationQueueEntry(); 106 ne.key = key; 107 ne.notification = notification; 108 mHandler.obtainMessage(MSG_ADD_NOTIFICATION, 0, 0, ne).sendToTarget(); 109 } 110 } 111 112 public void updateNotification(IBinder key, StatusBarNotification notification) { 113 synchronized (mList) { 114 NotificationQueueEntry ne = new NotificationQueueEntry(); 115 ne.key = key; 116 ne.notification = notification; 117 mHandler.obtainMessage(MSG_UPDATE_NOTIFICATION, 0, 0, ne).sendToTarget(); 118 } 119 } 120 121 public void removeNotification(IBinder key) { 122 synchronized (mList) { 123 mHandler.obtainMessage(MSG_REMOVE_NOTIFICATION, 0, 0, key).sendToTarget(); 124 } 125 } 126 127 public void disable(int state) { 128 synchronized (mList) { 129 mHandler.removeMessages(MSG_DISABLE); 130 mHandler.obtainMessage(MSG_DISABLE, state, 0, null).sendToTarget(); 131 } 132 } 133 134 public void animateExpand() { 135 synchronized (mList) { 136 mHandler.removeMessages(MSG_SET_VISIBILITY); 137 mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_EXPAND, 0, null).sendToTarget(); 138 } 139 } 140 141 public void animateCollapse() { 142 synchronized (mList) { 143 mHandler.removeMessages(MSG_SET_VISIBILITY); 144 mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_COLLAPSE, 0, null).sendToTarget(); 145 } 146 } 147 148 private final class H extends Handler { 149 public void handleMessage(Message msg) { 150 final int what = msg.what & MSG_MASK; 151 Slog.d(TAG, "handleMessage what=0x" + Integer.toHexString(what) + " arg1=" + msg.arg1); 152 switch (what) { 153 case MSG_ICON: { 154 final int index = msg.what & INDEX_MASK; 155 final int viewIndex = mList.getViewIndex(index); 156 switch (msg.arg1) { 157 case OP_SET_ICON: { 158 StatusBarIcon icon = (StatusBarIcon)msg.obj; 159 StatusBarIcon old = mList.getIcon(index); 160 if (old == null) { 161 mList.setIcon(index, icon); 162 mCallbacks.addIcon(mList.getSlot(index), index, viewIndex, icon); 163 } else { 164 mList.setIcon(index, icon); 165 mCallbacks.updateIcon(mList.getSlot(index), index, viewIndex, 166 old, icon); 167 } 168 break; 169 } 170 case OP_REMOVE_ICON: 171 mList.removeIcon(index); 172 mCallbacks.removeIcon(mList.getSlot(index), index, viewIndex); 173 break; 174 } 175 break; 176 } 177 case MSG_ADD_NOTIFICATION: { 178 final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj; 179 mCallbacks.addNotification(ne.key, ne.notification); 180 break; 181 } 182 case MSG_UPDATE_NOTIFICATION: { 183 final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj; 184 mCallbacks.updateNotification(ne.key, ne.notification); 185 break; 186 } 187 case MSG_REMOVE_NOTIFICATION: { 188 mCallbacks.removeNotification((IBinder)msg.obj); 189 break; 190 } 191 case MSG_DISABLE: 192 mCallbacks.disable(msg.arg1); 193 break; 194 case MSG_SET_VISIBILITY: 195 if (msg.arg1 == OP_EXPAND) { 196 mCallbacks.animateExpand(); 197 } else { 198 mCallbacks.animateCollapse(); 199 } 200 } 201 } 202 } 203} 204 205