TabletStatusBar.java revision 82beadfa067b1e286fa604f8d7960d769411c954
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
17package com.android.systemui.statusbar.tablet;
18
19import java.io.FileDescriptor;
20import java.io.PrintWriter;
21import java.util.ArrayList;
22import java.util.Map;
23import java.util.IdentityHashMap;
24
25import android.animation.LayoutTransition;
26import android.animation.ObjectAnimator;
27import android.animation.AnimatorSet;
28import android.app.ActivityManagerNative;
29import android.app.PendingIntent;
30import android.app.Notification;
31import android.app.StatusBarManager;
32import android.content.Context;
33import android.content.Intent;
34import android.content.res.Configuration;
35import android.content.res.Resources;
36import android.graphics.PixelFormat;
37import android.graphics.Rect;
38import android.graphics.drawable.Drawable;
39import android.graphics.drawable.LayerDrawable;
40import android.os.Handler;
41import android.os.IBinder;
42import android.os.Message;
43import android.os.RemoteException;
44import android.os.ServiceManager;
45import android.text.TextUtils;
46import android.util.Slog;
47import android.view.animation.Animation;
48import android.view.animation.AnimationUtils;
49import android.view.Gravity;
50import android.view.IWindowManager;
51import android.view.KeyEvent;
52import android.view.LayoutInflater;
53import android.view.MotionEvent;
54import android.view.VelocityTracker;
55import android.view.View;
56import android.view.ViewConfiguration;
57import android.view.ViewGroup;
58import android.view.WindowManager;
59import android.view.WindowManagerImpl;
60import android.widget.FrameLayout;
61import android.widget.ImageView;
62import android.widget.LinearLayout;
63import android.widget.RemoteViews;
64import android.widget.ScrollView;
65import android.widget.TextSwitcher;
66import android.widget.TextView;
67
68import com.android.internal.statusbar.StatusBarIcon;
69import com.android.internal.statusbar.StatusBarNotification;
70
71import com.android.systemui.R;
72import com.android.systemui.statusbar.*;
73import com.android.systemui.statusbar.policy.BatteryController;
74import com.android.systemui.statusbar.policy.NetworkController;
75import com.android.systemui.recent.RecentApplicationsActivity;
76
77public class TabletStatusBar extends StatusBar implements
78        HeightReceiver.OnBarHeightChangedListener {
79    public static final boolean DEBUG = false;
80    public static final String TAG = "TabletStatusBar";
81
82    public static final int MAX_NOTIFICATION_ICONS = 5;
83    // IME switcher icon is big and occupy width of two icons
84    public static final int MAX_NOTIFICATION_ICONS_IME_BUTTON_VISIBLE = MAX_NOTIFICATION_ICONS - 2;
85
86    public static final int MSG_OPEN_NOTIFICATION_PANEL = 1000;
87    public static final int MSG_CLOSE_NOTIFICATION_PANEL = 1001;
88    public static final int MSG_OPEN_NOTIFICATION_PEEK = 1002;
89    public static final int MSG_CLOSE_NOTIFICATION_PEEK = 1003;
90    public static final int MSG_OPEN_RECENTS_PANEL = 1020;
91    public static final int MSG_CLOSE_RECENTS_PANEL = 1021;
92    public static final int MSG_SHOW_CHROME = 1030;
93    public static final int MSG_HIDE_CHROME = 1031;
94    public static final int MSG_OPEN_INPUT_METHODS_PANEL = 1040;
95    public static final int MSG_CLOSE_INPUT_METHODS_PANEL = 1041;
96
97    // Fitts' Law assistance for LatinIME; TODO: replace with a more general approach
98    private static final boolean FAKE_SPACE_BAR = true;
99
100    public static final int LIGHTS_ON_DELAY = 5000;
101
102    // The height of the bar, as definied by the build.  It may be taller if we're plugged
103    // into hdmi.
104    int mNaturalBarHeight = -1;
105    int mIconSize = -1;
106    int mIconHPadding = -1;
107
108    H mHandler = new H();
109
110    IWindowManager mWindowManager;
111
112    // tracking all current notifications
113    private NotificationData mNotns = new NotificationData();
114
115    TabletStatusBarView mStatusBarView;
116    View mNotificationArea;
117    View mNotificationTrigger;
118    NotificationIconArea mNotificationIconArea;
119    View mNavigationArea;
120
121    ImageView mBackButton;
122    View mHomeButton;
123    View mMenuButton;
124    View mRecentButton;
125
126    ViewGroup mNotificationAndImeArea;
127    InputMethodButton mInputMethodSwitchButton;
128
129    NotificationPanel mNotificationPanel;
130    NotificationPeekPanel mNotificationPeekWindow;
131    ViewGroup mNotificationPeekRow;
132    int mNotificationPeekIndex;
133    IBinder mNotificationPeekKey;
134    LayoutTransition mNotificationPeekScrubLeft, mNotificationPeekScrubRight;
135
136    int mNotificationPeekTapDuration;
137    int mNotificationFlingVelocity;
138
139    ViewGroup mPile;
140
141    HeightReceiver mHeightReceiver;
142    BatteryController mBatteryController;
143    NetworkController mNetworkController;
144
145    View mBarContents;
146
147    // hide system chrome ("lights out") support
148    View mShadow;
149
150    NotificationIconArea.IconLayout mIconLayout;
151
152    TabletTicker mTicker;
153
154    View mFakeSpaceBar;
155    KeyEvent mSpaceBarKeyEvent = null;
156
157    // for disabling the status bar
158    int mDisabled = 0;
159
160    boolean mNotificationsOn = true;
161    private RecentAppsPanel mRecentsPanel;
162    private InputMethodsPanel mInputMethodsPanel;
163
164    public Context getContext() { return mContext; }
165
166    protected void addPanelWindows() {
167        final Context context = mContext;
168
169        // Notification Panel
170        mNotificationPanel = (NotificationPanel)View.inflate(context,
171                R.layout.status_bar_notification_panel, null);
172        mNotificationPanel.show(false, false);
173        mNotificationPanel.setOnTouchListener(
174                new TouchOutsideListener(MSG_CLOSE_NOTIFICATION_PANEL, mNotificationPanel));
175
176        // the battery and network icons
177        mBatteryController.addIconView((ImageView)mNotificationPanel.findViewById(R.id.battery));
178        mBatteryController.addLabelView(
179                (TextView)mNotificationPanel.findViewById(R.id.battery_text));
180        mNetworkController.addCombinedSignalIconView(
181                (ImageView)mNotificationPanel.findViewById(R.id.network_signal));
182        mNetworkController.addDataTypeIconView(
183                (ImageView)mNotificationPanel.findViewById(R.id.network_type));
184        mNetworkController.addLabelView(
185                (TextView)mNotificationPanel.findViewById(R.id.network_text));
186        mNetworkController.addLabelView(
187                (TextView)mBarContents.findViewById(R.id.network_text));
188
189        mStatusBarView.setIgnoreChildren(0, mNotificationTrigger, mNotificationPanel);
190
191        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
192                720, // ViewGroup.LayoutParams.MATCH_PARENT,
193                ViewGroup.LayoutParams.MATCH_PARENT,
194                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
195                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
196                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
197                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
198                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
199                PixelFormat.TRANSLUCENT);
200        lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
201        lp.setTitle("NotificationPanel");
202        lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED
203                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING;
204        lp.windowAnimations = com.android.internal.R.style.Animation; // == no animation
205//        lp.windowAnimations = com.android.internal.R.style.Animation_ZoomButtons; // simple fade
206
207        WindowManagerImpl.getDefault().addView(mNotificationPanel, lp);
208
209        // Notification preview window
210        mNotificationPeekWindow = (NotificationPeekPanel) View.inflate(context,
211                R.layout.status_bar_notification_peek, null);
212        mNotificationPeekRow = (ViewGroup) mNotificationPeekWindow.findViewById(R.id.content);
213        mNotificationPeekWindow.setVisibility(View.GONE);
214        mNotificationPeekWindow.setOnTouchListener(
215                new TouchOutsideListener(MSG_CLOSE_NOTIFICATION_PEEK, mNotificationPeekWindow));
216        mNotificationPeekScrubRight = new LayoutTransition();
217        mNotificationPeekScrubRight.setAnimator(LayoutTransition.APPEARING,
218                ObjectAnimator.ofInt(null, "left", -512, 0));
219        mNotificationPeekScrubRight.setAnimator(LayoutTransition.DISAPPEARING,
220                ObjectAnimator.ofInt(null, "left", -512, 0));
221        mNotificationPeekScrubRight.setDuration(500);
222
223        mNotificationPeekScrubLeft = new LayoutTransition();
224        mNotificationPeekScrubLeft.setAnimator(LayoutTransition.APPEARING,
225                ObjectAnimator.ofInt(null, "left", 512, 0));
226        mNotificationPeekScrubLeft.setAnimator(LayoutTransition.DISAPPEARING,
227                ObjectAnimator.ofInt(null, "left", 512, 0));
228        mNotificationPeekScrubLeft.setDuration(500);
229
230        // XXX: setIgnoreChildren?
231        lp = new WindowManager.LayoutParams(
232                512, // ViewGroup.LayoutParams.WRAP_CONTENT,
233                ViewGroup.LayoutParams.WRAP_CONTENT,
234                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
235                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
236                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
237                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH,
238                PixelFormat.TRANSLUCENT);
239        lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
240        lp.setTitle("NotificationPeekWindow");
241        lp.windowAnimations = com.android.internal.R.style.Animation_Toast;
242
243        WindowManagerImpl.getDefault().addView(mNotificationPeekWindow, lp);
244
245        // Recents Panel
246        mRecentsPanel = (RecentAppsPanel) View.inflate(context,
247                R.layout.status_bar_recent_panel, null);
248        mRecentsPanel.setVisibility(View.GONE);
249        mRecentsPanel.setOnTouchListener(new TouchOutsideListener(MSG_CLOSE_RECENTS_PANEL,
250                mRecentsPanel));
251        mStatusBarView.setIgnoreChildren(2, mRecentButton, mRecentsPanel);
252
253        lp = new WindowManager.LayoutParams(
254                ViewGroup.LayoutParams.WRAP_CONTENT,
255                ViewGroup.LayoutParams.WRAP_CONTENT,
256                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
257                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
258                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
259                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
260                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
261                PixelFormat.TRANSLUCENT);
262        lp.gravity = Gravity.BOTTOM | Gravity.LEFT;
263        lp.setTitle("RecentsPanel");
264        lp.windowAnimations = R.style.Animation_RecentPanel;
265
266        WindowManagerImpl.getDefault().addView(mRecentsPanel, lp);
267        mRecentsPanel.setBar(this);
268
269        // Input methods Panel
270        mInputMethodsPanel = (InputMethodsPanel) View.inflate(context,
271                R.layout.status_bar_input_methods_panel, null);
272        mInputMethodsPanel.setVisibility(View.GONE);
273        mInputMethodsPanel.setOnTouchListener(new TouchOutsideListener(
274                MSG_CLOSE_INPUT_METHODS_PANEL, mInputMethodsPanel));
275        mStatusBarView.setIgnoreChildren(3, mInputMethodSwitchButton, mInputMethodsPanel);
276        lp = new WindowManager.LayoutParams(
277                ViewGroup.LayoutParams.WRAP_CONTENT,
278                ViewGroup.LayoutParams.WRAP_CONTENT,
279                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
280                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
281                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
282                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
283                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
284                PixelFormat.TRANSLUCENT);
285        lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
286        lp.setTitle("InputMethodsPanel");
287        lp.windowAnimations = R.style.Animation_RecentPanel;
288
289        WindowManagerImpl.getDefault().addView(mInputMethodsPanel, lp);
290    }
291
292    @Override
293    public void start() {
294        super.start(); // will add the main bar view
295    }
296
297    @Override
298    protected void onConfigurationChanged(Configuration newConfig) {
299        loadDimens();
300    }
301
302    protected void loadDimens() {
303        final Resources res = mContext.getResources();
304
305        mNaturalBarHeight = res.getDimensionPixelSize(
306                com.android.internal.R.dimen.status_bar_height);
307
308        int newIconSize = res.getDimensionPixelSize(
309            com.android.internal.R.dimen.status_bar_icon_size);
310        int newIconHPadding = res.getDimensionPixelSize(
311            R.dimen.status_bar_icon_padding);
312
313        if (newIconHPadding != mIconHPadding || newIconSize != mIconSize) {
314//            Slog.d(TAG, "size=" + newIconSize + " padding=" + newIconHPadding);
315            mIconHPadding = newIconHPadding;
316            mIconSize = newIconSize;
317            reloadAllNotificationIcons(); // reload the tray
318        }
319    }
320
321    protected View makeStatusBarView() {
322        final Context context = mContext;
323
324        mWindowManager = IWindowManager.Stub.asInterface(
325                ServiceManager.getService(Context.WINDOW_SERVICE));
326
327        // This guy will listen for HDMI plugged broadcasts so we can resize the
328        // status bar as appropriate.
329        mHeightReceiver = new HeightReceiver(mContext);
330        mHeightReceiver.registerReceiver();
331        loadDimens();
332
333        final TabletStatusBarView sb = (TabletStatusBarView)View.inflate(
334                context, R.layout.status_bar, null);
335        mStatusBarView = sb;
336
337        sb.setHandler(mHandler);
338
339        mBarContents = sb.findViewById(R.id.bar_contents);
340
341        // the whole right-hand side of the bar
342        mNotificationArea = sb.findViewById(R.id.notificationArea);
343
344        // the button to open the notification area
345        mNotificationTrigger = sb.findViewById(R.id.notificationTrigger);
346        mNotificationTrigger.setOnClickListener(mOnClickListener);
347
348        // the more notifications icon
349        mNotificationIconArea = (NotificationIconArea)sb.findViewById(R.id.notificationIcons);
350
351        // where the icons go
352        mIconLayout = (NotificationIconArea.IconLayout) sb.findViewById(R.id.icons);
353        mIconLayout.setOnTouchListener(new NotificationIconTouchListener());
354
355        ViewConfiguration vc = ViewConfiguration.get(context);
356        mNotificationPeekTapDuration = vc.getTapTimeout();
357        mNotificationFlingVelocity = 300; // px/s
358
359        mTicker = new TabletTicker(this);
360
361        // The icons
362        mBatteryController = new BatteryController(mContext);
363        mBatteryController.addIconView((ImageView)sb.findViewById(R.id.battery));
364        mNetworkController = new NetworkController(mContext);
365        mNetworkController.addCombinedSignalIconView(
366                (ImageView)sb.findViewById(R.id.network_signal));
367        mNetworkController.addDataTypeIconView(
368                (ImageView)sb.findViewById(R.id.network_type));
369
370        // The navigation buttons
371        mNavigationArea = sb.findViewById(R.id.navigationArea);
372        mBackButton = (ImageView)mNavigationArea.findViewById(R.id.back);
373        mHomeButton = mNavigationArea.findViewById(R.id.home);
374        mMenuButton = mNavigationArea.findViewById(R.id.menu);
375        mRecentButton = mNavigationArea.findViewById(R.id.recent_apps);
376        mRecentButton.setOnClickListener(mOnClickListener);
377
378        // The bar contents buttons
379        mNotificationAndImeArea = (ViewGroup)sb.findViewById(R.id.notificationAndImeArea);
380        mInputMethodSwitchButton = (InputMethodButton) sb.findViewById(R.id.imeSwitchButton);
381        // Overwrite the lister
382        mInputMethodSwitchButton.setOnClickListener(mOnClickListener);
383
384        // for redirecting errant bar taps to the IME
385        mFakeSpaceBar = sb.findViewById(R.id.fake_space_bar);
386
387        // "shadows" of the status bar features, for lights-out mode
388        mShadow = sb.findViewById(R.id.bar_shadow);
389        mShadow.setOnTouchListener(
390            new View.OnTouchListener() {
391                public boolean onTouch(View v, MotionEvent ev) {
392                    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
393                        mShadow.setVisibility(View.GONE);
394                        mBarContents.setVisibility(View.VISIBLE);
395                    }
396                    return false;
397                }
398            });
399
400        // tuning parameters
401        final int LIGHTS_GOING_OUT_SYSBAR_DURATION = 600;
402        final int LIGHTS_GOING_OUT_SHADOW_DURATION = 1000;
403        final int LIGHTS_GOING_OUT_SHADOW_DELAY    = 500;
404
405        final int LIGHTS_COMING_UP_SYSBAR_DURATION = 200;
406//        final int LIGHTS_COMING_UP_SYSBAR_DELAY    = 50;
407        final int LIGHTS_COMING_UP_SHADOW_DURATION = 0;
408
409        LayoutTransition xition = new LayoutTransition();
410        xition.setAnimator(LayoutTransition.APPEARING,
411               ObjectAnimator.ofFloat(null, "alpha", 0.5f, 1f));
412        xition.setDuration(LayoutTransition.APPEARING, LIGHTS_COMING_UP_SYSBAR_DURATION);
413        xition.setStartDelay(LayoutTransition.APPEARING, 0);
414        xition.setAnimator(LayoutTransition.DISAPPEARING,
415               ObjectAnimator.ofFloat(null, "alpha", 1f, 0f));
416        xition.setDuration(LayoutTransition.DISAPPEARING, LIGHTS_GOING_OUT_SYSBAR_DURATION);
417        xition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
418        ((ViewGroup)sb.findViewById(R.id.bar_contents_holder)).setLayoutTransition(xition);
419
420        xition = new LayoutTransition();
421        xition.setAnimator(LayoutTransition.APPEARING,
422               ObjectAnimator.ofFloat(null, "alpha", 0f, 1f));
423        xition.setDuration(LayoutTransition.APPEARING, LIGHTS_GOING_OUT_SHADOW_DURATION);
424        xition.setStartDelay(LayoutTransition.APPEARING, LIGHTS_GOING_OUT_SHADOW_DELAY);
425        xition.setAnimator(LayoutTransition.DISAPPEARING,
426               ObjectAnimator.ofFloat(null, "alpha", 1f, 0f));
427        xition.setDuration(LayoutTransition.DISAPPEARING, LIGHTS_COMING_UP_SHADOW_DURATION);
428        xition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
429        ((ViewGroup)sb.findViewById(R.id.bar_shadow_holder)).setLayoutTransition(xition);
430
431        // set the initial view visibility
432        setAreThereNotifications();
433
434        // Add the windows
435        addPanelWindows();
436
437        mPile = (ViewGroup)mNotificationPanel.findViewById(R.id.content);
438        mPile.removeAllViews();
439
440        ScrollView scroller = (ScrollView)mPile.getParent();
441        scroller.setFillViewport(true);
442
443        mHeightReceiver.addOnBarHeightChangedListener(this);
444
445        return sb;
446    }
447
448    public int getStatusBarHeight() {
449        return mHeightReceiver.getHeight();
450    }
451
452    protected int getStatusBarGravity() {
453        return Gravity.BOTTOM | Gravity.FILL_HORIZONTAL;
454    }
455
456    public void onBarHeightChanged(int height) {
457        final WindowManager.LayoutParams lp
458                = (WindowManager.LayoutParams)mStatusBarView.getLayoutParams();
459        if (lp == null) {
460            // haven't been added yet
461            return;
462        }
463        if (lp.height != height) {
464            lp.height = height;
465            final WindowManager wm = WindowManagerImpl.getDefault();
466            wm.updateViewLayout(mStatusBarView, lp);
467        }
468    }
469
470    private class H extends Handler {
471        public void handleMessage(Message m) {
472            switch (m.what) {
473                case MSG_OPEN_NOTIFICATION_PEEK:
474                    if (DEBUG) Slog.d(TAG, "opening notification peek window; arg=" + m.arg1);
475                    if (m.arg1 >= 0) {
476                        final int N = mNotns.size();
477                        if (mNotificationPeekIndex >= 0 && mNotificationPeekIndex < N) {
478                            NotificationData.Entry entry = mNotns.get(N-1-mNotificationPeekIndex);
479                            entry.icon.setBackgroundColor(0);
480                            mNotificationPeekIndex = -1;
481                            mNotificationPeekKey = null;
482                        }
483
484                        final int peekIndex = m.arg1;
485                        if (peekIndex < N) {
486                            //Slog.d(TAG, "loading peek: " + peekIndex);
487                            NotificationData.Entry entry = mNotns.get(N-1-peekIndex);
488                            NotificationData.Entry copy = new NotificationData.Entry(
489                                    entry.key,
490                                    entry.notification,
491                                    entry.icon);
492                            inflateViews(copy, mNotificationPeekRow);
493
494                            entry.icon.setBackgroundColor(0x20FFFFFF);
495
496//                          mNotificationPeekRow.setLayoutTransition(
497//                              peekIndex < mNotificationPeekIndex
498//                                  ? mNotificationPeekScrubLeft
499//                                  : mNotificationPeekScrubRight);
500
501                            mNotificationPeekRow.removeAllViews();
502                            mNotificationPeekRow.addView(copy.row);
503
504                            mNotificationPeekWindow.setVisibility(View.VISIBLE);
505                            mNotificationPanel.show(false, true);
506
507                            mNotificationPeekIndex = peekIndex;
508                            mNotificationPeekKey = entry.key;
509                        }
510                    }
511                    break;
512                case MSG_CLOSE_NOTIFICATION_PEEK:
513                    if (DEBUG) Slog.d(TAG, "closing notification peek window");
514                    mNotificationPeekWindow.setVisibility(View.GONE);
515                    mNotificationPeekRow.removeAllViews();
516                    final int N = mNotns.size();
517                    if (mNotificationPeekIndex >= 0 && mNotificationPeekIndex < N) {
518                        NotificationData.Entry entry = mNotns.get(N-1-mNotificationPeekIndex);
519                        entry.icon.setBackgroundColor(0);
520                    }
521
522                    mNotificationPeekIndex = -1;
523                    mNotificationPeekKey = null;
524                    break;
525                case MSG_OPEN_NOTIFICATION_PANEL:
526                    if (DEBUG) Slog.d(TAG, "opening notifications panel");
527                    if (!mNotificationPanel.isShowing()) {
528                        mNotificationPeekWindow.setVisibility(View.GONE);
529                        mNotificationPanel.show(true, true);
530                        mNotificationArea.setVisibility(View.GONE);
531                        mTicker.halt();
532                    }
533                    break;
534                case MSG_CLOSE_NOTIFICATION_PANEL:
535                    if (DEBUG) Slog.d(TAG, "closing notifications panel");
536                    if (mNotificationPanel.isShowing()) {
537                        mNotificationPanel.show(false, true);
538                        mNotificationArea.setVisibility(View.VISIBLE);
539                    }
540                    break;
541                case MSG_OPEN_RECENTS_PANEL:
542                    if (DEBUG) Slog.d(TAG, "opening recents panel");
543                    if (mRecentsPanel != null) {
544                        mRecentsPanel.setVisibility(View.VISIBLE);
545                        mRecentsPanel.show(true, true);
546                    }
547                    break;
548                case MSG_CLOSE_RECENTS_PANEL:
549                    if (DEBUG) Slog.d(TAG, "closing recents panel");
550                    if (mRecentsPanel != null && mRecentsPanel.isShowing()) {
551                        mRecentsPanel.show(false, true);
552                    }
553                    break;
554                case MSG_OPEN_INPUT_METHODS_PANEL:
555                    if (DEBUG) Slog.d(TAG, "opening input methods panel");
556                    if (mInputMethodsPanel != null) mInputMethodsPanel.setVisibility(View.VISIBLE);
557                    break;
558                case MSG_CLOSE_INPUT_METHODS_PANEL:
559                    if (DEBUG) Slog.d(TAG, "closing input methods panel");
560                    if (mInputMethodsPanel != null) mInputMethodsPanel.setVisibility(View.GONE);
561                    break;
562                case MSG_SHOW_CHROME:
563                    if (DEBUG) Slog.d(TAG, "hiding shadows (lights on)");
564                    mBarContents.setVisibility(View.VISIBLE);
565                    mShadow.setVisibility(View.GONE);
566                    break;
567                case MSG_HIDE_CHROME:
568                    if (DEBUG) Slog.d(TAG, "showing shadows (lights out)");
569                    animateCollapse();
570                    mBarContents.setVisibility(View.GONE);
571                    mShadow.setVisibility(View.VISIBLE);
572                    break;
573            }
574        }
575    }
576
577    public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon) {
578        if (DEBUG) Slog.d(TAG, "addIcon(" + slot + ") -> " + icon);
579    }
580
581    public void updateIcon(String slot, int index, int viewIndex,
582            StatusBarIcon old, StatusBarIcon icon) {
583        if (DEBUG) Slog.d(TAG, "updateIcon(" + slot + ") -> " + icon);
584    }
585
586    public void removeIcon(String slot, int index, int viewIndex) {
587        if (DEBUG) Slog.d(TAG, "removeIcon(" + slot + ")");
588    }
589
590    public void addNotification(IBinder key, StatusBarNotification notification) {
591        if (DEBUG) Slog.d(TAG, "addNotification(" + key + " -> " + notification + ")");
592        addNotificationViews(key, notification);
593
594        final boolean immersive = isImmersive();
595        if (false && immersive) {
596            // TODO: immersive mode popups for tablet
597        } else if (notification.notification.fullScreenIntent != null) {
598            // not immersive & a full-screen alert should be shown
599            Slog.d(TAG, "Notification has fullScreenIntent and activity is not immersive;"
600                    + " sending fullScreenIntent");
601            try {
602                notification.notification.fullScreenIntent.send();
603            } catch (PendingIntent.CanceledException e) {
604            }
605        } else {
606            tick(key, notification, true);
607        }
608
609        setAreThereNotifications();
610    }
611
612    public void updateNotification(IBinder key, StatusBarNotification notification) {
613        if (DEBUG) Slog.d(TAG, "updateNotification(" + key + " -> " + notification + ") // TODO");
614
615        final NotificationData.Entry oldEntry = mNotns.findByKey(key);
616        if (oldEntry == null) {
617            Slog.w(TAG, "updateNotification for unknown key: " + key);
618            return;
619        }
620
621        final StatusBarNotification oldNotification = oldEntry.notification;
622        final RemoteViews oldContentView = oldNotification.notification.contentView;
623
624        final RemoteViews contentView = notification.notification.contentView;
625
626        if (DEBUG) {
627            Slog.d(TAG, "old notification: when=" + oldNotification.notification.when
628                    + " ongoing=" + oldNotification.isOngoing()
629                    + " expanded=" + oldEntry.expanded
630                    + " contentView=" + oldContentView);
631            Slog.d(TAG, "new notification: when=" + notification.notification.when
632                    + " ongoing=" + oldNotification.isOngoing()
633                    + " contentView=" + contentView);
634        }
635
636        // Can we just reapply the RemoteViews in place?  If when didn't change, the order
637        // didn't change.
638        boolean contentsUnchanged = notification.isOngoing() == oldNotification.isOngoing()
639                && oldEntry.expanded != null
640                && contentView != null
641                && oldContentView != null
642                && contentView.getPackage() != null
643                && oldContentView.getPackage() != null
644                && oldContentView.getPackage().equals(contentView.getPackage())
645                && oldContentView.getLayoutId() == contentView.getLayoutId();
646        ViewGroup rowParent = (ViewGroup) oldEntry.row.getParent();
647        boolean orderUnchanged = notification.notification.when==oldNotification.notification.when;
648        boolean isLastAnyway = rowParent.indexOfChild(oldEntry.row) == rowParent.getChildCount()-1;
649        if (contentsUnchanged && (orderUnchanged || isLastAnyway)) {
650            if (DEBUG) Slog.d(TAG, "reusing notification for key: " + key);
651            oldEntry.notification = notification;
652            try {
653                // Reapply the RemoteViews
654                contentView.reapply(mContext, oldEntry.content);
655                // update the contentIntent
656                final PendingIntent contentIntent = notification.notification.contentIntent;
657                if (contentIntent != null) {
658                    oldEntry.content.setOnClickListener(new NotificationClicker(contentIntent,
659                                notification.pkg, notification.tag, notification.id));
660                } else {
661                    oldEntry.content.setOnClickListener(null);
662                }
663                // Update the icon.
664                final StatusBarIcon ic = new StatusBarIcon(notification.pkg,
665                        notification.notification.icon, notification.notification.iconLevel,
666                        notification.notification.number);
667                if (!oldEntry.icon.set(ic)) {
668                    handleNotificationError(key, notification, "Couldn't update icon: " + ic);
669                    return;
670                }
671
672                if (key == mNotificationPeekKey) {
673                    // must update the peek window
674                    Message peekMsg = mHandler.obtainMessage(MSG_OPEN_NOTIFICATION_PEEK);
675                    peekMsg.arg1 = mNotificationPeekIndex;
676                    mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
677                    mHandler.sendMessage(peekMsg);
678                }
679            }
680            catch (RuntimeException e) {
681                // It failed to add cleanly.  Log, and remove the view from the panel.
682                Slog.w(TAG, "Couldn't reapply views for package " + contentView.getPackage(), e);
683                removeNotificationViews(key);
684                addNotificationViews(key, notification);
685            }
686        } else {
687            if (DEBUG) Slog.d(TAG, "not reusing notification for key: " + key);
688            removeNotificationViews(key);
689            addNotificationViews(key, notification);
690        }
691        // fullScreenIntent doesn't happen on updates.  You need to clear & repost a new
692        // notification.
693        final boolean immersive = isImmersive();
694        if (false && immersive) {
695            // TODO: immersive mode
696        } else {
697            tick(key, notification, false);
698        }
699
700        setAreThereNotifications();
701    }
702
703    public void removeNotification(IBinder key) {
704        if (DEBUG) Slog.d(TAG, "removeNotification(" + key + ") // TODO");
705        removeNotificationViews(key);
706        mTicker.remove(key);
707        setAreThereNotifications();
708    }
709
710    public void showClock(boolean show) {
711        View clock = mBarContents.findViewById(R.id.clock);
712        View network_text = mBarContents.findViewById(R.id.network_text);
713        if (clock != null) {
714            clock.setVisibility(show ? View.VISIBLE : View.GONE);
715        }
716        if (network_text != null) {
717            network_text.setVisibility((!show) ? View.VISIBLE : View.GONE);
718        }
719    }
720
721    public void disable(int state) {
722        int old = mDisabled;
723        int diff = state ^ old;
724        mDisabled = state;
725
726        // act accordingly
727        if ((diff & StatusBarManager.DISABLE_CLOCK) != 0) {
728            boolean show = (state & StatusBarManager.DISABLE_CLOCK) == 0;
729            Slog.d(TAG, "DISABLE_CLOCK: " + (show ? "no" : "yes"));
730            showClock(show);
731        }
732        if ((diff & StatusBarManager.DISABLE_SYSTEM_INFO) != 0) {
733            boolean show = (state & StatusBarManager.DISABLE_SYSTEM_INFO) == 0;
734            Slog.d(TAG, "DISABLE_SYSTEM_INFO: " + (show ? "no" : "yes"));
735            mNotificationTrigger.setVisibility(show ? View.VISIBLE : View.GONE);
736        }
737        if ((diff & StatusBarManager.DISABLE_EXPAND) != 0) {
738            if ((state & StatusBarManager.DISABLE_EXPAND) != 0) {
739                Slog.d(TAG, "DISABLE_EXPAND: yes");
740                animateCollapse();
741            }
742        }
743        if ((diff & StatusBarManager.DISABLE_NOTIFICATION_ICONS) != 0) {
744            if ((state & StatusBarManager.DISABLE_NOTIFICATION_ICONS) != 0) {
745                Slog.d(TAG, "DISABLE_NOTIFICATION_ICONS: yes");
746                // synchronize with current shadow state
747                mNotificationIconArea.setVisibility(View.GONE);
748                mTicker.halt();
749            } else {
750                Slog.d(TAG, "DISABLE_NOTIFICATION_ICONS: no");
751                // synchronize with current shadow state
752                mNotificationIconArea.setVisibility(View.VISIBLE);
753            }
754        } else if ((diff & StatusBarManager.DISABLE_NOTIFICATION_TICKER) != 0) {
755            if ((state & StatusBarManager.DISABLE_NOTIFICATION_TICKER) != 0) {
756                mTicker.halt();
757            }
758        }
759        if ((diff & StatusBarManager.DISABLE_NAVIGATION) != 0) {
760            if ((state & StatusBarManager.DISABLE_NAVIGATION) != 0) {
761                Slog.d(TAG, "DISABLE_NAVIGATION: yes");
762                mNavigationArea.setVisibility(View.GONE);
763                mInputMethodSwitchButton.setScreenLocked(true);
764            } else {
765                Slog.d(TAG, "DISABLE_NAVIGATION: no");
766                mNavigationArea.setVisibility(View.VISIBLE);
767                mInputMethodSwitchButton.setScreenLocked(false);
768            }
769        }
770    }
771
772    private boolean hasTicker(Notification n) {
773        return n.tickerView != null || !TextUtils.isEmpty(n.tickerText);
774    }
775
776    private void tick(IBinder key, StatusBarNotification n, boolean firstTime) {
777        // Don't show the ticker when the windowshade is open.
778        if (mNotificationPanel.isShowing()) {
779            return;
780        }
781        // If they asked for FLAG_ONLY_ALERT_ONCE, then only show this notification
782        // if it's a new notification.
783        if (!firstTime && (n.notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) {
784            return;
785        }
786        // Show the ticker if one is requested. Also don't do this
787        // until status bar window is attached to the window manager,
788        // because...  well, what's the point otherwise?  And trying to
789        // run a ticker without being attached will crash!
790        if (hasTicker(n.notification) && mStatusBarView.getWindowToken() != null) {
791            if (0 == (mDisabled & (StatusBarManager.DISABLE_NOTIFICATION_ICONS
792                            | StatusBarManager.DISABLE_NOTIFICATION_TICKER))) {
793                mTicker.add(key, n);
794                mNotificationAndImeArea.setVisibility(View.GONE);
795            }
796        }
797    }
798
799    // called by TabletTicker when it's done with all queued ticks
800    public void doneTicking() {
801        mNotificationAndImeArea.setVisibility(View.VISIBLE);
802    }
803
804    public void animateExpand() {
805        mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PANEL);
806        mHandler.sendEmptyMessage(MSG_OPEN_NOTIFICATION_PANEL);
807    }
808
809    public void animateCollapse() {
810        mHandler.removeMessages(MSG_CLOSE_NOTIFICATION_PANEL);
811        mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PANEL);
812        mHandler.removeMessages(MSG_CLOSE_RECENTS_PANEL);
813        mHandler.sendEmptyMessage(MSG_CLOSE_RECENTS_PANEL);
814        mHandler.removeMessages(MSG_CLOSE_INPUT_METHODS_PANEL);
815        mHandler.sendEmptyMessage(MSG_CLOSE_INPUT_METHODS_PANEL);
816    }
817
818    // called by StatusBar
819    @Override
820    public void setLightsOn(boolean on) {
821        // Policy note: if the frontmost activity needs the menu key, we assume it is a legacy app
822        // that can't handle lights-out mode.
823        if (mMenuButton.getVisibility() == View.VISIBLE) {
824            on = true;
825        }
826        mHandler.removeMessages(MSG_HIDE_CHROME);
827        mHandler.removeMessages(MSG_SHOW_CHROME);
828        mHandler.sendEmptyMessage(on ? MSG_SHOW_CHROME : MSG_HIDE_CHROME);
829    }
830
831    public void setMenuKeyVisible(boolean visible) {
832        if (DEBUG) {
833            Slog.d(TAG, (visible?"showing":"hiding") + " the MENU button");
834        }
835        mMenuButton.setVisibility(visible ? View.VISIBLE : View.GONE);
836
837        // See above re: lights-out policy for legacy apps.
838        if (visible) setLightsOn(true);
839    }
840
841    public void setIMEButtonVisible(IBinder token, boolean visible) {
842        if (DEBUG) {
843            Slog.d(TAG, (visible?"showing":"hiding") + " the IME button");
844        }
845        int oldVisibility = mInputMethodSwitchButton.getVisibility();
846        mInputMethodSwitchButton.setIMEButtonVisible(token, visible);
847        if (oldVisibility != mInputMethodSwitchButton.getVisibility()) {
848            updateNotificationIcons();
849        }
850        mInputMethodsPanel.setIMEToken(token);
851        mBackButton.setImageResource(
852                visible ? R.drawable.ic_sysbar_back_ime : R.drawable.ic_sysbar_back);
853        if (FAKE_SPACE_BAR) {
854            mFakeSpaceBar.setVisibility(visible ? View.VISIBLE : View.GONE);
855        }
856    }
857
858    private boolean isImmersive() {
859        try {
860            return ActivityManagerNative.getDefault().isTopActivityImmersive();
861            //Slog.d(TAG, "Top activity is " + (immersive?"immersive":"not immersive"));
862        } catch (RemoteException ex) {
863            // the end is nigh
864            return false;
865        }
866    }
867
868    private void setAreThereNotifications() {
869        final boolean hasClearable = mNotns.hasClearableItems();
870    }
871
872    /**
873     * Cancel this notification and tell the status bar service about the failure. Hold no locks.
874     */
875    void handleNotificationError(IBinder key, StatusBarNotification n, String message) {
876        removeNotification(key);
877        try {
878            mBarService.onNotificationError(n.pkg, n.tag, n.id, n.uid, n.initialPid, message);
879        } catch (RemoteException ex) {
880            // The end is nigh.
881        }
882    }
883
884    private void sendKey(KeyEvent key) {
885        try {
886            if (DEBUG) Slog.d(TAG, "injecting key event: " + key);
887            mWindowManager.injectInputEventNoWait(key);
888        } catch (RemoteException ex) {
889        }
890    }
891
892    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
893        public void onClick(View v) {
894            if (v == mNotificationTrigger) {
895                onClickNotificationTrigger();
896            } else if (v == mRecentButton) {
897                onClickRecentButton();
898            } else if (v == mInputMethodSwitchButton) {
899                onClickInputMethodSwitchButton();
900            }
901        }
902    };
903
904    public void onClickNotificationTrigger() {
905        if (DEBUG) Slog.d(TAG, "clicked notification icons; disabled=" + mDisabled);
906        if ((mDisabled & StatusBarManager.DISABLE_EXPAND) == 0) {
907            if (!mNotificationsOn) {
908                mNotificationsOn = true;
909                mIconLayout.setVisibility(View.VISIBLE); // TODO: animation
910            } else {
911                int msg = !mNotificationPanel.isShowing()
912                    ? MSG_OPEN_NOTIFICATION_PANEL
913                    : MSG_CLOSE_NOTIFICATION_PANEL;
914                mHandler.removeMessages(msg);
915                mHandler.sendEmptyMessage(msg);
916            }
917        }
918    }
919
920    public void onClickRecentButton() {
921        if (DEBUG) Slog.d(TAG, "clicked recent apps; disabled=" + mDisabled);
922        if (mRecentsPanel == null) {
923            Intent intent = new Intent();
924            intent.setClass(mContext, RecentApplicationsActivity.class);
925            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
926                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
927            mContext.startActivity(intent);
928        } else {
929            if ((mDisabled & StatusBarManager.DISABLE_EXPAND) == 0) {
930                int msg = (mRecentsPanel.getVisibility() == View.GONE)
931                    ? MSG_OPEN_RECENTS_PANEL
932                    : MSG_CLOSE_RECENTS_PANEL;
933                mHandler.removeMessages(msg);
934                mHandler.sendEmptyMessage(msg);
935            }
936        }
937    }
938
939    public void onClickInputMethodSwitchButton() {
940        if (DEBUG) Slog.d(TAG, "clicked input methods panel; disabled=" + mDisabled);
941        int msg = (mInputMethodsPanel.getVisibility() == View.GONE) ?
942                MSG_OPEN_INPUT_METHODS_PANEL : MSG_CLOSE_INPUT_METHODS_PANEL;
943        mHandler.removeMessages(msg);
944        mHandler.sendEmptyMessage(msg);
945    }
946
947    public NotificationClicker makeClicker(PendingIntent intent, String pkg, String tag, int id) {
948        return new NotificationClicker(intent, pkg, tag, id);
949    }
950
951    private class NotificationClicker implements View.OnClickListener {
952        private PendingIntent mIntent;
953        private String mPkg;
954        private String mTag;
955        private int mId;
956
957        NotificationClicker(PendingIntent intent, String pkg, String tag, int id) {
958            mIntent = intent;
959            mPkg = pkg;
960            mTag = tag;
961            mId = id;
962        }
963
964        public void onClick(View v) {
965            try {
966                // The intent we are sending is for the application, which
967                // won't have permission to immediately start an activity after
968                // the user switches to home.  We know it is safe to do at this
969                // point, so make sure new activity switches are now allowed.
970                ActivityManagerNative.getDefault().resumeAppSwitches();
971            } catch (RemoteException e) {
972            }
973
974            if (mIntent != null) {
975                int[] pos = new int[2];
976                v.getLocationOnScreen(pos);
977                Intent overlay = new Intent();
978                overlay.setSourceBounds(
979                        new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
980                try {
981                    mIntent.send(mContext, 0, overlay);
982                } catch (PendingIntent.CanceledException e) {
983                    // the stack trace isn't very helpful here.  Just log the exception message.
984                    Slog.w(TAG, "Sending contentIntent failed: " + e);
985                }
986            }
987
988            try {
989                mBarService.onNotificationClick(mPkg, mTag, mId);
990            } catch (RemoteException ex) {
991                // system process is dead if we're here.
992            }
993
994            // close the shade if it was open
995            animateCollapse();
996
997            // If this click was on the intruder alert, hide that instead
998//            mHandler.sendEmptyMessage(MSG_HIDE_INTRUDER);
999        }
1000    }
1001
1002    StatusBarNotification removeNotificationViews(IBinder key) {
1003        NotificationData.Entry entry = mNotns.remove(key);
1004        if (entry == null) {
1005            Slog.w(TAG, "removeNotification for unknown key: " + key);
1006            return null;
1007        }
1008        // Remove the expanded view.
1009        ViewGroup rowParent = (ViewGroup)entry.row.getParent();
1010        if (rowParent != null) rowParent.removeView(entry.row);
1011
1012        if (key == mNotificationPeekKey) {
1013            // must close the peek as well, since it's gone
1014            mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PEEK);
1015        }
1016        // Remove the icon.
1017//        ViewGroup iconParent = (ViewGroup)entry.icon.getParent();
1018//        if (iconParent != null) iconParent.removeView(entry.icon);
1019        updateNotificationIcons();
1020
1021        return entry.notification;
1022    }
1023
1024    private class NotificationIconTouchListener implements View.OnTouchListener {
1025        VelocityTracker mVT;
1026
1027        public NotificationIconTouchListener() {
1028        }
1029
1030        public boolean onTouch(View v, MotionEvent event) {
1031            boolean peeking = mNotificationPeekWindow.getVisibility() != View.GONE;
1032            boolean panelShowing = mNotificationPanel.isShowing();
1033            if (panelShowing) return false;
1034
1035            switch (event.getAction()) {
1036                case MotionEvent.ACTION_DOWN:
1037                    mVT = VelocityTracker.obtain();
1038
1039                    // fall through
1040                case MotionEvent.ACTION_OUTSIDE:
1041                case MotionEvent.ACTION_MOVE:
1042                    // peek and switch icons if necessary
1043                    int numIcons = mIconLayout.getChildCount();
1044                    int peekIndex = (int)((float)event.getX() * numIcons / mIconLayout.getWidth());
1045                    if (peekIndex > numIcons - 1) peekIndex = numIcons - 1;
1046                    else if (peekIndex < 0) peekIndex = 0;
1047
1048                    if (!peeking || mNotificationPeekIndex != peekIndex) {
1049                        if (DEBUG) Slog.d(TAG, "will peek at notification #" + peekIndex);
1050                        Message peekMsg = mHandler.obtainMessage(MSG_OPEN_NOTIFICATION_PEEK);
1051                        peekMsg.arg1 = peekIndex;
1052
1053                        mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
1054
1055                        // no delay if we're scrubbing left-right
1056                        mHandler.sendMessage(peekMsg);
1057                    }
1058
1059                    // check for fling
1060                    if (mVT != null) {
1061                        mVT.addMovement(event);
1062                        mVT.computeCurrentVelocity(1000);
1063                        // require a little more oomph once we're already in peekaboo mode
1064                        if (!panelShowing && (
1065                               (peeking && mVT.getYVelocity() < -mNotificationFlingVelocity*3)
1066                            || (mVT.getYVelocity() < -mNotificationFlingVelocity))) {
1067                            mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
1068                            mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PANEL);
1069                            mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PEEK);
1070                            mHandler.sendEmptyMessage(MSG_OPEN_NOTIFICATION_PANEL);
1071                        }
1072                    }
1073                    return true;
1074                case MotionEvent.ACTION_UP:
1075                case MotionEvent.ACTION_CANCEL:
1076                    mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
1077                    if (peeking) {
1078                        mHandler.sendEmptyMessageDelayed(MSG_CLOSE_NOTIFICATION_PEEK, 5000);
1079                    }
1080                    mVT.recycle();
1081                    mVT = null;
1082                    return true;
1083            }
1084            return false;
1085        }
1086    }
1087
1088    StatusBarIconView addNotificationViews(IBinder key, StatusBarNotification notification) {
1089        if (DEBUG) {
1090            Slog.d(TAG, "addNotificationViews(key=" + key + ", notification=" + notification);
1091        }
1092        // Construct the icon.
1093        final StatusBarIconView iconView = new StatusBarIconView(mContext,
1094                notification.pkg + "/0x" + Integer.toHexString(notification.id));
1095        iconView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
1096
1097        final StatusBarIcon ic = new StatusBarIcon(notification.pkg,
1098                    notification.notification.icon,
1099                    notification.notification.iconLevel,
1100                    notification.notification.number);
1101        if (!iconView.set(ic)) {
1102            handleNotificationError(key, notification, "Couldn't attach StatusBarIcon: " + ic);
1103            return null;
1104        }
1105        // Construct the expanded view.
1106        NotificationData.Entry entry = new NotificationData.Entry(key, notification, iconView);
1107        if (!inflateViews(entry, mPile)) {
1108            handleNotificationError(key, notification, "Couldn't expand RemoteViews for: "
1109                    + notification);
1110            return null;
1111        }
1112
1113        // Add the icon.
1114        mNotns.add(entry);
1115        updateNotificationIcons();
1116
1117        return iconView;
1118    }
1119
1120    private void reloadAllNotificationIcons() {
1121        if (mIconLayout == null) return;
1122        mIconLayout.removeAllViews();
1123        updateNotificationIcons();
1124    }
1125
1126    private void updateNotificationIcons() {
1127        // XXX: need to implement a new limited linear layout class
1128        // to avoid removing & readding everything
1129
1130        if (mIconLayout == null) return;
1131
1132        final LinearLayout.LayoutParams params
1133            = new LinearLayout.LayoutParams(mIconSize + 2*mIconHPadding, mNaturalBarHeight);
1134
1135        int N = mNotns.size();
1136
1137        if (DEBUG) {
1138            Slog.d(TAG, "refreshing icons: " + N + " notifications, mIconLayout=" + mIconLayout);
1139        }
1140
1141        ArrayList<View> toShow = new ArrayList<View>();
1142
1143        // When IME button is visible, the number of notification icons should be decremented
1144        // to fit the upper limit.
1145        final int maxNotificationIconsCount =
1146                (mInputMethodSwitchButton.getVisibility() != View.GONE) ?
1147                        MAX_NOTIFICATION_ICONS_IME_BUTTON_VISIBLE : MAX_NOTIFICATION_ICONS;
1148        for (int i=0; i< maxNotificationIconsCount; i++) {
1149            if (i>=N) break;
1150            toShow.add(mNotns.get(N-i-1).icon);
1151        }
1152
1153        ArrayList<View> toRemove = new ArrayList<View>();
1154        for (int i=0; i<mIconLayout.getChildCount(); i++) {
1155            View child = mIconLayout.getChildAt(i);
1156            if (!toShow.contains(child)) {
1157                toRemove.add(child);
1158            }
1159        }
1160
1161        for (View remove : toRemove) {
1162            mIconLayout.removeView(remove);
1163        }
1164
1165        for (int i=0; i<toShow.size(); i++) {
1166            View v = toShow.get(i);
1167            v.setPadding(mIconHPadding, 0, mIconHPadding, 0);
1168            if (v.getParent() == null) {
1169                mIconLayout.addView(v, i, params);
1170            }
1171        }
1172
1173        loadNotificationPanel();
1174    }
1175
1176    private void loadNotificationPanel() {
1177        int N = mNotns.size();
1178
1179        ArrayList<View> toShow = new ArrayList<View>();
1180
1181        for (int i=0; i<N; i++) {
1182            View row = mNotns.get(N-i-1).row;
1183            toShow.add(row);
1184        }
1185
1186        ArrayList<View> toRemove = new ArrayList<View>();
1187        for (int i=0; i<mPile.getChildCount(); i++) {
1188            View child = mPile.getChildAt(i);
1189            if (!toShow.contains(child)) {
1190                toRemove.add(child);
1191            }
1192        }
1193
1194        for (View remove : toRemove) {
1195            mPile.removeView(remove);
1196        }
1197
1198        for (int i=0; i<toShow.size(); i++) {
1199            View v = toShow.get(i);
1200            if (v.getParent() == null) {
1201                mPile.addView(toShow.get(i));
1202            }
1203        }
1204    }
1205
1206    void workAroundBadLayerDrawableOpacity(View v) {
1207        LayerDrawable d = (LayerDrawable)v.getBackground();
1208        v.setBackgroundDrawable(null);
1209        d.setOpacity(PixelFormat.TRANSLUCENT);
1210        v.setBackgroundDrawable(d);
1211    }
1212
1213    private boolean inflateViews(NotificationData.Entry entry, ViewGroup parent) {
1214        StatusBarNotification sbn = entry.notification;
1215        RemoteViews remoteViews = sbn.notification.contentView;
1216        if (remoteViews == null) {
1217            return false;
1218        }
1219
1220        // create the row view
1221        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(
1222                Context.LAYOUT_INFLATER_SERVICE);
1223        View row = inflater.inflate(R.layout.status_bar_notification_row, parent, false);
1224        workAroundBadLayerDrawableOpacity(row);
1225        View vetoButton = row.findViewById(R.id.veto);
1226        if (entry.notification.isClearable()) {
1227            final String _pkg = sbn.pkg;
1228            final String _tag = sbn.tag;
1229            final int _id = sbn.id;
1230            vetoButton.setOnClickListener(new View.OnClickListener() {
1231                    public void onClick(View v) {
1232                        try {
1233                            mBarService.onNotificationClear(_pkg, _tag, _id);
1234                        } catch (RemoteException ex) {
1235                            // system process is dead if we're here.
1236                        }
1237                    }
1238                });
1239        } else {
1240            if ((sbn.notification.flags & Notification.FLAG_ONGOING_EVENT) == 0) {
1241                vetoButton.setVisibility(View.INVISIBLE);
1242            } else {
1243                vetoButton.setVisibility(View.GONE);
1244            }
1245        }
1246
1247        // the large icon
1248        ImageView largeIcon = (ImageView)row.findViewById(R.id.large_icon);
1249        if (sbn.notification.largeIcon != null) {
1250            largeIcon.setImageBitmap(sbn.notification.largeIcon);
1251        } else {
1252            largeIcon.getLayoutParams().width = 0;
1253            largeIcon.setVisibility(View.INVISIBLE);
1254        }
1255
1256        // bind the click event to the content area
1257        ViewGroup content = (ViewGroup)row.findViewById(R.id.content);
1258        // XXX: update to allow controls within notification views
1259        content.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
1260//        content.setOnFocusChangeListener(mFocusChangeListener);
1261        PendingIntent contentIntent = sbn.notification.contentIntent;
1262        if (contentIntent != null) {
1263            content.setOnClickListener(new NotificationClicker(contentIntent,
1264                        sbn.pkg, sbn.tag, sbn.id));
1265        } else {
1266            content.setOnClickListener(null);
1267        }
1268
1269        View expanded = null;
1270        Exception exception = null;
1271        try {
1272            expanded = remoteViews.apply(mContext, content);
1273        }
1274        catch (RuntimeException e) {
1275            exception = e;
1276        }
1277        if (expanded == null) {
1278            final String ident = sbn.pkg + "/0x" + Integer.toHexString(sbn.id);
1279            Slog.e(TAG, "couldn't inflate view for notification " + ident, exception);
1280            return false;
1281        } else {
1282            content.addView(expanded);
1283            row.setDrawingCacheEnabled(true);
1284        }
1285
1286        entry.row = row;
1287        entry.content = content;
1288        entry.expanded = expanded;
1289
1290        return true;
1291    }
1292
1293/*
1294    public class ShadowController {
1295        boolean mShowShadows;
1296        Map<View, View> mShadowsForElements = new IdentityHashMap<View, View>(7);
1297        Map<View, View> mElementsForShadows = new IdentityHashMap<View, View>(7);
1298        LayoutTransition mElementTransition, mShadowTransition;
1299
1300        View mTouchTarget;
1301
1302        ShadowController(boolean showShadows) {
1303            mShowShadows = showShadows;
1304            mTouchTarget = null;
1305
1306            mElementTransition = new LayoutTransition();
1307//            AnimatorSet s = new AnimatorSet();
1308//            s.play(ObjectAnimator.ofInt(null, "top", 48, 0))
1309//                .with(ObjectAnimator.ofFloat(null, "scaleY", 0.5f, 1f))
1310//                .with(ObjectAnimator.ofFloat(null, "alpha", 0.5f, 1f))
1311//                ;
1312            mElementTransition.setAnimator(LayoutTransition.APPEARING, //s);
1313                   ObjectAnimator.ofInt(null, "top", 48, 0));
1314            mElementTransition.setDuration(LayoutTransition.APPEARING, 100);
1315            mElementTransition.setStartDelay(LayoutTransition.APPEARING, 0);
1316
1317//            s = new AnimatorSet();
1318//            s.play(ObjectAnimator.ofInt(null, "top", 0, 48))
1319//                .with(ObjectAnimator.ofFloat(null, "scaleY", 1f, 0.5f))
1320//                .with(ObjectAnimator.ofFloat(null, "alpha", 1f, 0.5f))
1321//                ;
1322            mElementTransition.setAnimator(LayoutTransition.DISAPPEARING, //s);
1323                    ObjectAnimator.ofInt(null, "top", 0, 48));
1324            mElementTransition.setDuration(LayoutTransition.DISAPPEARING, 400);
1325
1326            mShadowTransition = new LayoutTransition();
1327            mShadowTransition.setAnimator(LayoutTransition.APPEARING,
1328                    ObjectAnimator.ofFloat(null, "alpha", 0f, 1f));
1329            mShadowTransition.setDuration(LayoutTransition.APPEARING, 200);
1330            mShadowTransition.setStartDelay(LayoutTransition.APPEARING, 100);
1331            mShadowTransition.setAnimator(LayoutTransition.DISAPPEARING,
1332                    ObjectAnimator.ofFloat(null, "alpha", 1f, 0f));
1333            mShadowTransition.setDuration(LayoutTransition.DISAPPEARING, 100);
1334
1335            ViewGroup bar = (ViewGroup) TabletStatusBar.this.mBarContents;
1336            bar.setLayoutTransition(mElementTransition);
1337            ViewGroup nav = (ViewGroup) TabletStatusBar.this.mNavigationArea;
1338            nav.setLayoutTransition(mElementTransition);
1339            ViewGroup shadowGroup = (ViewGroup) bar.findViewById(R.id.shadows);
1340            shadowGroup.setLayoutTransition(mShadowTransition);
1341        }
1342
1343        public void add(View element, View shadow) {
1344            shadow.setOnTouchListener(makeTouchListener());
1345            mShadowsForElements.put(element, shadow);
1346            mElementsForShadows.put(shadow, element);
1347        }
1348
1349        public boolean getShadowState() {
1350            return mShowShadows;
1351        }
1352
1353        public View.OnTouchListener makeTouchListener() {
1354            return new View.OnTouchListener() {
1355                public boolean onTouch(View v, MotionEvent ev) {
1356                    final int action = ev.getAction();
1357
1358                    if (DEBUG) Slog.d(TAG, "ShadowController: v=" + v + ", ev=" + ev);
1359
1360                    // currently redirecting events?
1361                    if (mTouchTarget == null) {
1362                        mTouchTarget = mElementsForShadows.get(v);
1363                    }
1364
1365                    if (mTouchTarget != null && mTouchTarget.getVisibility() != View.GONE) {
1366                        boolean last = false;
1367                        switch (action) {
1368                            case MotionEvent.ACTION_CANCEL:
1369                            case MotionEvent.ACTION_UP:
1370                                mHandler.removeMessages(MSG_RESTORE_SHADOWS);
1371                                if (mShowShadows) {
1372                                    mHandler.sendEmptyMessageDelayed(MSG_RESTORE_SHADOWS,
1373                                            v == mNotificationShadow ? 5000 : 500);
1374                                }
1375                                last = true;
1376                                break;
1377                            case MotionEvent.ACTION_DOWN:
1378                                mHandler.removeMessages(MSG_RESTORE_SHADOWS);
1379                                setElementShadow(mTouchTarget, false);
1380                                break;
1381                        }
1382                        mTouchTarget.dispatchTouchEvent(ev);
1383                        if (last) mTouchTarget = null;
1384                        return true;
1385                    }
1386
1387                    return false;
1388                }
1389            };
1390        }
1391
1392        public void refresh() {
1393            for (View element : mShadowsForElements.keySet()) {
1394                setElementShadow(element, mShowShadows);
1395            }
1396        }
1397
1398        public void showAllShadows() {
1399            mShowShadows = true;
1400            refresh();
1401        }
1402
1403        public void hideAllShadows() {
1404            mShowShadows = false;
1405            refresh();
1406        }
1407
1408        // Use View.INVISIBLE for things hidden due to shadowing, and View.GONE for things that are
1409        // disabled (and should not be shadowed or re-shown)
1410        public void setElementShadow(View button, boolean shade) {
1411            View shadow = mShadowsForElements.get(button);
1412            if (shadow != null) {
1413                if (button.getVisibility() != View.GONE) {
1414                    shadow.setVisibility(shade ? View.VISIBLE : View.INVISIBLE);
1415                    button.setVisibility(shade ? View.INVISIBLE : View.VISIBLE);
1416                }
1417            }
1418        }
1419
1420        // Hide both element and shadow, using default layout animations.
1421        public void hideElement(View button) {
1422            Slog.d(TAG, "hiding: " + button);
1423            View shadow = mShadowsForElements.get(button);
1424            if (shadow != null) {
1425                shadow.setVisibility(View.GONE);
1426            }
1427            button.setVisibility(View.GONE);
1428        }
1429
1430        // Honoring the current shadow state.
1431        public void showElement(View button) {
1432            Slog.d(TAG, "showing: " + button);
1433            View shadow = mShadowsForElements.get(button);
1434            if (shadow != null) {
1435                shadow.setVisibility(mShowShadows ? View.VISIBLE : View.INVISIBLE);
1436            }
1437            button.setVisibility(mShowShadows ? View.INVISIBLE : View.VISIBLE);
1438        }
1439    }
1440    */
1441
1442    public class TouchOutsideListener implements View.OnTouchListener {
1443        private int mMsg;
1444        private StatusBarPanel mPanel;
1445
1446        public TouchOutsideListener(int msg, StatusBarPanel panel) {
1447            mMsg = msg;
1448            mPanel = panel;
1449        }
1450
1451        public boolean onTouch(View v, MotionEvent ev) {
1452            final int action = ev.getAction();
1453            if (action == MotionEvent.ACTION_OUTSIDE
1454                    || (action == MotionEvent.ACTION_DOWN
1455                        && !mPanel.isInContentArea((int)ev.getX(), (int)ev.getY()))) {
1456                mHandler.removeMessages(mMsg);
1457                mHandler.sendEmptyMessage(mMsg);
1458                return true;
1459            }
1460            return false;
1461        }
1462    }
1463
1464    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1465        pw.print("mDisabled=0x");
1466        pw.println(Integer.toHexString(mDisabled));
1467        pw.println("mNetworkController:");
1468        mNetworkController.dump(fd, pw, args);
1469    }
1470}
1471
1472
1473