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