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