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