blob: 00ad77cf4a834aed5b1946e7a0bc5b793d6061e4 [file] [log] [blame]
Joe Onorato0cbda992010-05-02 16:28:15 -07001/*
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.policy.statusbar.phone;
18
19import android.os.Handler;
20import android.os.Message;
Joe Onorato66d7d012010-05-14 10:05:10 -070021import android.util.Slog;
Joe Onorato0cbda992010-05-02 16:28:15 -070022
23import com.android.internal.statusbar.IStatusBar;
24import com.android.internal.statusbar.StatusBarIcon;
25import com.android.internal.statusbar.StatusBarIconList;
26
Joe Onoratof3f0e052010-05-14 18:49:29 -070027/**
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 Onorato4762c2d2010-05-17 15:42:59 -070031 * coalescing these calls so they don't stack up. For the calls
32 * are coalesced, note that they are all idempotent.
Joe Onoratof3f0e052010-05-14 18:49:29 -070033 */
Joe Onorato0cbda992010-05-02 16:28:15 -070034class CommandQueue extends IStatusBar.Stub {
Joe Onorato514ad6632010-05-13 18:49:00 -070035 private static final String TAG = "StatusBar.CommandQueue";
Joe Onorato0cbda992010-05-02 16:28:15 -070036
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 Onoratof3f0e052010-05-14 18:49:29 -070044 private static final int MSG_DISABLE = 0x00020000;
45
Joe Onorato4762c2d2010-05-17 15:42:59 -070046 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 Onorato0cbda992010-05-02 16:28:15 -070050 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 Onoratof3f0e052010-05-14 18:49:29 -070062 public void disable(int state);
Joe Onorato4762c2d2010-05-17 15:42:59 -070063 public void animateExpand();
64 public void animateCollapse();
Joe Onorato0cbda992010-05-02 16:28:15 -070065 }
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 Onoratof3f0e052010-05-14 18:49:29 -070088 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 Onorato4762c2d2010-05-17 15:42:59 -070095 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 Onorato0cbda992010-05-02 16:28:15 -0700109 private final class H extends Handler {
110 public void handleMessage(Message msg) {
Joe Onoratof3f0e052010-05-14 18:49:29 -0700111 final int what = msg.what & MSG_MASK;
Joe Onorato4762c2d2010-05-17 15:42:59 -0700112 Slog.d(TAG, "handleMessage what=0x" + Integer.toHexString(what) + " arg1=" + msg.arg1);
Joe Onorato66d7d012010-05-14 10:05:10 -0700113 switch (what) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700114 case MSG_ICON: {
Joe Onoratof3f0e052010-05-14 18:49:29 -0700115 final int index = msg.what & INDEX_MASK;
116 final int viewIndex = mList.getViewIndex(index);
Joe Onorato0cbda992010-05-02 16:28:15 -0700117 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 Onoratof3f0e052010-05-14 18:49:29 -0700137 }
138 case MSG_DISABLE:
139 mCallbacks.disable(msg.arg1);
140 break;
Joe Onorato4762c2d2010-05-17 15:42:59 -0700141 case MSG_SET_VISIBILITY:
142 if (msg.arg1 == OP_EXPAND) {
143 mCallbacks.animateExpand();
144 } else {
145 mCallbacks.animateCollapse();
146 }
Joe Onorato0cbda992010-05-02 16:28:15 -0700147 }
148 }
149 }
150}
151
152