CommandQueue.java revision 808182dc874e93582da38d013a4a790d6bc08fc9
10cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato/*
20cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato * Copyright (C) 2010 The Android Open Source Project
30cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato *
40cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato * Licensed under the Apache License, Version 2.0 (the "License");
50cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato * you may not use this file except in compliance with the License.
60cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato * You may obtain a copy of the License at
70cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato *
80cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato *      http://www.apache.org/licenses/LICENSE-2.0
90cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato *
100cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato * Unless required by applicable law or agreed to in writing, software
110cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato * distributed under the License is distributed on an "AS IS" BASIS,
120cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato * See the License for the specific language governing permissions and
140cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato * limitations under the License.
150cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato */
160cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato
1779de0c550037a5328bbc7f4fddaf02f192a5c283Joe Onoratopackage com.android.systemui.statusbar;
180cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato
190cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onoratoimport android.os.Handler;
20a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onoratoimport android.os.IBinder;
210cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onoratoimport android.os.Message;
220cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato
230cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onoratoimport com.android.internal.statusbar.IStatusBar;
240cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onoratoimport com.android.internal.statusbar.StatusBarIcon;
250cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onoratoimport com.android.internal.statusbar.StatusBarIconList;
26a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onoratoimport com.android.internal.statusbar.StatusBarNotification;
270cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato
28f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato/**
29f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato * This class takes the functions from IStatusBar that come in on
30f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato * binder pool threads and posts messages to get them onto the main
31f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato * thread, and calls onto Callbacks.  It also takes care of
324762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato * coalescing these calls so they don't stack up.  For the calls
334762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato * are coalesced, note that they are all idempotent.
34f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato */
35808182dc874e93582da38d013a4a790d6bc08fc9Joe Onoratopublic class CommandQueue extends IStatusBar.Stub {
36514ad663f0a8b239cc59409175e0bd489c591aa0Joe Onorato    private static final String TAG = "StatusBar.CommandQueue";
370cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato
380cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    private static final int MSG_MASK = 0xffff0000;
390cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    private static final int INDEX_MASK = 0x0000ffff;
400cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato
410cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    private static final int MSG_ICON = 0x00010000;
420cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    private static final int OP_SET_ICON = 1;
430cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    private static final int OP_REMOVE_ICON = 2;
440cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato
45a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato    private static final int MSG_ADD_NOTIFICATION = 0x00020000;
46a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato    private static final int MSG_UPDATE_NOTIFICATION = 0x00030000;
47a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato    private static final int MSG_REMOVE_NOTIFICATION = 0x00040000;
48f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato
49a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato    private static final int MSG_DISABLE = 0x00050000;
50a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato
51a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato    private static final int MSG_SET_VISIBILITY = 0x00060000;
524762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato    private static final int OP_EXPAND = 1;
534762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato    private static final int OP_COLLAPSE = 2;
544762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato
550cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    private StatusBarIconList mList;
560cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    private Callbacks mCallbacks;
570cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    private Handler mHandler = new H();
580cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato
59a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato    private class NotificationQueueEntry {
60a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato        IBinder key;
61a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato        StatusBarNotification notification;
62a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato    }
63a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato
640cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    /**
650cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato     * These methods are called back on the main thread.
660cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato     */
670cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    public interface Callbacks {
680cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato        public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon);
690cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato        public void updateIcon(String slot, int index, int viewIndex,
700cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                StatusBarIcon old, StatusBarIcon icon);
710cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato        public void removeIcon(String slot, int index, int viewIndex);
72a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato        public void addNotification(IBinder key, StatusBarNotification notification);
73a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato        public void updateNotification(IBinder key, StatusBarNotification notification);
74a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato        public void removeNotification(IBinder key);
75f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato        public void disable(int state);
764762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato        public void animateExpand();
774762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato        public void animateCollapse();
780cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    }
790cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato
800cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    public CommandQueue(Callbacks callbacks, StatusBarIconList list) {
810cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato        mCallbacks = callbacks;
820cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato        mList = list;
830cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    }
840cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato
850cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    public void setIcon(int index, StatusBarIcon icon) {
860cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato        synchronized (mList) {
870cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato            int what = MSG_ICON | index;
880cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato            mHandler.removeMessages(what);
890cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato            mHandler.obtainMessage(what, OP_SET_ICON, 0, icon.clone()).sendToTarget();
900cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato        }
910cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    }
920cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato
930cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    public void removeIcon(int index) {
940cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato        synchronized (mList) {
950cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato            int what = MSG_ICON | index;
960cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato            mHandler.removeMessages(what);
970cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato            mHandler.obtainMessage(what, OP_REMOVE_ICON, 0, null).sendToTarget();
980cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato        }
990cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    }
1000cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato
101a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato    public void addNotification(IBinder key, StatusBarNotification notification) {
102a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato        synchronized (mList) {
103a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato            NotificationQueueEntry ne = new NotificationQueueEntry();
104a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato            ne.key = key;
105a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato            ne.notification = notification;
106a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato            mHandler.obtainMessage(MSG_ADD_NOTIFICATION, 0, 0, ne).sendToTarget();
107a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato        }
108a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato    }
109a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato
110a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato    public void updateNotification(IBinder key, StatusBarNotification notification) {
111a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato        synchronized (mList) {
112a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato            NotificationQueueEntry ne = new NotificationQueueEntry();
113a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato            ne.key = key;
114a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato            ne.notification = notification;
115a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato            mHandler.obtainMessage(MSG_UPDATE_NOTIFICATION, 0, 0, ne).sendToTarget();
116a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato        }
117a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato    }
118a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato
119a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato    public void removeNotification(IBinder key) {
120a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato        synchronized (mList) {
121a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato            mHandler.obtainMessage(MSG_REMOVE_NOTIFICATION, 0, 0, key).sendToTarget();
122a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato        }
123a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato    }
124a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato
125f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato    public void disable(int state) {
126f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato        synchronized (mList) {
127f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato            mHandler.removeMessages(MSG_DISABLE);
128f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato            mHandler.obtainMessage(MSG_DISABLE, state, 0, null).sendToTarget();
129f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato        }
130f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato    }
131f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato
1324762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato    public void animateExpand() {
1334762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato        synchronized (mList) {
1344762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato            mHandler.removeMessages(MSG_SET_VISIBILITY);
1354762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato            mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_EXPAND, 0, null).sendToTarget();
1364762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato        }
1374762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato    }
1384762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato
1394762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato    public void animateCollapse() {
1404762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato        synchronized (mList) {
1414762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato            mHandler.removeMessages(MSG_SET_VISIBILITY);
1424762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato            mHandler.obtainMessage(MSG_SET_VISIBILITY, OP_COLLAPSE, 0, null).sendToTarget();
1434762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato        }
1444762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato    }
1454762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato
1460cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    private final class H extends Handler {
1470cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato        public void handleMessage(Message msg) {
148f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato            final int what = msg.what & MSG_MASK;
14966d7d01ed91968f4ed2e2669fd306aa2af61cd16Joe Onorato            switch (what) {
1500cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                case MSG_ICON: {
151f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato                    final int index = msg.what & INDEX_MASK;
152f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato                    final int viewIndex = mList.getViewIndex(index);
1530cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                    switch (msg.arg1) {
1540cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                        case OP_SET_ICON: {
1550cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                            StatusBarIcon icon = (StatusBarIcon)msg.obj;
1560cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                            StatusBarIcon old = mList.getIcon(index);
1570cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                            if (old == null) {
1580cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                                mList.setIcon(index, icon);
1590cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                                mCallbacks.addIcon(mList.getSlot(index), index, viewIndex, icon);
1600cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                            } else {
1610cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                                mList.setIcon(index, icon);
1620cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                                mCallbacks.updateIcon(mList.getSlot(index), index, viewIndex,
1630cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                                        old, icon);
1640cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                            }
1650cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                            break;
1660cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                        }
1670cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                        case OP_REMOVE_ICON:
1680cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                            mList.removeIcon(index);
1690cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                            mCallbacks.removeIcon(mList.getSlot(index), index, viewIndex);
1700cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                            break;
1710cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                    }
1720cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato                    break;
173f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato                }
174a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato                case MSG_ADD_NOTIFICATION: {
175a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato                    final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj;
176a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato                    mCallbacks.addNotification(ne.key, ne.notification);
177a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato                    break;
178a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato                }
179a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato                case MSG_UPDATE_NOTIFICATION: {
180a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato                    final NotificationQueueEntry ne = (NotificationQueueEntry)msg.obj;
181a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato                    mCallbacks.updateNotification(ne.key, ne.notification);
182a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato                    break;
183a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato                }
184a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato                case MSG_REMOVE_NOTIFICATION: {
185a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato                    mCallbacks.removeNotification((IBinder)msg.obj);
186a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato                    break;
187a0c56fe93925d20d9c0b830b9664699ce557e78cJoe Onorato                }
188f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato                case MSG_DISABLE:
189f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato                    mCallbacks.disable(msg.arg1);
190f3f0e053f0cc66249a11639eb67d0cdc2da26dedJoe Onorato                    break;
1914762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato                case MSG_SET_VISIBILITY:
1924762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato                    if (msg.arg1 == OP_EXPAND) {
1934762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato                        mCallbacks.animateExpand();
1944762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato                    } else {
1954762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato                        mCallbacks.animateCollapse();
1964762c2d75a55e0854bbff2f996748116d4ab1a37Joe Onorato                    }
1970cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato            }
1980cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato        }
1990cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato    }
2000cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato}
2010cbda99f8721ad9b03ada04d2637fb75a2a0fecaJoe Onorato
202