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