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