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; |
| 21 | |
| 22 | import com.android.internal.statusbar.IStatusBar; |
| 23 | import com.android.internal.statusbar.StatusBarIcon; |
| 24 | import com.android.internal.statusbar.StatusBarIconList; |
| 25 | |
| 26 | class CommandQueue extends IStatusBar.Stub { |
| 27 | |
| 28 | private static final int MSG_MASK = 0xffff0000; |
| 29 | private static final int INDEX_MASK = 0x0000ffff; |
| 30 | |
| 31 | private static final int MSG_ICON = 0x00010000; |
| 32 | private static final int OP_SET_ICON = 1; |
| 33 | private static final int OP_REMOVE_ICON = 2; |
| 34 | |
| 35 | private StatusBarIconList mList; |
| 36 | private Callbacks mCallbacks; |
| 37 | private Handler mHandler = new H(); |
| 38 | |
| 39 | /** |
| 40 | * These methods are called back on the main thread. |
| 41 | */ |
| 42 | public interface Callbacks { |
| 43 | public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon); |
| 44 | public void updateIcon(String slot, int index, int viewIndex, |
| 45 | StatusBarIcon old, StatusBarIcon icon); |
| 46 | public void removeIcon(String slot, int index, int viewIndex); |
| 47 | } |
| 48 | |
| 49 | public CommandQueue(Callbacks callbacks, StatusBarIconList list) { |
| 50 | mCallbacks = callbacks; |
| 51 | mList = list; |
| 52 | } |
| 53 | |
| 54 | public void setIcon(int index, StatusBarIcon icon) { |
| 55 | synchronized (mList) { |
| 56 | int what = MSG_ICON | index; |
| 57 | mHandler.removeMessages(what); |
| 58 | mHandler.obtainMessage(what, OP_SET_ICON, 0, icon.clone()).sendToTarget(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public void removeIcon(int index) { |
| 63 | synchronized (mList) { |
| 64 | int what = MSG_ICON | index; |
| 65 | mHandler.removeMessages(what); |
| 66 | mHandler.obtainMessage(what, OP_REMOVE_ICON, 0, null).sendToTarget(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private final class H extends Handler { |
| 71 | public void handleMessage(Message msg) { |
| 72 | int what = msg.what & MSG_MASK; |
| 73 | switch (msg.what) { |
| 74 | case MSG_ICON: { |
| 75 | int index = msg.what & INDEX_MASK; |
| 76 | int viewIndex = mList.getViewIndex(index); |
| 77 | switch (msg.arg1) { |
| 78 | case OP_SET_ICON: { |
| 79 | StatusBarIcon icon = (StatusBarIcon)msg.obj; |
| 80 | StatusBarIcon old = mList.getIcon(index); |
| 81 | if (old == null) { |
| 82 | mList.setIcon(index, icon); |
| 83 | mCallbacks.addIcon(mList.getSlot(index), index, viewIndex, icon); |
| 84 | } else { |
| 85 | mList.setIcon(index, icon); |
| 86 | mCallbacks.updateIcon(mList.getSlot(index), index, viewIndex, |
| 87 | old, icon); |
| 88 | } |
| 89 | break; |
| 90 | } |
| 91 | case OP_REMOVE_ICON: |
| 92 | mList.removeIcon(index); |
| 93 | mCallbacks.removeIcon(mList.getSlot(index), index, viewIndex); |
| 94 | break; |
| 95 | } |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |