PhoneStatusBar.java revision 6179ea3196e9306d3f14361fe9ef14191b1edba6
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.phone;
18
19import android.app.Service;
20import android.app.ActivityManagerNative;
21import android.app.Dialog;
22import android.app.Notification;
23import android.app.PendingIntent;
24import android.app.Service;
25import android.app.StatusBarManager;
26import android.content.BroadcastReceiver;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.res.Resources;
31import android.content.res.Configuration;
32import android.graphics.PixelFormat;
33import android.graphics.Rect;
34import android.graphics.drawable.Drawable;
35import android.net.Uri;
36import android.os.Bundle;
37import android.os.IBinder;
38import android.os.RemoteException;
39import android.os.Handler;
40import android.os.Message;
41import android.os.ServiceManager;
42import android.os.SystemClock;
43import android.text.TextUtils;
44import android.util.Slog;
45import android.util.Log;
46import android.view.Display;
47import android.view.Gravity;
48import android.view.IWindowManager;
49import android.view.KeyEvent;
50import android.view.LayoutInflater;
51import android.view.MotionEvent;
52import android.view.Surface;
53import android.view.VelocityTracker;
54import android.view.View;
55import android.view.ViewGroup;
56import android.view.ViewGroup.LayoutParams;
57import android.view.Window;
58import android.view.WindowManager;
59import android.view.WindowManagerImpl;
60import android.view.animation.Animation;
61import android.view.animation.AnimationUtils;
62import android.widget.ImageView;
63import android.widget.LinearLayout;
64import android.widget.RemoteViews;
65import android.widget.ScrollView;
66import android.widget.TextView;
67import android.widget.FrameLayout;
68
69import java.io.FileDescriptor;
70import java.io.PrintWriter;
71import java.util.ArrayList;
72import java.util.HashMap;
73import java.util.Set;
74
75import com.android.internal.statusbar.StatusBarIcon;
76import com.android.internal.statusbar.StatusBarIconList;
77import com.android.internal.statusbar.StatusBarNotification;
78
79import com.android.systemui.R;
80import com.android.systemui.recent.RecentsPanelView;
81import com.android.systemui.statusbar.NotificationData;
82import com.android.systemui.statusbar.StatusBar;
83import com.android.systemui.statusbar.StatusBarIconView;
84import com.android.systemui.statusbar.policy.DateView;
85
86
87public class PhoneStatusBar extends StatusBar {
88    static final String TAG = "PhoneStatusBar";
89    static final boolean SPEW = false;
90    public static final boolean DEBUG = false;
91
92    public static final String ACTION_STATUSBAR_START
93            = "com.android.internal.policy.statusbar.START";
94
95    static final int EXPANDED_LEAVE_ALONE = -10000;
96    static final int EXPANDED_FULL_OPEN = -10001;
97
98    private static final int MSG_ANIMATE = 1000;
99    private static final int MSG_ANIMATE_REVEAL = 1001;
100    private static final int MSG_SHOW_INTRUDER = 1002;
101    private static final int MSG_HIDE_INTRUDER = 1003;
102    private static final int MSG_OPEN_RECENTS_PANEL = 1020;
103    private static final int MSG_CLOSE_RECENTS_PANEL = 1021;
104
105    // will likely move to a resource or other tunable param at some point
106    private static final int INTRUDER_ALERT_DECAY_MS = 10000;
107
108    PhoneStatusBarPolicy mIconPolicy;
109
110    int mIconSize;
111    Display mDisplay;
112
113    IWindowManager mWindowManager;
114
115    PhoneStatusBarView mStatusBarView;
116    int mPixelFormat;
117    H mHandler = new H();
118    Object mQueueLock = new Object();
119
120    // icons
121    LinearLayout mIcons;
122    IconMerger mNotificationIcons;
123    LinearLayout mStatusIcons;
124
125    // expanded notifications
126    Dialog mExpandedDialog;
127    ExpandedView mExpandedView;
128    WindowManager.LayoutParams mExpandedParams;
129    ScrollView mScrollView;
130    View mNotificationLinearLayout;
131    View mExpandedContents;
132    // top bar
133    TextView mNoNotificationsTitle;
134    TextView mClearButton;
135    // drag bar
136    CloseDragHandle mCloseView;
137    // ongoing
138    NotificationData mOngoing = new NotificationData();
139    TextView mOngoingTitle;
140    ViewGroup mOngoingItems;
141    // latest
142    NotificationData mLatest = new NotificationData();
143    TextView mLatestTitle;
144    ViewGroup mLatestItems;
145    // position
146    int[] mPositionTmp = new int[2];
147    boolean mExpanded;
148    boolean mExpandedVisible;
149
150    // the date view
151    DateView mDateView;
152
153    // for immersive activities
154    private View mIntruderAlertView;
155
156    // on-screen navigation buttons
157    private NavigationBarView mNavigationBarView = null;
158
159    // the tracker view
160    TrackingView mTrackingView;
161    WindowManager.LayoutParams mTrackingParams;
162    int mTrackingPosition; // the position of the top of the tracking view.
163    private boolean mPanelSlightlyVisible;
164
165    // ticker
166    private Ticker mTicker;
167    private View mTickerView;
168    private boolean mTicking;
169
170    // Recent applications
171    private RecentsPanelView mRecentsPanel;
172
173    // Tracking finger for opening/closing.
174    int mEdgeBorder; // corresponds to R.dimen.status_bar_edge_ignore
175    boolean mTracking;
176    VelocityTracker mVelocityTracker;
177
178    static final int ANIM_FRAME_DURATION = (1000/60);
179
180    boolean mAnimating;
181    long mCurAnimationTime;
182    float mDisplayHeight;
183    float mAnimY;
184    float mAnimVel;
185    float mAnimAccel;
186    long mAnimLastTime;
187    boolean mAnimatingReveal = false;
188    int mViewDelta;
189    int[] mAbsPos = new int[2];
190
191    // for disabling the status bar
192    int mDisabled = 0;
193
194    private class ExpandedDialog extends Dialog {
195        ExpandedDialog(Context context) {
196            super(context, com.android.internal.R.style.Theme_Light_NoTitleBar);
197        }
198
199        @Override
200        public boolean dispatchKeyEvent(KeyEvent event) {
201            boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
202            switch (event.getKeyCode()) {
203            case KeyEvent.KEYCODE_BACK:
204                if (!down) {
205                    animateCollapse();
206                }
207                return true;
208            }
209            return super.dispatchKeyEvent(event);
210        }
211    }
212
213    @Override
214    public void start() {
215        mDisplay = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
216                .getDefaultDisplay();
217
218        mWindowManager = IWindowManager.Stub.asInterface(
219                ServiceManager.getService(Context.WINDOW_SERVICE));
220
221        super.start();
222
223        addNavigationBar();
224
225        //addIntruderView();
226
227        // Lastly, call to the icon policy to install/update all the icons.
228        mIconPolicy = new PhoneStatusBarPolicy(mContext);
229    }
230
231    // ================================================================================
232    // Constructing the view
233    // ================================================================================
234    protected View makeStatusBarView() {
235        final Context context = mContext;
236
237        Resources res = context.getResources();
238
239        mIconSize = res.getDimensionPixelSize(com.android.internal.R.dimen.status_bar_icon_size);
240
241        ExpandedView expanded = (ExpandedView)View.inflate(context,
242                R.layout.status_bar_expanded, null);
243        expanded.mService = this;
244
245        mIntruderAlertView = View.inflate(context, R.layout.intruder_alert, null);
246        mIntruderAlertView.setVisibility(View.GONE);
247        mIntruderAlertView.setClickable(true);
248
249        try {
250            boolean showNav = res.getBoolean(com.android.internal.R.bool.config_showNavigationBar);
251            if (showNav) {
252                mNavigationBarView =
253                    (NavigationBarView) View.inflate(context, R.layout.navigation_bar, null);
254            }
255        } catch (Resources.NotFoundException ex) {
256            // no nav bar for you
257        }
258
259        PhoneStatusBarView sb = (PhoneStatusBarView)View.inflate(context,
260                R.layout.status_bar, null);
261        sb.mService = this;
262
263        // figure out which pixel-format to use for the status bar.
264        mPixelFormat = PixelFormat.TRANSLUCENT;
265        Drawable bg = sb.getBackground();
266        if (bg != null) {
267            mPixelFormat = bg.getOpacity();
268        }
269
270        mStatusBarView = sb;
271        mStatusIcons = (LinearLayout)sb.findViewById(R.id.statusIcons);
272        mNotificationIcons = (IconMerger)sb.findViewById(R.id.notificationIcons);
273        mIcons = (LinearLayout)sb.findViewById(R.id.icons);
274        mTickerView = sb.findViewById(R.id.ticker);
275        mDateView = (DateView)sb.findViewById(R.id.date);
276
277        mExpandedDialog = new ExpandedDialog(context);
278        mExpandedView = expanded;
279        mExpandedContents = expanded.findViewById(R.id.notificationLinearLayout);
280        mOngoingTitle = (TextView)expanded.findViewById(R.id.ongoingTitle);
281        mOngoingItems = (ViewGroup)expanded.findViewById(R.id.ongoingItems);
282        mLatestTitle = (TextView)expanded.findViewById(R.id.latestTitle);
283        mLatestItems = (ViewGroup)expanded.findViewById(R.id.latestItems);
284        mNoNotificationsTitle = (TextView)expanded.findViewById(R.id.noNotificationsTitle);
285        mClearButton = (TextView)expanded.findViewById(R.id.clear_all_button);
286        mClearButton.setOnClickListener(mClearButtonListener);
287        mScrollView = (ScrollView)expanded.findViewById(R.id.scroll);
288        mNotificationLinearLayout = expanded.findViewById(R.id.notificationLinearLayout);
289
290        mOngoingTitle.setVisibility(View.GONE);
291        mLatestTitle.setVisibility(View.GONE);
292
293        mTicker = new MyTicker(context, sb);
294
295        TickerView tickerView = (TickerView)sb.findViewById(R.id.tickerText);
296        tickerView.mTicker = mTicker;
297
298        mTrackingView = (TrackingView)View.inflate(context, R.layout.status_bar_tracking, null);
299        mTrackingView.mService = this;
300        mCloseView = (CloseDragHandle)mTrackingView.findViewById(R.id.close);
301        mCloseView.mService = this;
302
303        mEdgeBorder = res.getDimensionPixelSize(R.dimen.status_bar_edge_ignore);
304
305        // set the inital view visibility
306        setAreThereNotifications();
307        mDateView.setVisibility(View.INVISIBLE);
308
309        // Recents Panel
310        updateRecentsPanel();
311
312        // receive broadcasts
313        IntentFilter filter = new IntentFilter();
314        filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
315        filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
316        filter.addAction(Intent.ACTION_SCREEN_OFF);
317        context.registerReceiver(mBroadcastReceiver, filter);
318
319        return sb;
320    }
321
322    protected WindowManager.LayoutParams getRecentsLayoutParams(LayoutParams layoutParams) {
323        boolean translucent = false;
324        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
325                layoutParams.width,
326                layoutParams.height,
327                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
328                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
329                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
330                | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
331                | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
332                (translucent ? PixelFormat.OPAQUE : PixelFormat.TRANSLUCENT));
333        lp.gravity = Gravity.BOTTOM | Gravity.LEFT;
334        lp.setTitle("RecentsPanel");
335        lp.windowAnimations = R.style.Animation_RecentPanel;
336        lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED
337        | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING;
338        return lp;
339    }
340
341    protected void updateRecentsPanel() {
342        // Recents Panel
343        boolean visible = false;
344        if (mRecentsPanel != null) {
345            visible = mRecentsPanel.getVisibility() == View.VISIBLE;
346            WindowManagerImpl.getDefault().removeView(mRecentsPanel);
347        }
348
349        // Provide RecentsPanelView with a temporary parent to allow layout params to work.
350        LinearLayout tmpRoot = new LinearLayout(mContext);
351        mRecentsPanel = (RecentsPanelView) LayoutInflater.from(mContext).inflate(
352                R.layout.status_bar_recent_panel, tmpRoot, false);
353
354        mRecentsPanel.setOnTouchListener(new TouchOutsideListener(MSG_CLOSE_RECENTS_PANEL,
355                mRecentsPanel));
356        mRecentsPanel.setVisibility(View.GONE);
357        WindowManager.LayoutParams lp = getRecentsLayoutParams(mRecentsPanel.getLayoutParams());
358
359        WindowManagerImpl.getDefault().addView(mRecentsPanel, lp);
360        mRecentsPanel.setBar(this);
361        if (visible) {
362            // need to set visibility to View.GONE earlier since that
363            // triggers refreshing application list
364            mRecentsPanel.setVisibility(View.VISIBLE);
365            mRecentsPanel.show(true, false);
366        }
367
368    }
369
370    protected int getStatusBarGravity() {
371        return Gravity.TOP | Gravity.FILL_HORIZONTAL;
372    }
373
374    public int getStatusBarHeight() {
375        final Resources res = mContext.getResources();
376        return res.getDimensionPixelSize(com.android.internal.R.dimen.status_bar_height);
377    }
378
379    private View.OnClickListener mRecentsClickListener = new View.OnClickListener() {
380        public void onClick(View v) {
381            toggleRecentApps();
382        }
383    };
384
385    // For small-screen devices (read: phones) that lack hardware navigation buttons
386    private void addNavigationBar() {
387        if (mNavigationBarView == null) return;
388
389        mNavigationBarView.reorient();
390
391        mNavigationBarView.getRecentsButton().setOnClickListener(mRecentsClickListener);
392
393        WindowManagerImpl.getDefault().addView(
394                mNavigationBarView, getNavigationBarLayoutParams());
395    }
396
397    private void repositionNavigationBar() {
398        if (mNavigationBarView == null) return;
399
400        mNavigationBarView.reorient();
401
402        mNavigationBarView.getRecentsButton().setOnClickListener(mRecentsClickListener);
403
404        WindowManagerImpl.getDefault().updateViewLayout(
405                mNavigationBarView, getNavigationBarLayoutParams());
406    }
407
408    private WindowManager.LayoutParams getNavigationBarLayoutParams() {
409        final int rotation = mDisplay.getRotation();
410        final boolean sideways =
411            (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270);
412
413        final Resources res = mContext.getResources();
414        final int size = res.getDimensionPixelSize(R.dimen.navigation_bar_size);
415
416        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
417                sideways ? size : ViewGroup.LayoutParams.MATCH_PARENT,
418                sideways ? ViewGroup.LayoutParams.MATCH_PARENT : size,
419                WindowManager.LayoutParams.TYPE_NAVIGATION_BAR,
420                    0
421                    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
422                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
423                    | WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
424                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
425                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
426                    | WindowManager.LayoutParams.FLAG_SLIPPERY,
427                PixelFormat.TRANSLUCENT);
428
429        lp.setTitle("NavigationBar");
430        switch (rotation) {
431            case Surface.ROTATION_90:
432                // device has been turned 90deg counter-clockwise
433                lp.gravity = Gravity.RIGHT | Gravity.FILL_VERTICAL;
434                break;
435            case Surface.ROTATION_270:
436                // device has been turned 90deg clockwise
437                lp.gravity = (NavigationBarView.NAVBAR_ALWAYS_AT_RIGHT ? Gravity.RIGHT
438                                                                       : Gravity.LEFT)
439                             | Gravity.FILL_VERTICAL;
440                break;
441            default:
442                lp.gravity = Gravity.BOTTOM | Gravity.FILL_HORIZONTAL;
443                break;
444        }
445        lp.windowAnimations = 0;
446
447        return lp;
448    }
449
450    private void addIntruderView() {
451        final int height = getStatusBarHeight();
452
453        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
454                ViewGroup.LayoutParams.MATCH_PARENT,
455                ViewGroup.LayoutParams.WRAP_CONTENT,
456                WindowManager.LayoutParams.TYPE_STATUS_BAR_SUB_PANEL,
457                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
458                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
459                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
460                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
461                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
462                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH,
463                PixelFormat.TRANSLUCENT);
464        lp.gravity = Gravity.TOP | Gravity.FILL_HORIZONTAL;
465        lp.y += height * 1.5; // FIXME
466        lp.setTitle("IntruderAlert");
467        lp.windowAnimations = com.android.internal.R.style.Animation_StatusBar_IntruderAlert;
468
469        WindowManagerImpl.getDefault().addView(mIntruderAlertView, lp);
470    }
471
472    public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon) {
473        if (SPEW) Slog.d(TAG, "addIcon slot=" + slot + " index=" + index + " viewIndex=" + viewIndex
474                + " icon=" + icon);
475        StatusBarIconView view = new StatusBarIconView(mContext, slot, null);
476        view.set(icon);
477        mStatusIcons.addView(view, viewIndex, new LinearLayout.LayoutParams(mIconSize, mIconSize));
478    }
479
480    public void updateIcon(String slot, int index, int viewIndex,
481            StatusBarIcon old, StatusBarIcon icon) {
482        if (SPEW) Slog.d(TAG, "updateIcon slot=" + slot + " index=" + index + " viewIndex=" + viewIndex
483                + " old=" + old + " icon=" + icon);
484        StatusBarIconView view = (StatusBarIconView)mStatusIcons.getChildAt(viewIndex);
485        view.set(icon);
486    }
487
488    public void removeIcon(String slot, int index, int viewIndex) {
489        if (SPEW) Slog.d(TAG, "removeIcon slot=" + slot + " index=" + index + " viewIndex=" + viewIndex);
490        mStatusIcons.removeViewAt(viewIndex);
491    }
492
493    public void addNotification(IBinder key, StatusBarNotification notification) {
494        StatusBarIconView iconView = addNotificationViews(key, notification);
495        if (iconView == null) return;
496
497        boolean immersive = false;
498        try {
499            immersive = ActivityManagerNative.getDefault().isTopActivityImmersive();
500            Slog.d(TAG, "Top activity is " + (immersive?"immersive":"not immersive"));
501        } catch (RemoteException ex) {
502        }
503        if (immersive) {
504            if ((notification.notification.flags & Notification.FLAG_HIGH_PRIORITY) != 0) {
505                Slog.d(TAG, "Presenting high-priority notification in immersive activity");
506                // special new transient ticker mode
507                // 1. Populate mIntruderAlertView
508
509                ImageView alertIcon = (ImageView) mIntruderAlertView.findViewById(R.id.alertIcon);
510                TextView alertText = (TextView) mIntruderAlertView.findViewById(R.id.alertText);
511                alertIcon.setImageDrawable(StatusBarIconView.getIcon(
512                    alertIcon.getContext(),
513                    iconView.getStatusBarIcon()));
514                alertText.setText(notification.notification.tickerText);
515
516                View button = mIntruderAlertView.findViewById(R.id.intruder_alert_content);
517                button.setOnClickListener(
518                    new Launcher(notification.notification.contentIntent,
519                        notification.pkg, notification.tag, notification.id));
520
521                // 2. Animate mIntruderAlertView in
522                mHandler.sendEmptyMessage(MSG_SHOW_INTRUDER);
523
524                // 3. Set alarm to age the notification off (TODO)
525                mHandler.removeMessages(MSG_HIDE_INTRUDER);
526                mHandler.sendEmptyMessageDelayed(MSG_HIDE_INTRUDER, INTRUDER_ALERT_DECAY_MS);
527            }
528        } else if (notification.notification.fullScreenIntent != null) {
529            // not immersive & a full-screen alert should be shown
530            Slog.d(TAG, "Notification has fullScreenIntent and activity is not immersive;"
531                    + " sending fullScreenIntent");
532            try {
533                notification.notification.fullScreenIntent.send();
534            } catch (PendingIntent.CanceledException e) {
535            }
536        } else {
537            // usual case: status bar visible & not immersive
538
539            // show the ticker
540            tick(notification);
541        }
542
543        // Recalculate the position of the sliding windows and the titles.
544        setAreThereNotifications();
545        updateExpandedViewPos(EXPANDED_LEAVE_ALONE);
546    }
547
548    public void updateNotification(IBinder key, StatusBarNotification notification) {
549        Slog.d(TAG, "updateNotification key=" + key + " notification=" + notification);
550
551        NotificationData oldList;
552        NotificationData.Entry oldEntry = mOngoing.findByKey(key);
553        if (oldEntry != null) {
554            oldList = mOngoing;
555        } else {
556            oldEntry = mLatest.findByKey(key);
557            if (oldEntry == null) {
558                Slog.w(TAG, "updateNotification for unknown key: " + key);
559                return;
560            }
561            oldList = mLatest;
562        }
563        final StatusBarNotification oldNotification = oldEntry.notification;
564        final RemoteViews oldContentView = oldNotification.notification.contentView;
565
566        final RemoteViews contentView = notification.notification.contentView;
567
568        if (DEBUG) {
569            Slog.d(TAG, "old notification: when=" + oldNotification.notification.when
570                    + " ongoing=" + oldNotification.isOngoing()
571                    + " expanded=" + oldEntry.expanded
572                    + " contentView=" + oldContentView
573                    + " rowParent=" + oldEntry.row.getParent());
574            Slog.d(TAG, "new notification: when=" + notification.notification.when
575                    + " ongoing=" + oldNotification.isOngoing()
576                    + " contentView=" + contentView);
577        }
578
579
580        // Can we just reapply the RemoteViews in place?  If when didn't change, the order
581        // didn't change.
582        boolean contentsUnchanged = oldEntry.expanded != null
583                && contentView != null && oldContentView != null
584                && contentView.getPackage() != null
585                && oldContentView.getPackage() != null
586                && oldContentView.getPackage().equals(contentView.getPackage())
587                && oldContentView.getLayoutId() == contentView.getLayoutId();
588        ViewGroup rowParent = (ViewGroup) oldEntry.row.getParent();
589        boolean orderUnchanged = notification.notification.when==oldNotification.notification.when
590                && notification.priority == oldNotification.priority;
591                // priority now encompasses isOngoing()
592        boolean isLastAnyway = rowParent.indexOfChild(oldEntry.row) == rowParent.getChildCount()-1;
593        if (contentsUnchanged && (orderUnchanged || isLastAnyway)) {
594            if (DEBUG) Slog.d(TAG, "reusing notification for key: " + key);
595            oldEntry.notification = notification;
596            try {
597                // Reapply the RemoteViews
598                contentView.reapply(mContext, oldEntry.content);
599                // update the contentIntent
600                final PendingIntent contentIntent = notification.notification.contentIntent;
601                if (contentIntent != null) {
602                    oldEntry.content.setOnClickListener(new Launcher(contentIntent,
603                                notification.pkg, notification.tag, notification.id));
604                } else {
605                    oldEntry.content.setOnClickListener(null);
606                }
607                // Update the icon.
608                final StatusBarIcon ic = new StatusBarIcon(notification.pkg,
609                        notification.notification.icon, notification.notification.iconLevel,
610                        notification.notification.number, notification.notification.tickerText);
611                if (!oldEntry.icon.set(ic)) {
612                    handleNotificationError(key, notification, "Couldn't update icon: " + ic);
613                    return;
614                }
615            }
616            catch (RuntimeException e) {
617                // It failed to add cleanly.  Log, and remove the view from the panel.
618                Slog.w(TAG, "Couldn't reapply views for package " + contentView.getPackage(), e);
619                removeNotificationViews(key);
620                addNotificationViews(key, notification);
621            }
622        } else {
623            if (SPEW) Slog.d(TAG, "not reusing notification");
624            removeNotificationViews(key);
625            addNotificationViews(key, notification);
626        }
627
628        // Restart the ticker if it's still running
629        if (notification.notification.tickerText != null
630                && !TextUtils.equals(notification.notification.tickerText,
631                    oldEntry.notification.notification.tickerText)) {
632            tick(notification);
633        }
634
635        // Recalculate the position of the sliding windows and the titles.
636        setAreThereNotifications();
637        updateExpandedViewPos(EXPANDED_LEAVE_ALONE);
638    }
639
640    public void removeNotification(IBinder key) {
641        if (SPEW) Slog.d(TAG, "removeNotification key=" + key);
642        StatusBarNotification old = removeNotificationViews(key);
643
644        if (old != null) {
645            // Cancel the ticker if it's still running
646            mTicker.removeEntry(old);
647
648            // Recalculate the position of the sliding windows and the titles.
649            setAreThereNotifications();
650            updateExpandedViewPos(EXPANDED_LEAVE_ALONE);
651        }
652    }
653
654    private int chooseIconIndex(boolean isOngoing, int viewIndex) {
655        final int latestSize = mLatest.size();
656        if (isOngoing) {
657            return latestSize + (mOngoing.size() - viewIndex);
658        } else {
659            return latestSize - viewIndex;
660        }
661    }
662
663    @Override
664    protected void onConfigurationChanged(Configuration newConfig) {
665        updateRecentsPanel();
666    }
667
668
669    View[] makeNotificationView(StatusBarNotification notification, ViewGroup parent) {
670        Notification n = notification.notification;
671        RemoteViews remoteViews = n.contentView;
672        if (remoteViews == null) {
673            return null;
674        }
675
676        // create the row view
677        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(
678                Context.LAYOUT_INFLATER_SERVICE);
679        View row = inflater.inflate(R.layout.status_bar_notification_row, parent, false);
680
681        // wire up the veto button
682        View vetoButton = row.findViewById(R.id.veto);
683        if (notification.isClearable()) {
684            final String _pkg = notification.pkg;
685            final String _tag = notification.tag;
686            final int _id = notification.id;
687            vetoButton.setOnClickListener(new View.OnClickListener() {
688                    public void onClick(View v) {
689                        try {
690                            mBarService.onNotificationClear(_pkg, _tag, _id);
691                        } catch (RemoteException ex) {
692                            // system process is dead if we're here.
693                        }
694                    }
695                });
696        } else {
697            if ((notification.notification.flags & Notification.FLAG_ONGOING_EVENT) == 0) {
698                vetoButton.setVisibility(View.INVISIBLE);
699            } else {
700                vetoButton.setVisibility(View.GONE);
701            }
702        }
703
704        // the large icon
705        ImageView largeIcon = (ImageView)row.findViewById(R.id.large_icon);
706        if (notification.notification.largeIcon != null) {
707            largeIcon.setImageBitmap(notification.notification.largeIcon);
708        } else {
709            largeIcon.getLayoutParams().width = 0;
710            largeIcon.setVisibility(View.INVISIBLE);
711        }
712
713        // bind the click event to the content area
714        ViewGroup content = (ViewGroup)row.findViewById(R.id.content);
715        content.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
716        content.setOnFocusChangeListener(mFocusChangeListener);
717        PendingIntent contentIntent = n.contentIntent;
718        if (contentIntent != null) {
719            content.setOnClickListener(new Launcher(contentIntent, notification.pkg,
720                        notification.tag, notification.id));
721        } else {
722            content.setOnClickListener(null);
723        }
724
725        View expanded = null;
726        Exception exception = null;
727        try {
728            expanded = remoteViews.apply(mContext, content);
729        }
730        catch (RuntimeException e) {
731            exception = e;
732        }
733        if (expanded == null) {
734            String ident = notification.pkg + "/0x" + Integer.toHexString(notification.id);
735            Slog.e(TAG, "couldn't inflate view for notification " + ident, exception);
736            return null;
737        } else {
738            content.addView(expanded);
739            row.setDrawingCacheEnabled(true);
740        }
741
742        return new View[] { row, content, expanded };
743    }
744
745    StatusBarIconView addNotificationViews(IBinder key, StatusBarNotification notification) {
746        NotificationData list;
747        ViewGroup parent;
748        final boolean isOngoing = notification.isOngoing();
749        if (isOngoing) {
750            list = mOngoing;
751            parent = mOngoingItems;
752        } else {
753            list = mLatest;
754            parent = mLatestItems;
755        }
756        // Construct the expanded view.
757        final View[] views = makeNotificationView(notification, parent);
758        if (views == null) {
759            handleNotificationError(key, notification, "Couldn't expand RemoteViews for: "
760                    + notification);
761            return null;
762        }
763        final View row = views[0];
764        final View content = views[1];
765        final View expanded = views[2];
766        // Construct the icon.
767        final StatusBarIconView iconView = new StatusBarIconView(mContext,
768                notification.pkg + "/0x" + Integer.toHexString(notification.id),
769                notification.notification);
770        final StatusBarIcon ic = new StatusBarIcon(notification.pkg, notification.notification.icon,
771                    notification.notification.iconLevel, notification.notification.number,
772                    notification.notification.tickerText);
773        if (!iconView.set(ic)) {
774            handleNotificationError(key, notification, "Coulding create icon: " + ic);
775            return null;
776        }
777        // Add the expanded view.
778        final int viewIndex = list.add(key, notification, row, content, expanded, iconView);
779        parent.addView(row, viewIndex);
780        // Add the icon.
781        final int iconIndex = chooseIconIndex(isOngoing, viewIndex);
782        mNotificationIcons.addView(iconView, iconIndex);
783        return iconView;
784    }
785
786    StatusBarNotification removeNotificationViews(IBinder key) {
787        NotificationData.Entry entry = mOngoing.remove(key);
788        if (entry == null) {
789            entry = mLatest.remove(key);
790            if (entry == null) {
791                Slog.w(TAG, "removeNotification for unknown key: " + key);
792                return null;
793            }
794        }
795        // Remove the expanded view.
796        ((ViewGroup)entry.row.getParent()).removeView(entry.row);
797        // Remove the icon.
798        ((ViewGroup)entry.icon.getParent()).removeView(entry.icon);
799
800        return entry.notification;
801    }
802
803    private void setAreThereNotifications() {
804        boolean ongoing = mOngoing.hasVisibleItems();
805        boolean latest = mLatest.hasVisibleItems();
806
807        // (no ongoing notifications are clearable)
808        if (mLatest.hasClearableItems()) {
809            mClearButton.setVisibility(View.VISIBLE);
810        } else {
811            mClearButton.setVisibility(View.INVISIBLE);
812        }
813
814        mOngoingTitle.setVisibility(ongoing ? View.VISIBLE : View.GONE);
815        mLatestTitle.setVisibility(latest ? View.VISIBLE : View.GONE);
816
817        if (ongoing || latest) {
818            mNoNotificationsTitle.setVisibility(View.GONE);
819        } else {
820            mNoNotificationsTitle.setVisibility(View.VISIBLE);
821        }
822    }
823
824
825    /**
826     * State is one or more of the DISABLE constants from StatusBarManager.
827     */
828    public void disable(int state) {
829        final int old = mDisabled;
830        final int diff = state ^ old;
831        mDisabled = state;
832
833        if ((diff & StatusBarManager.DISABLE_EXPAND) != 0) {
834            if ((state & StatusBarManager.DISABLE_EXPAND) != 0) {
835                Slog.d(TAG, "DISABLE_EXPAND: yes");
836                animateCollapse();
837            }
838        }
839        if ((diff & StatusBarManager.DISABLE_NOTIFICATION_ICONS) != 0) {
840            if ((state & StatusBarManager.DISABLE_NOTIFICATION_ICONS) != 0) {
841                Slog.d(TAG, "DISABLE_NOTIFICATION_ICONS: yes");
842                if (mTicking) {
843                    mTicker.halt();
844                } else {
845                    setNotificationIconVisibility(false, com.android.internal.R.anim.fade_out);
846                }
847            } else {
848                Slog.d(TAG, "DISABLE_NOTIFICATION_ICONS: no");
849                if (!mExpandedVisible) {
850                    setNotificationIconVisibility(true, com.android.internal.R.anim.fade_in);
851                }
852            }
853        } else if ((diff & StatusBarManager.DISABLE_NOTIFICATION_TICKER) != 0) {
854            if (mTicking && (state & StatusBarManager.DISABLE_NOTIFICATION_TICKER) != 0) {
855                Slog.d(TAG, "DISABLE_NOTIFICATION_TICKER: yes");
856                mTicker.halt();
857            }
858        }
859    }
860
861    /**
862     * All changes to the status bar and notifications funnel through here and are batched.
863     */
864    private class H extends Handler {
865        public void handleMessage(Message m) {
866            switch (m.what) {
867                case MSG_ANIMATE:
868                    doAnimation();
869                    break;
870                case MSG_ANIMATE_REVEAL:
871                    doRevealAnimation();
872                    break;
873                case MSG_SHOW_INTRUDER:
874                    setIntruderAlertVisibility(true);
875                    break;
876                case MSG_HIDE_INTRUDER:
877                    setIntruderAlertVisibility(false);
878                    break;
879                case MSG_OPEN_RECENTS_PANEL:
880                    if (DEBUG) Slog.d(TAG, "opening recents panel");
881                    if (mRecentsPanel != null) {
882                        disable(StatusBarManager.DISABLE_BACK);
883                        mRecentsPanel.setVisibility(View.VISIBLE);
884                        mRecentsPanel.show(true, true);
885                    }
886                    break;
887                case MSG_CLOSE_RECENTS_PANEL:
888                    if (DEBUG) Slog.d(TAG, "closing recents panel");
889                    if (mRecentsPanel != null && mRecentsPanel.isShowing()) {
890                        disable(StatusBarManager.DISABLE_NONE);
891                        mRecentsPanel.show(false, true);
892                    }
893                    break;
894            }
895        }
896    }
897
898    View.OnFocusChangeListener mFocusChangeListener = new View.OnFocusChangeListener() {
899        public void onFocusChange(View v, boolean hasFocus) {
900            // Because 'v' is a ViewGroup, all its children will be (un)selected
901            // too, which allows marqueeing to work.
902            v.setSelected(hasFocus);
903        }
904    };
905
906    private void makeExpandedVisible() {
907        if (SPEW) Slog.d(TAG, "Make expanded visible: expanded visible=" + mExpandedVisible);
908        if (mExpandedVisible) {
909            return;
910        }
911        mExpandedVisible = true;
912        visibilityChanged(true);
913
914        updateExpandedViewPos(EXPANDED_LEAVE_ALONE);
915        mExpandedParams.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
916        mExpandedParams.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
917        mExpandedDialog.getWindow().setAttributes(mExpandedParams);
918        mExpandedView.requestFocus(View.FOCUS_FORWARD);
919        mTrackingView.setVisibility(View.VISIBLE);
920
921        if (!mTicking) {
922            setDateViewVisibility(true, com.android.internal.R.anim.fade_in);
923        }
924    }
925
926    public void animateExpand() {
927        if (SPEW) Slog.d(TAG, "Animate expand: expanded=" + mExpanded);
928        if ((mDisabled & StatusBarManager.DISABLE_EXPAND) != 0) {
929            return ;
930        }
931        if (mExpanded) {
932            return;
933        }
934
935        prepareTracking(0, true);
936        performFling(0, 2000.0f, true);
937    }
938
939    public void animateCollapse() {
940        animateCollapse(false);
941    }
942
943    public void animateCollapse(boolean excludeRecents) {
944        if (SPEW) {
945            Slog.d(TAG, "animateCollapse(): mExpanded=" + mExpanded
946                    + " mExpandedVisible=" + mExpandedVisible
947                    + " mExpanded=" + mExpanded
948                    + " mAnimating=" + mAnimating
949                    + " mAnimY=" + mAnimY
950                    + " mAnimVel=" + mAnimVel);
951        }
952
953        if (!excludeRecents) {
954            mHandler.removeMessages(MSG_CLOSE_RECENTS_PANEL);
955            mHandler.sendEmptyMessage(MSG_CLOSE_RECENTS_PANEL);
956        }
957
958        if (!mExpandedVisible) {
959            return;
960        }
961
962        int y;
963        if (mAnimating) {
964            y = (int)mAnimY;
965        } else {
966            y = mDisplay.getHeight()-1;
967        }
968        // Let the fling think that we're open so it goes in the right direction
969        // and doesn't try to re-open the windowshade.
970        mExpanded = true;
971        prepareTracking(y, false);
972        performFling(y, -2000.0f, true);
973    }
974
975    void performExpand() {
976        if (SPEW) Slog.d(TAG, "performExpand: mExpanded=" + mExpanded);
977        if ((mDisabled & StatusBarManager.DISABLE_EXPAND) != 0) {
978            return ;
979        }
980        if (mExpanded) {
981            return;
982        }
983
984        mExpanded = true;
985        makeExpandedVisible();
986        updateExpandedViewPos(EXPANDED_FULL_OPEN);
987
988        if (false) postStartTracing();
989    }
990
991    void performCollapse() {
992        if (SPEW) Slog.d(TAG, "performCollapse: mExpanded=" + mExpanded
993                + " mExpandedVisible=" + mExpandedVisible);
994
995        if (!mExpandedVisible) {
996            return;
997        }
998        mExpandedVisible = false;
999        visibilityChanged(false);
1000        mExpandedParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
1001        mExpandedParams.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
1002        mExpandedDialog.getWindow().setAttributes(mExpandedParams);
1003        mTrackingView.setVisibility(View.GONE);
1004
1005        if ((mDisabled & StatusBarManager.DISABLE_NOTIFICATION_ICONS) == 0) {
1006            setNotificationIconVisibility(true, com.android.internal.R.anim.fade_in);
1007        }
1008        setDateViewVisibility(false, com.android.internal.R.anim.fade_out);
1009
1010        if (!mExpanded) {
1011            return;
1012        }
1013        mExpanded = false;
1014    }
1015
1016    void doAnimation() {
1017        if (mAnimating) {
1018            if (SPEW) Slog.d(TAG, "doAnimation");
1019            if (SPEW) Slog.d(TAG, "doAnimation before mAnimY=" + mAnimY);
1020            incrementAnim();
1021            if (SPEW) Slog.d(TAG, "doAnimation after  mAnimY=" + mAnimY);
1022            if (mAnimY >= mDisplay.getHeight()-1) {
1023                if (SPEW) Slog.d(TAG, "Animation completed to expanded state.");
1024                mAnimating = false;
1025                updateExpandedViewPos(EXPANDED_FULL_OPEN);
1026                performExpand();
1027            }
1028            else if (mAnimY < mStatusBarView.getHeight()) {
1029                if (SPEW) Slog.d(TAG, "Animation completed to collapsed state.");
1030                mAnimating = false;
1031                updateExpandedViewPos(0);
1032                performCollapse();
1033            }
1034            else {
1035                updateExpandedViewPos((int)mAnimY);
1036                mCurAnimationTime += ANIM_FRAME_DURATION;
1037                mHandler.sendMessageAtTime(mHandler.obtainMessage(MSG_ANIMATE), mCurAnimationTime);
1038            }
1039        }
1040    }
1041
1042    void stopTracking() {
1043        mTracking = false;
1044        mVelocityTracker.recycle();
1045        mVelocityTracker = null;
1046    }
1047
1048    void incrementAnim() {
1049        long now = SystemClock.uptimeMillis();
1050        float t = ((float)(now - mAnimLastTime)) / 1000;            // ms -> s
1051        final float y = mAnimY;
1052        final float v = mAnimVel;                                   // px/s
1053        final float a = mAnimAccel;                                 // px/s/s
1054        mAnimY = y + (v*t) + (0.5f*a*t*t);                          // px
1055        mAnimVel = v + (a*t);                                       // px/s
1056        mAnimLastTime = now;                                        // ms
1057        //Slog.d(TAG, "y=" + y + " v=" + v + " a=" + a + " t=" + t + " mAnimY=" + mAnimY
1058        //        + " mAnimAccel=" + mAnimAccel);
1059    }
1060
1061    void doRevealAnimation() {
1062        final int h = mCloseView.getHeight() + mStatusBarView.getHeight();
1063        if (mAnimatingReveal && mAnimating && mAnimY < h) {
1064            incrementAnim();
1065            if (mAnimY >= h) {
1066                mAnimY = h;
1067                updateExpandedViewPos((int)mAnimY);
1068            } else {
1069                updateExpandedViewPos((int)mAnimY);
1070                mCurAnimationTime += ANIM_FRAME_DURATION;
1071                mHandler.sendMessageAtTime(mHandler.obtainMessage(MSG_ANIMATE_REVEAL),
1072                        mCurAnimationTime);
1073            }
1074        }
1075    }
1076
1077    void prepareTracking(int y, boolean opening) {
1078        mTracking = true;
1079        mVelocityTracker = VelocityTracker.obtain();
1080        if (opening) {
1081            mAnimAccel = 2000.0f;
1082            mAnimVel = 200;
1083            mAnimY = mStatusBarView.getHeight();
1084            updateExpandedViewPos((int)mAnimY);
1085            mAnimating = true;
1086            mAnimatingReveal = true;
1087            mHandler.removeMessages(MSG_ANIMATE);
1088            mHandler.removeMessages(MSG_ANIMATE_REVEAL);
1089            long now = SystemClock.uptimeMillis();
1090            mAnimLastTime = now;
1091            mCurAnimationTime = now + ANIM_FRAME_DURATION;
1092            mAnimating = true;
1093            mHandler.sendMessageAtTime(mHandler.obtainMessage(MSG_ANIMATE_REVEAL),
1094                    mCurAnimationTime);
1095            makeExpandedVisible();
1096        } else {
1097            // it's open, close it?
1098            if (mAnimating) {
1099                mAnimating = false;
1100                mHandler.removeMessages(MSG_ANIMATE);
1101            }
1102            updateExpandedViewPos(y + mViewDelta);
1103        }
1104    }
1105
1106    void performFling(int y, float vel, boolean always) {
1107        mAnimatingReveal = false;
1108        mDisplayHeight = mDisplay.getHeight();
1109
1110        mAnimY = y;
1111        mAnimVel = vel;
1112
1113        //Slog.d(TAG, "starting with mAnimY=" + mAnimY + " mAnimVel=" + mAnimVel);
1114
1115        if (mExpanded) {
1116            if (!always && (
1117                    vel > 200.0f
1118                    || (y > (mDisplayHeight-25) && vel > -200.0f))) {
1119                // We are expanded, but they didn't move sufficiently to cause
1120                // us to retract.  Animate back to the expanded position.
1121                mAnimAccel = 2000.0f;
1122                if (vel < 0) {
1123                    mAnimVel = 0;
1124                }
1125            }
1126            else {
1127                // We are expanded and are now going to animate away.
1128                mAnimAccel = -2000.0f;
1129                if (vel > 0) {
1130                    mAnimVel = 0;
1131                }
1132            }
1133        } else {
1134            if (always || (
1135                    vel > 200.0f
1136                    || (y > (mDisplayHeight/2) && vel > -200.0f))) {
1137                // We are collapsed, and they moved enough to allow us to
1138                // expand.  Animate in the notifications.
1139                mAnimAccel = 2000.0f;
1140                if (vel < 0) {
1141                    mAnimVel = 0;
1142                }
1143            }
1144            else {
1145                // We are collapsed, but they didn't move sufficiently to cause
1146                // us to retract.  Animate back to the collapsed position.
1147                mAnimAccel = -2000.0f;
1148                if (vel > 0) {
1149                    mAnimVel = 0;
1150                }
1151            }
1152        }
1153        //Slog.d(TAG, "mAnimY=" + mAnimY + " mAnimVel=" + mAnimVel
1154        //        + " mAnimAccel=" + mAnimAccel);
1155
1156        long now = SystemClock.uptimeMillis();
1157        mAnimLastTime = now;
1158        mCurAnimationTime = now + ANIM_FRAME_DURATION;
1159        mAnimating = true;
1160        mHandler.removeMessages(MSG_ANIMATE);
1161        mHandler.removeMessages(MSG_ANIMATE_REVEAL);
1162        mHandler.sendMessageAtTime(mHandler.obtainMessage(MSG_ANIMATE), mCurAnimationTime);
1163        stopTracking();
1164    }
1165
1166    boolean interceptTouchEvent(MotionEvent event) {
1167        if (SPEW) {
1168            Slog.d(TAG, "Touch: rawY=" + event.getRawY() + " event=" + event + " mDisabled="
1169                + mDisabled);
1170        }
1171
1172        if ((mDisabled & StatusBarManager.DISABLE_EXPAND) != 0) {
1173            return false;
1174        }
1175
1176        final int statusBarSize = mStatusBarView.getHeight();
1177        final int hitSize = statusBarSize*2;
1178        if (event.getAction() == MotionEvent.ACTION_DOWN) {
1179            final int y = (int)event.getRawY();
1180
1181            if (!mExpanded) {
1182                mViewDelta = statusBarSize - y;
1183            } else {
1184                mTrackingView.getLocationOnScreen(mAbsPos);
1185                mViewDelta = mAbsPos[1] + mTrackingView.getHeight() - y;
1186            }
1187            if ((!mExpanded && y < hitSize) ||
1188                    (mExpanded && y > (mDisplay.getHeight()-hitSize))) {
1189
1190                // We drop events at the edge of the screen to make the windowshade come
1191                // down by accident less, especially when pushing open a device with a keyboard
1192                // that rotates (like g1 and droid)
1193                int x = (int)event.getRawX();
1194                final int edgeBorder = mEdgeBorder;
1195                if (x >= edgeBorder && x < mDisplay.getWidth() - edgeBorder) {
1196                    prepareTracking(y, !mExpanded);// opening if we're not already fully visible
1197                    mVelocityTracker.addMovement(event);
1198                }
1199            }
1200        } else if (mTracking) {
1201            mVelocityTracker.addMovement(event);
1202            final int minY = statusBarSize + mCloseView.getHeight();
1203            if (event.getAction() == MotionEvent.ACTION_MOVE) {
1204                int y = (int)event.getRawY();
1205                if (mAnimatingReveal && y < minY) {
1206                    // nothing
1207                } else  {
1208                    mAnimatingReveal = false;
1209                    updateExpandedViewPos(y + mViewDelta);
1210                }
1211            } else if (event.getAction() == MotionEvent.ACTION_UP) {
1212                mVelocityTracker.computeCurrentVelocity(1000);
1213
1214                float yVel = mVelocityTracker.getYVelocity();
1215                boolean negative = yVel < 0;
1216
1217                float xVel = mVelocityTracker.getXVelocity();
1218                if (xVel < 0) {
1219                    xVel = -xVel;
1220                }
1221                if (xVel > 150.0f) {
1222                    xVel = 150.0f; // limit how much we care about the x axis
1223                }
1224
1225                float vel = (float)Math.hypot(yVel, xVel);
1226                if (negative) {
1227                    vel = -vel;
1228                }
1229
1230                performFling((int)event.getRawY(), vel, false);
1231            }
1232
1233        }
1234        return false;
1235    }
1236
1237    public void setLightsOn(boolean on) {
1238        Log.v(TAG, "lights " + (on ? "on" : "off"));
1239        if (!on) {
1240            // All we do for "lights out" mode on a phone is hide the status bar,
1241            // which the window manager does.  But we do need to hide the windowshade
1242            // on our own.
1243            animateCollapse();
1244        }
1245        notifyLightsChanged(on);
1246    }
1247
1248    private void notifyLightsChanged(boolean shown) {
1249        try {
1250            Slog.d(TAG, "lights " + (shown?"on":"out"));
1251            mWindowManager.statusBarVisibilityChanged(
1252                    shown ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN);
1253        } catch (RemoteException ex) {
1254        }
1255    }
1256
1257    public void topAppWindowChanged(boolean showMenu) {
1258        if (DEBUG) {
1259            Slog.d(TAG, (showMenu?"showing":"hiding") + " the MENU button");
1260        }
1261        if (mNavigationBarView != null) {
1262            mNavigationBarView.getMenuButton().setVisibility(showMenu
1263                ? View.VISIBLE : View.INVISIBLE);
1264        }
1265
1266        // See above re: lights-out policy for legacy apps.
1267        if (showMenu) setLightsOn(true);
1268    }
1269
1270    // Not supported
1271    public void setImeWindowStatus(IBinder token, int vis, int backDisposition) { }
1272    @Override
1273    public void setHardKeyboardStatus(boolean available, boolean enabled) { }
1274
1275    private class Launcher implements View.OnClickListener {
1276        private PendingIntent mIntent;
1277        private String mPkg;
1278        private String mTag;
1279        private int mId;
1280
1281        Launcher(PendingIntent intent, String pkg, String tag, int id) {
1282            mIntent = intent;
1283            mPkg = pkg;
1284            mTag = tag;
1285            mId = id;
1286        }
1287
1288        public void onClick(View v) {
1289            try {
1290                // The intent we are sending is for the application, which
1291                // won't have permission to immediately start an activity after
1292                // the user switches to home.  We know it is safe to do at this
1293                // point, so make sure new activity switches are now allowed.
1294                ActivityManagerNative.getDefault().resumeAppSwitches();
1295            } catch (RemoteException e) {
1296            }
1297
1298            if (mIntent != null) {
1299                int[] pos = new int[2];
1300                v.getLocationOnScreen(pos);
1301                Intent overlay = new Intent();
1302                overlay.setSourceBounds(
1303                        new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
1304                try {
1305                    mIntent.send(mContext, 0, overlay);
1306                } catch (PendingIntent.CanceledException e) {
1307                    // the stack trace isn't very helpful here.  Just log the exception message.
1308                    Slog.w(TAG, "Sending contentIntent failed: " + e);
1309                }
1310            }
1311
1312            try {
1313                mBarService.onNotificationClick(mPkg, mTag, mId);
1314            } catch (RemoteException ex) {
1315                // system process is dead if we're here.
1316            }
1317
1318            // close the shade if it was open
1319            animateCollapse();
1320
1321            // If this click was on the intruder alert, hide that instead
1322            mHandler.sendEmptyMessage(MSG_HIDE_INTRUDER);
1323        }
1324    }
1325
1326    private void tick(StatusBarNotification n) {
1327        // Show the ticker if one is requested. Also don't do this
1328        // until status bar window is attached to the window manager,
1329        // because...  well, what's the point otherwise?  And trying to
1330        // run a ticker without being attached will crash!
1331        if (n.notification.tickerText != null && mStatusBarView.getWindowToken() != null) {
1332            if (0 == (mDisabled & (StatusBarManager.DISABLE_NOTIFICATION_ICONS
1333                            | StatusBarManager.DISABLE_NOTIFICATION_TICKER))) {
1334                mTicker.addEntry(n);
1335            }
1336        }
1337    }
1338
1339    /**
1340     * Cancel this notification and tell the StatusBarManagerService / NotificationManagerService
1341     * about the failure.
1342     *
1343     * WARNING: this will call back into us.  Don't hold any locks.
1344     */
1345    void handleNotificationError(IBinder key, StatusBarNotification n, String message) {
1346        removeNotification(key);
1347        try {
1348            mBarService.onNotificationError(n.pkg, n.tag, n.id, n.uid, n.initialPid, message);
1349        } catch (RemoteException ex) {
1350            // The end is nigh.
1351        }
1352    }
1353
1354    private class MyTicker extends Ticker {
1355        MyTicker(Context context, View sb) {
1356            super(context, sb);
1357        }
1358
1359        @Override
1360        public void tickerStarting() {
1361            mTicking = true;
1362            mIcons.setVisibility(View.GONE);
1363            mTickerView.setVisibility(View.VISIBLE);
1364            mTickerView.startAnimation(loadAnim(com.android.internal.R.anim.push_up_in, null));
1365            mIcons.startAnimation(loadAnim(com.android.internal.R.anim.push_up_out, null));
1366            if (mExpandedVisible) {
1367                setDateViewVisibility(false, com.android.internal.R.anim.push_up_out);
1368            }
1369        }
1370
1371        @Override
1372        public void tickerDone() {
1373            mIcons.setVisibility(View.VISIBLE);
1374            mTickerView.setVisibility(View.GONE);
1375            mIcons.startAnimation(loadAnim(com.android.internal.R.anim.push_down_in, null));
1376            mTickerView.startAnimation(loadAnim(com.android.internal.R.anim.push_down_out,
1377                        mTickingDoneListener));
1378            if (mExpandedVisible) {
1379                setDateViewVisibility(true, com.android.internal.R.anim.push_down_in);
1380            }
1381        }
1382
1383        public void tickerHalting() {
1384            mIcons.setVisibility(View.VISIBLE);
1385            mTickerView.setVisibility(View.GONE);
1386            mIcons.startAnimation(loadAnim(com.android.internal.R.anim.fade_in, null));
1387            mTickerView.startAnimation(loadAnim(com.android.internal.R.anim.fade_out,
1388                        mTickingDoneListener));
1389            if (mExpandedVisible) {
1390                setDateViewVisibility(true, com.android.internal.R.anim.fade_in);
1391            }
1392        }
1393    }
1394
1395    Animation.AnimationListener mTickingDoneListener = new Animation.AnimationListener() {;
1396        public void onAnimationEnd(Animation animation) {
1397            mTicking = false;
1398        }
1399        public void onAnimationRepeat(Animation animation) {
1400        }
1401        public void onAnimationStart(Animation animation) {
1402        }
1403    };
1404
1405    private Animation loadAnim(int id, Animation.AnimationListener listener) {
1406        Animation anim = AnimationUtils.loadAnimation(mContext, id);
1407        if (listener != null) {
1408            anim.setAnimationListener(listener);
1409        }
1410        return anim;
1411    }
1412
1413    public String viewInfo(View v) {
1414        return "(" + v.getLeft() + "," + v.getTop() + ")(" + v.getRight() + "," + v.getBottom()
1415                + " " + v.getWidth() + "x" + v.getHeight() + ")";
1416    }
1417
1418    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1419        synchronized (mQueueLock) {
1420            pw.println("Current Status Bar state:");
1421            pw.println("  mExpanded=" + mExpanded
1422                    + ", mExpandedVisible=" + mExpandedVisible);
1423            pw.println("  mTicking=" + mTicking);
1424            pw.println("  mTracking=" + mTracking);
1425            pw.println("  mAnimating=" + mAnimating
1426                    + ", mAnimY=" + mAnimY + ", mAnimVel=" + mAnimVel
1427                    + ", mAnimAccel=" + mAnimAccel);
1428            pw.println("  mCurAnimationTime=" + mCurAnimationTime
1429                    + " mAnimLastTime=" + mAnimLastTime);
1430            pw.println("  mDisplayHeight=" + mDisplayHeight
1431                    + " mAnimatingReveal=" + mAnimatingReveal
1432                    + " mViewDelta=" + mViewDelta);
1433            pw.println("  mDisplayHeight=" + mDisplayHeight);
1434            pw.println("  mExpandedParams: " + mExpandedParams);
1435            pw.println("  mExpandedView: " + viewInfo(mExpandedView));
1436            pw.println("  mExpandedDialog: " + mExpandedDialog);
1437            pw.println("  mTrackingParams: " + mTrackingParams);
1438            pw.println("  mTrackingView: " + viewInfo(mTrackingView));
1439            pw.println("  mOngoingTitle: " + viewInfo(mOngoingTitle));
1440            pw.println("  mOngoingItems: " + viewInfo(mOngoingItems));
1441            pw.println("  mLatestTitle: " + viewInfo(mLatestTitle));
1442            pw.println("  mLatestItems: " + viewInfo(mLatestItems));
1443            pw.println("  mNoNotificationsTitle: " + viewInfo(mNoNotificationsTitle));
1444            pw.println("  mCloseView: " + viewInfo(mCloseView));
1445            pw.println("  mTickerView: " + viewInfo(mTickerView));
1446            pw.println("  mScrollView: " + viewInfo(mScrollView)
1447                    + " scroll " + mScrollView.getScrollX() + "," + mScrollView.getScrollY());
1448            pw.println("mNotificationLinearLayout: " + viewInfo(mNotificationLinearLayout));
1449        }
1450        /*
1451        synchronized (mNotificationData) {
1452            int N = mNotificationData.ongoingCount();
1453            pw.println("  ongoingCount.size=" + N);
1454            for (int i=0; i<N; i++) {
1455                StatusBarNotification n = mNotificationData.getOngoing(i);
1456                pw.println("    [" + i + "] key=" + n.key + " view=" + n.view);
1457                pw.println("           data=" + n.data);
1458            }
1459            N = mNotificationData.latestCount();
1460            pw.println("  ongoingCount.size=" + N);
1461            for (int i=0; i<N; i++) {
1462                StatusBarNotification n = mNotificationData.getLatest(i);
1463                pw.println("    [" + i + "] key=" + n.key + " view=" + n.view);
1464                pw.println("           data=" + n.data);
1465            }
1466        }
1467        */
1468
1469        if (false) {
1470            pw.println("see the logcat for a dump of the views we have created.");
1471            // must happen on ui thread
1472            mHandler.post(new Runnable() {
1473                    public void run() {
1474                        mStatusBarView.getLocationOnScreen(mAbsPos);
1475                        Slog.d(TAG, "mStatusBarView: ----- (" + mAbsPos[0] + "," + mAbsPos[1]
1476                                + ") " + mStatusBarView.getWidth() + "x"
1477                                + mStatusBarView.getHeight());
1478                        mStatusBarView.debug();
1479
1480                        mExpandedView.getLocationOnScreen(mAbsPos);
1481                        Slog.d(TAG, "mExpandedView: ----- (" + mAbsPos[0] + "," + mAbsPos[1]
1482                                + ") " + mExpandedView.getWidth() + "x"
1483                                + mExpandedView.getHeight());
1484                        mExpandedView.debug();
1485
1486                        mTrackingView.getLocationOnScreen(mAbsPos);
1487                        Slog.d(TAG, "mTrackingView: ----- (" + mAbsPos[0] + "," + mAbsPos[1]
1488                                + ") " + mTrackingView.getWidth() + "x"
1489                                + mTrackingView.getHeight());
1490                        mTrackingView.debug();
1491                    }
1492                });
1493        }
1494    }
1495
1496    void onBarViewAttached() {
1497        WindowManager.LayoutParams lp;
1498        int pixelFormat;
1499        Drawable bg;
1500
1501        /// ---------- Tracking View --------------
1502        pixelFormat = PixelFormat.RGBX_8888;
1503        bg = mTrackingView.getBackground();
1504        if (bg != null) {
1505            pixelFormat = bg.getOpacity();
1506        }
1507
1508        lp = new WindowManager.LayoutParams(
1509                ViewGroup.LayoutParams.MATCH_PARENT,
1510                ViewGroup.LayoutParams.MATCH_PARENT,
1511                WindowManager.LayoutParams.TYPE_STATUS_BAR_SUB_PANEL,
1512                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
1513                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
1514                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
1515                pixelFormat);
1516//        lp.token = mStatusBarView.getWindowToken();
1517        lp.gravity = Gravity.TOP | Gravity.FILL_HORIZONTAL;
1518        lp.setTitle("TrackingView");
1519        lp.y = mTrackingPosition;
1520        mTrackingParams = lp;
1521
1522        WindowManagerImpl.getDefault().addView(mTrackingView, lp);
1523    }
1524
1525    void onTrackingViewAttached() {
1526        WindowManager.LayoutParams lp;
1527        int pixelFormat;
1528        Drawable bg;
1529
1530        /// ---------- Expanded View --------------
1531        pixelFormat = PixelFormat.TRANSLUCENT;
1532
1533        final int disph = mDisplay.getHeight();
1534        lp = mExpandedDialog.getWindow().getAttributes();
1535        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
1536        lp.height = getExpandedHeight();
1537        lp.x = 0;
1538        mTrackingPosition = lp.y = -disph; // sufficiently large negative
1539        lp.type = WindowManager.LayoutParams.TYPE_STATUS_BAR_SUB_PANEL;
1540        lp.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
1541                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
1542                | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
1543                | WindowManager.LayoutParams.FLAG_DITHER
1544                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
1545        lp.format = pixelFormat;
1546        lp.gravity = Gravity.TOP | Gravity.FILL_HORIZONTAL;
1547        lp.setTitle("StatusBarExpanded");
1548        mExpandedDialog.getWindow().setAttributes(lp);
1549        mExpandedDialog.getWindow().setFormat(pixelFormat);
1550        mExpandedParams = lp;
1551
1552        mExpandedDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
1553        mExpandedDialog.setContentView(mExpandedView,
1554                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
1555                                           ViewGroup.LayoutParams.MATCH_PARENT));
1556        mExpandedDialog.getWindow().setBackgroundDrawable(null);
1557        mExpandedDialog.show();
1558        FrameLayout hack = (FrameLayout)mExpandedView.getParent();
1559    }
1560
1561    void setDateViewVisibility(boolean visible, int anim) {
1562        mDateView.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
1563        mDateView.startAnimation(loadAnim(anim, null));
1564    }
1565
1566    void setNotificationIconVisibility(boolean visible, int anim) {
1567        int old = mNotificationIcons.getVisibility();
1568        int v = visible ? View.VISIBLE : View.INVISIBLE;
1569        if (old != v) {
1570            mNotificationIcons.setVisibility(v);
1571            mNotificationIcons.startAnimation(loadAnim(anim, null));
1572        }
1573    }
1574
1575    void updateExpandedViewPos(int expandedPosition) {
1576        if (SPEW) {
1577            Slog.d(TAG, "updateExpandedViewPos before expandedPosition=" + expandedPosition
1578                    + " mTrackingParams.y=" + mTrackingParams.y
1579                    + " mTrackingPosition=" + mTrackingPosition);
1580        }
1581
1582        int h = mStatusBarView.getHeight();
1583        int disph = mDisplay.getHeight();
1584
1585        // If the expanded view is not visible, make sure they're still off screen.
1586        // Maybe the view was resized.
1587        if (!mExpandedVisible) {
1588            if (mTrackingView != null) {
1589                mTrackingPosition = -disph;
1590                if (mTrackingParams != null) {
1591                    mTrackingParams.y = mTrackingPosition;
1592                    WindowManagerImpl.getDefault().updateViewLayout(mTrackingView, mTrackingParams);
1593                }
1594            }
1595            if (mExpandedParams != null) {
1596                mExpandedParams.y = -disph;
1597                mExpandedDialog.getWindow().setAttributes(mExpandedParams);
1598            }
1599            return;
1600        }
1601
1602        // tracking view...
1603        int pos;
1604        if (expandedPosition == EXPANDED_FULL_OPEN) {
1605            pos = h;
1606        }
1607        else if (expandedPosition == EXPANDED_LEAVE_ALONE) {
1608            pos = mTrackingPosition;
1609        }
1610        else {
1611            if (expandedPosition <= disph) {
1612                pos = expandedPosition;
1613            } else {
1614                pos = disph;
1615            }
1616            pos -= disph-h;
1617        }
1618        mTrackingPosition = mTrackingParams.y = pos;
1619        mTrackingParams.height = disph-h;
1620        WindowManagerImpl.getDefault().updateViewLayout(mTrackingView, mTrackingParams);
1621
1622        if (mExpandedParams != null) {
1623            mCloseView.getLocationInWindow(mPositionTmp);
1624            final int closePos = mPositionTmp[1];
1625
1626            mExpandedContents.getLocationInWindow(mPositionTmp);
1627            final int contentsBottom = mPositionTmp[1] + mExpandedContents.getHeight();
1628
1629            mExpandedParams.y = pos + mTrackingView.getHeight()
1630                    - (mTrackingParams.height-closePos) - contentsBottom;
1631            int max = h;
1632            if (mExpandedParams.y > max) {
1633                mExpandedParams.y = max;
1634            }
1635            int min = mTrackingPosition;
1636            if (mExpandedParams.y < min) {
1637                mExpandedParams.y = min;
1638            }
1639
1640            boolean visible = (mTrackingPosition + mTrackingView.getHeight()) > h;
1641            if (!visible) {
1642                // if the contents aren't visible, move the expanded view way off screen
1643                // because the window itself extends below the content view.
1644                mExpandedParams.y = -disph;
1645            }
1646            mExpandedDialog.getWindow().setAttributes(mExpandedParams);
1647
1648            // As long as this isn't just a repositioning that's not supposed to affect
1649            // the user's perception of what's showing, call to say that the visibility
1650            // has changed. (Otherwise, someone else will call to do that).
1651            if (expandedPosition != EXPANDED_LEAVE_ALONE) {
1652                if (SPEW) Slog.d(TAG, "updateExpandedViewPos visibilityChanged(" + visible + ")");
1653                visibilityChanged(visible);
1654            }
1655        }
1656
1657        if (SPEW) {
1658            Slog.d(TAG, "updateExpandedViewPos after  expandedPosition=" + expandedPosition
1659                    + " mTrackingParams.y=" + mTrackingParams.y
1660                    + " mTrackingPosition=" + mTrackingPosition
1661                    + " mExpandedParams.y=" + mExpandedParams.y
1662                    + " mExpandedParams.height=" + mExpandedParams.height);
1663        }
1664    }
1665
1666    int getExpandedHeight() {
1667        return mDisplay.getHeight() - mStatusBarView.getHeight() - mCloseView.getHeight();
1668    }
1669
1670    void updateExpandedHeight() {
1671        if (mExpandedView != null) {
1672            mExpandedParams.height = getExpandedHeight();
1673            mExpandedDialog.getWindow().setAttributes(mExpandedParams);
1674        }
1675    }
1676
1677    public void userActivity() {
1678        try {
1679            mBarService.setSystemUiVisibility(View.STATUS_BAR_VISIBLE);
1680        } catch (RemoteException ex) { }
1681    }
1682
1683    public void toggleRecentApps() {
1684        int msg = (mRecentsPanel.getVisibility() == View.GONE)
1685                ? MSG_OPEN_RECENTS_PANEL : MSG_CLOSE_RECENTS_PANEL;
1686        mHandler.removeMessages(msg);
1687        mHandler.sendEmptyMessage(msg);
1688    }
1689
1690    /**
1691     * The LEDs are turned o)ff when the notification panel is shown, even just a little bit.
1692     * This was added last-minute and is inconsistent with the way the rest of the notifications
1693     * are handled, because the notification isn't really cancelled.  The lights are just
1694     * turned off.  If any other notifications happen, the lights will turn back on.  Steve says
1695     * this is what he wants. (see bug 1131461)
1696     */
1697    void visibilityChanged(boolean visible) {
1698        if (mPanelSlightlyVisible != visible) {
1699            mPanelSlightlyVisible = visible;
1700            try {
1701                mBarService.onPanelRevealed();
1702            } catch (RemoteException ex) {
1703                // Won't fail unless the world has ended.
1704            }
1705        }
1706    }
1707
1708    void performDisableActions(int net) {
1709        int old = mDisabled;
1710        int diff = net ^ old;
1711        mDisabled = net;
1712
1713        // act accordingly
1714        if ((diff & StatusBarManager.DISABLE_EXPAND) != 0) {
1715            if ((net & StatusBarManager.DISABLE_EXPAND) != 0) {
1716                Slog.d(TAG, "DISABLE_EXPAND: yes");
1717                animateCollapse();
1718            }
1719        }
1720        if ((diff & StatusBarManager.DISABLE_NOTIFICATION_ICONS) != 0) {
1721            if ((net & StatusBarManager.DISABLE_NOTIFICATION_ICONS) != 0) {
1722                Slog.d(TAG, "DISABLE_NOTIFICATION_ICONS: yes");
1723                if (mTicking) {
1724                    mNotificationIcons.setVisibility(View.INVISIBLE);
1725                    mTicker.halt();
1726                } else {
1727                    setNotificationIconVisibility(false, com.android.internal.R.anim.fade_out);
1728                }
1729            } else {
1730                Slog.d(TAG, "DISABLE_NOTIFICATION_ICONS: no");
1731                if (!mExpandedVisible) {
1732                    setNotificationIconVisibility(true, com.android.internal.R.anim.fade_in);
1733                }
1734            }
1735        } else if ((diff & StatusBarManager.DISABLE_NOTIFICATION_TICKER) != 0) {
1736            if (mTicking && (net & StatusBarManager.DISABLE_NOTIFICATION_TICKER) != 0) {
1737                mTicker.halt();
1738            }
1739        }
1740    }
1741
1742    private View.OnClickListener mClearButtonListener = new View.OnClickListener() {
1743        public void onClick(View v) {
1744            try {
1745                mBarService.onClearAllNotifications();
1746            } catch (RemoteException ex) {
1747                // system process is dead if we're here.
1748            }
1749            animateCollapse();
1750        }
1751    };
1752
1753    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1754        public void onReceive(Context context, Intent intent) {
1755            String action = intent.getAction();
1756            if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1757                    || Intent.ACTION_SCREEN_OFF.equals(action)) {
1758                boolean excludeRecents = false;
1759                if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
1760                    String reason = intent.getStringExtra("reason");
1761                    if (reason != null) {
1762                        excludeRecents = reason.equals("recentapps");
1763                    }
1764                }
1765                animateCollapse(excludeRecents);
1766            }
1767            else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1768                repositionNavigationBar();
1769                updateResources();
1770            }
1771        }
1772    };
1773
1774    private void setIntruderAlertVisibility(boolean vis) {
1775        mIntruderAlertView.setVisibility(vis ? View.VISIBLE : View.GONE);
1776    }
1777
1778    /**
1779     * Reload some of our resources when the configuration changes.
1780     *
1781     * We don't reload everything when the configuration changes -- we probably
1782     * should, but getting that smooth is tough.  Someday we'll fix that.  In the
1783     * meantime, just update the things that we know change.
1784     */
1785    void updateResources() {
1786        final Context context = mContext;
1787        final Resources res = context.getResources();
1788
1789        mClearButton.setText(context.getText(R.string.status_bar_clear_all_button));
1790        mOngoingTitle.setText(context.getText(R.string.status_bar_ongoing_events_title));
1791        mLatestTitle.setText(context.getText(R.string.status_bar_latest_events_title));
1792        mNoNotificationsTitle.setText(context.getText(R.string.status_bar_no_notifications_title));
1793
1794        mEdgeBorder = res.getDimensionPixelSize(R.dimen.status_bar_edge_ignore);
1795
1796        if (false) Slog.v(TAG, "updateResources");
1797    }
1798
1799    //
1800    // tracing
1801    //
1802
1803    void postStartTracing() {
1804        mHandler.postDelayed(mStartTracing, 3000);
1805    }
1806
1807    void vibrate() {
1808        android.os.Vibrator vib = (android.os.Vibrator)mContext.getSystemService(
1809                Context.VIBRATOR_SERVICE);
1810        vib.vibrate(250);
1811    }
1812
1813    Runnable mStartTracing = new Runnable() {
1814        public void run() {
1815            vibrate();
1816            SystemClock.sleep(250);
1817            Slog.d(TAG, "startTracing");
1818            android.os.Debug.startMethodTracing("/data/statusbar-traces/trace");
1819            mHandler.postDelayed(mStopTracing, 10000);
1820        }
1821    };
1822
1823    Runnable mStopTracing = new Runnable() {
1824        public void run() {
1825            android.os.Debug.stopMethodTracing();
1826            Slog.d(TAG, "stopTracing");
1827            vibrate();
1828        }
1829    };
1830
1831    public class TouchOutsideListener implements View.OnTouchListener {
1832        private int mMsg;
1833        private RecentsPanelView mPanel;
1834
1835        public TouchOutsideListener(int msg, RecentsPanelView panel) {
1836            mMsg = msg;
1837            mPanel = panel;
1838        }
1839
1840        public boolean onTouch(View v, MotionEvent ev) {
1841            final int action = ev.getAction();
1842            if (action == MotionEvent.ACTION_OUTSIDE
1843                || (action == MotionEvent.ACTION_DOWN
1844                    && !mPanel.isInContentArea((int)ev.getX(), (int)ev.getY()))) {
1845                mHandler.removeMessages(mMsg);
1846                mHandler.sendEmptyMessage(mMsg);
1847                return true;
1848            }
1849            return false;
1850        }
1851    }
1852}
1853
1854