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