TabletStatusBar.java revision 2b69735e014872ca2183b4a39a8381daa337e146
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;
22
23import android.animation.LayoutTransition;
24import android.animation.ObjectAnimator;
25import android.app.ActivityManagerNative;
26import android.app.Dialog;
27import android.app.PendingIntent;
28import android.app.Notification;
29import android.app.StatusBarManager;
30import android.content.Context;
31import android.content.Intent;
32import android.content.SharedPreferences;
33import android.content.res.Configuration;
34import android.content.res.Resources;
35import android.inputmethodservice.InputMethodService;
36import android.graphics.PixelFormat;
37import android.graphics.Rect;
38import android.graphics.drawable.LayerDrawable;
39import android.provider.Settings;
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.accessibility.AccessibilityEvent;
48import android.view.Display;
49import android.view.Gravity;
50import android.view.IWindowManager;
51import android.view.KeyEvent;
52import android.view.LayoutInflater;
53import android.view.MotionEvent;
54import android.view.SoundEffectConstants;
55import android.view.VelocityTracker;
56import android.view.View;
57import android.view.ViewConfiguration;
58import android.view.ViewGroup;
59import android.view.WindowManager;
60import android.view.WindowManagerImpl;
61import android.widget.ImageView;
62import android.widget.LinearLayout;
63import android.widget.RemoteViews;
64import android.widget.ScrollView;
65import android.widget.TextView;
66
67import com.android.internal.statusbar.StatusBarIcon;
68import com.android.internal.statusbar.StatusBarNotification;
69
70import com.android.systemui.R;
71import com.android.systemui.statusbar.*;
72import com.android.systemui.statusbar.policy.BatteryController;
73import com.android.systemui.statusbar.policy.BluetoothController;
74import com.android.systemui.statusbar.policy.CompatModeButton;
75import com.android.systemui.statusbar.policy.LocationController;
76import com.android.systemui.statusbar.policy.NetworkController;
77import com.android.systemui.statusbar.policy.Prefs;
78import com.android.systemui.recent.RecentsPanelView;
79
80public class TabletStatusBar extends StatusBar implements
81        HeightReceiver.OnBarHeightChangedListener,
82        InputMethodsPanel.OnHardKeyboardEnabledChangeListener {
83    public static final boolean DEBUG = false;
84    public static final boolean DEBUG_COMPAT_HELP = false;
85    public static final String TAG = "TabletStatusBar";
86
87
88    public static final int MSG_OPEN_NOTIFICATION_PANEL = 1000;
89    public static final int MSG_CLOSE_NOTIFICATION_PANEL = 1001;
90    public static final int MSG_OPEN_NOTIFICATION_PEEK = 1002;
91    public static final int MSG_CLOSE_NOTIFICATION_PEEK = 1003;
92    public static final int MSG_OPEN_RECENTS_PANEL = 1020;
93    public static final int MSG_CLOSE_RECENTS_PANEL = 1021;
94    public static final int MSG_SHOW_CHROME = 1030;
95    public static final int MSG_HIDE_CHROME = 1031;
96    public static final int MSG_OPEN_INPUT_METHODS_PANEL = 1040;
97    public static final int MSG_CLOSE_INPUT_METHODS_PANEL = 1041;
98    public static final int MSG_OPEN_COMPAT_MODE_PANEL = 1050;
99    public static final int MSG_CLOSE_COMPAT_MODE_PANEL = 1051;
100    public static final int MSG_STOP_TICKER = 2000;
101
102    // Fitts' Law assistance for LatinIME; see policy.EventHole
103    private static final boolean FAKE_SPACE_BAR = true;
104
105    // The height of the bar, as definied by the build.  It may be taller if we're plugged
106    // into hdmi.
107    int mNaturalBarHeight = -1;
108    int mIconSize = -1;
109    int mIconHPadding = -1;
110    private int mMaxNotificationIcons = 5;
111
112    H mHandler = new H();
113
114    IWindowManager mWindowManager;
115
116    // tracking all current notifications
117    private NotificationData mNotificationData = new NotificationData();
118
119    TabletStatusBarView mStatusBarView;
120    View mNotificationArea;
121    View mNotificationTrigger;
122    NotificationIconArea mNotificationIconArea;
123    ViewGroup mNavigationArea;
124
125    boolean mNotificationDNDMode;
126    NotificationData.Entry mNotificationDNDDummyEntry;
127
128    ImageView mBackButton;
129    View mHomeButton;
130    View mMenuButton;
131    View mRecentButton;
132
133    ViewGroup mFeedbackIconArea; // notification icons, IME icon, compat icon
134    InputMethodButton mInputMethodSwitchButton;
135    CompatModeButton mCompatModeButton;
136
137    NotificationPanel mNotificationPanel;
138    WindowManager.LayoutParams mNotificationPanelParams;
139    NotificationPeekPanel mNotificationPeekWindow;
140    ViewGroup mNotificationPeekRow;
141    int mNotificationPeekIndex;
142    IBinder mNotificationPeekKey;
143    LayoutTransition mNotificationPeekScrubLeft, mNotificationPeekScrubRight;
144
145    int mNotificationPeekTapDuration;
146    int mNotificationFlingVelocity;
147
148    ViewGroup mPile;
149
150    HeightReceiver mHeightReceiver;
151    BatteryController mBatteryController;
152    BluetoothController mBluetoothController;
153    LocationController mLocationController;
154    NetworkController mNetworkController;
155
156    ViewGroup mBarContents;
157    LayoutTransition mBarContentsLayoutTransition;
158
159    // hide system chrome ("lights out") support
160    View mShadow;
161
162    NotificationIconArea.IconLayout mIconLayout;
163
164    TabletTicker mTicker;
165
166    View mFakeSpaceBar;
167    KeyEvent mSpaceBarKeyEvent = null;
168
169    View mCompatibilityHelpDialog = null;
170
171    // for disabling the status bar
172    int mDisabled = 0;
173
174    private RecentsPanelView mRecentsPanel;
175    private InputMethodsPanel mInputMethodsPanel;
176    private CompatModePanel mCompatModePanel;
177
178    public Context getContext() { return mContext; }
179
180    protected void addPanelWindows() {
181        final Context context = mContext;
182        final Resources res = mContext.getResources();
183
184        // Notification Panel
185        mNotificationPanel = (NotificationPanel)View.inflate(context,
186                R.layout.status_bar_notification_panel, null);
187        mNotificationPanel.show(false, false);
188        mNotificationPanel.setOnTouchListener(
189                new TouchOutsideListener(MSG_CLOSE_NOTIFICATION_PANEL, mNotificationPanel));
190
191        // the battery icon
192        mBatteryController.addIconView((ImageView)mNotificationPanel.findViewById(R.id.battery));
193        mBatteryController.addLabelView(
194                (TextView)mNotificationPanel.findViewById(R.id.battery_text));
195
196        // Bt
197        mBluetoothController.addIconView(
198                (ImageView)mNotificationPanel.findViewById(R.id.bluetooth));
199
200        // network icons: either a combo icon that switches between mobile and data, or distinct
201        // mobile and data icons
202        final ImageView comboRSSI =
203                (ImageView)mNotificationPanel.findViewById(R.id.network_signal);
204        if (comboRSSI != null) {
205            mNetworkController.addCombinedSignalIconView(comboRSSI);
206        }
207        final ImageView mobileRSSI =
208                (ImageView)mNotificationPanel.findViewById(R.id.mobile_signal);
209        if (mobileRSSI != null) {
210            mNetworkController.addPhoneSignalIconView(mobileRSSI);
211        }
212        final ImageView wifiRSSI =
213                (ImageView)mNotificationPanel.findViewById(R.id.wifi_signal);
214        if (wifiRSSI != null) {
215            mNetworkController.addWifiIconView(wifiRSSI);
216        }
217
218        mNetworkController.addDataTypeIconView(
219                (ImageView)mNotificationPanel.findViewById(R.id.network_type));
220        mNetworkController.addDataDirectionOverlayIconView(
221                (ImageView)mNotificationPanel.findViewById(R.id.network_direction));
222        mNetworkController.addLabelView(
223                (TextView)mNotificationPanel.findViewById(R.id.network_text));
224        mNetworkController.addLabelView(
225                (TextView)mBarContents.findViewById(R.id.network_text));
226
227        mStatusBarView.setIgnoreChildren(0, mNotificationTrigger, mNotificationPanel);
228
229        WindowManager.LayoutParams lp = mNotificationPanelParams = new WindowManager.LayoutParams(
230                res.getDimensionPixelSize(R.dimen.notification_panel_width),
231                getNotificationPanelHeight(),
232                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
233                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
234                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
235                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
236                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
237                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
238                PixelFormat.TRANSLUCENT);
239        lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
240        lp.setTitle("NotificationPanel");
241        lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED
242                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING;
243        lp.windowAnimations = com.android.internal.R.style.Animation; // == no animation
244//        lp.windowAnimations = com.android.internal.R.style.Animation_ZoomButtons; // simple fade
245
246        WindowManagerImpl.getDefault().addView(mNotificationPanel, lp);
247
248        // Notification preview window
249        mNotificationPeekWindow = (NotificationPeekPanel) View.inflate(context,
250                R.layout.status_bar_notification_peek, null);
251        mNotificationPeekWindow.setBar(this);
252
253        mNotificationPeekRow = (ViewGroup) mNotificationPeekWindow.findViewById(R.id.content);
254        mNotificationPeekWindow.setVisibility(View.GONE);
255        mNotificationPeekWindow.setOnTouchListener(
256                new TouchOutsideListener(MSG_CLOSE_NOTIFICATION_PEEK, mNotificationPeekWindow));
257        mNotificationPeekScrubRight = new LayoutTransition();
258        mNotificationPeekScrubRight.setAnimator(LayoutTransition.APPEARING,
259                ObjectAnimator.ofInt(null, "left", -512, 0));
260        mNotificationPeekScrubRight.setAnimator(LayoutTransition.DISAPPEARING,
261                ObjectAnimator.ofInt(null, "left", -512, 0));
262        mNotificationPeekScrubRight.setDuration(500);
263
264        mNotificationPeekScrubLeft = new LayoutTransition();
265        mNotificationPeekScrubLeft.setAnimator(LayoutTransition.APPEARING,
266                ObjectAnimator.ofInt(null, "left", 512, 0));
267        mNotificationPeekScrubLeft.setAnimator(LayoutTransition.DISAPPEARING,
268                ObjectAnimator.ofInt(null, "left", 512, 0));
269        mNotificationPeekScrubLeft.setDuration(500);
270
271        // XXX: setIgnoreChildren?
272        lp = new WindowManager.LayoutParams(
273                512, // ViewGroup.LayoutParams.WRAP_CONTENT,
274                ViewGroup.LayoutParams.WRAP_CONTENT,
275                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
276                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
277                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
278                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH,
279                PixelFormat.TRANSLUCENT);
280        lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
281        lp.y = res.getDimensionPixelOffset(R.dimen.peek_window_y_offset);
282        lp.setTitle("NotificationPeekWindow");
283        lp.windowAnimations = com.android.internal.R.style.Animation_Toast;
284
285        WindowManagerImpl.getDefault().addView(mNotificationPeekWindow, lp);
286
287        // Recents Panel
288        mRecentsPanel = (RecentsPanelView) View.inflate(context,
289                R.layout.status_bar_recent_panel, null);
290        mRecentsPanel.setVisibility(View.GONE);
291        mRecentsPanel.setOnTouchListener(new TouchOutsideListener(MSG_CLOSE_RECENTS_PANEL,
292                mRecentsPanel));
293        mStatusBarView.setIgnoreChildren(2, mRecentButton, mRecentsPanel);
294
295        lp = new WindowManager.LayoutParams(
296                ViewGroup.LayoutParams.WRAP_CONTENT,
297                ViewGroup.LayoutParams.WRAP_CONTENT,
298                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
299                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
300                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
301                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
302                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
303                PixelFormat.TRANSLUCENT);
304        lp.gravity = Gravity.BOTTOM | Gravity.LEFT;
305        lp.setTitle("RecentsPanel");
306        lp.windowAnimations = R.style.Animation_RecentPanel;
307        lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED
308                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING;
309
310        WindowManagerImpl.getDefault().addView(mRecentsPanel, lp);
311        mRecentsPanel.setBar(this);
312
313        // Input methods Panel
314        mInputMethodsPanel = (InputMethodsPanel) View.inflate(context,
315                R.layout.status_bar_input_methods_panel, null);
316        mInputMethodsPanel.setHardKeyboardEnabledChangeListener(this);
317        mInputMethodsPanel.setOnTouchListener(new TouchOutsideListener(
318                MSG_CLOSE_INPUT_METHODS_PANEL, mInputMethodsPanel));
319        mInputMethodsPanel.setImeSwitchButton(mInputMethodSwitchButton);
320        mStatusBarView.setIgnoreChildren(3, mInputMethodSwitchButton, mInputMethodsPanel);
321        lp = new WindowManager.LayoutParams(
322                ViewGroup.LayoutParams.WRAP_CONTENT,
323                ViewGroup.LayoutParams.WRAP_CONTENT,
324                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
325                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
326                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
327                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
328                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
329                PixelFormat.TRANSLUCENT);
330        lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
331        lp.setTitle("InputMethodsPanel");
332        lp.windowAnimations = R.style.Animation_RecentPanel;
333
334        WindowManagerImpl.getDefault().addView(mInputMethodsPanel, lp);
335
336        // Compatibility mode selector panel
337        mCompatModePanel = (CompatModePanel) View.inflate(context,
338                R.layout.status_bar_compat_mode_panel, null);
339        mCompatModePanel.setOnTouchListener(new TouchOutsideListener(
340                MSG_CLOSE_COMPAT_MODE_PANEL, mCompatModePanel));
341        mCompatModePanel.setTrigger(mCompatModeButton);
342        mCompatModePanel.setVisibility(View.GONE);
343        mStatusBarView.setIgnoreChildren(4, mCompatModeButton, mCompatModePanel);
344        lp = new WindowManager.LayoutParams(
345                250,
346                ViewGroup.LayoutParams.WRAP_CONTENT,
347                WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
348                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
349                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
350                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
351                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
352                PixelFormat.TRANSLUCENT);
353        lp.gravity = Gravity.BOTTOM | Gravity.RIGHT;
354        lp.setTitle("CompatModePanel");
355        lp.windowAnimations = android.R.style.Animation_Dialog;
356
357        WindowManagerImpl.getDefault().addView(mCompatModePanel, lp);
358    }
359
360    private int getNotificationPanelHeight() {
361        final Resources res = mContext.getResources();
362        final Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
363        return Math.max(res.getDimensionPixelSize(R.dimen.notification_panel_min_height),
364                d.getRealHeight());
365    }
366
367    @Override
368    public void start() {
369        super.start(); // will add the main bar view
370    }
371
372    @Override
373    protected void onConfigurationChanged(Configuration newConfig) {
374        loadDimens();
375        mNotificationPanelParams.height = getNotificationPanelHeight();
376        WindowManagerImpl.getDefault().updateViewLayout(mNotificationPanel,
377                mNotificationPanelParams);
378    }
379
380    protected void loadDimens() {
381        final Resources res = mContext.getResources();
382
383        mNaturalBarHeight = res.getDimensionPixelSize(
384                com.android.internal.R.dimen.system_bar_height);
385
386        int newIconSize = res.getDimensionPixelSize(
387            com.android.internal.R.dimen.system_bar_icon_size);
388        int newIconHPadding = res.getDimensionPixelSize(
389            R.dimen.status_bar_icon_padding);
390
391        if (newIconHPadding != mIconHPadding || newIconSize != mIconSize) {
392//            Slog.d(TAG, "size=" + newIconSize + " padding=" + newIconHPadding);
393            mIconHPadding = newIconHPadding;
394            mIconSize = newIconSize;
395            reloadAllNotificationIcons(); // reload the tray
396        }
397
398        final int numIcons = res.getInteger(R.integer.config_maxNotificationIcons);
399        if (numIcons != mMaxNotificationIcons) {
400            mMaxNotificationIcons = numIcons;
401            if (DEBUG) Slog.d(TAG, "max notification icons: " + mMaxNotificationIcons);
402            reloadAllNotificationIcons();
403        }
404    }
405
406    protected View makeStatusBarView() {
407        final Context context = mContext;
408
409        mWindowManager = IWindowManager.Stub.asInterface(
410                ServiceManager.getService(Context.WINDOW_SERVICE));
411
412        // This guy will listen for HDMI plugged broadcasts so we can resize the
413        // status bar as appropriate.
414        mHeightReceiver = new HeightReceiver(mContext);
415        mHeightReceiver.registerReceiver();
416        loadDimens();
417
418        final TabletStatusBarView sb = (TabletStatusBarView)View.inflate(
419                context, R.layout.status_bar, null);
420        mStatusBarView = sb;
421
422        sb.setHandler(mHandler);
423
424        mBarContents = (ViewGroup) sb.findViewById(R.id.bar_contents);
425        // layout transitions for the status bar's contents
426        mBarContentsLayoutTransition = new LayoutTransition();
427        // add/removal will fade as normal
428        mBarContentsLayoutTransition.setAnimator(LayoutTransition.APPEARING,
429                ObjectAnimator.ofFloat(null, "alpha", 0f, 1f));
430        mBarContentsLayoutTransition.setAnimator(LayoutTransition.DISAPPEARING,
431                ObjectAnimator.ofFloat(null, "alpha", 1f, 0f));
432        // no animations for siblings on change: just jump into place please
433        mBarContentsLayoutTransition.setAnimator(LayoutTransition.CHANGE_APPEARING, null);
434        mBarContentsLayoutTransition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, null);
435        // quick like bunny
436        mBarContentsLayoutTransition.setDuration(250 * (DEBUG?10:1));
437        mBarContents.setLayoutTransition(mBarContentsLayoutTransition);
438
439        // the whole right-hand side of the bar
440        mNotificationArea = sb.findViewById(R.id.notificationArea);
441
442        // the button to open the notification area
443        mNotificationTrigger = sb.findViewById(R.id.notificationTrigger);
444        mNotificationTrigger.setOnTouchListener(new NotificationTriggerTouchListener());
445
446        // the more notifications icon
447        mNotificationIconArea = (NotificationIconArea)sb.findViewById(R.id.notificationIcons);
448
449        // where the icons go
450        mIconLayout = (NotificationIconArea.IconLayout) sb.findViewById(R.id.icons);
451        mIconLayout.setOnTouchListener(new NotificationIconTouchListener());
452
453        ViewConfiguration vc = ViewConfiguration.get(context);
454        mNotificationPeekTapDuration = vc.getTapTimeout();
455        mNotificationFlingVelocity = 300; // px/s
456
457        mTicker = new TabletTicker(this);
458
459        // The icons
460        mLocationController = new LocationController(mContext); // will post a notification
461
462        mBatteryController = new BatteryController(mContext);
463        mBatteryController.addIconView((ImageView)sb.findViewById(R.id.battery));
464        mBluetoothController = new BluetoothController(mContext);
465        mBluetoothController.addIconView((ImageView)sb.findViewById(R.id.bluetooth));
466
467        mNetworkController = new NetworkController(mContext);
468        final ImageView comboRSSI =
469                (ImageView)sb.findViewById(R.id.network_signal);
470        if (comboRSSI != null) {
471            mNetworkController.addCombinedSignalIconView(comboRSSI);
472        }
473        final ImageView mobileRSSI =
474                (ImageView)sb.findViewById(R.id.mobile_signal);
475        if (mobileRSSI != null) {
476            mNetworkController.addPhoneSignalIconView(mobileRSSI);
477        }
478        final ImageView wifiRSSI =
479                (ImageView)sb.findViewById(R.id.wifi_signal);
480        if (wifiRSSI != null) {
481            mNetworkController.addWifiIconView(wifiRSSI);
482        }
483        mNetworkController.addDataTypeIconView(
484                (ImageView)sb.findViewById(R.id.network_type));
485        mNetworkController.addDataDirectionOverlayIconView(
486                (ImageView)sb.findViewById(R.id.network_direction));
487
488        // The navigation buttons
489        mBackButton = (ImageView)sb.findViewById(R.id.back);
490        mNavigationArea = (ViewGroup) sb.findViewById(R.id.navigationArea);
491        mHomeButton = mNavigationArea.findViewById(R.id.home);
492        mMenuButton = mNavigationArea.findViewById(R.id.menu);
493        mRecentButton = mNavigationArea.findViewById(R.id.recent_apps);
494        mRecentButton.setOnClickListener(mOnClickListener);
495        mNavigationArea.setLayoutTransition(mBarContentsLayoutTransition);
496
497        // The bar contents buttons
498        mFeedbackIconArea = (ViewGroup)sb.findViewById(R.id.feedbackIconArea);
499        mInputMethodSwitchButton = (InputMethodButton) sb.findViewById(R.id.imeSwitchButton);
500        // Overwrite the lister
501        mInputMethodSwitchButton.setOnClickListener(mOnClickListener);
502
503        mCompatModeButton = (CompatModeButton) sb.findViewById(R.id.compatModeButton);
504        mCompatModeButton.setOnClickListener(mOnClickListener);
505
506        // for redirecting errant bar taps to the IME
507        mFakeSpaceBar = sb.findViewById(R.id.fake_space_bar);
508
509        // "shadows" of the status bar features, for lights-out mode
510        mShadow = sb.findViewById(R.id.bar_shadow);
511        mShadow.setOnTouchListener(
512            new View.OnTouchListener() {
513                public boolean onTouch(View v, MotionEvent ev) {
514                    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
515                        // even though setting the systemUI visibility below will turn these views
516                        // on, we need them to come up faster so that they can catch this motion
517                        // event
518                        mShadow.setVisibility(View.GONE);
519                        mBarContents.setVisibility(View.VISIBLE);
520
521                        try {
522                            mBarService.setSystemUiVisibility(View.STATUS_BAR_VISIBLE);
523                        } catch (RemoteException ex) {
524                            // system process dead
525                        }
526                    }
527                    return false;
528                }
529            });
530
531        // tuning parameters
532        final int LIGHTS_GOING_OUT_SYSBAR_DURATION = 600;
533        final int LIGHTS_GOING_OUT_SHADOW_DURATION = 1000;
534        final int LIGHTS_GOING_OUT_SHADOW_DELAY    = 500;
535
536        final int LIGHTS_COMING_UP_SYSBAR_DURATION = 200;
537//        final int LIGHTS_COMING_UP_SYSBAR_DELAY    = 50;
538        final int LIGHTS_COMING_UP_SHADOW_DURATION = 0;
539
540        LayoutTransition xition = new LayoutTransition();
541        xition.setAnimator(LayoutTransition.APPEARING,
542               ObjectAnimator.ofFloat(null, "alpha", 0.5f, 1f));
543        xition.setDuration(LayoutTransition.APPEARING, LIGHTS_COMING_UP_SYSBAR_DURATION);
544        xition.setStartDelay(LayoutTransition.APPEARING, 0);
545        xition.setAnimator(LayoutTransition.DISAPPEARING,
546               ObjectAnimator.ofFloat(null, "alpha", 1f, 0f));
547        xition.setDuration(LayoutTransition.DISAPPEARING, LIGHTS_GOING_OUT_SYSBAR_DURATION);
548        xition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
549        ((ViewGroup)sb.findViewById(R.id.bar_contents_holder)).setLayoutTransition(xition);
550
551        xition = new LayoutTransition();
552        xition.setAnimator(LayoutTransition.APPEARING,
553               ObjectAnimator.ofFloat(null, "alpha", 0f, 1f));
554        xition.setDuration(LayoutTransition.APPEARING, LIGHTS_GOING_OUT_SHADOW_DURATION);
555        xition.setStartDelay(LayoutTransition.APPEARING, LIGHTS_GOING_OUT_SHADOW_DELAY);
556        xition.setAnimator(LayoutTransition.DISAPPEARING,
557               ObjectAnimator.ofFloat(null, "alpha", 1f, 0f));
558        xition.setDuration(LayoutTransition.DISAPPEARING, LIGHTS_COMING_UP_SHADOW_DURATION);
559        xition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
560        ((ViewGroup)sb.findViewById(R.id.bar_shadow_holder)).setLayoutTransition(xition);
561
562        // set the initial view visibility
563        setAreThereNotifications();
564
565        // Add the windows
566        addPanelWindows();
567
568        mPile = (ViewGroup)mNotificationPanel.findViewById(R.id.content);
569        mPile.removeAllViews();
570
571        ScrollView scroller = (ScrollView)mPile.getParent();
572        scroller.setFillViewport(true);
573
574        mHeightReceiver.addOnBarHeightChangedListener(this);
575
576        return sb;
577    }
578
579    public int getStatusBarHeight() {
580        return mHeightReceiver.getHeight();
581    }
582
583    protected int getStatusBarGravity() {
584        return Gravity.BOTTOM | Gravity.FILL_HORIZONTAL;
585    }
586
587    public void onBarHeightChanged(int height) {
588        final WindowManager.LayoutParams lp
589                = (WindowManager.LayoutParams)mStatusBarView.getLayoutParams();
590        if (lp == null) {
591            // haven't been added yet
592            return;
593        }
594        if (lp.height != height) {
595            lp.height = height;
596            final WindowManager wm = WindowManagerImpl.getDefault();
597            wm.updateViewLayout(mStatusBarView, lp);
598        }
599    }
600
601    private class H extends Handler {
602        public void handleMessage(Message m) {
603            switch (m.what) {
604                case MSG_OPEN_NOTIFICATION_PEEK:
605                    if (DEBUG) Slog.d(TAG, "opening notification peek window; arg=" + m.arg1);
606
607                    if (m.arg1 >= 0) {
608                        final int N = mNotificationData.size();
609
610                        if (!mNotificationDNDMode) {
611                            if (mNotificationPeekIndex >= 0 && mNotificationPeekIndex < N) {
612                                NotificationData.Entry entry = mNotificationData.get(N-1-mNotificationPeekIndex);
613                                entry.icon.setBackgroundColor(0);
614                                mNotificationPeekIndex = -1;
615                                mNotificationPeekKey = null;
616                            }
617                        }
618
619                        final int peekIndex = m.arg1;
620                        if (peekIndex < N) {
621                            //Slog.d(TAG, "loading peek: " + peekIndex);
622                            NotificationData.Entry entry =
623                                mNotificationDNDMode
624                                    ? mNotificationDNDDummyEntry
625                                    : mNotificationData.get(N-1-peekIndex);
626                            NotificationData.Entry copy = new NotificationData.Entry(
627                                    entry.key,
628                                    entry.notification,
629                                    entry.icon);
630                            inflateViews(copy, mNotificationPeekRow);
631
632                            if (mNotificationDNDMode) {
633                                copy.content.setOnClickListener(new View.OnClickListener() {
634                                    public void onClick(View v) {
635                                        SharedPreferences.Editor editor = Prefs.edit(mContext);
636                                        editor.putBoolean(Prefs.DO_NOT_DISTURB_PREF, false);
637                                        editor.apply();
638                                        animateCollapse();
639                                    }
640                                });
641                            }
642
643                            entry.icon.setBackgroundColor(0x20FFFFFF);
644
645//                          mNotificationPeekRow.setLayoutTransition(
646//                              peekIndex < mNotificationPeekIndex
647//                                  ? mNotificationPeekScrubLeft
648//                                  : mNotificationPeekScrubRight);
649
650                            mNotificationPeekRow.removeAllViews();
651                            mNotificationPeekRow.addView(copy.row);
652
653                            mNotificationPeekWindow.setVisibility(View.VISIBLE);
654                            mNotificationPanel.show(false, true);
655
656                            mNotificationPeekIndex = peekIndex;
657                            mNotificationPeekKey = entry.key;
658                        }
659                    }
660                    break;
661                case MSG_CLOSE_NOTIFICATION_PEEK:
662                    if (DEBUG) Slog.d(TAG, "closing notification peek window");
663                    mNotificationPeekWindow.setVisibility(View.GONE);
664                    mNotificationPeekRow.removeAllViews();
665
666                    final int N = mNotificationData.size();
667                    if (mNotificationPeekIndex >= 0 && mNotificationPeekIndex < N) {
668                        NotificationData.Entry entry =
669                            mNotificationDNDMode
670                                ? mNotificationDNDDummyEntry
671                                : mNotificationData.get(N-1-mNotificationPeekIndex);
672                        entry.icon.setBackgroundColor(0);
673                    }
674
675                    mNotificationPeekIndex = -1;
676                    mNotificationPeekKey = null;
677                    break;
678                case MSG_OPEN_NOTIFICATION_PANEL:
679                    if (DEBUG) Slog.d(TAG, "opening notifications panel");
680                    if (!mNotificationPanel.isShowing()) {
681                        mNotificationPeekWindow.setVisibility(View.GONE);
682                        mNotificationPanel.show(true, true);
683                        mNotificationArea.setVisibility(View.INVISIBLE);
684                        mTicker.halt();
685                    }
686                    break;
687                case MSG_CLOSE_NOTIFICATION_PANEL:
688                    if (DEBUG) Slog.d(TAG, "closing notifications panel");
689                    if (mNotificationPanel.isShowing()) {
690                        mNotificationPanel.show(false, true);
691                        mNotificationArea.setVisibility(View.VISIBLE);
692                    }
693                    break;
694                case MSG_OPEN_RECENTS_PANEL:
695                    if (DEBUG) Slog.d(TAG, "opening recents panel");
696                    if (mRecentsPanel != null) {
697                        disable(StatusBarManager.DISABLE_BACK);
698                        mRecentsPanel.setVisibility(View.VISIBLE);
699                        mRecentsPanel.show(true, true);
700                    }
701                    break;
702                case MSG_CLOSE_RECENTS_PANEL:
703                    if (DEBUG) Slog.d(TAG, "closing recents panel");
704                    if (mRecentsPanel != null && mRecentsPanel.isShowing()) {
705                        disable(StatusBarManager.DISABLE_NONE);
706                        mRecentsPanel.show(false, true);
707                    }
708                    break;
709                case MSG_OPEN_INPUT_METHODS_PANEL:
710                    if (DEBUG) Slog.d(TAG, "opening input methods panel");
711                    if (mInputMethodsPanel != null) mInputMethodsPanel.openPanel();
712                    break;
713                case MSG_CLOSE_INPUT_METHODS_PANEL:
714                    if (DEBUG) Slog.d(TAG, "closing input methods panel");
715                    if (mInputMethodsPanel != null) mInputMethodsPanel.closePanel(false);
716                    break;
717                case MSG_OPEN_COMPAT_MODE_PANEL:
718                    if (DEBUG) Slog.d(TAG, "opening compat panel");
719                    if (mCompatModePanel != null) mCompatModePanel.openPanel();
720                    break;
721                case MSG_CLOSE_COMPAT_MODE_PANEL:
722                    if (DEBUG) Slog.d(TAG, "closing compat panel");
723                    if (mCompatModePanel != null) mCompatModePanel.closePanel();
724                    break;
725                case MSG_SHOW_CHROME:
726                    if (DEBUG) Slog.d(TAG, "hiding shadows (lights on)");
727                    mBarContents.setVisibility(View.VISIBLE);
728                    mShadow.setVisibility(View.GONE);
729                    notifyLightsChanged(true);
730                    break;
731                case MSG_HIDE_CHROME:
732                    if (DEBUG) Slog.d(TAG, "showing shadows (lights out)");
733                    animateCollapse();
734                    mBarContents.setVisibility(View.GONE);
735                    mShadow.setVisibility(View.VISIBLE);
736                    notifyLightsChanged(false);
737                    break;
738                case MSG_STOP_TICKER:
739                    mTicker.halt();
740                    break;
741            }
742        }
743    }
744
745    private void notifyLightsChanged(boolean shown) {
746        try {
747            Slog.d(TAG, "lights " + (shown?"on":"out"));
748            mWindowManager.statusBarVisibilityChanged(
749                    shown ? View.STATUS_BAR_VISIBLE : View.STATUS_BAR_HIDDEN);
750        } catch (RemoteException ex) {
751        }
752    }
753
754    public void addIcon(String slot, int index, int viewIndex, StatusBarIcon icon) {
755        if (DEBUG) Slog.d(TAG, "addIcon(" + slot + ") -> " + icon);
756    }
757
758    public void updateIcon(String slot, int index, int viewIndex,
759            StatusBarIcon old, StatusBarIcon icon) {
760        if (DEBUG) Slog.d(TAG, "updateIcon(" + slot + ") -> " + icon);
761    }
762
763    public void removeIcon(String slot, int index, int viewIndex) {
764        if (DEBUG) Slog.d(TAG, "removeIcon(" + slot + ")");
765    }
766
767    public void addNotification(IBinder key, StatusBarNotification notification) {
768        if (DEBUG) Slog.d(TAG, "addNotification(" + key + " -> " + notification + ")");
769        addNotificationViews(key, notification);
770
771        final boolean immersive = isImmersive();
772        if (false && immersive) {
773            // TODO: immersive mode popups for tablet
774        } else if (notification.notification.fullScreenIntent != null) {
775            // not immersive & a full-screen alert should be shown
776            Slog.w(TAG, "Notification has fullScreenIntent and activity is not immersive;"
777                    + " sending fullScreenIntent");
778            try {
779                notification.notification.fullScreenIntent.send();
780            } catch (PendingIntent.CanceledException e) {
781            }
782        } else {
783            tick(key, notification, true);
784        }
785
786        setAreThereNotifications();
787    }
788
789    public void updateNotification(IBinder key, StatusBarNotification notification) {
790        if (DEBUG) Slog.d(TAG, "updateNotification(" + key + " -> " + notification + ")");
791
792        final NotificationData.Entry oldEntry = mNotificationData.findByKey(key);
793        if (oldEntry == null) {
794            Slog.w(TAG, "updateNotification for unknown key: " + key);
795            return;
796        }
797
798        final StatusBarNotification oldNotification = oldEntry.notification;
799        final RemoteViews oldContentView = oldNotification.notification.contentView;
800
801        final RemoteViews contentView = notification.notification.contentView;
802
803        if (DEBUG) {
804            Slog.d(TAG, "old notification: when=" + oldNotification.notification.when
805                    + " ongoing=" + oldNotification.isOngoing()
806                    + " expanded=" + oldEntry.expanded
807                    + " contentView=" + oldContentView
808                    + " rowParent=" + oldEntry.row.getParent());
809            Slog.d(TAG, "new notification: when=" + notification.notification.when
810                    + " ongoing=" + oldNotification.isOngoing()
811                    + " contentView=" + contentView);
812        }
813
814        // Can we just reapply the RemoteViews in place?  If when didn't change, the order
815        // didn't change.
816        boolean contentsUnchanged = oldEntry.expanded != null
817                && contentView != null && oldContentView != null
818                && contentView.getPackage() != null
819                && oldContentView.getPackage() != null
820                && oldContentView.getPackage().equals(contentView.getPackage())
821                && oldContentView.getLayoutId() == contentView.getLayoutId();
822        ViewGroup rowParent = (ViewGroup) oldEntry.row.getParent();
823        boolean orderUnchanged = notification.notification.when==oldNotification.notification.when
824                && notification.priority == oldNotification.priority;
825                // priority now encompasses isOngoing()
826        boolean isLastAnyway = rowParent.indexOfChild(oldEntry.row) == rowParent.getChildCount()-1;
827        if (contentsUnchanged && (orderUnchanged || isLastAnyway)) {
828            if (DEBUG) Slog.d(TAG, "reusing notification for key: " + key);
829            oldEntry.notification = notification;
830            try {
831                // Reapply the RemoteViews
832                contentView.reapply(mContext, oldEntry.content);
833                // update the contentIntent
834                final PendingIntent contentIntent = notification.notification.contentIntent;
835                if (contentIntent != null) {
836                    oldEntry.content.setOnClickListener(new NotificationClicker(contentIntent,
837                                notification.pkg, notification.tag, notification.id));
838                } else {
839                    oldEntry.content.setOnClickListener(null);
840                }
841                // Update the icon.
842                final StatusBarIcon ic = new StatusBarIcon(notification.pkg,
843                        notification.notification.icon, notification.notification.iconLevel,
844                        notification.notification.number,
845                        notification.notification.tickerText);
846                if (!oldEntry.icon.set(ic)) {
847                    handleNotificationError(key, notification, "Couldn't update icon: " + ic);
848                    return;
849                }
850                // Update the large icon
851                if (notification.notification.largeIcon != null) {
852                    oldEntry.largeIcon.setImageBitmap(notification.notification.largeIcon);
853                } else {
854                    oldEntry.largeIcon.getLayoutParams().width = 0;
855                    oldEntry.largeIcon.setVisibility(View.INVISIBLE);
856                }
857
858                if (key == mNotificationPeekKey) {
859                    // must update the peek window
860                    Message peekMsg = mHandler.obtainMessage(MSG_OPEN_NOTIFICATION_PEEK);
861                    peekMsg.arg1 = mNotificationPeekIndex;
862                    mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
863                    mHandler.sendMessage(peekMsg);
864                }
865            }
866            catch (RuntimeException e) {
867                // It failed to add cleanly.  Log, and remove the view from the panel.
868                Slog.w(TAG, "Couldn't reapply views for package " + contentView.getPackage(), e);
869                removeNotificationViews(key);
870                addNotificationViews(key, notification);
871            }
872        } else {
873            if (DEBUG) Slog.d(TAG, "not reusing notification for key: " + key);
874            removeNotificationViews(key);
875            addNotificationViews(key, notification);
876        }
877        // fullScreenIntent doesn't happen on updates.  You need to clear & repost a new
878        // notification.
879        final boolean immersive = isImmersive();
880        if (false && immersive) {
881            // TODO: immersive mode
882        } else {
883            tick(key, notification, false);
884        }
885
886        setAreThereNotifications();
887    }
888
889    public void removeNotification(IBinder key) {
890        if (DEBUG) Slog.d(TAG, "removeNotification(" + key + ")");
891        removeNotificationViews(key);
892        mTicker.remove(key);
893        setAreThereNotifications();
894    }
895
896    public void showClock(boolean show) {
897        View clock = mBarContents.findViewById(R.id.clock);
898        View network_text = mBarContents.findViewById(R.id.network_text);
899        if (clock != null) {
900            clock.setVisibility(show ? View.VISIBLE : View.GONE);
901        }
902        if (network_text != null) {
903            network_text.setVisibility((!show) ? View.VISIBLE : View.GONE);
904        }
905    }
906
907    public void disable(int state) {
908        int old = mDisabled;
909        int diff = state ^ old;
910        mDisabled = state;
911
912        // act accordingly
913        if ((diff & StatusBarManager.DISABLE_CLOCK) != 0) {
914            boolean show = (state & StatusBarManager.DISABLE_CLOCK) == 0;
915            Slog.i(TAG, "DISABLE_CLOCK: " + (show ? "no" : "yes"));
916            showClock(show);
917        }
918        if ((diff & StatusBarManager.DISABLE_SYSTEM_INFO) != 0) {
919            boolean show = (state & StatusBarManager.DISABLE_SYSTEM_INFO) == 0;
920            Slog.i(TAG, "DISABLE_SYSTEM_INFO: " + (show ? "no" : "yes"));
921            mNotificationTrigger.setVisibility(show ? View.VISIBLE : View.GONE);
922        }
923        if ((diff & StatusBarManager.DISABLE_EXPAND) != 0) {
924            if ((state & StatusBarManager.DISABLE_EXPAND) != 0) {
925                Slog.i(TAG, "DISABLE_EXPAND: yes");
926                animateCollapse();
927            }
928        }
929        if ((diff & StatusBarManager.DISABLE_NOTIFICATION_ICONS) != 0) {
930            mNotificationDNDMode = Prefs.read(mContext)
931                        .getBoolean(Prefs.DO_NOT_DISTURB_PREF, Prefs.DO_NOT_DISTURB_DEFAULT);
932
933            if ((state & StatusBarManager.DISABLE_NOTIFICATION_ICONS) != 0) {
934                Slog.i(TAG, "DISABLE_NOTIFICATION_ICONS: yes" + (mNotificationDNDMode?" (DND)":""));
935                mTicker.halt();
936            } else {
937                Slog.i(TAG, "DISABLE_NOTIFICATION_ICONS: no" + (mNotificationDNDMode?" (DND)":""));
938            }
939
940            // refresh icons to show either notifications or the DND message
941            reloadAllNotificationIcons();
942        } else if ((diff & StatusBarManager.DISABLE_NOTIFICATION_TICKER) != 0) {
943            if ((state & StatusBarManager.DISABLE_NOTIFICATION_TICKER) != 0) {
944                mTicker.halt();
945            }
946        }
947        if ((diff & StatusBarManager.DISABLE_NAVIGATION) != 0) {
948            if ((state & StatusBarManager.DISABLE_NAVIGATION) != 0) {
949                Slog.i(TAG, "DISABLE_NAVIGATION: yes");
950                mNavigationArea.setVisibility(View.INVISIBLE);
951                mInputMethodSwitchButton.setScreenLocked(true);
952            } else {
953                Slog.i(TAG, "DISABLE_NAVIGATION: no");
954                mNavigationArea.setVisibility(View.VISIBLE);
955                mInputMethodSwitchButton.setScreenLocked(false);
956            }
957        }
958        if ((diff & StatusBarManager.DISABLE_BACK) != 0) {
959            if ((state & StatusBarManager.DISABLE_BACK) != 0) {
960                Slog.i(TAG, "DISABLE_BACK: yes");
961                mBackButton.setVisibility(View.INVISIBLE);
962                mInputMethodSwitchButton.setScreenLocked(true);
963            } else {
964                Slog.i(TAG, "DISABLE_BACK: no");
965                mBackButton.setVisibility(View.VISIBLE);
966                mInputMethodSwitchButton.setScreenLocked(false);
967            }
968        }
969
970    }
971
972    private boolean hasTicker(Notification n) {
973        return n.tickerView != null || !TextUtils.isEmpty(n.tickerText);
974    }
975
976    private void tick(IBinder key, StatusBarNotification n, boolean firstTime) {
977        // Don't show the ticker when the windowshade is open.
978        if (mNotificationPanel.isShowing()) {
979            return;
980        }
981        // If they asked for FLAG_ONLY_ALERT_ONCE, then only show this notification
982        // if it's a new notification.
983        if (!firstTime && (n.notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) {
984            return;
985        }
986        // Show the ticker if one is requested. Also don't do this
987        // until status bar window is attached to the window manager,
988        // because...  well, what's the point otherwise?  And trying to
989        // run a ticker without being attached will crash!
990        if (hasTicker(n.notification) && mStatusBarView.getWindowToken() != null) {
991            if (0 == (mDisabled & (StatusBarManager.DISABLE_NOTIFICATION_ICONS
992                            | StatusBarManager.DISABLE_NOTIFICATION_TICKER))) {
993                mTicker.add(key, n);
994                mFeedbackIconArea.setVisibility(View.GONE);
995            }
996        }
997    }
998
999    // called by TabletTicker when it's done with all queued ticks
1000    public void doneTicking() {
1001        mFeedbackIconArea.setVisibility(View.VISIBLE);
1002    }
1003
1004    public void animateExpand() {
1005        mHandler.removeMessages(MSG_CLOSE_NOTIFICATION_PEEK);
1006        mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
1007        mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PEEK);
1008        mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PANEL);
1009        mHandler.sendEmptyMessage(MSG_OPEN_NOTIFICATION_PANEL);
1010    }
1011
1012    public void animateCollapse() {
1013        mHandler.removeMessages(MSG_CLOSE_NOTIFICATION_PANEL);
1014        mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PANEL);
1015        mHandler.removeMessages(MSG_CLOSE_RECENTS_PANEL);
1016        mHandler.sendEmptyMessage(MSG_CLOSE_RECENTS_PANEL);
1017        mHandler.removeMessages(MSG_CLOSE_INPUT_METHODS_PANEL);
1018        mHandler.sendEmptyMessage(MSG_CLOSE_INPUT_METHODS_PANEL);
1019        mHandler.removeMessages(MSG_CLOSE_COMPAT_MODE_PANEL);
1020        mHandler.sendEmptyMessage(MSG_CLOSE_COMPAT_MODE_PANEL);
1021        mHandler.removeMessages(MSG_CLOSE_NOTIFICATION_PEEK);
1022        mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PEEK);
1023    }
1024
1025    // called by StatusBar
1026    @Override
1027    public void setLightsOn(boolean on) {
1028        // Policy note: if the frontmost activity needs the menu key, we assume it is a legacy app
1029        // that can't handle lights-out mode.
1030        if (mMenuButton.getVisibility() == View.VISIBLE) {
1031            on = true;
1032        }
1033        mHandler.removeMessages(MSG_HIDE_CHROME);
1034        mHandler.removeMessages(MSG_SHOW_CHROME);
1035        mHandler.sendEmptyMessage(on ? MSG_SHOW_CHROME : MSG_HIDE_CHROME);
1036    }
1037
1038    public void topAppWindowChanged(boolean showMenu) {
1039        if (DEBUG) {
1040            Slog.d(TAG, (showMenu?"showing":"hiding") + " the MENU button");
1041        }
1042        mMenuButton.setVisibility(showMenu ? View.VISIBLE : View.GONE);
1043
1044        // See above re: lights-out policy for legacy apps.
1045        if (showMenu) setLightsOn(true);
1046
1047        mCompatModeButton.refresh();
1048        if (mCompatModeButton.getVisibility() == View.VISIBLE) {
1049                showCompatibilityHelp();
1050        } else {
1051            hideCompatibilityHelp();
1052            mCompatModePanel.closePanel();
1053        }
1054    }
1055
1056    private void showCompatibilityHelp() {
1057        if (mCompatibilityHelpDialog != null) {
1058            return;
1059        }
1060
1061        mCompatibilityHelpDialog = View.inflate(mContext, R.layout.compat_mode_help, null);
1062        View button = mCompatibilityHelpDialog.findViewById(R.id.button);
1063
1064        button.setOnClickListener(new View.OnClickListener() {
1065            @Override
1066            public void onClick(View v) {
1067                hideCompatibilityHelp();
1068                SharedPreferences.Editor editor = Prefs.edit(mContext);
1069                editor.putBoolean(Prefs.SHOWN_COMPAT_MODE_HELP, true);
1070                editor.apply();
1071            }
1072        });
1073
1074        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
1075                ViewGroup.LayoutParams.MATCH_PARENT,
1076                ViewGroup.LayoutParams.MATCH_PARENT,
1077                WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG,
1078                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
1079                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
1080                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
1081                PixelFormat.TRANSLUCENT);
1082        lp.setTitle("CompatibilityModeDialog");
1083        lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED
1084                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING;
1085        lp.windowAnimations = com.android.internal.R.style.Animation_ZoomButtons; // simple fade
1086
1087        WindowManagerImpl.getDefault().addView(mCompatibilityHelpDialog, lp);
1088    }
1089
1090    private void hideCompatibilityHelp() {
1091        if (mCompatibilityHelpDialog != null) {
1092            WindowManagerImpl.getDefault().removeView(mCompatibilityHelpDialog);
1093            mCompatibilityHelpDialog = null;
1094        }
1095    }
1096
1097    public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
1098        mInputMethodSwitchButton.setImeWindowStatus(token,
1099                (vis & InputMethodService.IME_ACTIVE) != 0);
1100        updateNotificationIcons();
1101        mInputMethodsPanel.setImeToken(token);
1102        int res;
1103        switch (backDisposition) {
1104            case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
1105                res = R.drawable.ic_sysbar_back;
1106                break;
1107            case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
1108                res = R.drawable.ic_sysbar_back_ime;
1109                break;
1110            case InputMethodService.BACK_DISPOSITION_DEFAULT:
1111            default:
1112                if ((vis & InputMethodService.IME_VISIBLE) != 0) {
1113                    res = R.drawable.ic_sysbar_back_ime;
1114                } else {
1115                    res = R.drawable.ic_sysbar_back;
1116                }
1117                break;
1118        }
1119        mBackButton.setImageResource(res);
1120        if (FAKE_SPACE_BAR) {
1121            mFakeSpaceBar.setVisibility(((vis & InputMethodService.IME_VISIBLE) != 0)
1122                    ? View.VISIBLE : View.GONE);
1123        }
1124    }
1125
1126    @Override
1127    public void setHardKeyboardStatus(boolean available, boolean enabled) {
1128        if (DEBUG) {
1129            Slog.d(TAG, "Set hard keyboard status: available=" + available
1130                    + ", enabled=" + enabled);
1131        }
1132        mInputMethodSwitchButton.setHardKeyboardStatus(available);
1133        updateNotificationIcons();
1134        mInputMethodsPanel.setHardKeyboardStatus(available, enabled);
1135    }
1136
1137    @Override
1138    public void onHardKeyboardEnabledChange(boolean enabled) {
1139        try {
1140            mBarService.setHardKeyboardEnabled(enabled);
1141        } catch (RemoteException ex) {
1142        }
1143    }
1144
1145    private boolean isImmersive() {
1146        try {
1147            return ActivityManagerNative.getDefault().isTopActivityImmersive();
1148            //Slog.d(TAG, "Top activity is " + (immersive?"immersive":"not immersive"));
1149        } catch (RemoteException ex) {
1150            // the end is nigh
1151            return false;
1152        }
1153    }
1154
1155    private void setAreThereNotifications() {
1156        final boolean hasClearable = mNotificationData.hasClearableItems();
1157    }
1158
1159    /**
1160     * Cancel this notification and tell the status bar service about the failure. Hold no locks.
1161     */
1162    void handleNotificationError(IBinder key, StatusBarNotification n, String message) {
1163        removeNotification(key);
1164        try {
1165            mBarService.onNotificationError(n.pkg, n.tag, n.id, n.uid, n.initialPid, message);
1166        } catch (RemoteException ex) {
1167            // The end is nigh.
1168        }
1169    }
1170
1171    private void sendKey(KeyEvent key) {
1172        try {
1173            if (DEBUG) Slog.d(TAG, "injecting key event: " + key);
1174            mWindowManager.injectInputEventNoWait(key);
1175        } catch (RemoteException ex) {
1176        }
1177    }
1178
1179    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
1180        public void onClick(View v) {
1181            if (v == mNotificationTrigger) {
1182                onClickNotificationTrigger();
1183            } else if (v == mRecentButton) {
1184                onClickRecentButton();
1185            } else if (v == mInputMethodSwitchButton) {
1186                onClickInputMethodSwitchButton();
1187            } else if (v == mCompatModeButton) {
1188                onClickCompatModeButton();
1189            }
1190        }
1191    };
1192
1193    public void onClickNotificationTrigger() {
1194        if (DEBUG) Slog.d(TAG, "clicked notification icons; disabled=" + mDisabled);
1195        if ((mDisabled & StatusBarManager.DISABLE_EXPAND) == 0) {
1196            int msg = !mNotificationPanel.isShowing()
1197                ? MSG_OPEN_NOTIFICATION_PANEL
1198                : MSG_CLOSE_NOTIFICATION_PANEL;
1199            mHandler.removeMessages(msg);
1200            mHandler.sendEmptyMessage(msg);
1201        }
1202    }
1203
1204    public void onClickRecentButton() {
1205        if (DEBUG) Slog.d(TAG, "clicked recent apps; disabled=" + mDisabled);
1206        if ((mDisabled & StatusBarManager.DISABLE_EXPAND) == 0) {
1207            int msg = (mRecentsPanel.getVisibility() == View.GONE)
1208                ? MSG_OPEN_RECENTS_PANEL
1209                : MSG_CLOSE_RECENTS_PANEL;
1210            mHandler.removeMessages(msg);
1211            mHandler.sendEmptyMessage(msg);
1212        }
1213    }
1214
1215    public void onClickInputMethodSwitchButton() {
1216        if (DEBUG) Slog.d(TAG, "clicked input methods panel; disabled=" + mDisabled);
1217        int msg = (mInputMethodsPanel.getVisibility() == View.GONE) ?
1218                MSG_OPEN_INPUT_METHODS_PANEL : MSG_CLOSE_INPUT_METHODS_PANEL;
1219        mHandler.removeMessages(msg);
1220        mHandler.sendEmptyMessage(msg);
1221    }
1222
1223    public void onClickCompatModeButton() {
1224        int msg = (mCompatModePanel.getVisibility() == View.GONE) ?
1225                MSG_OPEN_COMPAT_MODE_PANEL : MSG_CLOSE_COMPAT_MODE_PANEL;
1226        mHandler.removeMessages(msg);
1227        mHandler.sendEmptyMessage(msg);
1228    }
1229
1230    public NotificationClicker makeClicker(PendingIntent intent, String pkg, String tag, int id) {
1231        return new NotificationClicker(intent, pkg, tag, id);
1232    }
1233
1234    private class NotificationClicker implements View.OnClickListener {
1235        private PendingIntent mIntent;
1236        private String mPkg;
1237        private String mTag;
1238        private int mId;
1239
1240        NotificationClicker(PendingIntent intent, String pkg, String tag, int id) {
1241            mIntent = intent;
1242            mPkg = pkg;
1243            mTag = tag;
1244            mId = id;
1245        }
1246
1247        public void onClick(View v) {
1248            try {
1249                // The intent we are sending is for the application, which
1250                // won't have permission to immediately start an activity after
1251                // the user switches to home.  We know it is safe to do at this
1252                // point, so make sure new activity switches are now allowed.
1253                ActivityManagerNative.getDefault().resumeAppSwitches();
1254            } catch (RemoteException e) {
1255            }
1256
1257            if (mIntent != null) {
1258                int[] pos = new int[2];
1259                v.getLocationOnScreen(pos);
1260                Intent overlay = new Intent();
1261                overlay.setSourceBounds(
1262                        new Rect(pos[0], pos[1], pos[0]+v.getWidth(), pos[1]+v.getHeight()));
1263                try {
1264                    mIntent.send(mContext, 0, overlay);
1265                } catch (PendingIntent.CanceledException e) {
1266                    // the stack trace isn't very helpful here.  Just log the exception message.
1267                    Slog.w(TAG, "Sending contentIntent failed: " + e);
1268                }
1269            }
1270
1271            try {
1272                mBarService.onNotificationClick(mPkg, mTag, mId);
1273            } catch (RemoteException ex) {
1274                // system process is dead if we're here.
1275            }
1276
1277            // close the shade if it was open
1278            animateCollapse();
1279
1280            // If this click was on the intruder alert, hide that instead
1281//            mHandler.sendEmptyMessage(MSG_HIDE_INTRUDER);
1282        }
1283    }
1284
1285    StatusBarNotification removeNotificationViews(IBinder key) {
1286        NotificationData.Entry entry = mNotificationData.remove(key);
1287        if (entry == null) {
1288            Slog.w(TAG, "removeNotification for unknown key: " + key);
1289            return null;
1290        }
1291        // Remove the expanded view.
1292        ViewGroup rowParent = (ViewGroup)entry.row.getParent();
1293        if (rowParent != null) rowParent.removeView(entry.row);
1294
1295        if (key == mNotificationPeekKey) {
1296            // must close the peek as well, since it's gone
1297            mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PEEK);
1298        }
1299        // Remove the icon.
1300//        ViewGroup iconParent = (ViewGroup)entry.icon.getParent();
1301//        if (iconParent != null) iconParent.removeView(entry.icon);
1302        updateNotificationIcons();
1303
1304        return entry.notification;
1305    }
1306
1307    private class NotificationTriggerTouchListener implements View.OnTouchListener {
1308        VelocityTracker mVT;
1309        float mInitialTouchX, mInitialTouchY;
1310        int mTouchSlop;
1311
1312        public NotificationTriggerTouchListener() {
1313            mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
1314        }
1315
1316        public boolean onTouch(View v, MotionEvent event) {
1317//            Slog.d(TAG, String.format("touch: (%.1f, %.1f) initial: (%.1f, %.1f)",
1318//                        event.getX(),
1319//                        event.getY(),
1320//                        mInitialTouchX,
1321//                        mInitialTouchY));
1322            final int action = event.getAction();
1323            switch (action) {
1324                case MotionEvent.ACTION_DOWN:
1325                    mVT = VelocityTracker.obtain();
1326                    mInitialTouchX = event.getX();
1327                    mInitialTouchY = event.getY();
1328                    // fall through
1329                case MotionEvent.ACTION_OUTSIDE:
1330                case MotionEvent.ACTION_MOVE:
1331                    // check for fling
1332                    if (mVT != null) {
1333                        mVT.addMovement(event);
1334                        mVT.computeCurrentVelocity(1000); // pixels per second
1335                        // require a little more oomph once we're already in peekaboo mode
1336                        if (mVT.getYVelocity() < -mNotificationFlingVelocity) {
1337                            animateExpand();
1338                            mVT.recycle();
1339                            mVT = null;
1340                        }
1341                    }
1342                    return true;
1343                case MotionEvent.ACTION_UP:
1344                case MotionEvent.ACTION_CANCEL:
1345                    if (mVT != null) {
1346                        if (action == MotionEvent.ACTION_UP
1347                         // was this a sloppy tap?
1348                         && Math.abs(event.getX() - mInitialTouchX) < mTouchSlop
1349                         && Math.abs(event.getY() - mInitialTouchY) < (mTouchSlop / 3)
1350                         // dragging off the bottom doesn't count
1351                         && (int)event.getY() < v.getBottom()) {
1352                            animateExpand();
1353                            v.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
1354                            v.playSoundEffect(SoundEffectConstants.CLICK);
1355                        }
1356
1357                        mVT.recycle();
1358                        mVT = null;
1359                        return true;
1360                    }
1361            }
1362            return false;
1363        }
1364    }
1365
1366    final static int NOTIFICATION_PEEK_HOLD_THRESH = 200; // ms
1367    final static int NOTIFICATION_PEEK_FADE_DELAY = 3000; // ms
1368
1369    public void resetNotificationPeekFadeTimer() {
1370        if (DEBUG) {
1371            Slog.d(TAG, "setting peek fade timer for " + NOTIFICATION_PEEK_FADE_DELAY
1372                + "ms from now");
1373        }
1374        mHandler.removeMessages(MSG_CLOSE_NOTIFICATION_PEEK);
1375        mHandler.sendEmptyMessageDelayed(MSG_CLOSE_NOTIFICATION_PEEK,
1376                NOTIFICATION_PEEK_FADE_DELAY);
1377    }
1378
1379    private class NotificationIconTouchListener implements View.OnTouchListener {
1380        VelocityTracker mVT;
1381        int mPeekIndex;
1382        float mInitialTouchX, mInitialTouchY;
1383        int mTouchSlop;
1384
1385        public NotificationIconTouchListener() {
1386            mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
1387        }
1388
1389        public boolean onTouch(View v, MotionEvent event) {
1390            boolean peeking = mNotificationPeekWindow.getVisibility() != View.GONE;
1391            boolean panelShowing = mNotificationPanel.isShowing();
1392            if (panelShowing) return false;
1393
1394            int numIcons = mIconLayout.getChildCount();
1395            int newPeekIndex = (int)(event.getX() * numIcons / mIconLayout.getWidth());
1396            if (newPeekIndex > numIcons - 1) newPeekIndex = numIcons - 1;
1397            else if (newPeekIndex < 0) newPeekIndex = 0;
1398
1399            final int action = event.getAction();
1400            switch (action) {
1401                case MotionEvent.ACTION_DOWN:
1402                    mVT = VelocityTracker.obtain();
1403                    mInitialTouchX = event.getX();
1404                    mInitialTouchY = event.getY();
1405                    mPeekIndex = -1;
1406
1407                    // fall through
1408                case MotionEvent.ACTION_OUTSIDE:
1409                case MotionEvent.ACTION_MOVE:
1410                    // peek and switch icons if necessary
1411
1412                    if (newPeekIndex != mPeekIndex) {
1413                        mPeekIndex = newPeekIndex;
1414
1415                        if (DEBUG) Slog.d(TAG, "will peek at notification #" + mPeekIndex);
1416                        Message peekMsg = mHandler.obtainMessage(MSG_OPEN_NOTIFICATION_PEEK);
1417                        peekMsg.arg1 = mPeekIndex;
1418
1419                        mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
1420
1421                        if (peeking) {
1422                            // no delay if we're scrubbing left-right
1423                            mHandler.sendMessage(peekMsg);
1424                        } else {
1425                            // wait for fling
1426                            mHandler.sendMessageDelayed(peekMsg, NOTIFICATION_PEEK_HOLD_THRESH);
1427                        }
1428                    }
1429
1430                    // check for fling
1431                    if (mVT != null) {
1432                        mVT.addMovement(event);
1433                        mVT.computeCurrentVelocity(1000); // pixels per second
1434                        // require a little more oomph once we're already in peekaboo mode
1435                        if (!panelShowing && (
1436                               (peeking && mVT.getYVelocity() < -mNotificationFlingVelocity*3)
1437                            || (mVT.getYVelocity() < -mNotificationFlingVelocity))) {
1438                            mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
1439                            mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PANEL);
1440                            mHandler.sendEmptyMessage(MSG_CLOSE_NOTIFICATION_PEEK);
1441                            mHandler.sendEmptyMessage(MSG_OPEN_NOTIFICATION_PANEL);
1442                        }
1443                    }
1444                    return true;
1445                case MotionEvent.ACTION_UP:
1446                case MotionEvent.ACTION_CANCEL:
1447                    mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
1448                    if (!peeking) {
1449                        if (action == MotionEvent.ACTION_UP
1450                                // was this a sloppy tap?
1451                                && Math.abs(event.getX() - mInitialTouchX) < mTouchSlop
1452                                && Math.abs(event.getY() - mInitialTouchY) < (mTouchSlop / 3)
1453                                // dragging off the bottom doesn't count
1454                                && (int)event.getY() < v.getBottom()) {
1455                            Message peekMsg = mHandler.obtainMessage(MSG_OPEN_NOTIFICATION_PEEK);
1456                            peekMsg.arg1 = mPeekIndex;
1457                            mHandler.removeMessages(MSG_OPEN_NOTIFICATION_PEEK);
1458                            mHandler.sendMessage(peekMsg);
1459
1460                            v.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
1461                            v.playSoundEffect(SoundEffectConstants.CLICK);
1462
1463                            peeking = true; // not technically true yet, but the next line will run
1464                        }
1465                    }
1466
1467                    if (peeking) {
1468                        resetNotificationPeekFadeTimer();
1469                    }
1470
1471                    mVT.recycle();
1472                    mVT = null;
1473                    return true;
1474            }
1475            return false;
1476        }
1477    }
1478
1479    StatusBarIconView addNotificationViews(IBinder key, StatusBarNotification notification) {
1480        if (DEBUG) {
1481            Slog.d(TAG, "addNotificationViews(key=" + key + ", notification=" + notification);
1482        }
1483        // Construct the icon.
1484        final StatusBarIconView iconView = new StatusBarIconView(mContext,
1485                notification.pkg + "/0x" + Integer.toHexString(notification.id),
1486                notification.notification);
1487        iconView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
1488
1489        final StatusBarIcon ic = new StatusBarIcon(notification.pkg,
1490                    notification.notification.icon,
1491                    notification.notification.iconLevel,
1492                    notification.notification.number,
1493                    notification.notification.tickerText);
1494        if (!iconView.set(ic)) {
1495            handleNotificationError(key, notification, "Couldn't attach StatusBarIcon: " + ic);
1496            return null;
1497        }
1498        // Construct the expanded view.
1499        NotificationData.Entry entry = new NotificationData.Entry(key, notification, iconView);
1500        if (!inflateViews(entry, mPile)) {
1501            handleNotificationError(key, notification, "Couldn't expand RemoteViews for: "
1502                    + notification);
1503            return null;
1504        }
1505
1506        // Add the icon.
1507        int pos = mNotificationData.add(entry);
1508        if (DEBUG) {
1509            Slog.d(TAG, "addNotificationViews: added at " + pos);
1510        }
1511        updateNotificationIcons();
1512
1513        return iconView;
1514    }
1515
1516    private void reloadAllNotificationIcons() {
1517        if (mIconLayout == null) return;
1518        mIconLayout.removeAllViews();
1519        updateNotificationIcons();
1520    }
1521
1522    private void updateNotificationIcons() {
1523        // XXX: need to implement a new limited linear layout class
1524        // to avoid removing & readding everything
1525
1526        if (mIconLayout == null) return;
1527
1528        // first, populate the main notification panel
1529        loadNotificationPanel();
1530
1531        final LinearLayout.LayoutParams params
1532            = new LinearLayout.LayoutParams(mIconSize + 2*mIconHPadding, mNaturalBarHeight);
1533
1534        // alternate behavior in DND mode
1535        if (mNotificationDNDMode) {
1536            if (mIconLayout.getChildCount() == 0) {
1537                final Notification dndNotification = new Notification.Builder(mContext)
1538                    .setContentTitle(mContext.getText(R.string.notifications_off_title))
1539                    .setContentText(mContext.getText(R.string.notifications_off_text))
1540                    .setSmallIcon(R.drawable.ic_notification_dnd)
1541                    .setOngoing(true)
1542                    .getNotification();
1543
1544                final StatusBarIconView iconView = new StatusBarIconView(mContext, "_dnd",
1545                        dndNotification);
1546                iconView.setImageResource(R.drawable.ic_notification_dnd);
1547                iconView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
1548                iconView.setPadding(mIconHPadding, 0, mIconHPadding, 0);
1549
1550                mNotificationDNDDummyEntry = new NotificationData.Entry(
1551                        null,
1552                        new StatusBarNotification("", 0, "", 0, 0, dndNotification),
1553                        iconView);
1554
1555                mIconLayout.addView(iconView, params);
1556            }
1557
1558            return;
1559        }
1560
1561        int N = mNotificationData.size();
1562
1563        if (DEBUG) {
1564            Slog.d(TAG, "refreshing icons: " + N + " notifications, mIconLayout=" + mIconLayout);
1565        }
1566
1567        ArrayList<View> toShow = new ArrayList<View>();
1568
1569        // Extra Special Icons
1570        // The IME switcher and compatibility mode icons take the place of notifications. You didn't
1571        // need to see all those new emails, did you?
1572        int maxNotificationIconsCount = mMaxNotificationIcons;
1573        if (mInputMethodSwitchButton.getVisibility() != View.GONE) maxNotificationIconsCount --;
1574        if (mCompatModeButton.getVisibility()        != View.GONE) maxNotificationIconsCount --;
1575
1576        for (int i=0; i< maxNotificationIconsCount; i++) {
1577            if (i>=N) break;
1578            toShow.add(mNotificationData.get(N-i-1).icon);
1579        }
1580
1581        ArrayList<View> toRemove = new ArrayList<View>();
1582        for (int i=0; i<mIconLayout.getChildCount(); i++) {
1583            View child = mIconLayout.getChildAt(i);
1584            if (!toShow.contains(child)) {
1585                toRemove.add(child);
1586            }
1587        }
1588
1589        for (View remove : toRemove) {
1590            mIconLayout.removeView(remove);
1591        }
1592
1593        for (int i=0; i<toShow.size(); i++) {
1594            View v = toShow.get(i);
1595            v.setPadding(mIconHPadding, 0, mIconHPadding, 0);
1596            if (v.getParent() == null) {
1597                mIconLayout.addView(v, i, params);
1598            }
1599        }
1600    }
1601
1602    private void loadNotificationPanel() {
1603        int N = mNotificationData.size();
1604
1605        ArrayList<View> toShow = new ArrayList<View>();
1606
1607        for (int i=0; i<N; i++) {
1608            View row = mNotificationData.get(N-i-1).row;
1609            toShow.add(row);
1610        }
1611
1612        ArrayList<View> toRemove = new ArrayList<View>();
1613        for (int i=0; i<mPile.getChildCount(); i++) {
1614            View child = mPile.getChildAt(i);
1615            if (!toShow.contains(child)) {
1616                toRemove.add(child);
1617            }
1618        }
1619
1620        for (View remove : toRemove) {
1621            mPile.removeView(remove);
1622        }
1623
1624        for (int i=0; i<toShow.size(); i++) {
1625            View v = toShow.get(i);
1626            if (v.getParent() == null) {
1627                mPile.addView(v, N-1-i); // the notification panel has newest at the bottom
1628            }
1629        }
1630
1631        mNotificationPanel.setNotificationCount(N);
1632    }
1633
1634    void workAroundBadLayerDrawableOpacity(View v) {
1635        LayerDrawable d = (LayerDrawable)v.getBackground();
1636        if (d == null) return;
1637        v.setBackgroundDrawable(null);
1638        d.setOpacity(PixelFormat.TRANSLUCENT);
1639        v.setBackgroundDrawable(d);
1640    }
1641
1642    private boolean inflateViews(NotificationData.Entry entry, ViewGroup parent) {
1643        StatusBarNotification sbn = entry.notification;
1644        RemoteViews remoteViews = sbn.notification.contentView;
1645        if (remoteViews == null) {
1646            return false;
1647        }
1648
1649        // create the row view
1650        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(
1651                Context.LAYOUT_INFLATER_SERVICE);
1652        View row = inflater.inflate(R.layout.status_bar_notification_row, parent, false);
1653        workAroundBadLayerDrawableOpacity(row);
1654        View vetoButton = row.findViewById(R.id.veto);
1655        if (entry.notification.isClearable()) {
1656            final String _pkg = sbn.pkg;
1657            final String _tag = sbn.tag;
1658            final int _id = sbn.id;
1659            vetoButton.setOnClickListener(new View.OnClickListener() {
1660                    public void onClick(View v) {
1661                        try {
1662                            mBarService.onNotificationClear(_pkg, _tag, _id);
1663                        } catch (RemoteException ex) {
1664                            // system process is dead if we're here.
1665                        }
1666                    }
1667                });
1668        } else {
1669            if ((sbn.notification.flags & Notification.FLAG_ONGOING_EVENT) == 0) {
1670                vetoButton.setVisibility(View.INVISIBLE);
1671                vetoButton.setContentDescription("VETO");
1672            } else {
1673                vetoButton.setVisibility(View.GONE);
1674            }
1675        }
1676        vetoButton.setContentDescription(mContext.getString(
1677                R.string.accessibility_remove_notification));
1678
1679        // the large icon
1680        ImageView largeIcon = (ImageView)row.findViewById(R.id.large_icon);
1681        if (sbn.notification.largeIcon != null) {
1682            largeIcon.setImageBitmap(sbn.notification.largeIcon);
1683            largeIcon.setContentDescription(sbn.notification.tickerText);
1684        } else {
1685            largeIcon.getLayoutParams().width = 0;
1686            largeIcon.setVisibility(View.INVISIBLE);
1687        }
1688        largeIcon.setContentDescription(sbn.notification.tickerText);
1689
1690        // bind the click event to the content area
1691        ViewGroup content = (ViewGroup)row.findViewById(R.id.content);
1692        // XXX: update to allow controls within notification views
1693        content.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
1694//        content.setOnFocusChangeListener(mFocusChangeListener);
1695        PendingIntent contentIntent = sbn.notification.contentIntent;
1696        if (contentIntent != null) {
1697            content.setOnClickListener(new NotificationClicker(contentIntent,
1698                        sbn.pkg, sbn.tag, sbn.id));
1699        } else {
1700            content.setOnClickListener(null);
1701        }
1702
1703        View expanded = null;
1704        Exception exception = null;
1705        try {
1706            expanded = remoteViews.apply(mContext, content);
1707        }
1708        catch (RuntimeException e) {
1709            exception = e;
1710        }
1711        if (expanded == null) {
1712            final String ident = sbn.pkg + "/0x" + Integer.toHexString(sbn.id);
1713            Slog.e(TAG, "couldn't inflate view for notification " + ident, exception);
1714            return false;
1715        } else {
1716            content.addView(expanded);
1717            row.setDrawingCacheEnabled(true);
1718        }
1719
1720        entry.row = row;
1721        entry.content = content;
1722        entry.expanded = expanded;
1723        entry.largeIcon = largeIcon;
1724
1725        return true;
1726    }
1727
1728    public void userActivity() {
1729    }
1730
1731    public void toggleRecentApps() {
1732        int msg = (mRecentsPanel.getVisibility() == View.GONE)
1733                ? MSG_OPEN_RECENTS_PANEL : MSG_CLOSE_RECENTS_PANEL;
1734        mHandler.removeMessages(msg);
1735        mHandler.sendEmptyMessage(msg);
1736    }
1737
1738    public class TouchOutsideListener implements View.OnTouchListener {
1739        private int mMsg;
1740        private StatusBarPanel mPanel;
1741
1742        public TouchOutsideListener(int msg, StatusBarPanel panel) {
1743            mMsg = msg;
1744            mPanel = panel;
1745        }
1746
1747        public boolean onTouch(View v, MotionEvent ev) {
1748            final int action = ev.getAction();
1749            if (action == MotionEvent.ACTION_OUTSIDE
1750                    || (action == MotionEvent.ACTION_DOWN
1751                        && !mPanel.isInContentArea((int)ev.getX(), (int)ev.getY()))) {
1752                mHandler.removeMessages(mMsg);
1753                mHandler.sendEmptyMessage(mMsg);
1754                return true;
1755            }
1756            return false;
1757        }
1758    }
1759
1760    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1761        pw.print("mDisabled=0x");
1762        pw.println(Integer.toHexString(mDisabled));
1763        pw.println("mNetworkController:");
1764        mNetworkController.dump(fd, pw, args);
1765    }
1766}
1767
1768
1769