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