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