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