TabletStatusBar.java revision dc10030581d6eec1c96acd62ed511f91d25d73a1
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    private static final int MAX_IMAGE_LEVEL = 10000;
97    private static final boolean USE_2D_RECENTS = true;
98
99    public static final int LIGHTS_ON_DELAY = 5000;
100
101    // The height of the bar, as definied by the build.  It may be taller if we're plugged
102    // into hdmi.
103    int mNaturalBarHeight = -1;
104    int mIconSize = -1;
105    int mIconHPadding = -1;
106
107    H mHandler = new H();
108
109    IWindowManager mWindowManager;
110
111    // tracking all current notifications
112    private NotificationData mNotns = new NotificationData();
113
114    TabletStatusBarView mStatusBarView;
115    View mNotificationArea;
116    View mNotificationTrigger;
117    NotificationIconArea mNotificationIconArea;
118    View mNavigationArea;
119
120    ImageView mBackButton;
121    View mHomeButton;
122    View mMenuButton;
123    View mRecentButton;
124
125    ViewGroup mNotificationAndImeArea;
126    InputMethodButton mInputMethodSwitchButton;
127
128    NotificationPanel mNotificationPanel;
129    NotificationPeekPanel mNotificationPeekWindow;
130    ViewGroup mNotificationPeekRow;
131    int mNotificationPeekIndex;
132    IBinder mNotificationPeekKey;
133    LayoutTransition mNotificationPeekScrubLeft, mNotificationPeekScrubRight;
134
135    int mNotificationPeekTapDuration;
136    int mNotificationFlingVelocity;
137
138    ViewGroup mPile;
139
140    HeightReceiver mHeightReceiver;
141    BatteryController mBatteryController;
142    NetworkController mNetworkController;
143
144    View mBarContents;
145
146    // hide system chrome ("lights out") support
147    View mShadow;
148
149    NotificationIconArea.IconLayout mIconLayout;
150
151    TabletTicker mTicker;
152
153    View mFakeSpaceBar;
154    KeyEvent mSpaceBarKeyEvent = null;
155
156    // for disabling the status bar
157    int mDisabled = 0;
158
159    boolean mNotificationsOn = true;
160    private RecentAppsPanel mRecentsPanel;
161
162    public Context getContext() { return mContext; }
163
164    protected void addPanelWindows() {
165        final Context context = mContext;
166
167        // Notification Panel
168        mNotificationPanel = (NotificationPanel)View.inflate(context,
169                R.layout.status_bar_notification_panel, null);
170        mNotificationPanel.show(false, false);
171        mNotificationPanel.setOnTouchListener(
172                new TouchOutsideListener(MSG_CLOSE_NOTIFICATION_PANEL, mNotificationPanel));
173
174        // the battery and network icons
175        mBatteryController.addIconView((ImageView)mNotificationPanel.findViewById(R.id.battery));
176        mBatteryController.addLabelView(
177                (TextView)mNotificationPanel.findViewById(R.id.battery_text));
178        mNetworkController.addCombinedSignalIconView(
179                (ImageView)mNotificationPanel.findViewById(R.id.network_signal));
180        mNetworkController.addDataTypeIconView(
181                (ImageView)mNotificationPanel.findViewById(R.id.network_type));
182        mNetworkController.addLabelView(
183                (TextView)mNotificationPanel.findViewById(R.id.network_text));
184        mNetworkController.addLabelView(
185                (TextView)mBarContents.findViewById(R.id.network_text));
186
187        mStatusBarView.setIgnoreChildren(0, mNotificationTrigger, mNotificationPanel);
188
189        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
190                720, // ViewGroup.LayoutParams.MATCH_PARENT,
191                ViewGroup.LayoutParams.MATCH_PARENT,
192                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
193                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
194                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
195                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
196                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
197                PixelFormat.TRANSLUCENT);
198        lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
199        lp.setTitle("NotificationPanel");
200        lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED
201                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING;
202        lp.windowAnimations = com.android.internal.R.style.Animation; // == no animation
203//        lp.windowAnimations = com.android.internal.R.style.Animation_ZoomButtons; // simple fade
204
205        WindowManagerImpl.getDefault().addView(mNotificationPanel, lp);
206
207        // Notification preview window
208        mNotificationPeekWindow = (NotificationPeekPanel) View.inflate(context,
209                R.layout.status_bar_notification_peek, null);
210        mNotificationPeekRow = (ViewGroup) mNotificationPeekWindow.findViewById(R.id.content);
211        mNotificationPeekWindow.setVisibility(View.GONE);
212        mNotificationPeekWindow.setOnTouchListener(
213                new TouchOutsideListener(MSG_CLOSE_NOTIFICATION_PEEK, mNotificationPeekWindow));
214        mNotificationPeekScrubRight = new LayoutTransition();
215        mNotificationPeekScrubRight.setAnimator(LayoutTransition.APPEARING,
216                ObjectAnimator.ofInt(null, "left", -512, 0));
217        mNotificationPeekScrubRight.setAnimator(LayoutTransition.DISAPPEARING,
218                ObjectAnimator.ofInt(null, "left", -512, 0));
219        mNotificationPeekScrubRight.setDuration(500);
220
221        mNotificationPeekScrubLeft = new LayoutTransition();
222        mNotificationPeekScrubLeft.setAnimator(LayoutTransition.APPEARING,
223                ObjectAnimator.ofInt(null, "left", 512, 0));
224        mNotificationPeekScrubLeft.setAnimator(LayoutTransition.DISAPPEARING,
225                ObjectAnimator.ofInt(null, "left", 512, 0));
226        mNotificationPeekScrubLeft.setDuration(500);
227
228        // XXX: setIgnoreChildren?
229        lp = new WindowManager.LayoutParams(
230                512, // ViewGroup.LayoutParams.WRAP_CONTENT,
231                ViewGroup.LayoutParams.WRAP_CONTENT,
232                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
233                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
234                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
235                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH,
236                PixelFormat.TRANSLUCENT);
237        lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
238        lp.setTitle("NotificationPeekWindow");
239        lp.windowAnimations = com.android.internal.R.style.Animation_Toast;
240
241        WindowManagerImpl.getDefault().addView(mNotificationPeekWindow, lp);
242
243        // Recents Panel
244        if (USE_2D_RECENTS) {
245            mRecentsPanel = (RecentAppsPanel) View.inflate(context,
246                    R.layout.status_bar_recent_panel, null);
247            mRecentsPanel.setVisibility(View.GONE);
248            mRecentsPanel.setOnTouchListener(new TouchOutsideListener(MSG_CLOSE_RECENTS_PANEL,
249                    mRecentsPanel));
250            mStatusBarView.setIgnoreChildren(2, mRecentButton, mRecentsPanel);
251
252            lp = new WindowManager.LayoutParams(
253                    ViewGroup.LayoutParams.WRAP_CONTENT,
254                    ViewGroup.LayoutParams.WRAP_CONTENT,
255                    WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
256                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
257                        | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
258                        | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
259                        | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
260                    PixelFormat.TRANSLUCENT);
261            lp.gravity = Gravity.BOTTOM | Gravity.LEFT;
262            lp.setTitle("RecentsPanel");
263            lp.windowAnimations = R.style.Animation_RecentPanel;
264
265            WindowManagerImpl.getDefault().addView(mRecentsPanel, lp);
266            mRecentsPanel.setBar(this);
267        }
268    }
269
270    @Override
271    public void start() {
272        super.start(); // will add the main bar view
273    }
274
275    @Override
276    protected void onConfigurationChanged(Configuration newConfig) {
277        loadDimens();
278    }
279
280    protected void loadDimens() {
281        final Resources res = mContext.getResources();
282
283        mNaturalBarHeight = res.getDimensionPixelSize(
284                com.android.internal.R.dimen.status_bar_height);
285
286        int newIconSize = res.getDimensionPixelSize(
287            com.android.internal.R.dimen.status_bar_icon_size);
288        int newIconHPadding = res.getDimensionPixelSize(
289            R.dimen.status_bar_icon_padding);
290
291        if (newIconHPadding != mIconHPadding || newIconSize != mIconSize) {
292//            Slog.d(TAG, "size=" + newIconSize + " padding=" + newIconHPadding);
293            mIconHPadding = newIconHPadding;
294            mIconSize = newIconSize;
295            reloadAllNotificationIcons(); // reload the tray
296        }
297    }
298
299    protected View makeStatusBarView() {
300        final Context context = mContext;
301
302        mWindowManager = IWindowManager.Stub.asInterface(
303                ServiceManager.getService(Context.WINDOW_SERVICE));
304
305        // This guy will listen for HDMI plugged broadcasts so we can resize the
306        // status bar as appropriate.
307        mHeightReceiver = new HeightReceiver(mContext);
308        mHeightReceiver.registerReceiver();
309        loadDimens();
310
311        final TabletStatusBarView sb = (TabletStatusBarView)View.inflate(
312                context, R.layout.status_bar, null);
313        mStatusBarView = sb;
314
315        sb.setHandler(mHandler);
316
317        mBarContents = sb.findViewById(R.id.bar_contents);
318
319        // the whole right-hand side of the bar
320        mNotificationArea = sb.findViewById(R.id.notificationArea);
321
322        // the button to open the notification area
323        mNotificationTrigger = sb.findViewById(R.id.notificationTrigger);
324        mNotificationTrigger.setOnClickListener(mOnClickListener);
325
326        // the more notifications icon
327        mNotificationIconArea = (NotificationIconArea)sb.findViewById(R.id.notificationIcons);
328
329        // where the icons go
330        mIconLayout = (NotificationIconArea.IconLayout) sb.findViewById(R.id.icons);
331        mIconLayout.setOnTouchListener(new NotificationIconTouchListener());
332
333        ViewConfiguration vc = ViewConfiguration.get(context);
334        mNotificationPeekTapDuration = vc.getTapTimeout();
335        mNotificationFlingVelocity = 300; // px/s
336
337        mTicker = new TabletTicker(this);
338
339        // The icons
340        mBatteryController = new BatteryController(mContext);
341        mBatteryController.addIconView((ImageView)sb.findViewById(R.id.battery));
342        mNetworkController = new NetworkController(mContext);
343        mNetworkController.addCombinedSignalIconView(
344                (ImageView)sb.findViewById(R.id.network_signal));
345        mNetworkController.addDataTypeIconView(
346                (ImageView)sb.findViewById(R.id.network_type));
347
348        // The navigation buttons
349        mNavigationArea = sb.findViewById(R.id.navigationArea);
350        mBackButton = (ImageView)mNavigationArea.findViewById(R.id.back);
351        mHomeButton = mNavigationArea.findViewById(R.id.home);
352        mMenuButton = mNavigationArea.findViewById(R.id.menu);
353        mRecentButton = mNavigationArea.findViewById(R.id.recent_apps);
354        mRecentButton.setOnClickListener(mOnClickListener);
355
356        // The bar contents buttons
357        mNotificationAndImeArea = (ViewGroup)sb.findViewById(R.id.notificationAndImeArea);
358        mInputMethodSwitchButton = (InputMethodButton) sb.findViewById(R.id.imeSwitchButton);
359
360        // for redirecting errant bar taps to the IME
361        mFakeSpaceBar = sb.findViewById(R.id.fake_space_bar);
362
363        // "shadows" of the status bar features, for lights-out mode
364        mShadow = sb.findViewById(R.id.bar_shadow);
365        mShadow.setOnTouchListener(
366            new View.OnTouchListener() {
367                public boolean onTouch(View v, MotionEvent ev) {
368                    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
369                        mShadow.setVisibility(View.GONE);
370                        mBarContents.setVisibility(View.VISIBLE);
371                    }
372                    return false;
373                }
374            });
375
376        // tuning parameters
377        final int LIGHTS_GOING_OUT_SYSBAR_DURATION = 600;
378        final int LIGHTS_GOING_OUT_SHADOW_DURATION = 1000;
379        final int LIGHTS_GOING_OUT_SHADOW_DELAY    = 500;
380
381        final int LIGHTS_COMING_UP_SYSBAR_DURATION = 200;
382//        final int LIGHTS_COMING_UP_SYSBAR_DELAY    = 50;
383        final int LIGHTS_COMING_UP_SHADOW_DURATION = 0;
384
385        LayoutTransition xition = new LayoutTransition();
386        xition.setAnimator(LayoutTransition.APPEARING,
387               ObjectAnimator.ofFloat(null, "alpha", 0.5f, 1f));
388        xition.setDuration(LayoutTransition.APPEARING, LIGHTS_COMING_UP_SYSBAR_DURATION);
389        xition.setStartDelay(LayoutTransition.APPEARING, 0);
390        xition.setAnimator(LayoutTransition.DISAPPEARING,
391               ObjectAnimator.ofFloat(null, "alpha", 1f, 0f));
392        xition.setDuration(LayoutTransition.DISAPPEARING, LIGHTS_GOING_OUT_SYSBAR_DURATION);
393        xition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
394        ((ViewGroup)sb.findViewById(R.id.bar_contents_holder)).setLayoutTransition(xition);
395
396        xition = new LayoutTransition();
397        xition.setAnimator(LayoutTransition.APPEARING,
398               ObjectAnimator.ofFloat(null, "alpha", 0f, 1f));
399        xition.setDuration(LayoutTransition.APPEARING, LIGHTS_GOING_OUT_SHADOW_DURATION);
400        xition.setStartDelay(LayoutTransition.APPEARING, LIGHTS_GOING_OUT_SHADOW_DELAY);
401        xition.setAnimator(LayoutTransition.DISAPPEARING,
402               ObjectAnimator.ofFloat(null, "alpha", 1f, 0f));
403        xition.setDuration(LayoutTransition.DISAPPEARING, LIGHTS_COMING_UP_SHADOW_DURATION);
404        xition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
405        ((ViewGroup)sb.findViewById(R.id.bar_shadow_holder)).setLayoutTransition(xition);
406
407        // set the initial view visibility
408        setAreThereNotifications();
409
410        // Add the windows
411        addPanelWindows();
412
413        mPile = (ViewGroup)mNotificationPanel.findViewById(R.id.content);
414        mPile.removeAllViews();
415
416        ScrollView scroller = (ScrollView)mPile.getParent();
417        scroller.setFillViewport(true);
418
419        mHeightReceiver.addOnBarHeightChangedListener(this);
420
421        return sb;
422    }
423
424    public int getStatusBarHeight() {
425        return mHeightReceiver.getHeight();
426    }
427
428    protected int getStatusBarGravity() {
429        return Gravity.BOTTOM | Gravity.FILL_HORIZONTAL;
430    }
431
432    public void onBarHeightChanged(int height) {
433        final WindowManager.LayoutParams lp
434                = (WindowManager.LayoutParams)mStatusBarView.getLayoutParams();
435        if (lp == null) {
436            // haven't been added yet
437            return;
438        }
439        if (lp.height != height) {
440            lp.height = height;
441            final WindowManager wm = WindowManagerImpl.getDefault();
442            wm.updateViewLayout(mStatusBarView, lp);
443        }
444    }
445
446    private class H extends Handler {
447        public void handleMessage(Message m) {
448            switch (m.what) {
449                case MSG_OPEN_NOTIFICATION_PEEK:
450                    if (DEBUG) Slog.d(TAG, "opening notification peek window; arg=" + m.arg1);
451                    if (m.arg1 >= 0) {
452                        final int N = mNotns.size();
453                        if (mNotificationPeekIndex >= 0 && mNotificationPeekIndex < N) {
454                            NotificationData.Entry entry = mNotns.get(N-1-mNotificationPeekIndex);
455                            entry.icon.setBackgroundColor(0);
456                            mNotificationPeekIndex = -1;
457                            mNotificationPeekKey = null;
458                        }
459
460                        final int peekIndex = m.arg1;
461                        if (peekIndex < N) {
462                            //Slog.d(TAG, "loading peek: " + peekIndex);
463                            NotificationData.Entry entry = mNotns.get(N-1-peekIndex);
464                            NotificationData.Entry copy = new NotificationData.Entry(
465                                    entry.key,
466                                    entry.notification,
467                                    entry.icon);
468                            inflateViews(copy, mNotificationPeekRow);
469
470                            entry.icon.setBackgroundColor(0x20FFFFFF);
471
472//                          mNotificationPeekRow.setLayoutTransition(
473//                              peekIndex < mNotificationPeekIndex
474//                                  ? mNotificationPeekScrubLeft
475//                                  : mNotificationPeekScrubRight);
476
477                            mNotificationPeekRow.removeAllViews();
478                            mNotificationPeekRow.addView(copy.row);
479
480                            mNotificationPeekWindow.setVisibility(View.VISIBLE);
481                            mNotificationPanel.show(false, true);
482
483                            mNotificationPeekIndex = peekIndex;
484                            mNotificationPeekKey = entry.key;
485                        }
486                    }
487                    break;
488                case MSG_CLOSE_NOTIFICATION_PEEK:
489                    if (DEBUG) Slog.d(TAG, "closing notification peek window");
490                    mNotificationPeekWindow.setVisibility(View.GONE);
491                    mNotificationPeekRow.removeAllViews();
492                    final int N = mNotns.size();
493                    if (mNotificationPeekIndex >= 0 && mNotificationPeekIndex < N) {
494                        NotificationData.Entry entry = mNotns.get(N-1-mNotificationPeekIndex);
495                        entry.icon.setBackgroundColor(0);
496                    }
497
498                    mNotificationPeekIndex = -1;
499                    mNotificationPeekKey = null;
500                    break;
501                case MSG_OPEN_NOTIFICATION_PANEL:
502                    if (DEBUG) Slog.d(TAG, "opening notifications panel");
503                    if (!mNotificationPanel.isShowing()) {
504                        mNotificationPeekWindow.setVisibility(View.GONE);
505                        mNotificationPanel.show(true, true);
506                        mNotificationArea.setVisibility(View.GONE);
507                        mTicker.halt();
508                    }
509                    break;
510                case MSG_CLOSE_NOTIFICATION_PANEL:
511                    if (DEBUG) Slog.d(TAG, "closing notifications panel");
512                    if (mNotificationPanel.isShowing()) {
513                        mNotificationPanel.show(false, true);
514                        mNotificationArea.setVisibility(View.VISIBLE);
515                    }
516                    break;
517                case MSG_OPEN_RECENTS_PANEL:
518                    if (DEBUG) Slog.d(TAG, "opening recents panel");
519                    if (mRecentsPanel != null) mRecentsPanel.setVisibility(View.VISIBLE);
520                    break;
521                case MSG_CLOSE_RECENTS_PANEL:
522                    if (DEBUG) Slog.d(TAG, "closing recents panel");
523                    if (mRecentsPanel != null) mRecentsPanel.setVisibility(View.GONE);
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