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