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