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