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 | |
| 17 | package com.android.policy.statusbar.phone; |
| 18 | |
| 19 | import android.os.Handler; |
| 20 | import android.os.Message; |
Joe Onorato | 66d7d01 | 2010-05-14 10:05:10 -0700 | [diff] [blame] | 21 | import android.util.Slog; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 22 | |
| 23 | import com.android.internal.statusbar.IStatusBar; |
| 24 | import com.android.internal.statusbar.StatusBarIcon; |
| 25 | import com.android.internal.statusbar.StatusBarIconList; |
| 26 | |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 27 | /** |
| 28 | * This class takes the functions from IStatusBar that come in on |
| 29 | * binder pool threads and posts messages to get them onto the main |
| 30 | * thread, and calls onto Callbacks. It also takes care of |
Joe Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame^] | 31 | * coalescing these calls so they don't stack up. For the calls |
| 32 | * are coalesced, note that they are all idempotent. |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 33 | */ |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 34 | class CommandQueue extends IStatusBar.Stub { |
Joe Onorato | 514ad663 | 2010-05-13 18:49:00 -0700 | [diff] [blame] | 35 | private static final String TAG = "StatusBar.CommandQueue"; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 36 | |
| 37 | private static final int MSG_MASK = 0xffff0000; |
| 38 | private static final int INDEX_MASK = 0x0000ffff; |
| 39 | |
| 40 | private static final int MSG_ICON = 0x00010000; |
| 41 | private static final int OP_SET_ICON = 1; |
| 42 | private static final int OP_REMOVE_ICON = 2; |
| 43 | |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 44 | private static final int MSG_DISABLE = 0x00020000; |
| 45 | |
Joe Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame^] | 46 | private static final int MSG_SET_VISIBILITY = 0x00030000; |
| 47 | private static final int OP_EXPAND = 1; |
| 48 | private static final int OP_COLLAPSE = 2; |
| 49 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 50 | private StatusBarIconList mList; |
| 51 | private Callbacks mCallbacks; |
| 52 | private Handler mHandler = new H(); |
| 53 | |
| 54 | /** |
| 55 | * These methods are called back on the main thread. |
| 56 | */ |
| 57 | public interface Callbacks { |
| 58 | public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon); |
| 59 | public void updateIcon(String slot, int index, int viewIndex, |
| 60 | StatusBarIcon old, StatusBarIcon icon); |
| 61 | public void removeIcon(String slot, int index, int viewIndex); |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 62 | public void disable(int state); |
Joe Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame^] | 63 | public void animateExpand(); |
| 64 | public void animateCollapse(); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | public CommandQueue(Callbacks callbacks, StatusBarIconList list) { |
| 68 | mCallbacks = callbacks; |
| 69 | mList = list; |
| 70 | } |
| 71 | |
| 72 | public void setIcon(int index, StatusBarIcon icon) { |
| 73 | synchronized (mList) { |
| 74 | int what = MSG_ICON | index; |
| 75 | mHandler.removeMessages(what); |
| 76 | mHandler.obtainMessage(what, OP_SET_ICON, 0, icon.clone()).sendToTarget(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | public void removeIcon(int index) { |
| 81 | synchronized (mList) { |
| 82 | int what = MSG_ICON | index; |
| 83 | mHandler.removeMessages(what); |
| 84 | mHandler.obtainMessage(what, OP_REMOVE_ICON, 0, null).sendToTarget(); |
| 85 | } |
| 86 | } |
| 87 | |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 88 | public void disable(int state) { |
| 89 | synchronized (mList) { |
| 90 | mHandler.removeMessages(MSG_DISABLE); |
| 91 | mHandler.obtainMessage(MSG_DISABLE, state, 0, null).sendToTarget(); |
| 92 | } |
| 93 | } |
| 94 | |
Joe Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame^] | 95 | public void animateExpand() { |
| 96 | synchronized (mList) { |
| 97 | mHandler.removeMessages(MSG_SET_VISIBILITY); |
| 98 | mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_EXPAND, 0, null).sendToTarget(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public void animateCollapse() { |
| 103 | synchronized (mList) { |
| 104 | mHandler.removeMessages(MSG_SET_VISIBILITY); |
| 105 | mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_COLLAPSE, 0, null).sendToTarget(); |
| 106 | } |
| 107 | } |
| 108 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 109 | private final class H extends Handler { |
| 110 | public void handleMessage(Message msg) { |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 111 | final int what = msg.what & MSG_MASK; |
Joe Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame^] | 112 | Slog.d(TAG, "handleMessage what=0x" + Integer.toHexString(what) + " arg1=" + msg.arg1); |
Joe Onorato | 66d7d01 | 2010-05-14 10:05:10 -0700 | [diff] [blame] | 113 | switch (what) { |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 114 | case MSG_ICON: { |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 115 | final int index = msg.what & INDEX_MASK; |
| 116 | final int viewIndex = mList.getViewIndex(index); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 117 | switch (msg.arg1) { |
| 118 | case OP_SET_ICON: { |
| 119 | StatusBarIcon icon = (StatusBarIcon)msg.obj; |
| 120 | StatusBarIcon old = mList.getIcon(index); |
| 121 | if (old == null) { |
| 122 | mList.setIcon(index, icon); |
| 123 | mCallbacks.addIcon(mList.getSlot(index), index, viewIndex, icon); |
| 124 | } else { |
| 125 | mList.setIcon(index, icon); |
| 126 | mCallbacks.updateIcon(mList.getSlot(index), index, viewIndex, |
| 127 | old, icon); |
| 128 | } |
| 129 | break; |
| 130 | } |
| 131 | case OP_REMOVE_ICON: |
| 132 | mList.removeIcon(index); |
| 133 | mCallbacks.removeIcon(mList.getSlot(index), index, viewIndex); |
| 134 | break; |
| 135 | } |
| 136 | break; |
Joe Onorato | f3f0e05 | 2010-05-14 18:49:29 -0700 | [diff] [blame] | 137 | } |
| 138 | case MSG_DISABLE: |
| 139 | mCallbacks.disable(msg.arg1); |
| 140 | break; |
Joe Onorato | 4762c2d | 2010-05-17 15:42:59 -0700 | [diff] [blame^] | 141 | case MSG_SET_VISIBILITY: |
| 142 | if (msg.arg1 == OP_EXPAND) { |
| 143 | mCallbacks.animateExpand(); |
| 144 | } else { |
| 145 | mCallbacks.animateCollapse(); |
| 146 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | |