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