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