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