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