TabletStatusBar.java revision 2561b0b10a55841a08e0e1d467e73e10b1bf256d
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;
22
23import android.animation.LayoutTransition;
24import android.animation.ObjectAnimator;
25import android.app.ActivityManagerNative;
26import android.app.KeyguardManager;
27import android.app.Notification;
28import android.app.PendingIntent;
29import android.app.StatusBarManager;
30import android.content.BroadcastReceiver;
31import android.content.Context;
32import android.content.Intent;
33import android.content.IntentFilter;
34import android.content.SharedPreferences;
35import android.content.pm.ApplicationInfo;
36import android.content.pm.PackageManager.NameNotFoundException;
37import android.content.res.Configuration;
38import android.content.res.Resources;
39import android.inputmethodservice.InputMethodService;
40import android.graphics.PixelFormat;
41import android.graphics.Point;
42import android.graphics.Rect;
43import android.graphics.drawable.Drawable;
44import android.graphics.drawable.LayerDrawable;
45import android.os.Build;
46import android.os.Handler;
47import android.os.IBinder;
48import android.os.Message;
49import android.os.RemoteException;
50import android.os.ServiceManager;
51import android.text.TextUtils;
52import android.util.Slog;
53import android.view.accessibility.AccessibilityEvent;
54import android.view.Display;
55import android.view.Gravity;
56import android.view.IWindowManager;
57import android.view.KeyEvent;
58import android.view.LayoutInflater;
59import android.view.MotionEvent;
60import android.view.SoundEffectConstants;
61import android.view.VelocityTracker;
62import android.view.View;
63import android.view.ViewConfiguration;
64import android.view.ViewGroup;
65import android.view.WindowManager;
66import android.view.WindowManagerImpl;
67import android.widget.ImageView;
68import android.widget.LinearLayout;
69import android.widget.RemoteViews;
70import android.widget.ScrollView;
71import android.widget.TextView;
72
73import com.android.internal.statusbar.StatusBarIcon;
74import com.android.internal.statusbar.StatusBarNotification;
75import com.android.systemui.R;
76import com.android.systemui.recent.RecentTasksLoader;
77import com.android.systemui.recent.RecentsPanelView;
78import com.android.systemui.statusbar.NotificationData;
79import com.android.systemui.statusbar.SignalClusterView;
80import com.android.systemui.statusbar.StatusBar;
81import com.android.systemui.statusbar.StatusBarIconView;
82import com.android.systemui.statusbar.policy.BatteryController;
83import com.android.systemui.statusbar.policy.BluetoothController;
84import com.android.systemui.statusbar.policy.CompatModeButton;
85import com.android.systemui.statusbar.policy.LocationController;
86import com.android.systemui.statusbar.policy.NetworkController;
87import com.android.systemui.statusbar.policy.Prefs;
88
89public class TabletStatusBar extends StatusBar implements
90        HeightReceiver.OnBarHeightChangedListener,
91        InputMethodsPanel.OnHardKeyboardEnabledChangeListener,
92        RecentsPanelView.OnRecentsPanelVisibilityChangedListener {
93    public static final boolean DEBUG = false;
94    public static final boolean DEBUG_COMPAT_HELP = false;
95    public static final String TAG = "TabletStatusBar";
96
97
98    public static final int MSG_OPEN_NOTIFICATION_PANEL = 1000;
99    public static final int MSG_CLOSE_NOTIFICATION_PANEL = 1001;
100    public static final int MSG_OPEN_NOTIFICATION_PEEK = 1002;
101    public static final int MSG_CLOSE_NOTIFICATION_PEEK = 1003;
102    public static final int MSG_OPEN_RECENTS_PANEL = 1020;
103    public static final int MSG_CLOSE_RECENTS_PANEL = 1021;
104    public static final int MSG_SHOW_CHROME = 1030;
105    public static final int MSG_HIDE_CHROME = 1031;
106    public static final int MSG_OPEN_INPUT_METHODS_PANEL = 1040;
107    public static final int MSG_CLOSE_INPUT_METHODS_PANEL = 1041;
108    public static final int MSG_OPEN_COMPAT_MODE_PANEL = 1050;
109    public static final int MSG_CLOSE_COMPAT_MODE_PANEL = 1051;
110    public static final int MSG_STOP_TICKER = 2000;
111
112    // Fitts' Law assistance for LatinIME; see policy.EventHole
113    private static final boolean FAKE_SPACE_BAR = true;
114
115    // Notification "peeking" (flyover preview of individual notifications)
116    final static boolean NOTIFICATION_PEEK_ENABLED = false;
117    final static int NOTIFICATION_PEEK_HOLD_THRESH = 200; // ms
118    final static int NOTIFICATION_PEEK_FADE_DELAY = 3000; // ms
119
120    // The height of the bar, as definied by the build.  It may be taller if we're plugged
121    // into hdmi.
122    int mNaturalBarHeight = -1;
123    int mIconSize = -1;
124    int mIconHPadding = -1;
125    private int mMaxNotificationIcons = 5;
126
127    H mHandler = new H();
128
129    IWindowManager mWindowManager;
130
131    // tracking all current notifications
132    private NotificationData mNotificationData = new NotificationData();
133
134    TabletStatusBarView mStatusBarView;
135    View mNotificationArea;
136    View mNotificationTrigger;
137    NotificationIconArea mNotificationIconArea;
138    ViewGroup mNavigationArea;
139
140    boolean mNotificationDNDMode;
141    NotificationData.Entry mNotificationDNDDummyEntry;
142
143    ImageView mBackButton;
144    View mHomeButton;
145    View mMenuButton;
146    View mRecentButton;
147    private boolean mAltBackButtonEnabledForIme;
148
149    ViewGroup mFeedbackIconArea; // notification icons, IME icon, compat icon
150    InputMethodButton mInputMethodSwitchButton;
151    CompatModeButton mCompatModeButton;
152
153    NotificationPanel mNotificationPanel;
154    WindowManager.LayoutParams mNotificationPanelParams;
155    NotificationPeekPanel mNotificationPeekWindow;
156    ViewGroup mNotificationPeekRow;
157    int mNotificationPeekIndex;
158    IBinder mNotificationPeekKey;
159    LayoutTransition mNotificationPeekScrubLeft, mNotificationPeekScrubRight;
160
161    int mNotificationPeekTapDuration;
162    int mNotificationFlingVelocity;
163
164    ViewGroup mPile;
165
166    HeightReceiver mHeightReceiver;
167    BatteryController mBatteryController;
168    BluetoothController mBluetoothController;
169    LocationController mLocationController;
170    NetworkController mNetworkController;
171
172    ViewGroup mBarContents;
173
174    // hide system chrome ("lights out") support
175    View mShadow;
176
177    NotificationIconArea.IconLayout mIconLayout;
178
179    TabletTicker mTicker;
180
181    View mFakeSpaceBar;
182    KeyEvent mSpaceBarKeyEvent = null;
183
184    View mCompatibilityHelpDialog = null;
185
186    // for disabling the status bar
187    int mDisabled = 0;
188
189    private RecentsPanelView mRecentsPanel;
190    private RecentTasksLoader mRecentTasksLoader;
191    private InputMethodsPanel mInputMethodsPanel;
192    private CompatModePanel mCompatModePanel;
193
194    private int mSystemUiVisibility = 0;
195    // used to notify status bar for suppressing notification LED
196    private boolean mPanelSlightlyVisible;
197
198    private int mNavigationIconHints = 0;
199
200    public Context getContext() { return mContext; }
201
202    protected void addPanelWindows() {
203        final Context context = mContext;
204        final Resources res = mContext.getResources();
205
206        // Notification Panel
207        mNotificationPanel = (NotificationPanel)View.inflate(context,
208                R.layout.status_bar_notification_panel, null);
209        mNotificationPanel.setBar(this);
210        mNotificationPanel.show(false, false);
211        mNotificationPanel.setOnTouchListener(
212                new TouchOutsideListener(MSG_CLOSE_NOTIFICATION_PANEL, mNotificationPanel));
213
214        // the battery icon
215        mBatteryController.addIconView((ImageView)mNotificationPanel.findViewById(R.id.battery));
216        mBatteryController.addLabelView(
217                (TextView)mNotificationPanel.findViewById(R.id.battery_text));
218
219        // Bt
220        mBluetoothController.addIconView(
221                (ImageView)mNotificationPanel.findViewById(R.id.bluetooth));
222
223        // network icons: either a combo icon that switches between mobile and data, or distinct
224        // mobile and data icons
225        final ImageView mobileRSSI =
226                (ImageView)mNotificationPanel.findViewById(R.id.mobile_signal);
227        if (mobileRSSI != null) {
228            mNetworkController.addPhoneSignalIconView(mobileRSSI);
229        }
230        final ImageView wifiRSSI =
231                (ImageView)mNotificationPanel.findViewById(R.id.wifi_signal);
232        if (wifiRSSI != null) {
233            mNetworkController.addWifiIconView(wifiRSSI);
234        }
235        mNetworkController.addWifiLabelView(
236                (TextView)mNotificationPanel.findViewById(R.id.wifi_text));
237
238        mNetworkController.addDataTypeIconView(
239                (ImageView)mNotificationPanel.findViewById(R.id.mobile_type));
240        mNetworkController.addMobileLabelView(
241                (TextView)mNotificationPanel.findViewById(R.id.mobile_text));
242        mNetworkController.addCombinedLabelView(
243                (TextView)mBarContents.findViewById(R.id.network_text));
244
245        mStatusBarView.setIgnoreChildren(0, mNotificationTrigger, mNotificationPanel);
246
247        WindowManager.LayoutParams lp = mNotificationPanelParams = new WindowManager.LayoutParams(
248                res.getDimensionPixelSize(R.dimen.notification_panel_width),
249                getNotificationPanelHeight(),
250                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
251                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
252                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
253                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
254                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
255                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
256                PixelFormat.TRANSLUCENT);
257        lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
258        lp.setTitle("NotificationPanel");
259        lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED
260                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING;
261        lp.windowAnimations = com.android.internal.R.style.Animation; // == no animation
262//        lp.windowAnimations = com.android.internal.R.style.Animation_ZoomButtons; // simple fade
263
264        WindowManagerImpl.getDefault().addView(mNotificationPanel, lp);
265
266        // Notification preview window
267        if (NOTIFICATION_PEEK_ENABLED) {
268            mNotificationPeekWindow = (NotificationPeekPanel) View.inflate(context,
269                    R.layout.status_bar_notification_peek, null);
270            mNotificationPeekWindow.setBar(this);
271
272            mNotificationPeekRow = (ViewGroup) mNotificationPeekWindow.findViewById(R.id.content);
273            mNotificationPeekWindow.setVisibility(View.GONE);
274            mNotificationPeekWindow.setOnTouchListener(
275                    new TouchOutsideListener(MSG_CLOSE_NOTIFICATION_PEEK, mNotificationPeekWindow));
276            mNotificationPeekScrubRight = new LayoutTransition();
277            mNotificationPeekScrubRight.setAnimator(LayoutTransition.APPEARING,
278                    ObjectAnimator.ofInt(null, "left", -512, 0));
279            mNotificationPeekScrubRight.setAnimator(LayoutTransition.DISAPPEARING,
280                    ObjectAnimator.ofInt(null, "left", -512, 0));
281            mNotificationPeekScrubRight.setDuration(500);
282
283            mNotificationPeekScrubLeft = new LayoutTransition();
284            mNotificationPeekScrubLeft.setAnimator(LayoutTransition.APPEARING,
285                    ObjectAnimator.ofInt(null, "left", 512, 0));
286            mNotificationPeekScrubLeft.setAnimator(LayoutTransition.DISAPPEARING,
287                    ObjectAnimator.ofInt(null, "left", 512, 0));
288            mNotificationPeekScrubLeft.setDuration(500);
289
290            // XXX: setIgnoreChildren?
291            lp = new WindowManager.LayoutParams(
292                    512, // ViewGroup.LayoutParams.WRAP_CONTENT,
293                    ViewGroup.LayoutParams.WRAP_CONTENT,
294                    WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
295                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
296                        | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
297                        | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH,
298                    PixelFormat.TRANSLUCENT);
299            lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
300            lp.y = res.getDimensionPixelOffset(R.dimen.peek_window_y_offset);
301            lp.setTitle("NotificationPeekWindow");
302            lp.windowAnimations = com.android.internal.R.style.Animation_Toast;
303
304            WindowManagerImpl.getDefault().addView(mNotificationPeekWindow, lp);
305        }
306
307        // Recents Panel
308        mRecentTasksLoader = new RecentTasksLoader(context);
309        mRecentsPanel = (RecentsPanelView) View.inflate(context,
310                R.layout.status_bar_recent_panel, null);
311        mRecentsPanel.setVisibility(View.GONE);
312        mRecentsPanel.setOnTouchListener(new TouchOutsideListener(MSG_CLOSE_RECENTS_PANEL,
313                mRecentsPanel));
314        mRecentsPanel.setOnVisibilityChangedListener(this);
315        mRecentsPanel.setRecentTasksLoader(mRecentTasksLoader);
316        mRecentTasksLoader.setRecentsPanel(mRecentsPanel);
317
318        lp = new WindowManager.LayoutParams(
319                (int) res.getDimension(R.dimen.status_bar_recents_width),
320                ViewGroup.LayoutParams.MATCH_PARENT,
321                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
322                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
323                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
324                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
325                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
326                PixelFormat.TRANSLUCENT);
327        lp.gravity = Gravity.BOTTOM | Gravity.LEFT;
328        lp.setTitle("RecentsPanel");
329        lp.windowAnimations = R.style.Animation_RecentPanel;
330        lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED
331                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING;
332
333        WindowManagerImpl.getDefault().addView(mRecentsPanel, lp);
334        mRecentsPanel.setBar(this);
335        mRecentsPanel.setStatusBarView(mStatusBarView);
336
337        // Input methods Panel
338        mInputMethodsPanel = (InputMethodsPanel) View.inflate(context,
339                R.layout.status_bar_input_methods_panel, null);
340        mInputMethodsPanel.setHardKeyboardEnabledChangeListener(this);
341        mInputMethodsPanel.setOnTouchListener(new TouchOutsideListener(
342                MSG_CLOSE_INPUT_METHODS_PANEL, mInputMethodsPanel));
343        mInputMethodsPanel.setImeSwitchButton(mInputMethodSwitchButton);
344        mStatusBarView.setIgnoreChildren(2, mInputMethodSwitchButton, mInputMethodsPanel);
345        lp = new WindowManager.LayoutParams(
346                ViewGroup.LayoutParams.WRAP_CONTENT,
347                ViewGroup.LayoutParams.WRAP_CONTENT,
348                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
349                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
350                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
351                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
352                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
353                PixelFormat.TRANSLUCENT);
354        lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
355        lp.setTitle("InputMethodsPanel");
356        lp.windowAnimations = R.style.Animation_RecentPanel;
357
358        WindowManagerImpl.getDefault().addView(mInputMethodsPanel, lp);
359
360        // Compatibility mode selector panel
361        mCompatModePanel = (CompatModePanel) View.inflate(context,
362                R.layout.status_bar_compat_mode_panel, null);
363        mCompatModePanel.setOnTouchListener(new TouchOutsideListener(
364                MSG_CLOSE_COMPAT_MODE_PANEL, mCompatModePanel));
365        mCompatModePanel.setTrigger(mCompatModeButton);
366        mCompatModePanel.setVisibility(View.GONE);
367        mStatusBarView.setIgnoreChildren(3, mCompatModeButton, mCompatModePanel);
368        lp = new WindowManager.LayoutParams(
369                250,
370                ViewGroup.LayoutParams.WRAP_CONTENT,
371                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
372                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
373                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
374                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
375                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
376                PixelFormat.TRANSLUCENT);
377        lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
378        lp.setTitle("CompatModePanel");
379        lp.windowAnimations = android.R.style.Animation_Dialog;
380
381        WindowManagerImpl.getDefault().addView(mCompatModePanel, lp);
382    }
383
384    private int getNotificationPanelHeight() {
385        final Resources res = mContext.getResources();
386        final Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
387        final Point size = new Point();
388        d.getRealSize(size);
389        return Math.max(res.getDimensionPixelSize(R.dimen.notification_panel_min_height), size.y);
390    }
391
392    @Override
393    public void start() {
394        super.start(); // will add the main bar view
395    }
396
397    @Override
398    protected void onConfigurationChanged(Configuration newConfig) {
399        mHeightReceiver.updateHeight(); // display size may have changed
400        loadDimens();
401        mNotificationPanelParams.height = getNotificationPanelHeight();
402        WindowManagerImpl.getDefault().updateViewLayout(mNotificationPanel,
403                mNotificationPanelParams);
404        mRecentsPanel.updateValuesFromResources();
405    }
406
407    protected void loadDimens() {
408        final Resources res = mContext.getResources();
409
410        mNaturalBarHeight = res.getDimensionPixelSize(
411                com.android.internal.R.dimen.system_bar_height);
412
413        int newIconSize = res.getDimensionPixelSize(
414            com.android.internal.R.dimen.system_bar_icon_size);
415        int newIconHPadding = res.getDimensionPixelSize(
416            R.dimen.status_bar_icon_padding);
417
418        if (newIconHPadding != mIconHPadding || newIconSize != mIconSize) {
419//            Slog.d(TAG, "size=" + newIconSize + " padding=" + newIconHPadding);
420            mIconHPadding = newIconHPadding;
421            mIconSize = newIconSize;
422            reloadAllNotificationIcons(); // reload the tray
423        }
424
425        final int numIcons = res.getInteger(R.integer.config_maxNotificationIcons);
426        if (numIcons != mMaxNotificationIcons) {
427            mMaxNotificationIcons = numIcons;
428            if (DEBUG) Slog.d(TAG, "max notification icons: " + mMaxNotificationIcons);
429            reloadAllNotificationIcons();
430        }
431    }
432
433    public View getStatusBarView() {
434        return mStatusBarView;
435    }
436
437    protected View makeStatusBarView() {
438        final Context context = mContext;
439
440        mWindowManager = IWindowManager.Stub.asInterface(
441                ServiceManager.getService(Context.WINDOW_SERVICE));
442
443        // This guy will listen for HDMI plugged broadcasts so we can resize the
444        // status bar as appropriate.
445        mHeightReceiver = new HeightReceiver(mContext);
446        mHeightReceiver.registerReceiver();
447        loadDimens();
448
449        final TabletStatusBarView sb = (TabletStatusBarView)View.inflate(
450                context, R.layout.status_bar, null);
451        mStatusBarView = sb;
452
453        sb.setHandler(mHandler);
454
455        try {
456            // Sanity-check that someone hasn't set up the config wrong and asked for a navigation
457            // bar on a tablet that has only the system bar
458            if (mWindowManager.hasNavigationBar()) {
459                throw new RuntimeException(
460                        "Tablet device cannot show navigation bar and system bar");
461            }
462        } catch (RemoteException ex) {
463        }
464
465        mBarContents = (ViewGroup) sb.findViewById(R.id.bar_contents);
466
467        // the whole right-hand side of the bar
468        mNotificationArea = sb.findViewById(R.id.notificationArea);
469        if (!NOTIFICATION_PEEK_ENABLED) {
470            mNotificationArea.setOnTouchListener(new NotificationTriggerTouchListener());
471        }
472
473        // the button to open the notification area
474        mNotificationTrigger = sb.findViewById(R.id.notificationTrigger);
475        if (NOTIFICATION_PEEK_ENABLED) {
476            mNotificationTrigger.setOnTouchListener(new NotificationTriggerTouchListener());
477        }
478
479        // the more notifications icon
480        mNotificationIconArea = (NotificationIconArea)sb.findViewById(R.id.notificationIcons);
481
482        // where the icons go
483        mIconLayout = (NotificationIconArea.IconLayout) sb.findViewById(R.id.icons);
484        if (NOTIFICATION_PEEK_ENABLED) {
485            mIconLayout.setOnTouchListener(new NotificationIconTouchListener());
486        }
487
488        ViewConfiguration vc = ViewConfiguration.get(context);
489        mNotificationPeekTapDuration = vc.getTapTimeout();
490        mNotificationFlingVelocity = 300; // px/s
491
492        mTicker = new TabletTicker(this);
493
494        // The icons
495        mLocationController = new LocationController(mContext); // will post a notification
496
497        mBatteryController = new BatteryController(mContext);
498        mBatteryController.addIconView((ImageView)sb.findViewById(R.id.battery));
499        mBluetoothController = new BluetoothController(mContext);
500        mBluetoothController.addIconView((ImageView)sb.findViewById(R.id.bluetooth));
501
502        mNetworkController = new NetworkController(mContext);
503        final SignalClusterView signalCluster =
504                (SignalClusterView)sb.findViewById(R.id.signal_cluster);
505        mNetworkController.addSignalCluster(signalCluster);
506
507        // The navigation buttons
508        mBackButton = (ImageView)sb.findViewById(R.id.back);
509        mNavigationArea = (ViewGroup) sb.findViewById(R.id.navigationArea);
510        mHomeButton = mNavigationArea.findViewById(R.id.home);
511        mMenuButton = mNavigationArea.findViewById(R.id.menu);
512        mRecentButton = mNavigationArea.findViewById(R.id.recent_apps);
513        mRecentButton.setOnClickListener(mOnClickListener);
514
515        LayoutTransition lt = new LayoutTransition();
516        lt.setDuration(250);
517        // don't wait for these transitions; we just want icons to fade in/out, not move around
518        lt.setDuration(LayoutTransition.CHANGE_APPEARING, 0);
519        lt.setDuration(LayoutTransition.CHANGE_DISAPPEARING, 0);
520        lt.addTransitionListener(new LayoutTransition.TransitionListener() {
521            public void endTransition(LayoutTransition transition, ViewGroup container,
522                    View view, int transitionType) {
523                // ensure the menu button doesn't stick around on the status bar after it's been
524                // removed
525                mBarContents.invalidate();
526            }
527            public void startTransition(LayoutTransition transition, ViewGroup container,
528                    View view, int transitionType) {}
529        });
530        mNavigationArea.setLayoutTransition(lt);
531        // no multi-touch on the nav buttons
532        mNavigationArea.setMotionEventSplittingEnabled(false);
533
534        // The bar contents buttons
535        mFeedbackIconArea = (ViewGroup)sb.findViewById(R.id.feedbackIconArea);
536        mInputMethodSwitchButton = (InputMethodButton) sb.findViewById(R.id.imeSwitchButton);
537        // Overwrite the lister
538        mInputMethodSwitchButton.setOnClickListener(mOnClickListener);
539
540        mCompatModeButton = (CompatModeButton) sb.findViewById(R.id.compatModeButton);
541        mCompatModeButton.setOnClickListener(mOnClickListener);
542        mCompatModeButton.setVisibility(View.GONE);
543
544        // for redirecting errant bar taps to the IME
545        mFakeSpaceBar = sb.findViewById(R.id.fake_space_bar);
546
547        // "shadows" of the status bar features, for lights-out mode
548        mShadow = sb.findViewById(R.id.bar_shadow);
549        mShadow.setOnTouchListener(
550            new View.OnTouchListener() {
551                public boolean onTouch(View v, MotionEvent ev) {
552                    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
553                        // even though setting the systemUI visibility below will turn these views
554                        // on, we need them to come up faster so that they can catch this motion
555                        // event
556                        mShadow.setVisibility(View.GONE);
557                        mBarContents.setVisibility(View.VISIBLE);
558
559                        try {
560                            mBarService.setSystemUiVisibility(View.STATUS_BAR_VISIBLE);
561                        } catch (RemoteException ex) {
562                            // system process dead
563                        }
564                    }
565                    return false;
566                }
567            });
568
569        // tuning parameters
570        final int LIGHTS_GOING_OUT_SYSBAR_DURATION = 600;
571        final int LIGHTS_GOING_OUT_SHADOW_DURATION = 1000;
572        final int LIGHTS_GOING_OUT_SHADOW_DELAY    = 500;
573
574        final int LIGHTS_COMING_UP_SYSBAR_DURATION = 200;
575//        final int LIGHTS_COMING_UP_SYSBAR_DELAY    = 50;
576        final int LIGHTS_COMING_UP_SHADOW_DURATION = 0;
577
578        LayoutTransition xition = new LayoutTransition();
579        xition.setAnimator(LayoutTransition.APPEARING,
580               ObjectAnimator.ofFloat(null, "alpha", 0.5f, 1f));
581        xition.setDuration(LayoutTransition.APPEARING, LIGHTS_COMING_UP_SYSBAR_DURATION);
582        xition.setStartDelay(LayoutTransition.APPEARING, 0);
583        xition.setAnimator(LayoutTransition.DISAPPEARING,
584               ObjectAnimator.ofFloat(null, "alpha", 1f, 0f));
585        xition.setDuration(LayoutTransition.DISAPPEARING, LIGHTS_GOING_OUT_SYSBAR_DURATION);
586        xition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
587        ((ViewGroup)sb.findViewById(R.id.bar_contents_holder)).setLayoutTransition(xition);
588
589        xition = new LayoutTransition();
590        xition.setAnimator(LayoutTransition.APPEARING,
591               ObjectAnimator.ofFloat(null, "alpha", 0f, 1f));
592        xition.setDuration(LayoutTransition.APPEARING, LIGHTS_GOING_OUT_SHADOW_DURATION);
593        xition.setStartDelay(LayoutTransition.APPEARING, LIGHTS_GOING_OUT_SHADOW_DELAY);
594        xition.setAnimator(LayoutTransition.DISAPPEARING,
595               ObjectAnimator.ofFloat(null, "alpha", 1f, 0f));
596        xition.setDuration(LayoutTransition.DISAPPEARING, LIGHTS_COMING_UP_SHADOW_DURATION);
597        xition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
598        ((ViewGroup)sb.findViewById(R.id.bar_shadow_holder)).setLayoutTransition(xition);
599
600        // set the initial view visibility
601        setAreThereNotifications();
602
603        // Add the windows
604        addPanelWindows();
605        mRecentButton.setOnTouchListener(mRecentsPanel);
606
607        mPile = (ViewGroup)mNotificationPanel.findViewById(R.id.content);
608        mPile.removeAllViews();
609
610        ScrollView scroller = (ScrollView)mPile.getParent();
611        scroller.setFillViewport(true);
612
613        mHeightReceiver.addOnBarHeightChangedListener(this);
614
615        // receive broadcasts
616        IntentFilter filter = new IntentFilter();
617        filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
618        filter.addAction(Intent.ACTION_SCREEN_OFF);
619        context.registerReceiver(mBroadcastReceiver, filter);
620
621        return sb;
622    }
623
624    public int getStatusBarHeight() {
625        return mHeightReceiver.getHeight();
626    }
627
628    protected int getStatusBarGravity() {
629        return Gravity.BOTTOM | Gravity.FILL_HORIZONTAL;
630    }
631
632    public void onBarHeightChanged(int height) {
633        final WindowManager.LayoutParams lp
634                = (WindowManager.LayoutParams)mStatusBarView.getLayoutParams();
635        if (lp == null) {
636            // haven't been added yet
637            return;
638        }
639        if (lp.height != height) {
640            lp.height = height;
641            final WindowManager wm = WindowManagerImpl.getDefault();
642            wm.updateViewLayout(mStatusBarView, lp);
643        }
644    }
645
646    private class H extends Handler {
647        public void handleMessage(Message m) {
648            switch (m.what) {
649                case MSG_OPEN_NOTIFICATION_PEEK:
650                    if (DEBUG) Slog.d(TAG, "opening notification peek window; arg=" + m.arg1);
651
652                    if (m.arg1 >= 0) {
653                        final int N = mNotificationData.size();
654
655                        if (!mNotificationDNDMode) {
656                            if (mNotificationPeekIndex >= 0 && mNotificationPeekIndex < N) {
657                                NotificationData.Entry entry = mNotificationData.get(N-1-mNotificationPeekIndex);
658                                entry.icon.setBackgroundColor(0);
659                                mNotificationPeekIndex = -1;
660                                mNotificationPeekKey = null;
661                            }
662                        }
663
664                        final int peekIndex = m.arg1;
665                        if (peekIndex < N) {
666                            //Slog.d(TAG, "loading peek: " + peekIndex);
667                            NotificationData.Entry entry =
668                                mNotificationDNDMode
669                                    ? mNotificationDNDDummyEntry
670                                    : mNotificationData.get(N-1-peekIndex);
671                            NotificationData.Entry copy = new NotificationData.Entry(
672                                    entry.key,
673                                    entry.notification,
674                                    entry.icon);
675                            inflateViews(copy, mNotificationPeekRow);
676
677                            if (mNotificationDNDMode) {
678                                copy.content.setOnClickListener(new View.OnClickListener() {
679                                    public void onClick(View v) {
680                                        SharedPreferences.Editor editor = Prefs.edit(mContext);
681                                        editor.putBoolean(Prefs.DO_NOT_DISTURB_PREF, false);
682                                        editor.apply();
683                                        animateCollapse();
684                                        visibilityChanged(false);
685                                    }
686                                });
687                            }
688
689                            entry.icon.setBackgroundColor(0x20FFFFFF);
690
691//                          mNotificationPeekRow.setLayoutTransition(
692//                              peekIndex < mNotificationPeekIndex
693//                                  ? mNotificationPeekScrubLeft
694//                                  : mNotificationPeekScrubRight);
695
696                            mNotificationPeekRow.removeAllViews();
697                            mNotificationPeekRow.addView(copy.row);
698
699                            mNotificationPeekWindow.setVisibility(View.VISIBLE);
700                            mNotificationPanel.show(false, true);
701
702                            mNotificationPeekIndex = peekIndex;
703                            mNotificationPeekKey = entry.key;
704                        }
705                    }
706                    break;
707                case MSG_CLOSE_NOTIFICATION_PEEK:
708                    if (DEBUG) Slog.d(TAG, "closing notification peek window");
709                    mNotificationPeekWindow.setVisibility(View.GONE);
710                    mNotificationPeekRow.removeAllViews();
711
712                    final int N = mNotificationData.size();
713                    if (mNotificationPeekIndex >= 0 && mNotificationPeekIndex < N) {
714                        NotificationData.Entry entry =
715                            mNotificationDNDMode
716                                ? mNotificationDNDDummyEntry
717                                : mNotificationData.get(N-1-mNotificationPeekIndex);
718                        entry.icon.setBackgroundColor(0);
719                    }
720
721                    mNotificationPeekIndex = -1;
722                    mNotificationPeekKey = null;
723                    break;
724                case MSG_OPEN_NOTIFICATION_PANEL:
725                    if (DEBUG) Slog.d(TAG, "opening notifications panel");
726                    if (!mNotificationPanel.isShowing()) {
727                        if (NOTIFICATION_PEEK_ENABLED) {
728                            mNotificationPeekWindow.setVisibility(View.GONE);
729                        }
730                        mNotificationPanel.show(true, true);
731                        mNotificationArea.setVisibility(View.INVISIBLE);
732                        mTicker.halt();
733                    }
734                    break;
735                case MSG_CLOSE_NOTIFICATION_PANEL:
736                    if (DEBUG) Slog.d(TAG, "closing notifications panel");
737                    if (mNotificationPanel.isShowing()) {
738                        mNotificationPanel.show(false, true);
739                        mNotificationArea.setVisibility(View.VISIBLE);
740                    }
741                    break;
742                case MSG_OPEN_RECENTS_PANEL:
743                    if (DEBUG) Slog.d(TAG, "opening recents panel");
744                    if (mRecentsPanel != null) {
745                        mRecentsPanel.show(true, true);
746                    }
747                    break;
748                case MSG_CLOSE_RECENTS_PANEL:
749                    if (DEBUG) Slog.d(TAG, "closing recents panel");
750                    if (mRecentsPanel != null && mRecentsPanel.isShowing()) {
751                        mRecentsPanel.show(false, true);
752                    }
753                    break;
754                case MSG_OPEN_INPUT_METHODS_PANEL:
755                    if (DEBUG) Slog.d(TAG, "opening input methods panel");
756                    if (mInputMethodsPanel != null) mInputMethodsPanel.openPanel();
757                    break;
758                case MSG_CLOSE_INPUT_METHODS_PANEL:
759                    if (DEBUG) Slog.d(TAG, "closing input methods panel");
760                    if (mInputMethodsPanel != null) mInputMethodsPanel.closePanel(false);
761                    break;
762                case MSG_OPEN_COMPAT_MODE_PANEL:
763                    if (DEBUG) Slog.d(TAG, "opening compat panel");
764                    if (mCompatModePanel != null) mCompatModePanel.openPanel();
765                    break;
766                case MSG_CLOSE_COMPAT_MODE_PANEL:
767                    if (DEBUG) Slog.d(TAG, "closing compat panel");
768                    if (mCompatModePanel != null) mCompatModePanel.closePanel();
769                    break;
770                case MSG_SHOW_CHROME:
771                    if (DEBUG) Slog.d(TAG, "hiding shadows (lights on)");
772                    mBarContents.setVisibility(View.VISIBLE);
773                    mShadow.setVisibility(View.GONE);
774                    mSystemUiVisibility &= ~View.SYSTEM_UI_FLAG_LOW_PROFILE;
775                    notifyUiVisibilityChanged();
776                    break;
777                case MSG_HIDE_CHROME:
778                    if (DEBUG) Slog.d(TAG, "showing shadows (lights out)");
779                    animateCollapse();
780                    visibilityChanged(false);
781                    mBarContents.setVisibility(View.GONE);
782                    mShadow.setVisibility(View.VISIBLE);
783                    mSystemUiVisibility |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
784                    notifyUiVisibilityChanged();
785                    break;
786                case MSG_STOP_TICKER:
787                    mTicker.halt();
788                    break;
789            }
790        }
791    }
792
793    public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon) {
794        if (DEBUG) Slog.d(TAG, "addIcon(" + slot + ") -> " + icon);
795    }
796
797    public void updateIcon(String slot, int index, int viewIndex,
798            StatusBarIcon old, StatusBarIcon icon) {
799        if (DEBUG) Slog.d(TAG, "updateIcon(" + slot + ") -> " + icon);
800    }
801
802    public void removeIcon(String slot, int index, int viewIndex) {
803        if (DEBUG) Slog.d(TAG, "removeIcon(" + slot + ")");
804    }
805
806    public void addNotification(IBinder key, StatusBarNotification notification) {
807        if (DEBUG) Slog.d(TAG, "addNotification(" + key + " -> " + notification + ")");
808        addNotificationViews(key, notification);
809
810        final boolean immersive = isImmersive();
811        if (false && immersive) {
812            // TODO: immersive mode popups for tablet
813        } else if (notification.notification.fullScreenIntent != null) {
814            // not immersive & a full-screen alert should be shown
815            Slog.w(TAG, "Notification has fullScreenIntent and activity is not immersive;"
816                    + " sending fullScreenIntent");
817            try {
818                notification.notification.fullScreenIntent.send();
819            } catch (PendingIntent.CanceledException e) {
820            }
821        } else {
822            tick(key, notification, true);
823        }
824
825        setAreThereNotifications();
826    }
827
828    public void updateNotification(IBinder key, StatusBarNotification notification) {
829        if (DEBUG) Slog.d(TAG, "updateNotification(" + key + " -> " + notification + ")");
830
831        final NotificationData.Entry oldEntry = mNotificationData.findByKey(key);
832        if (oldEntry == null) {
833            Slog.w(TAG, "updateNotification for unknown key: " + key);
834            return;
835        }
836
837        final StatusBarNotification oldNotification = oldEntry.notification;
838        final RemoteViews oldContentView = oldNotification.notification.contentView;
839
840        final RemoteViews contentView = notification.notification.contentView;
841
842        if (DEBUG) {
843            Slog.d(TAG, "old notification: when=" + oldNotification.notification.when
844                    + " ongoing=" + oldNotification.isOngoing()
845                    + " expanded=" + oldEntry.expanded
846                    + " contentView=" + oldContentView
847                    + " rowParent=" + oldEntry.row.getParent());
848            Slog.d(TAG, "new notification: when=" + notification.notification.when
849                    + " ongoing=" + oldNotification.isOngoing()
850                    + " contentView=" + contentView);
851        }
852
853        // Can we just reapply the RemoteViews in place?  If when didn't change, the order
854        // didn't change.
855        boolean contentsUnchanged = oldEntry.expanded != null
856                && contentView != null && oldContentView != null
857                && contentView.getPackage() != null
858                && oldContentView.getPackage() != null
859                && oldContentView.getPackage().equals(contentView.getPackage())
860                && oldContentView.getLayoutId() == contentView.getLayoutId();
861        ViewGroup rowParent = (ViewGroup) oldEntry.row.getParent();
862        boolean orderUnchanged = notification.notification.when==oldNotification.notification.when
863                && notification.score == oldNotification.score;
864                // score now encompasses/supersedes isOngoing()
865        boolean updateTicker = notification.notification.tickerText != null
866                && !TextUtils.equals(notification.notification.tickerText,
867                        oldEntry.notification.notification.tickerText);
868        boolean isLastAnyway = rowParent.indexOfChild(oldEntry.row) == rowParent.getChildCount()-1;
869        if (contentsUnchanged && (orderUnchanged || isLastAnyway)) {
870            if (DEBUG) Slog.d(TAG, "reusing notification for key: " + key);
871            oldEntry.notification = notification;
872            try {
873                // Reapply the RemoteViews
874                contentView.reapply(mContext, oldEntry.content);
875                // update the contentIntent
876                final PendingIntent contentIntent = notification.notification.contentIntent;
877                if (contentIntent != null) {
878                    final View.OnClickListener listener = new NotificationClicker(contentIntent,
879                            notification.pkg, notification.tag, notification.id);
880                    oldEntry.largeIcon.setOnClickListener(listener);
881                    oldEntry.content.setOnClickListener(listener);
882                } else {
883                    oldEntry.largeIcon.setOnClickListener(null);
884                    oldEntry.content.setOnClickListener(null);
885                }
886                // Update the icon.
887                final StatusBarIcon ic = new StatusBarIcon(notification.pkg,
888                        notification.notification.icon, notification.notification.iconLevel,
889                        notification.notification.number,
890                        notification.notification.tickerText);
891                if (!oldEntry.icon.set(ic)) {
892                    handleNotificationError(key, notification, "Couldn't update icon: " + ic);
893                    return;
894                }
895                // Update the large icon
896                if (notification.notification.largeIcon != null) {
897                    oldEntry.largeIcon.setImageBitmap(notification.notification.largeIcon);
898                } else {
899                    oldEntry.largeIcon.getLayoutParams().width = 0;
900                    oldEntry.largeIcon.setVisibility(View.INVISIBLE);
901                }
902
903                if (NOTIFICATION_PEEK_ENABLED && key == mNotificationPeekKey) {
904                    // must update the peek window
905                    Message peekMsg = mHandler.obtainMessage(MSG_OPEN_NOTIFICATION_PEEK);
906                    peekMsg.arg1 = mNotificationPeekIndex;
907                    mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
908                    mHandler.sendMessage(peekMsg);
909                }
910            }
911            catch (RuntimeException e) {
912                // It failed to add cleanly.  Log, and remove the view from the panel.
913                Slog.w(TAG, "Couldn't reapply views for package " + contentView.getPackage(), e);
914                removeNotificationViews(key);
915                addNotificationViews(key, notification);
916            }
917        } else {
918            if (DEBUG) Slog.d(TAG, "not reusing notification for key: " + key);
919            removeNotificationViews(key);
920            addNotificationViews(key, notification);
921        }
922
923        // Restart the ticker if it's still running
924        if (updateTicker) {
925            mTicker.halt();
926            tick(key, notification, false);
927        }
928
929        setAreThereNotifications();
930    }
931
932    public void removeNotification(IBinder key) {
933        if (DEBUG) Slog.d(TAG, "removeNotification(" + key + ")");
934        removeNotificationViews(key);
935        mTicker.remove(key);
936        setAreThereNotifications();
937    }
938
939    public void showClock(boolean show) {
940        View clock = mBarContents.findViewById(R.id.clock);
941        View network_text = mBarContents.findViewById(R.id.network_text);
942        if (clock != null) {
943            clock.setVisibility(show ? View.VISIBLE : View.GONE);
944        }
945        if (network_text != null) {
946            network_text.setVisibility((!show) ? View.VISIBLE : View.GONE);
947        }
948    }
949
950    public void disable(int state) {
951        int old = mDisabled;
952        int diff = state ^ old;
953        mDisabled = state;
954
955        // act accordingly
956        if ((diff & StatusBarManager.DISABLE_CLOCK) != 0) {
957            boolean show = (state & StatusBarManager.DISABLE_CLOCK) == 0;
958            Slog.i(TAG, "DISABLE_CLOCK: " + (show ? "no" : "yes"));
959            showClock(show);
960        }
961        if ((diff & StatusBarManager.DISABLE_SYSTEM_INFO) != 0) {
962            boolean show = (state & StatusBarManager.DISABLE_SYSTEM_INFO) == 0;
963            Slog.i(TAG, "DISABLE_SYSTEM_INFO: " + (show ? "no" : "yes"));
964            mNotificationTrigger.setVisibility(show ? View.VISIBLE : View.GONE);
965        }
966        if ((diff & StatusBarManager.DISABLE_EXPAND) != 0) {
967            if ((state & StatusBarManager.DISABLE_EXPAND) != 0) {
968                Slog.i(TAG, "DISABLE_EXPAND: yes");
969                animateCollapse();
970                visibilityChanged(false);
971            }
972        }
973        if ((diff & StatusBarManager.DISABLE_NOTIFICATION_ICONS) != 0) {
974            mNotificationDNDMode = Prefs.read(mContext)
975                        .getBoolean(Prefs.DO_NOT_DISTURB_PREF, Prefs.DO_NOT_DISTURB_DEFAULT);
976
977            if ((state & StatusBarManager.DISABLE_NOTIFICATION_ICONS) != 0) {
978                Slog.i(TAG, "DISABLE_NOTIFICATION_ICONS: yes" + (mNotificationDNDMode?" (DND)":""));
979                mTicker.halt();
980            } else {
981                Slog.i(TAG, "DISABLE_NOTIFICATION_ICONS: no" + (mNotificationDNDMode?" (DND)":""));
982            }
983
984            // refresh icons to show either notifications or the DND message
985            reloadAllNotificationIcons();
986        } else if ((diff & StatusBarManager.DISABLE_NOTIFICATION_TICKER) != 0) {
987            if ((state & StatusBarManager.DISABLE_NOTIFICATION_TICKER) != 0) {
988                mTicker.halt();
989            }
990        }
991        if ((diff & (StatusBarManager.DISABLE_RECENT
992                        | StatusBarManager.DISABLE_BACK
993                        | StatusBarManager.DISABLE_HOME)) != 0) {
994            setNavigationVisibility(state);
995
996            if ((state & StatusBarManager.DISABLE_RECENT) != 0) {
997                // close recents if it's visible
998                mHandler.removeMessages(MSG_CLOSE_RECENTS_PANEL);
999                mHandler.sendEmptyMessage(MSG_CLOSE_RECENTS_PANEL);
1000            }
1001        }
1002    }
1003
1004    private void setNavigationVisibility(int visibility) {
1005        boolean disableHome = ((visibility & StatusBarManager.DISABLE_HOME) != 0);
1006        boolean disableRecent = ((visibility & StatusBarManager.DISABLE_RECENT) != 0);
1007        boolean disableBack = ((visibility & StatusBarManager.DISABLE_BACK) != 0);
1008
1009        mBackButton.setVisibility(disableBack ? View.INVISIBLE : View.VISIBLE);
1010        mHomeButton.setVisibility(disableHome ? View.INVISIBLE : View.VISIBLE);
1011        mRecentButton.setVisibility(disableRecent ? View.INVISIBLE : View.VISIBLE);
1012
1013        mInputMethodSwitchButton.setScreenLocked(
1014                (visibility & StatusBarManager.DISABLE_SYSTEM_INFO) != 0);
1015    }
1016
1017    private boolean hasTicker(Notification n) {
1018        return n.tickerView != null || !TextUtils.isEmpty(n.tickerText);
1019    }
1020
1021    private void tick(IBinder key, StatusBarNotification n, boolean firstTime) {
1022        // Don't show the ticker when the windowshade is open.
1023        if (mNotificationPanel.isShowing()) {
1024            return;
1025        }
1026        // If they asked for FLAG_ONLY_ALERT_ONCE, then only show this notification
1027        // if it's a new notification.
1028        if (!firstTime && (n.notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) {
1029            return;
1030        }
1031        // Show the ticker if one is requested. Also don't do this
1032        // until status bar window is attached to the window manager,
1033        // because...  well, what's the point otherwise?  And trying to
1034        // run a ticker without being attached will crash!
1035        if (hasTicker(n.notification) && mStatusBarView.getWindowToken() != null) {
1036            if (0 == (mDisabled & (StatusBarManager.DISABLE_NOTIFICATION_ICONS
1037                            | StatusBarManager.DISABLE_NOTIFICATION_TICKER))) {
1038                mTicker.add(key, n);
1039                mFeedbackIconArea.setVisibility(View.GONE);
1040            }
1041        }
1042    }
1043
1044    // called by TabletTicker when it's done with all queued ticks
1045    public void doneTicking() {
1046        mFeedbackIconArea.setVisibility(View.VISIBLE);
1047    }
1048
1049    public void animateExpand() {
1050        if (NOTIFICATION_PEEK_ENABLED) {
1051            mHandler.removeMessages(MSG_CLOSE_NOTIFICATION_PEEK);
1052            mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
1053            mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PEEK);
1054        }
1055        mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PANEL);
1056        mHandler.sendEmptyMessage(MSG_OPEN_NOTIFICATION_PANEL);
1057    }
1058
1059    public void animateCollapse() {
1060        animateCollapse(false);
1061    }
1062
1063    private void animateCollapse(boolean excludeRecents) {
1064        mHandler.removeMessages(MSG_CLOSE_NOTIFICATION_PANEL);
1065        mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PANEL);
1066        if (!excludeRecents) {
1067            mHandler.removeMessages(MSG_CLOSE_RECENTS_PANEL);
1068            mHandler.sendEmptyMessage(MSG_CLOSE_RECENTS_PANEL);
1069        }
1070        mHandler.removeMessages(MSG_CLOSE_INPUT_METHODS_PANEL);
1071        mHandler.sendEmptyMessage(MSG_CLOSE_INPUT_METHODS_PANEL);
1072        mHandler.removeMessages(MSG_CLOSE_COMPAT_MODE_PANEL);
1073        mHandler.sendEmptyMessage(MSG_CLOSE_COMPAT_MODE_PANEL);
1074        if (NOTIFICATION_PEEK_ENABLED) {
1075            mHandler.removeMessages(MSG_CLOSE_NOTIFICATION_PEEK);
1076            mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PEEK);
1077        }
1078    }
1079
1080    /**
1081     * The LEDs are turned o)ff when the notification panel is shown, even just a little bit.
1082     * This was added last-minute and is inconsistent with the way the rest of the notifications
1083     * are handled, because the notification isn't really cancelled.  The lights are just
1084     * turned off.  If any other notifications happen, the lights will turn back on.  Steve says
1085     * this is what he wants. (see bug 1131461)
1086     */
1087    void visibilityChanged(boolean visible) {
1088        if (mPanelSlightlyVisible != visible) {
1089            mPanelSlightlyVisible = visible;
1090            try {
1091                mBarService.onPanelRevealed();
1092            } catch (RemoteException ex) {
1093                // Won't fail unless the world has ended.
1094            }
1095        }
1096    }
1097
1098    @Override // CommandQueue
1099    public void setNavigationIconHints(int hints) {
1100        if (hints == mNavigationIconHints) return;
1101
1102        if (DEBUG) {
1103            android.widget.Toast.makeText(mContext,
1104                "Navigation icon hints = " + hints,
1105                500).show();
1106        }
1107
1108        mNavigationIconHints = hints;
1109
1110        mBackButton.setAlpha(
1111            (0 != (hints & StatusBarManager.NAVIGATION_HINT_BACK_NOP)) ? 0.5f : 1.0f);
1112        mHomeButton.setAlpha(
1113            (0 != (hints & StatusBarManager.NAVIGATION_HINT_HOME_NOP)) ? 0.5f : 1.0f);
1114        mRecentButton.setAlpha(
1115            (0 != (hints & StatusBarManager.NAVIGATION_HINT_RECENT_NOP)) ? 0.5f : 1.0f);
1116
1117        mBackButton.setImageResource(
1118            (0 != (hints & StatusBarManager.NAVIGATION_HINT_BACK_ALT))
1119                ? R.drawable.ic_sysbar_back_ime
1120                : R.drawable.ic_sysbar_back);
1121    }
1122
1123    private void notifyUiVisibilityChanged() {
1124        try {
1125            mWindowManager.statusBarVisibilityChanged(mSystemUiVisibility);
1126        } catch (RemoteException ex) {
1127        }
1128    }
1129
1130    @Override // CommandQueue
1131    public void setSystemUiVisibility(int vis) {
1132        if (vis != mSystemUiVisibility) {
1133            mSystemUiVisibility = vis;
1134
1135            mHandler.removeMessages(MSG_HIDE_CHROME);
1136            mHandler.removeMessages(MSG_SHOW_CHROME);
1137            mHandler.sendEmptyMessage(0 == (vis & View.SYSTEM_UI_FLAG_LOW_PROFILE)
1138                    ? MSG_SHOW_CHROME : MSG_HIDE_CHROME);
1139
1140            notifyUiVisibilityChanged();
1141        }
1142    }
1143
1144    public void setLightsOn(boolean on) {
1145        // Policy note: if the frontmost activity needs the menu key, we assume it is a legacy app
1146        // that can't handle lights-out mode.
1147        if (mMenuButton.getVisibility() == View.VISIBLE) {
1148            on = true;
1149        }
1150
1151        Slog.v(TAG, "setLightsOn(" + on + ")");
1152        if (on) {
1153            setSystemUiVisibility(mSystemUiVisibility & ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
1154        } else {
1155            setSystemUiVisibility(mSystemUiVisibility | View.SYSTEM_UI_FLAG_LOW_PROFILE);
1156        }
1157    }
1158
1159    public void topAppWindowChanged(boolean showMenu) {
1160        if (DEBUG) {
1161            Slog.d(TAG, (showMenu?"showing":"hiding") + " the MENU button");
1162        }
1163        mMenuButton.setVisibility(showMenu ? View.VISIBLE : View.GONE);
1164
1165        // See above re: lights-out policy for legacy apps.
1166        if (showMenu) setLightsOn(true);
1167
1168        mCompatModeButton.refresh();
1169        if (mCompatModeButton.getVisibility() == View.VISIBLE) {
1170            if (DEBUG_COMPAT_HELP
1171                    || ! Prefs.read(mContext).getBoolean(Prefs.SHOWN_COMPAT_MODE_HELP, false)) {
1172                showCompatibilityHelp();
1173            }
1174        } else {
1175            hideCompatibilityHelp();
1176            mCompatModePanel.closePanel();
1177        }
1178    }
1179
1180    private void showCompatibilityHelp() {
1181        if (mCompatibilityHelpDialog != null) {
1182            return;
1183        }
1184
1185        mCompatibilityHelpDialog = View.inflate(mContext, R.layout.compat_mode_help, null);
1186        View button = mCompatibilityHelpDialog.findViewById(R.id.button);
1187
1188        button.setOnClickListener(new View.OnClickListener() {
1189            @Override
1190            public void onClick(View v) {
1191                hideCompatibilityHelp();
1192                SharedPreferences.Editor editor = Prefs.edit(mContext);
1193                editor.putBoolean(Prefs.SHOWN_COMPAT_MODE_HELP, true);
1194                editor.apply();
1195            }
1196        });
1197
1198        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
1199                ViewGroup.LayoutParams.MATCH_PARENT,
1200                ViewGroup.LayoutParams.MATCH_PARENT,
1201                WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG,
1202                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
1203                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
1204                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
1205                PixelFormat.TRANSLUCENT);
1206        lp.setTitle("CompatibilityModeDialog");
1207        lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED
1208                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING;
1209        lp.windowAnimations = com.android.internal.R.style.Animation_ZoomButtons; // simple fade
1210
1211        WindowManagerImpl.getDefault().addView(mCompatibilityHelpDialog, lp);
1212    }
1213
1214    private void hideCompatibilityHelp() {
1215        if (mCompatibilityHelpDialog != null) {
1216            WindowManagerImpl.getDefault().removeView(mCompatibilityHelpDialog);
1217            mCompatibilityHelpDialog = null;
1218        }
1219    }
1220
1221    public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
1222        mInputMethodSwitchButton.setImeWindowStatus(token,
1223                (vis & InputMethodService.IME_ACTIVE) != 0);
1224        updateNotificationIcons();
1225        mInputMethodsPanel.setImeToken(token);
1226
1227        boolean altBack = (backDisposition == InputMethodService.BACK_DISPOSITION_WILL_DISMISS)
1228            || ((vis & InputMethodService.IME_VISIBLE) != 0);
1229        mAltBackButtonEnabledForIme = altBack;
1230
1231        mCommandQueue.setNavigationIconHints(
1232                altBack ? (mNavigationIconHints | StatusBarManager.NAVIGATION_HINT_BACK_ALT)
1233                        : (mNavigationIconHints & ~StatusBarManager.NAVIGATION_HINT_BACK_ALT));
1234
1235        if (FAKE_SPACE_BAR) {
1236            mFakeSpaceBar.setVisibility(((vis & InputMethodService.IME_VISIBLE) != 0)
1237                    ? View.VISIBLE : View.GONE);
1238        }
1239    }
1240
1241    @Override
1242    public void onRecentsPanelVisibilityChanged(boolean visible) {
1243        boolean altBack = visible || mAltBackButtonEnabledForIme;
1244        mCommandQueue.setNavigationIconHints(
1245                altBack ? (mNavigationIconHints | StatusBarManager.NAVIGATION_HINT_BACK_ALT)
1246                        : (mNavigationIconHints & ~StatusBarManager.NAVIGATION_HINT_BACK_ALT));
1247    }
1248
1249    @Override
1250    public void setHardKeyboardStatus(boolean available, boolean enabled) {
1251        if (DEBUG) {
1252            Slog.d(TAG, "Set hard keyboard status: available=" + available
1253                    + ", enabled=" + enabled);
1254        }
1255        mInputMethodSwitchButton.setHardKeyboardStatus(available);
1256        updateNotificationIcons();
1257        mInputMethodsPanel.setHardKeyboardStatus(available, enabled);
1258    }
1259
1260    @Override
1261    public void onHardKeyboardEnabledChange(boolean enabled) {
1262        try {
1263            mBarService.setHardKeyboardEnabled(enabled);
1264        } catch (RemoteException ex) {
1265        }
1266    }
1267
1268    private boolean isImmersive() {
1269        try {
1270            return ActivityManagerNative.getDefault().isTopActivityImmersive();
1271            //Slog.d(TAG, "Top activity is " + (immersive?"immersive":"not immersive"));
1272        } catch (RemoteException ex) {
1273            // the end is nigh
1274            return false;
1275        }
1276    }
1277
1278    private void setAreThereNotifications() {
1279        if (mNotificationPanel != null) {
1280            mNotificationPanel.setClearable(mNotificationData.hasClearableItems());
1281        }
1282    }
1283
1284    /**
1285     * Cancel this notification and tell the status bar service about the failure. Hold no locks.
1286     */
1287    void handleNotificationError(IBinder key, StatusBarNotification n, String message) {
1288        removeNotification(key);
1289        try {
1290            mBarService.onNotificationError(n.pkg, n.tag, n.id, n.uid, n.initialPid, message);
1291        } catch (RemoteException ex) {
1292            // The end is nigh.
1293        }
1294    }
1295
1296    private void sendKey(KeyEvent key) {
1297        try {
1298            if (DEBUG) Slog.d(TAG, "injecting key event: " + key);
1299            mWindowManager.injectInputEventNoWait(key);
1300        } catch (RemoteException ex) {
1301        }
1302    }
1303
1304    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
1305        public void onClick(View v) {
1306            if (v == mRecentButton) {
1307                onClickRecentButton();
1308            } else if (v == mInputMethodSwitchButton) {
1309                onClickInputMethodSwitchButton();
1310            } else if (v == mCompatModeButton) {
1311                onClickCompatModeButton();
1312            }
1313        }
1314    };
1315
1316    public void onClickRecentButton() {
1317        if (DEBUG) Slog.d(TAG, "clicked recent apps; disabled=" + mDisabled);
1318        if ((mDisabled & StatusBarManager.DISABLE_EXPAND) == 0) {
1319            int msg = (mRecentsPanel.getVisibility() == View.VISIBLE)
1320                ? MSG_CLOSE_RECENTS_PANEL : MSG_OPEN_RECENTS_PANEL;
1321            mHandler.removeMessages(msg);
1322            mHandler.sendEmptyMessage(msg);
1323        }
1324    }
1325
1326    public void onClickInputMethodSwitchButton() {
1327        if (DEBUG) Slog.d(TAG, "clicked input methods panel; disabled=" + mDisabled);
1328        int msg = (mInputMethodsPanel.getVisibility() == View.GONE) ?
1329                MSG_OPEN_INPUT_METHODS_PANEL : MSG_CLOSE_INPUT_METHODS_PANEL;
1330        mHandler.removeMessages(msg);
1331        mHandler.sendEmptyMessage(msg);
1332    }
1333
1334    public void onClickCompatModeButton() {
1335        int msg = (mCompatModePanel.getVisibility() == View.GONE) ?
1336                MSG_OPEN_COMPAT_MODE_PANEL : MSG_CLOSE_COMPAT_MODE_PANEL;
1337        mHandler.removeMessages(msg);
1338        mHandler.sendEmptyMessage(msg);
1339    }
1340
1341    public NotificationClicker makeClicker(PendingIntent intent, String pkg, String tag, int id) {
1342        return new NotificationClicker(intent, pkg, tag, id);
1343    }
1344
1345    private class NotificationClicker implements View.OnClickListener {
1346        private PendingIntent mIntent;
1347        private String mPkg;
1348        private String mTag;
1349        private int mId;
1350
1351        NotificationClicker(PendingIntent intent, String pkg, String tag, int id) {
1352            mIntent = intent;
1353            mPkg = pkg;
1354            mTag = tag;
1355            mId = id;
1356        }
1357
1358        public void onClick(View v) {
1359            try {
1360                // The intent we are sending is for the application, which
1361                // won't have permission to immediately start an activity after
1362                // the user switches to home.  We know it is safe to do at this
1363                // point, so make sure new activity switches are now allowed.
1364                ActivityManagerNative.getDefault().resumeAppSwitches();
1365                // Also, notifications can be launched from the lock screen,
1366                // so dismiss the lock screen when the activity starts.
1367                ActivityManagerNative.getDefault().dismissKeyguardOnNextActivity();
1368            } catch (RemoteException e) {
1369            }
1370
1371            if (mIntent != null) {
1372                int[] pos = new int[2];
1373                v.getLocationOnScreen(pos);
1374                Intent overlay = new Intent();
1375                overlay.setSourceBounds(
1376                        new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
1377                try {
1378                    mIntent.send(mContext, 0, overlay);
1379
1380                } catch (PendingIntent.CanceledException e) {
1381                    // the stack trace isn't very helpful here.  Just log the exception message.
1382                    Slog.w(TAG, "Sending contentIntent failed: " + e);
1383                }
1384
1385                KeyguardManager kgm =
1386                    (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
1387                if (kgm != null) kgm.exitKeyguardSecurely(null);
1388            }
1389
1390            try {
1391                mBarService.onNotificationClick(mPkg, mTag, mId);
1392            } catch (RemoteException ex) {
1393                // system process is dead if we're here.
1394            }
1395
1396            // close the shade if it was open
1397            animateCollapse();
1398            visibilityChanged(false);
1399
1400            // If this click was on the intruder alert, hide that instead
1401//            mHandler.sendEmptyMessage(MSG_HIDE_INTRUDER);
1402        }
1403    }
1404
1405    StatusBarNotification removeNotificationViews(IBinder key) {
1406        NotificationData.Entry entry = mNotificationData.remove(key);
1407        if (entry == null) {
1408            Slog.w(TAG, "removeNotification for unknown key: " + key);
1409            return null;
1410        }
1411        // Remove the expanded view.
1412        ViewGroup rowParent = (ViewGroup)entry.row.getParent();
1413        if (rowParent != null) rowParent.removeView(entry.row);
1414
1415        if (NOTIFICATION_PEEK_ENABLED && key == mNotificationPeekKey) {
1416            // must close the peek as well, since it's gone
1417            mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PEEK);
1418        }
1419        // Remove the icon.
1420//        ViewGroup iconParent = (ViewGroup)entry.icon.getParent();
1421//        if (iconParent != null) iconParent.removeView(entry.icon);
1422        updateNotificationIcons();
1423
1424        return entry.notification;
1425    }
1426
1427    private class NotificationTriggerTouchListener implements View.OnTouchListener {
1428        VelocityTracker mVT;
1429        float mInitialTouchX, mInitialTouchY;
1430        int mTouchSlop;
1431
1432        public NotificationTriggerTouchListener() {
1433            mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
1434        }
1435
1436        private Runnable mHiliteOnR = new Runnable() { public void run() {
1437            mNotificationArea.setBackgroundResource(
1438                com.android.internal.R.drawable.list_selector_pressed_holo_dark);
1439        }};
1440        public void hilite(final boolean on) {
1441            if (on) {
1442                mNotificationArea.postDelayed(mHiliteOnR, 100);
1443            } else {
1444                mNotificationArea.removeCallbacks(mHiliteOnR);
1445                mNotificationArea.setBackgroundDrawable(null);
1446            }
1447        }
1448
1449        public boolean onTouch(View v, MotionEvent event) {
1450//            Slog.d(TAG, String.format("touch: (%.1f, %.1f) initial: (%.1f, %.1f)",
1451//                        event.getX(),
1452//                        event.getY(),
1453//                        mInitialTouchX,
1454//                        mInitialTouchY));
1455
1456            if ((mDisabled & StatusBarManager.DISABLE_EXPAND) != 0) {
1457                return true;
1458            }
1459
1460            final int action = event.getAction();
1461            switch (action) {
1462                case MotionEvent.ACTION_DOWN:
1463                    mVT = VelocityTracker.obtain();
1464                    mInitialTouchX = event.getX();
1465                    mInitialTouchY = event.getY();
1466                    hilite(true);
1467                    // fall through
1468                case MotionEvent.ACTION_OUTSIDE:
1469                case MotionEvent.ACTION_MOVE:
1470                    // check for fling
1471                    if (mVT != null) {
1472                        mVT.addMovement(event);
1473                        mVT.computeCurrentVelocity(1000); // pixels per second
1474                        // require a little more oomph once we're already in peekaboo mode
1475                        if (mVT.getYVelocity() < -mNotificationFlingVelocity) {
1476                            animateExpand();
1477                            visibilityChanged(true);
1478                            hilite(false);
1479                            mVT.recycle();
1480                            mVT = null;
1481                        }
1482                    }
1483                    return true;
1484                case MotionEvent.ACTION_UP:
1485                case MotionEvent.ACTION_CANCEL:
1486                    hilite(false);
1487                    if (mVT != null) {
1488                        if (action == MotionEvent.ACTION_UP
1489                         // was this a sloppy tap?
1490                         && Math.abs(event.getX() - mInitialTouchX) < mTouchSlop
1491                         && Math.abs(event.getY() - mInitialTouchY) < (mTouchSlop / 3)
1492                         // dragging off the bottom doesn't count
1493                         && (int)event.getY() < v.getBottom()) {
1494                            animateExpand();
1495                            visibilityChanged(true);
1496                            v.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
1497                            v.playSoundEffect(SoundEffectConstants.CLICK);
1498                        }
1499
1500                        mVT.recycle();
1501                        mVT = null;
1502                        return true;
1503                    }
1504            }
1505            return false;
1506        }
1507    }
1508
1509    public void resetNotificationPeekFadeTimer() {
1510        if (DEBUG) {
1511            Slog.d(TAG, "setting peek fade timer for " + NOTIFICATION_PEEK_FADE_DELAY
1512                + "ms from now");
1513        }
1514        mHandler.removeMessages(MSG_CLOSE_NOTIFICATION_PEEK);
1515        mHandler.sendEmptyMessageDelayed(MSG_CLOSE_NOTIFICATION_PEEK,
1516                NOTIFICATION_PEEK_FADE_DELAY);
1517    }
1518
1519    private class NotificationIconTouchListener implements View.OnTouchListener {
1520        VelocityTracker mVT;
1521        int mPeekIndex;
1522        float mInitialTouchX, mInitialTouchY;
1523        int mTouchSlop;
1524
1525        public NotificationIconTouchListener() {
1526            mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
1527        }
1528
1529        public boolean onTouch(View v, MotionEvent event) {
1530            boolean peeking = mNotificationPeekWindow.getVisibility() != View.GONE;
1531            boolean panelShowing = mNotificationPanel.isShowing();
1532            if (panelShowing) return false;
1533
1534            int numIcons = mIconLayout.getChildCount();
1535            int newPeekIndex = (int)(event.getX() * numIcons / mIconLayout.getWidth());
1536            if (newPeekIndex > numIcons - 1) newPeekIndex = numIcons - 1;
1537            else if (newPeekIndex < 0) newPeekIndex = 0;
1538
1539            final int action = event.getAction();
1540            switch (action) {
1541                case MotionEvent.ACTION_DOWN:
1542                    mVT = VelocityTracker.obtain();
1543                    mInitialTouchX = event.getX();
1544                    mInitialTouchY = event.getY();
1545                    mPeekIndex = -1;
1546
1547                    // fall through
1548                case MotionEvent.ACTION_OUTSIDE:
1549                case MotionEvent.ACTION_MOVE:
1550                    // peek and switch icons if necessary
1551
1552                    if (newPeekIndex != mPeekIndex) {
1553                        mPeekIndex = newPeekIndex;
1554
1555                        if (DEBUG) Slog.d(TAG, "will peek at notification #" + mPeekIndex);
1556                        Message peekMsg = mHandler.obtainMessage(MSG_OPEN_NOTIFICATION_PEEK);
1557                        peekMsg.arg1 = mPeekIndex;
1558
1559                        mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
1560
1561                        if (peeking) {
1562                            // no delay if we're scrubbing left-right
1563                            mHandler.sendMessage(peekMsg);
1564                        } else {
1565                            // wait for fling
1566                            mHandler.sendMessageDelayed(peekMsg, NOTIFICATION_PEEK_HOLD_THRESH);
1567                        }
1568                    }
1569
1570                    // check for fling
1571                    if (mVT != null) {
1572                        mVT.addMovement(event);
1573                        mVT.computeCurrentVelocity(1000); // pixels per second
1574                        // require a little more oomph once we're already in peekaboo mode
1575                        if (!panelShowing && (
1576                               (peeking && mVT.getYVelocity() < -mNotificationFlingVelocity*3)
1577                            || (mVT.getYVelocity() < -mNotificationFlingVelocity))) {
1578                            mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
1579                            mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PANEL);
1580                            mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PEEK);
1581                            mHandler.sendEmptyMessage(MSG_OPEN_NOTIFICATION_PANEL);
1582                        }
1583                    }
1584                    return true;
1585                case MotionEvent.ACTION_UP:
1586                case MotionEvent.ACTION_CANCEL:
1587                    mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
1588                    if (!peeking) {
1589                        if (action == MotionEvent.ACTION_UP
1590                                // was this a sloppy tap?
1591                                && Math.abs(event.getX() - mInitialTouchX) < mTouchSlop
1592                                && Math.abs(event.getY() - mInitialTouchY) < (mTouchSlop / 3)
1593                                // dragging off the bottom doesn't count
1594                                && (int)event.getY() < v.getBottom()) {
1595                            Message peekMsg = mHandler.obtainMessage(MSG_OPEN_NOTIFICATION_PEEK);
1596                            peekMsg.arg1 = mPeekIndex;
1597                            mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
1598                            mHandler.sendMessage(peekMsg);
1599
1600                            v.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
1601                            v.playSoundEffect(SoundEffectConstants.CLICK);
1602
1603                            peeking = true; // not technically true yet, but the next line will run
1604                        }
1605                    }
1606
1607                    if (peeking) {
1608                        resetNotificationPeekFadeTimer();
1609                    }
1610
1611                    mVT.recycle();
1612                    mVT = null;
1613                    return true;
1614            }
1615            return false;
1616        }
1617    }
1618
1619    StatusBarIconView addNotificationViews(IBinder key, StatusBarNotification notification) {
1620        if (DEBUG) {
1621            Slog.d(TAG, "addNotificationViews(key=" + key + ", notification=" + notification);
1622        }
1623        // Construct the icon.
1624        final StatusBarIconView iconView = new StatusBarIconView(mContext,
1625                notification.pkg + "/0x" + Integer.toHexString(notification.id),
1626                notification.notification);
1627        iconView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
1628
1629        final StatusBarIcon ic = new StatusBarIcon(notification.pkg,
1630                    notification.notification.icon,
1631                    notification.notification.iconLevel,
1632                    notification.notification.number,
1633                    notification.notification.tickerText);
1634        if (!iconView.set(ic)) {
1635            handleNotificationError(key, notification, "Couldn't attach StatusBarIcon: " + ic);
1636            return null;
1637        }
1638        // Construct the expanded view.
1639        NotificationData.Entry entry = new NotificationData.Entry(key, notification, iconView);
1640        if (!inflateViews(entry, mPile)) {
1641            handleNotificationError(key, notification, "Couldn't expand RemoteViews for: "
1642                    + notification);
1643            return null;
1644        }
1645
1646        // Add the icon.
1647        int pos = mNotificationData.add(entry);
1648        if (DEBUG) {
1649            Slog.d(TAG, "addNotificationViews: added at " + pos);
1650        }
1651        updateNotificationIcons();
1652
1653        return iconView;
1654    }
1655
1656    private void reloadAllNotificationIcons() {
1657        if (mIconLayout == null) return;
1658        mIconLayout.removeAllViews();
1659        updateNotificationIcons();
1660    }
1661
1662    private void updateNotificationIcons() {
1663        // XXX: need to implement a new limited linear layout class
1664        // to avoid removing & readding everything
1665
1666        if (mIconLayout == null) return;
1667
1668        // first, populate the main notification panel
1669        loadNotificationPanel();
1670
1671        final LinearLayout.LayoutParams params
1672            = new LinearLayout.LayoutParams(mIconSize + 2*mIconHPadding, mNaturalBarHeight);
1673
1674        // alternate behavior in DND mode
1675        if (mNotificationDNDMode) {
1676            if (mIconLayout.getChildCount() == 0) {
1677                final Notification dndNotification = new Notification.Builder(mContext)
1678                    .setContentTitle(mContext.getText(R.string.notifications_off_title))
1679                    .setContentText(mContext.getText(R.string.notifications_off_text))
1680                    .setSmallIcon(R.drawable.ic_notification_dnd)
1681                    .setOngoing(true)
1682                    .getNotification();
1683
1684                final StatusBarIconView iconView = new StatusBarIconView(mContext, "_dnd",
1685                        dndNotification);
1686                iconView.setImageResource(R.drawable.ic_notification_dnd);
1687                iconView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
1688                iconView.setPadding(mIconHPadding, 0, mIconHPadding, 0);
1689
1690                mNotificationDNDDummyEntry = new NotificationData.Entry(
1691                        null,
1692                        new StatusBarNotification("", 0, "", 0, 0, Notification.PRIORITY_MAX, dndNotification),
1693                        iconView);
1694
1695                mIconLayout.addView(iconView, params);
1696            }
1697
1698            return;
1699        } else if (0 != (mDisabled & StatusBarManager.DISABLE_NOTIFICATION_ICONS)) {
1700            // if icons are disabled but we're not in DND mode, this is probably Setup and we should
1701            // just leave the area totally empty
1702            return;
1703        }
1704
1705        int N = mNotificationData.size();
1706
1707        if (DEBUG) {
1708            Slog.d(TAG, "refreshing icons: " + N + " notifications, mIconLayout=" + mIconLayout);
1709        }
1710
1711        ArrayList<View> toShow = new ArrayList<View>();
1712
1713        // Extra Special Icons
1714        // The IME switcher and compatibility mode icons take the place of notifications. You didn't
1715        // need to see all those new emails, did you?
1716        int maxNotificationIconsCount = mMaxNotificationIcons;
1717        if (mInputMethodSwitchButton.getVisibility() != View.GONE) maxNotificationIconsCount --;
1718        if (mCompatModeButton.getVisibility()        != View.GONE) maxNotificationIconsCount --;
1719
1720        for (int i=0; i< maxNotificationIconsCount; i++) {
1721            if (i>=N) break;
1722            toShow.add(mNotificationData.get(N-i-1).icon);
1723        }
1724
1725        ArrayList<View> toRemove = new ArrayList<View>();
1726        for (int i=0; i<mIconLayout.getChildCount(); i++) {
1727            View child = mIconLayout.getChildAt(i);
1728            if (!toShow.contains(child)) {
1729                toRemove.add(child);
1730            }
1731        }
1732
1733        for (View remove : toRemove) {
1734            mIconLayout.removeView(remove);
1735        }
1736
1737        for (int i=0; i<toShow.size(); i++) {
1738            View v = toShow.get(i);
1739            v.setPadding(mIconHPadding, 0, mIconHPadding, 0);
1740            if (v.getParent() == null) {
1741                mIconLayout.addView(v, i, params);
1742            }
1743        }
1744    }
1745
1746    private void loadNotificationPanel() {
1747        int N = mNotificationData.size();
1748
1749        ArrayList<View> toShow = new ArrayList<View>();
1750
1751        for (int i=0; i<N; i++) {
1752            View row = mNotificationData.get(N-i-1).row;
1753            toShow.add(row);
1754        }
1755
1756        ArrayList<View> toRemove = new ArrayList<View>();
1757        for (int i=0; i<mPile.getChildCount(); i++) {
1758            View child = mPile.getChildAt(i);
1759            if (!toShow.contains(child)) {
1760                toRemove.add(child);
1761            }
1762        }
1763
1764        for (View remove : toRemove) {
1765            mPile.removeView(remove);
1766        }
1767
1768        for (int i=0; i<toShow.size(); i++) {
1769            View v = toShow.get(i);
1770            if (v.getParent() == null) {
1771                mPile.addView(v, N-1-i); // the notification panel has newest at the bottom
1772            }
1773        }
1774
1775        mNotificationPanel.setNotificationCount(N);
1776    }
1777
1778    void workAroundBadLayerDrawableOpacity(View v) {
1779        Drawable bgd = v.getBackground();
1780        if (!(bgd instanceof LayerDrawable)) return;
1781
1782        LayerDrawable d = (LayerDrawable) bgd;
1783        v.setBackgroundDrawable(null);
1784        d.setOpacity(PixelFormat.TRANSLUCENT);
1785        v.setBackgroundDrawable(d);
1786    }
1787
1788    private boolean inflateViews(NotificationData.Entry entry, ViewGroup parent) {
1789        StatusBarNotification sbn = entry.notification;
1790        RemoteViews remoteViews = sbn.notification.contentView;
1791        if (remoteViews == null) {
1792            return false;
1793        }
1794
1795        // create the row view
1796        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(
1797                Context.LAYOUT_INFLATER_SERVICE);
1798        View row = inflater.inflate(R.layout.status_bar_notification_row, parent, false);
1799        workAroundBadLayerDrawableOpacity(row);
1800        View vetoButton = updateNotificationVetoButton(row, entry.notification);
1801        vetoButton.setContentDescription(mContext.getString(
1802                R.string.accessibility_remove_notification));
1803
1804        // the large icon
1805        ImageView largeIcon = (ImageView)row.findViewById(R.id.large_icon);
1806        if (sbn.notification.largeIcon != null) {
1807            largeIcon.setImageBitmap(sbn.notification.largeIcon);
1808            largeIcon.setContentDescription(sbn.notification.tickerText);
1809        } else {
1810            largeIcon.getLayoutParams().width = 0;
1811            largeIcon.setVisibility(View.INVISIBLE);
1812        }
1813        largeIcon.setContentDescription(sbn.notification.tickerText);
1814
1815        // bind the click event to the content area
1816        ViewGroup content = (ViewGroup)row.findViewById(R.id.content);
1817        // XXX: update to allow controls within notification views
1818        content.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
1819//        content.setOnFocusChangeListener(mFocusChangeListener);
1820        PendingIntent contentIntent = sbn.notification.contentIntent;
1821        if (contentIntent != null) {
1822            final View.OnClickListener listener = new NotificationClicker(
1823                    contentIntent, sbn.pkg, sbn.tag, sbn.id);
1824            largeIcon.setOnClickListener(listener);
1825            content.setOnClickListener(listener);
1826        } else {
1827            largeIcon.setOnClickListener(null);
1828            content.setOnClickListener(null);
1829        }
1830
1831        View expanded = null;
1832        Exception exception = null;
1833        try {
1834            expanded = remoteViews.apply(mContext, content);
1835        }
1836        catch (RuntimeException e) {
1837            exception = e;
1838        }
1839        if (expanded == null) {
1840            final String ident = sbn.pkg + "/0x" + Integer.toHexString(sbn.id);
1841            Slog.e(TAG, "couldn't inflate view for notification " + ident, exception);
1842            return false;
1843        } else {
1844            content.addView(expanded);
1845            row.setDrawingCacheEnabled(true);
1846        }
1847
1848        applyLegacyRowBackground(sbn, content);
1849
1850        entry.row = row;
1851        entry.content = content;
1852        entry.expanded = expanded;
1853        entry.largeIcon = largeIcon;
1854
1855        return true;
1856    }
1857
1858    void applyLegacyRowBackground(StatusBarNotification sbn, View content) {
1859        if (sbn.notification.contentView.getLayoutId() !=
1860                com.android.internal.R.layout.status_bar_latest_event_content) {
1861            int version = 0;
1862            try {
1863                ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(sbn.pkg, 0);
1864                version = info.targetSdkVersion;
1865            } catch (NameNotFoundException ex) {
1866                Slog.e(TAG, "Failed looking up ApplicationInfo for " + sbn.pkg, ex);
1867            }
1868            if (version > 0 && version < Build.VERSION_CODES.GINGERBREAD) {
1869                content.setBackgroundResource(R.drawable.notification_row_legacy_bg);
1870            } else {
1871                content.setBackgroundResource(R.drawable.notification_row_bg);
1872            }
1873        }
1874    }
1875
1876    public void clearAll() {
1877        try {
1878            mBarService.onClearAllNotifications();
1879        } catch (RemoteException ex) {
1880            // system process is dead if we're here.
1881        }
1882        animateCollapse();
1883        visibilityChanged(false);
1884    }
1885
1886    public void toggleRecentApps() {
1887        int msg = (mRecentsPanel.getVisibility() == View.VISIBLE)
1888                ? MSG_CLOSE_RECENTS_PANEL : MSG_OPEN_RECENTS_PANEL;
1889        mHandler.removeMessages(msg);
1890        mHandler.sendEmptyMessage(msg);
1891    }
1892
1893    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1894        public void onReceive(Context context, Intent intent) {
1895            String action = intent.getAction();
1896            if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1897                || Intent.ACTION_SCREEN_OFF.equals(action)) {
1898                boolean excludeRecents = false;
1899                if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
1900                    String reason = intent.getStringExtra("reason");
1901                    if (reason != null) {
1902                        excludeRecents = reason.equals("recentapps");
1903                    }
1904                }
1905                if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1906                    // If we're turning the screen off, we want to hide the
1907                    // recents panel with no animation
1908                    // TODO: hide other things, like the notification tray,
1909                    // with no animation as well
1910                    mRecentsPanel.show(false, false);
1911                    excludeRecents = true;
1912                }
1913                animateCollapse(excludeRecents);
1914            }
1915        }
1916    };
1917
1918    public class TouchOutsideListener implements View.OnTouchListener {
1919        private int mMsg;
1920        private StatusBarPanel mPanel;
1921
1922        public TouchOutsideListener(int msg, StatusBarPanel panel) {
1923            mMsg = msg;
1924            mPanel = panel;
1925        }
1926
1927        public boolean onTouch(View v, MotionEvent ev) {
1928            final int action = ev.getAction();
1929            if (action == MotionEvent.ACTION_OUTSIDE
1930                    || (action == MotionEvent.ACTION_DOWN
1931                        && !mPanel.isInContentArea((int)ev.getX(), (int)ev.getY()))) {
1932                mHandler.removeMessages(mMsg);
1933                mHandler.sendEmptyMessage(mMsg);
1934                return true;
1935            }
1936            return false;
1937        }
1938    }
1939
1940    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1941        pw.print("mDisabled=0x");
1942        pw.println(Integer.toHexString(mDisabled));
1943        pw.println("mNetworkController:");
1944        mNetworkController.dump(fd, pw, args);
1945    }
1946}
1947
1948
1949