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 | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 35 | 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 | |
| 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 Onorato | a0c56fe | 2010-05-20 10:21:52 -0700 | [diff] [blame] | 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; |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 48 | |
Joe Onorato | a0c56fe | 2010-05-20 10:21:52 -0700 | [diff] [blame] | 49 | private static final int MSG_DISABLE = 0x00050000; |
| 50 | |
| 51 | private static final int MSG_SET_VISIBILITY = 0x00060000; |
Joe Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame] | 52 | private static final int OP_EXPAND = 1; |
| 53 | private static final int OP_COLLAPSE = 2; |
| 54 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 55 | private StatusBarIconList mList; |
| 56 | private Callbacks mCallbacks; |
| 57 | private Handler mHandler = new H(); |
| 58 | |
Joe Onorato | a0c56fe | 2010-05-20 10:21:52 -0700 | [diff] [blame] | 59 | private class NotificationQueueEntry { |
| 60 | IBinder key; |
| 61 | StatusBarNotification notification; |
| 62 | } |
| 63 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 64 | /** |
| 65 | * These methods are called back on the main thread. |
| 66 | */ |
| 67 | public interface Callbacks { |
| 68 | public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon); |
| 69 | public void updateIcon(String slot, int index, int viewIndex, |
| 70 | StatusBarIcon old, StatusBarIcon icon); |
| 71 | public void removeIcon(String slot, int index, int viewIndex); |
Joe Onorato | a0c56fe | 2010-05-20 10:21:52 -0700 | [diff] [blame] | 72 | public void addNotification(IBinder key, StatusBarNotification notification); |
| 73 | public void updateNotification(IBinder key, StatusBarNotification notification); |
| 74 | public void removeNotification(IBinder key); |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 75 | public void disable(int state); |
Joe Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame] | 76 | public void animateExpand(); |
| 77 | public void animateCollapse(); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | public CommandQueue(Callbacks callbacks, StatusBarIconList list) { |
| 81 | mCallbacks = callbacks; |
| 82 | mList = list; |
| 83 | } |
| 84 | |
| 85 | public void setIcon(int index, StatusBarIcon icon) { |
| 86 | synchronized (mList) { |
| 87 | int what = MSG_ICON | index; |
| 88 | mHandler.removeMessages(what); |
| 89 | mHandler.obtainMessage(what, OP_SET_ICON, 0, icon.clone()).sendToTarget(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public void removeIcon(int index) { |
| 94 | synchronized (mList) { |
| 95 | int what = MSG_ICON | index; |
| 96 | mHandler.removeMessages(what); |
| 97 | mHandler.obtainMessage(what, OP_REMOVE_ICON, 0, null).sendToTarget(); |
| 98 | } |
| 99 | } |
| 100 | |
Joe Onorato | a0c56fe | 2010-05-20 10:21:52 -0700 | [diff] [blame] | 101 | public void addNotification(IBinder key, StatusBarNotification notification) { |
| 102 | synchronized (mList) { |
| 103 | NotificationQueueEntry ne = new NotificationQueueEntry(); |
| 104 | ne.key = key; |
| 105 | ne.notification = notification; |
| 106 | mHandler.obtainMessage(MSG_ADD_NOTIFICATION, 0, 0, ne).sendToTarget(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | public void updateNotification(IBinder key, StatusBarNotification notification) { |
| 111 | synchronized (mList) { |
| 112 | NotificationQueueEntry ne = new NotificationQueueEntry(); |
| 113 | ne.key = key; |
| 114 | ne.notification = notification; |
| 115 | mHandler.obtainMessage(MSG_UPDATE_NOTIFICATION, 0, 0, ne).sendToTarget(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public void removeNotification(IBinder key) { |
| 120 | synchronized (mList) { |
| 121 | mHandler.obtainMessage(MSG_REMOVE_NOTIFICATION, 0, 0, key).sendToTarget(); |
| 122 | } |
| 123 | } |
| 124 | |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 125 | public void disable(int state) { |
| 126 | synchronized (mList) { |
| 127 | mHandler.removeMessages(MSG_DISABLE); |
| 128 | mHandler.obtainMessage(MSG_DISABLE, state, 0, null).sendToTarget(); |
| 129 | } |
| 130 | } |
| 131 | |
Joe Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame] | 132 | public void animateExpand() { |
| 133 | synchronized (mList) { |
| 134 | mHandler.removeMessages(MSG_SET_VISIBILITY); |
| 135 | mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_EXPAND, 0, null).sendToTarget(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | public void animateCollapse() { |
| 140 | synchronized (mList) { |
| 141 | mHandler.removeMessages(MSG_SET_VISIBILITY); |
| 142 | mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_COLLAPSE, 0, null).sendToTarget(); |
| 143 | } |
| 144 | } |
| 145 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 146 | private final class H extends Handler { |
| 147 | public void handleMessage(Message msg) { |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 148 | final int what = msg.what & MSG_MASK; |
Joe Onorato | 66d7d01 | 2010-05-14 10:05:10 -0700 | [diff] [blame] | 149 | switch (what) { |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 150 | case MSG_ICON: { |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 151 | final int index = msg.what & INDEX_MASK; |
| 152 | final int viewIndex = mList.getViewIndex(index); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 153 | switch (msg.arg1) { |
| 154 | case OP_SET_ICON: { |
| 155 | StatusBarIcon icon = (StatusBarIcon)msg.obj; |
| 156 | StatusBarIcon old = mList.getIcon(index); |
| 157 | if (old == null) { |
| 158 | mList.setIcon(index, icon); |
| 159 | mCallbacks.addIcon(mList.getSlot(index), index, viewIndex, icon); |
| 160 | } else { |
| 161 | mList.setIcon(index, icon); |
| 162 | mCallbacks.updateIcon(mList.getSlot(index), index, viewIndex, |
| 163 | old, icon); |
| 164 | } |
| 165 | break; |
| 166 | } |
| 167 | case OP_REMOVE_ICON: |
| 168 | mList.removeIcon(index); |
| 169 | mCallbacks.removeIcon(mList.getSlot(index), index, viewIndex); |
| 170 | break; |
| 171 | } |
| 172 | break; |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 173 | } |
Joe Onorato | a0c56fe | 2010-05-20 10:21:52 -0700 | [diff] [blame] | 174 | case MSG_ADD_NOTIFICATION: { |
| 175 | final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj; |
| 176 | mCallbacks.addNotification(ne.key, ne.notification); |
| 177 | break; |
| 178 | } |
| 179 | case MSG_UPDATE_NOTIFICATION: { |
| 180 | final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj; |
| 181 | mCallbacks.updateNotification(ne.key, ne.notification); |
| 182 | break; |
| 183 | } |
| 184 | case MSG_REMOVE_NOTIFICATION: { |
| 185 | mCallbacks.removeNotification((IBinder)msg.obj); |
| 186 | break; |
| 187 | } |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 188 | case MSG_DISABLE: |
| 189 | mCallbacks.disable(msg.arg1); |
| 190 | break; |
Joe Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame] | 191 | case MSG_SET_VISIBILITY: |
| 192 | if (msg.arg1 == OP_EXPAND) { |
| 193 | mCallbacks.animateExpand(); |
| 194 | } else { |
| 195 | mCallbacks.animateCollapse(); |
| 196 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |