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