NavigationBarFragment.java revision f4c989962cd8accf027b1f10d4cbd51249886b33
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.statusbar.phone;
16
17import static android.app.StatusBarManager.NAVIGATION_HINT_BACK_ALT;
18import static android.app.StatusBarManager.NAVIGATION_HINT_IME_SHOWN;
19import static android.app.StatusBarManager.WINDOW_STATE_SHOWING;
20import static android.app.StatusBarManager.windowStateToString;
21
22import static com.android.internal.view.RotationPolicy.NATURAL_ROTATION;
23
24import static com.android.systemui.shared.system.NavigationBarCompat.InteractionType;
25import static com.android.systemui.statusbar.phone.BarTransitions.MODE_SEMI_TRANSPARENT;
26import static com.android.systemui.statusbar.phone.StatusBar.DEBUG_WINDOW_STATE;
27import static com.android.systemui.statusbar.phone.StatusBar.dumpBarTransitions;
28import static com.android.systemui.OverviewProxyService.OverviewProxyListener;
29
30import android.accessibilityservice.AccessibilityServiceInfo;
31import android.animation.Animator;
32import android.animation.AnimatorListenerAdapter;
33import android.animation.ObjectAnimator;
34import android.annotation.IdRes;
35import android.annotation.Nullable;
36import android.app.ActivityManager;
37import android.app.ActivityManagerNative;
38import android.app.Fragment;
39import android.app.IActivityManager;
40import android.app.StatusBarManager;
41import android.content.BroadcastReceiver;
42import android.content.ContentResolver;
43import android.content.Context;
44import android.content.Intent;
45import android.content.IntentFilter;
46import android.content.res.Configuration;
47import android.database.ContentObserver;
48import android.graphics.PixelFormat;
49import android.graphics.Rect;
50import android.graphics.drawable.AnimatedVectorDrawable;
51import android.inputmethodservice.InputMethodService;
52import android.os.Binder;
53import android.os.Bundle;
54import android.os.Handler;
55import android.os.IBinder;
56import android.os.Message;
57import android.os.RemoteException;
58import android.os.UserHandle;
59import android.provider.Settings;
60import android.support.annotation.VisibleForTesting;
61import android.telecom.TelecomManager;
62import android.text.TextUtils;
63import android.util.Log;
64import android.view.IRotationWatcher.Stub;
65import android.view.KeyEvent;
66import android.view.LayoutInflater;
67import android.view.MotionEvent;
68import android.view.Surface;
69import android.view.View;
70import android.view.ViewGroup;
71import android.view.WindowManager;
72import android.view.WindowManager.LayoutParams;
73import android.view.WindowManagerGlobal;
74import android.view.accessibility.AccessibilityEvent;
75import android.view.accessibility.AccessibilityManager;
76import android.view.accessibility.AccessibilityManager.AccessibilityServicesStateChangeListener;
77
78import com.android.internal.logging.MetricsLogger;
79import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
80import com.android.internal.util.LatencyTracker;
81import com.android.systemui.Dependency;
82import com.android.systemui.Interpolators;
83import com.android.systemui.OverviewProxyService;
84import com.android.systemui.R;
85import com.android.systemui.SysUiServiceProvider;
86import com.android.systemui.assist.AssistManager;
87import com.android.systemui.fragments.FragmentHostManager;
88import com.android.systemui.fragments.FragmentHostManager.FragmentListener;
89import com.android.systemui.recents.Recents;
90import com.android.systemui.recents.misc.SysUiTaskStackChangeListener;
91import com.android.systemui.shared.system.ActivityManagerWrapper;
92import com.android.systemui.stackdivider.Divider;
93import com.android.systemui.statusbar.CommandQueue;
94import com.android.systemui.statusbar.CommandQueue.Callbacks;
95import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper;
96import com.android.systemui.statusbar.policy.KeyButtonDrawable;
97import com.android.systemui.statusbar.policy.KeyButtonView;
98import com.android.systemui.statusbar.policy.RotationLockController;
99import com.android.systemui.statusbar.stack.StackStateAnimator;
100
101import java.io.FileDescriptor;
102import java.io.PrintWriter;
103import java.util.List;
104import java.util.Locale;
105import java.util.Optional;
106
107/**
108 * Fragment containing the NavigationBarFragment. Contains logic for what happens
109 * on clicks and view states of the nav bar.
110 */
111public class NavigationBarFragment extends Fragment implements Callbacks {
112
113    public static final String TAG = "NavigationBar";
114    private static final boolean DEBUG = false;
115    private static final boolean DEBUG_ROTATION = true;
116    private static final String EXTRA_DISABLE_STATE = "disabled_state";
117    private static final String EXTRA_DISABLE2_STATE = "disabled2_state";
118
119    private final static int BUTTON_FADE_IN_OUT_DURATION_MS = 100;
120    private final static int NAVBAR_HIDDEN_PENDING_ICON_TIMEOUT_MS = 20000;
121
122    private static final int NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION = 3;
123
124    /** Allow some time inbetween the long press for back and recents. */
125    private static final int LOCK_TO_APP_GESTURE_TOLERENCE = 200;
126
127    protected NavigationBarView mNavigationBarView = null;
128    protected AssistManager mAssistManager;
129
130    private int mNavigationBarWindowState = WINDOW_STATE_SHOWING;
131
132    private int mNavigationIconHints = 0;
133    private int mNavigationBarMode;
134    private boolean mAccessibilityFeedbackEnabled;
135    private AccessibilityManager mAccessibilityManager;
136    private MagnificationContentObserver mMagnificationObserver;
137    private ContentResolver mContentResolver;
138    private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
139
140    private int mDisabledFlags1;
141    private int mDisabledFlags2;
142    private StatusBar mStatusBar;
143    private Recents mRecents;
144    private Divider mDivider;
145    private WindowManager mWindowManager;
146    private CommandQueue mCommandQueue;
147    private long mLastLockToAppLongPress;
148
149    private Locale mLocale;
150    private int mLayoutDirection;
151
152    private int mSystemUiVisibility;
153    private LightBarController mLightBarController;
154
155    private OverviewProxyService mOverviewProxyService;
156
157    public boolean mHomeBlockedThisTouch;
158
159    private int mLastRotationSuggestion;
160    private boolean mPendingRotationSuggestion;
161    private boolean mHoveringRotationSuggestion;
162    private RotationLockController mRotationLockController;
163    private TaskStackListenerImpl mTaskStackListener;
164
165    private final Runnable mRemoveRotationProposal = () -> setRotateSuggestionButtonState(false);
166    private final Runnable mCancelPendingRotationProposal =
167            () -> mPendingRotationSuggestion = false;
168    private Animator mRotateHideAnimator;
169    private ViewRippler mViewRippler = new ViewRippler();
170
171    private final OverviewProxyListener mOverviewProxyListener = new OverviewProxyListener() {
172        @Override
173        public void onConnectionChanged(boolean isConnected) {
174            mNavigationBarView.updateStates();
175            updateScreenPinningGestures();
176        }
177
178        @Override
179        public void onQuickStepStarted() {
180            // Use navbar dragging as a signal to hide the rotate button
181            setRotateSuggestionButtonState(false);
182        }
183
184        @Override
185        public void onInteractionFlagsChanged(@InteractionType int flags) {
186            mNavigationBarView.updateStates();
187            updateScreenPinningGestures();
188        }
189    };
190
191    // ----- Fragment Lifecycle Callbacks -----
192
193    @Override
194    public void onCreate(@Nullable Bundle savedInstanceState) {
195        super.onCreate(savedInstanceState);
196        mCommandQueue = SysUiServiceProvider.getComponent(getContext(), CommandQueue.class);
197        mCommandQueue.addCallbacks(this);
198        mStatusBar = SysUiServiceProvider.getComponent(getContext(), StatusBar.class);
199        mRecents = SysUiServiceProvider.getComponent(getContext(), Recents.class);
200        mDivider = SysUiServiceProvider.getComponent(getContext(), Divider.class);
201        mWindowManager = getContext().getSystemService(WindowManager.class);
202        mAccessibilityManager = getContext().getSystemService(AccessibilityManager.class);
203        Dependency.get(AccessibilityManagerWrapper.class).addCallback(
204                mAccessibilityListener);
205        mContentResolver = getContext().getContentResolver();
206        mMagnificationObserver = new MagnificationContentObserver(
207                getContext().getMainThreadHandler());
208        mContentResolver.registerContentObserver(Settings.Secure.getUriFor(
209                Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED), false,
210                mMagnificationObserver, UserHandle.USER_ALL);
211
212        if (savedInstanceState != null) {
213            mDisabledFlags1 = savedInstanceState.getInt(EXTRA_DISABLE_STATE, 0);
214            mDisabledFlags2 = savedInstanceState.getInt(EXTRA_DISABLE2_STATE, 0);
215        }
216        mAssistManager = Dependency.get(AssistManager.class);
217        mOverviewProxyService = Dependency.get(OverviewProxyService.class);
218
219        try {
220            WindowManagerGlobal.getWindowManagerService()
221                    .watchRotation(mRotationWatcher, getContext().getDisplay().getDisplayId());
222        } catch (RemoteException e) {
223            throw e.rethrowFromSystemServer();
224        }
225
226        mRotationLockController = Dependency.get(RotationLockController.class);
227
228        // Reset user rotation pref to match that of the WindowManager if starting in locked mode
229        // This will automatically happen when switching from auto-rotate to locked mode
230        if (mRotationLockController.isRotationLocked()) {
231            final int winRotation = mWindowManager.getDefaultDisplay().getRotation();
232            mRotationLockController.setRotationLockedAtAngle(true, winRotation);
233        }
234
235        // Register the task stack listener
236        mTaskStackListener = new TaskStackListenerImpl();
237        ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
238    }
239
240    @Override
241    public void onDestroy() {
242        super.onDestroy();
243        mCommandQueue.removeCallbacks(this);
244        Dependency.get(AccessibilityManagerWrapper.class).removeCallback(
245                mAccessibilityListener);
246        mContentResolver.unregisterContentObserver(mMagnificationObserver);
247        try {
248            WindowManagerGlobal.getWindowManagerService()
249                    .removeRotationWatcher(mRotationWatcher);
250        } catch (RemoteException e) {
251            throw e.rethrowFromSystemServer();
252        }
253
254        // Unregister the task stack listener
255        ActivityManagerWrapper.getInstance().unregisterTaskStackListener(mTaskStackListener);
256    }
257
258    @Override
259    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
260            Bundle savedInstanceState) {
261        return inflater.inflate(R.layout.navigation_bar, container, false);
262    }
263
264    @Override
265    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
266        super.onViewCreated(view, savedInstanceState);
267        mNavigationBarView = (NavigationBarView) view;
268
269        mNavigationBarView.setDisabledFlags(mDisabledFlags1);
270        mNavigationBarView.setComponents(mRecents, mDivider, mStatusBar.getPanel());
271        mNavigationBarView.setOnVerticalChangedListener(this::onVerticalChanged);
272        mNavigationBarView.setOnTouchListener(this::onNavigationTouch);
273        if (savedInstanceState != null) {
274            mNavigationBarView.getLightTransitionsController().restoreState(savedInstanceState);
275        }
276
277        prepareNavigationBarView();
278        checkNavBarModes();
279
280        setDisabled2Flags(mDisabledFlags2);
281
282        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
283        filter.addAction(Intent.ACTION_SCREEN_ON);
284        getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);
285        notifyNavigationBarScreenOn();
286        mOverviewProxyService.addCallback(mOverviewProxyListener);
287    }
288
289    @Override
290    public void onDestroyView() {
291        super.onDestroyView();
292        mNavigationBarView.getLightTransitionsController().destroy(getContext());
293        mOverviewProxyService.removeCallback(mOverviewProxyListener);
294        getContext().unregisterReceiver(mBroadcastReceiver);
295    }
296
297    @Override
298    public void onSaveInstanceState(Bundle outState) {
299        super.onSaveInstanceState(outState);
300        outState.putInt(EXTRA_DISABLE_STATE, mDisabledFlags1);
301        outState.putInt(EXTRA_DISABLE2_STATE, mDisabledFlags2);
302        if (mNavigationBarView != null) {
303            mNavigationBarView.getLightTransitionsController().saveState(outState);
304        }
305    }
306
307    @Override
308    public void onConfigurationChanged(Configuration newConfig) {
309        super.onConfigurationChanged(newConfig);
310        final Locale locale = getContext().getResources().getConfiguration().locale;
311        final int ld = TextUtils.getLayoutDirectionFromLocale(locale);
312        if (!locale.equals(mLocale) || ld != mLayoutDirection) {
313            if (DEBUG) {
314                Log.v(TAG, String.format(
315                        "config changed locale/LD: %s (%d) -> %s (%d)", mLocale, mLayoutDirection,
316                        locale, ld));
317            }
318            mLocale = locale;
319            mLayoutDirection = ld;
320            refreshLayout(ld);
321        }
322        repositionNavigationBar();
323    }
324
325    @Override
326    public void dump(String prefix, FileDescriptor fd, PrintWriter pw, String[] args) {
327        if (mNavigationBarView != null) {
328            pw.print("  mNavigationBarWindowState=");
329            pw.println(windowStateToString(mNavigationBarWindowState));
330            pw.print("  mNavigationBarMode=");
331            pw.println(BarTransitions.modeToString(mNavigationBarMode));
332            dumpBarTransitions(pw, "mNavigationBarView", mNavigationBarView.getBarTransitions());
333        }
334
335        pw.print("  mNavigationBarView=");
336        if (mNavigationBarView == null) {
337            pw.println("null");
338        } else {
339            mNavigationBarView.dump(fd, pw, args);
340        }
341    }
342
343    // ----- CommandQueue Callbacks -----
344
345    @Override
346    public void setImeWindowStatus(IBinder token, int vis, int backDisposition,
347            boolean showImeSwitcher) {
348        boolean imeShown = (vis & InputMethodService.IME_VISIBLE) != 0;
349        int hints = mNavigationIconHints;
350        switch (backDisposition) {
351            case InputMethodService.BACK_DISPOSITION_DEFAULT:
352            case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
353            case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
354                if (imeShown) {
355                    hints |= NAVIGATION_HINT_BACK_ALT;
356                } else {
357                    hints &= ~NAVIGATION_HINT_BACK_ALT;
358                }
359                break;
360            case InputMethodService.BACK_DISPOSITION_ADJUST_NOTHING:
361                hints &= ~NAVIGATION_HINT_BACK_ALT;
362                break;
363        }
364        if (showImeSwitcher) {
365            hints |= NAVIGATION_HINT_IME_SHOWN;
366        } else {
367            hints &= ~NAVIGATION_HINT_IME_SHOWN;
368        }
369        if (hints == mNavigationIconHints) return;
370
371        mNavigationIconHints = hints;
372
373        if (mNavigationBarView != null) {
374            mNavigationBarView.setNavigationIconHints(hints);
375        }
376        mStatusBar.checkBarModes();
377    }
378
379    @Override
380    public void topAppWindowChanged(boolean showMenu) {
381        if (mNavigationBarView != null) {
382            mNavigationBarView.setMenuVisibility(showMenu);
383        }
384    }
385
386    @Override
387    public void setWindowState(int window, int state) {
388        if (mNavigationBarView != null
389                && window == StatusBarManager.WINDOW_NAVIGATION_BAR
390                && mNavigationBarWindowState != state) {
391            mNavigationBarWindowState = state;
392            if (DEBUG_WINDOW_STATE) Log.d(TAG, "Navigation bar " + windowStateToString(state));
393
394            // If the navbar is visible, show the rotate button if there's a pending suggestion
395            if (state == WINDOW_STATE_SHOWING && mPendingRotationSuggestion) {
396                showAndLogRotationSuggestion();
397            }
398        }
399    }
400
401    @Override
402    public void onRotationProposal(final int rotation, boolean isValid) {
403        final int winRotation = mWindowManager.getDefaultDisplay().getRotation();
404        final boolean rotateSuggestionsDisabled = hasDisable2RotateSuggestionFlag(mDisabledFlags2);
405        if (DEBUG_ROTATION) {
406            Log.v(TAG, "onRotationProposal proposedRotation=" + Surface.rotationToString(rotation)
407                    + ", winRotation=" + Surface.rotationToString(winRotation)
408                    + ", isValid=" + isValid + ", mNavBarWindowState="
409                    + StatusBarManager.windowStateToString(mNavigationBarWindowState)
410                    + ", rotateSuggestionsDisabled=" + rotateSuggestionsDisabled
411                    + ", isRotateButtonVisible=" + (mNavigationBarView == null ? "null" :
412                        mNavigationBarView.isRotateButtonVisible()));
413        }
414
415        // Respect the disabled flag, no need for action as flag change callback will handle hiding
416        if (rotateSuggestionsDisabled) return;
417
418        // This method will be called on rotation suggestion changes even if the proposed rotation
419        // is not valid for the top app. Use invalid rotation choices as a signal to remove the
420        // rotate button if shown.
421        if (!isValid) {
422            setRotateSuggestionButtonState(false);
423            return;
424        }
425
426        // If window rotation matches suggested rotation, remove any current suggestions
427        if (rotation == winRotation) {
428            getView().removeCallbacks(mRemoveRotationProposal);
429            setRotateSuggestionButtonState(false);
430            return;
431        }
432
433        // Prepare to show the navbar icon by updating the icon style to change anim params
434        mLastRotationSuggestion = rotation; // Remember rotation for click
435        if (mNavigationBarView != null) {
436            final boolean rotationCCW = isRotationAnimationCCW(winRotation, rotation);
437            int style;
438            if (winRotation == Surface.ROTATION_0 || winRotation == Surface.ROTATION_180) {
439                style = rotationCCW ? R.style.RotateButtonCCWStart90 :
440                        R.style.RotateButtonCWStart90;
441            } else { // 90 or 270
442                style = rotationCCW ? R.style.RotateButtonCCWStart0 :
443                        R.style.RotateButtonCWStart0;
444            }
445            mNavigationBarView.updateRotateSuggestionButtonStyle(style, true);
446        }
447
448        if (mNavigationBarWindowState != WINDOW_STATE_SHOWING) {
449            // If the navbar isn't shown, flag the rotate icon to be shown should the navbar become
450            // visible given some time limit.
451            mPendingRotationSuggestion = true;
452            getView().removeCallbacks(mCancelPendingRotationProposal);
453            getView().postDelayed(mCancelPendingRotationProposal,
454                    NAVBAR_HIDDEN_PENDING_ICON_TIMEOUT_MS);
455
456        } else { // The navbar is visible so show the icon right away
457            showAndLogRotationSuggestion();
458        }
459    }
460
461    private void onRotationSuggestionsDisabled() {
462        // Immediately hide the rotate button and clear any planned removal
463        setRotateSuggestionButtonState(false, true);
464
465        // This method can be called before view setup is done, ensure getView isn't null
466        final View v = getView();
467        if (v != null) v.removeCallbacks(mRemoveRotationProposal);
468    }
469
470    private void showAndLogRotationSuggestion() {
471        setRotateSuggestionButtonState(true);
472        rescheduleRotationTimeout(false);
473        mMetricsLogger.visible(MetricsEvent.ROTATION_SUGGESTION_SHOWN);
474    }
475
476    private boolean isRotationAnimationCCW(int from, int to) {
477        // All 180deg WM rotation animations are CCW, match that
478        if (from == Surface.ROTATION_0 && to == Surface.ROTATION_90) return false;
479        if (from == Surface.ROTATION_0 && to == Surface.ROTATION_180) return true; //180d so CCW
480        if (from == Surface.ROTATION_0 && to == Surface.ROTATION_270) return true;
481        if (from == Surface.ROTATION_90 && to == Surface.ROTATION_0) return true;
482        if (from == Surface.ROTATION_90 && to == Surface.ROTATION_180) return false;
483        if (from == Surface.ROTATION_90 && to == Surface.ROTATION_270) return true; //180d so CCW
484        if (from == Surface.ROTATION_180 && to == Surface.ROTATION_0) return true; //180d so CCW
485        if (from == Surface.ROTATION_180 && to == Surface.ROTATION_90) return true;
486        if (from == Surface.ROTATION_180 && to == Surface.ROTATION_270) return false;
487        if (from == Surface.ROTATION_270 && to == Surface.ROTATION_0) return false;
488        if (from == Surface.ROTATION_270 && to == Surface.ROTATION_90) return true; //180d so CCW
489        if (from == Surface.ROTATION_270 && to == Surface.ROTATION_180) return true;
490        return false; // Default
491    }
492
493    public void setRotateSuggestionButtonState(final boolean visible) {
494        setRotateSuggestionButtonState(visible, false);
495    }
496
497    public void setRotateSuggestionButtonState(final boolean visible, final boolean force) {
498        if (mNavigationBarView == null) return;
499
500        // At any point the the button can become invisible because an a11y service became active.
501        // Similarly, a call to make the button visible may be rejected because an a11y service is
502        // active. Must account for this.
503
504        ButtonDispatcher rotBtn = mNavigationBarView.getRotateSuggestionButton();
505        final boolean currentlyVisible = mNavigationBarView.isRotateButtonVisible();
506
507        // Rerun a show animation to indicate change but don't rerun a hide animation
508        if (!visible && !currentlyVisible) return;
509
510        View view = rotBtn.getCurrentView();
511        if (view == null) return;
512
513        KeyButtonDrawable kbd = rotBtn.getImageDrawable();
514        if (kbd == null) return;
515
516        // The KBD and AVD is recreated every new valid suggestion because of style changes.
517        AnimatedVectorDrawable animIcon = null;
518        if (kbd.getDrawable(0) instanceof AnimatedVectorDrawable) {
519            animIcon = (AnimatedVectorDrawable) kbd.getDrawable(0);
520        }
521
522        // Clear any pending suggestion flag as it has either been nullified or is being shown
523        mPendingRotationSuggestion = false;
524        if (getView() != null) getView().removeCallbacks(mCancelPendingRotationProposal);
525
526        // Handle the visibility change and animation
527        if (visible) { // Appear and change (cannot force)
528            // Stop and clear any currently running hide animations
529            if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
530                mRotateHideAnimator.cancel();
531            }
532            mRotateHideAnimator = null;
533
534            // Reset the alpha if any has changed due to hide animation
535            view.setAlpha(1f);
536
537            // Run the rotate icon's animation if it has one
538            if (animIcon != null) {
539                animIcon.reset();
540                animIcon.start();
541            }
542
543            if (!isRotateSuggestionIntroduced()) mViewRippler.start(view);
544
545            // Set visibility, may fail if a11y service is active.
546            // If invisible, call will stop animation.
547            int appliedVisibility = mNavigationBarView.setRotateButtonVisibility(true);
548            if (appliedVisibility == View.VISIBLE) {
549                // If the button will actually become visible and the navbar is about to hide,
550                // tell the statusbar to keep it around for longer
551                mStatusBar.touchAutoHide();
552            }
553
554        } else { // Hide
555
556            mViewRippler.stop(); // Prevent any pending ripples, force hide or not
557
558            if (force) {
559                // If a hide animator is running stop it and make invisible
560                if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
561                    mRotateHideAnimator.pause();
562                }
563                mNavigationBarView.setRotateButtonVisibility(false);
564                return;
565            }
566
567            // Don't start any new hide animations if one is running
568            if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
569
570            ObjectAnimator fadeOut = ObjectAnimator.ofFloat(view, "alpha",
571                    0f);
572            fadeOut.setDuration(BUTTON_FADE_IN_OUT_DURATION_MS);
573            fadeOut.setInterpolator(Interpolators.LINEAR);
574            fadeOut.addListener(new AnimatorListenerAdapter() {
575                @Override
576                public void onAnimationEnd(Animator animation) {
577                    mNavigationBarView.setRotateButtonVisibility(false);
578                }
579            });
580
581            mRotateHideAnimator = fadeOut;
582            fadeOut.start();
583        }
584    }
585
586    private void rescheduleRotationTimeout(final boolean reasonHover) {
587        // May be called due to a new rotation proposal or a change in hover state
588        if (reasonHover) {
589            // Don't reschedule if a hide animator is running
590            if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
591            // Don't reschedule if not visible
592            if (!mNavigationBarView.isRotateButtonVisible()) return;
593        }
594
595        getView().removeCallbacks(mRemoveRotationProposal); // Stop any pending removal
596        getView().postDelayed(mRemoveRotationProposal,
597                computeRotationProposalTimeout()); // Schedule timeout
598    }
599
600    private int computeRotationProposalTimeout() {
601        if (mAccessibilityFeedbackEnabled) return 20000;
602        if (mHoveringRotationSuggestion) return 16000;
603        return 6000;
604    }
605
606    private boolean isRotateSuggestionIntroduced() {
607        ContentResolver cr = getContext().getContentResolver();
608        return Settings.Secure.getInt(cr, Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED, 0)
609                >= NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION;
610    }
611
612    private void incrementNumAcceptedRotationSuggestionsIfNeeded() {
613        // Get the number of accepted suggestions
614        ContentResolver cr = getContext().getContentResolver();
615        final int numSuggestions = Settings.Secure.getInt(cr,
616                Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED, 0);
617
618        // Increment the number of accepted suggestions only if it would change intro mode
619        if (numSuggestions < NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION) {
620            Settings.Secure.putInt(cr, Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED,
621                    numSuggestions + 1);
622        }
623    }
624
625    // Injected from StatusBar at creation.
626    public void setCurrentSysuiVisibility(int systemUiVisibility) {
627        mSystemUiVisibility = systemUiVisibility;
628        mNavigationBarMode = mStatusBar.computeBarMode(0, mSystemUiVisibility,
629                View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
630                View.NAVIGATION_BAR_TRANSPARENT);
631        checkNavBarModes();
632        mStatusBar.touchAutoHide();
633        mLightBarController.onNavigationVisibilityChanged(mSystemUiVisibility, 0 /* mask */,
634                true /* nbModeChanged */, mNavigationBarMode);
635    }
636
637    @Override
638    public void setSystemUiVisibility(int vis, int fullscreenStackVis, int dockedStackVis,
639            int mask, Rect fullscreenStackBounds, Rect dockedStackBounds) {
640        final int oldVal = mSystemUiVisibility;
641        final int newVal = (oldVal & ~mask) | (vis & mask);
642        final int diff = newVal ^ oldVal;
643        boolean nbModeChanged = false;
644        if (diff != 0) {
645            mSystemUiVisibility = newVal;
646
647            // update navigation bar mode
648            final int nbMode = getView() == null
649                    ? -1 : mStatusBar.computeBarMode(oldVal, newVal,
650                    View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
651                    View.NAVIGATION_BAR_TRANSPARENT);
652            nbModeChanged = nbMode != -1;
653            if (nbModeChanged) {
654                if (mNavigationBarMode != nbMode) {
655                    mNavigationBarMode = nbMode;
656                    checkNavBarModes();
657                }
658                mStatusBar.touchAutoHide();
659            }
660        }
661
662        mLightBarController.onNavigationVisibilityChanged(vis, mask, nbModeChanged,
663                mNavigationBarMode);
664    }
665
666    @Override
667    public void disable(int state1, int state2, boolean animate) {
668        // Navigation bar flags are in both state1 and state2.
669        final int masked = state1 & (StatusBarManager.DISABLE_HOME
670                | StatusBarManager.DISABLE_RECENT
671                | StatusBarManager.DISABLE_BACK
672                | StatusBarManager.DISABLE_SEARCH);
673        if (masked != mDisabledFlags1) {
674            mDisabledFlags1 = masked;
675            if (mNavigationBarView != null) mNavigationBarView.setDisabledFlags(state1);
676            updateScreenPinningGestures();
677        }
678
679        final int masked2 = state2 & (StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS);
680        if (masked2 != mDisabledFlags2) {
681            mDisabledFlags2 = masked2;
682            setDisabled2Flags(masked2);
683        }
684    }
685
686    private void setDisabled2Flags(int state2) {
687        // Method only called on change of disable2 flags
688        final boolean rotateSuggestionsDisabled = hasDisable2RotateSuggestionFlag(state2);
689        if (rotateSuggestionsDisabled) onRotationSuggestionsDisabled();
690    }
691
692    private boolean hasDisable2RotateSuggestionFlag(int disable2Flags) {
693        return (disable2Flags & StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS) != 0;
694    }
695
696    // ----- Internal stuffz -----
697
698    private void refreshLayout(int layoutDirection) {
699        if (mNavigationBarView != null) {
700            mNavigationBarView.setLayoutDirection(layoutDirection);
701        }
702    }
703
704    private boolean shouldDisableNavbarGestures() {
705        return !mStatusBar.isDeviceProvisioned()
706                || (mDisabledFlags1 & StatusBarManager.DISABLE_SEARCH) != 0;
707    }
708
709    private void repositionNavigationBar() {
710        if (mNavigationBarView == null || !mNavigationBarView.isAttachedToWindow()) return;
711
712        prepareNavigationBarView();
713
714        mWindowManager.updateViewLayout((View) mNavigationBarView.getParent(),
715                ((View) mNavigationBarView.getParent()).getLayoutParams());
716    }
717
718    private void updateScreenPinningGestures() {
719        if (mNavigationBarView == null) {
720            return;
721        }
722
723        // Change the cancel pin gesture to home and back if recents button is invisible
724        boolean recentsVisible = mNavigationBarView.isRecentsButtonVisible();
725        ButtonDispatcher backButton = mNavigationBarView.getBackButton();
726        if (recentsVisible) {
727            backButton.setOnLongClickListener(this::onLongPressBackRecents);
728        } else {
729            backButton.setOnLongClickListener(this::onLongPressBackHome);
730        }
731    }
732
733    private void notifyNavigationBarScreenOn() {
734        mNavigationBarView.updateNavButtonIcons();
735    }
736
737    private void prepareNavigationBarView() {
738        mNavigationBarView.reorient();
739
740        ButtonDispatcher recentsButton = mNavigationBarView.getRecentsButton();
741        recentsButton.setOnClickListener(this::onRecentsClick);
742        recentsButton.setOnTouchListener(this::onRecentsTouch);
743        recentsButton.setLongClickable(true);
744        recentsButton.setOnLongClickListener(this::onLongPressBackRecents);
745
746        ButtonDispatcher backButton = mNavigationBarView.getBackButton();
747        backButton.setLongClickable(true);
748
749        ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();
750        homeButton.setOnTouchListener(this::onHomeTouch);
751        homeButton.setOnLongClickListener(this::onHomeLongClick);
752
753        ButtonDispatcher accessibilityButton = mNavigationBarView.getAccessibilityButton();
754        accessibilityButton.setOnClickListener(this::onAccessibilityClick);
755        accessibilityButton.setOnLongClickListener(this::onAccessibilityLongClick);
756        updateAccessibilityServicesState(mAccessibilityManager);
757
758        ButtonDispatcher rotateSuggestionButton = mNavigationBarView.getRotateSuggestionButton();
759        rotateSuggestionButton.setOnClickListener(this::onRotateSuggestionClick);
760        rotateSuggestionButton.setOnHoverListener(this::onRotateSuggestionHover);
761        updateScreenPinningGestures();
762    }
763
764    private boolean onHomeTouch(View v, MotionEvent event) {
765        if (mHomeBlockedThisTouch && event.getActionMasked() != MotionEvent.ACTION_DOWN) {
766            return true;
767        }
768        // If an incoming call is ringing, HOME is totally disabled.
769        // (The user is already on the InCallUI at this point,
770        // and his ONLY options are to answer or reject the call.)
771        switch (event.getAction()) {
772            case MotionEvent.ACTION_DOWN:
773                mHomeBlockedThisTouch = false;
774                TelecomManager telecomManager =
775                        getContext().getSystemService(TelecomManager.class);
776                if (telecomManager != null && telecomManager.isRinging()) {
777                    if (mStatusBar.isKeyguardShowing()) {
778                        Log.i(TAG, "Ignoring HOME; there's a ringing incoming call. " +
779                                "No heads up");
780                        mHomeBlockedThisTouch = true;
781                        return true;
782                    }
783                }
784                break;
785            case MotionEvent.ACTION_UP:
786            case MotionEvent.ACTION_CANCEL:
787                mStatusBar.awakenDreams();
788                break;
789        }
790        return false;
791    }
792
793    private void onVerticalChanged(boolean isVertical) {
794        mStatusBar.setQsScrimEnabled(!isVertical);
795    }
796
797    private boolean onNavigationTouch(View v, MotionEvent event) {
798        mStatusBar.checkUserAutohide(event);
799        return false;
800    }
801
802    @VisibleForTesting
803    boolean onHomeLongClick(View v) {
804        if (!mNavigationBarView.isRecentsButtonVisible()
805                && ActivityManagerWrapper.getInstance().isScreenPinningActive()) {
806            return onLongPressBackHome(v);
807        }
808        if (shouldDisableNavbarGestures()) {
809            return false;
810        }
811        mNavigationBarView.onNavigationButtonLongPress(v);
812        mMetricsLogger.action(MetricsEvent.ACTION_ASSIST_LONG_PRESS);
813        mAssistManager.startAssist(new Bundle() /* args */);
814        mStatusBar.awakenDreams();
815
816        if (mNavigationBarView != null) {
817            mNavigationBarView.abortCurrentGesture();
818        }
819        return true;
820    }
821
822    // additional optimization when we have software system buttons - start loading the recent
823    // tasks on touch down
824    private boolean onRecentsTouch(View v, MotionEvent event) {
825        int action = event.getAction() & MotionEvent.ACTION_MASK;
826        if (action == MotionEvent.ACTION_DOWN) {
827            mCommandQueue.preloadRecentApps();
828        } else if (action == MotionEvent.ACTION_CANCEL) {
829            mCommandQueue.cancelPreloadRecentApps();
830        } else if (action == MotionEvent.ACTION_UP) {
831            if (!v.isPressed()) {
832                mCommandQueue.cancelPreloadRecentApps();
833            }
834        }
835        return false;
836    }
837
838    private void onRecentsClick(View v) {
839        if (LatencyTracker.isEnabled(getContext())) {
840            LatencyTracker.getInstance(getContext()).onActionStart(
841                    LatencyTracker.ACTION_TOGGLE_RECENTS);
842        }
843        mStatusBar.awakenDreams();
844        mCommandQueue.toggleRecentApps();
845    }
846
847    private boolean onLongPressBackHome(View v) {
848        mNavigationBarView.onNavigationButtonLongPress(v);
849        return onLongPressNavigationButtons(v, R.id.back, R.id.home);
850    }
851
852    private boolean onLongPressBackRecents(View v) {
853        mNavigationBarView.onNavigationButtonLongPress(v);
854        return onLongPressNavigationButtons(v, R.id.back, R.id.recent_apps);
855    }
856
857    /**
858     * This handles long-press of both back and recents/home. Back is the common button with
859     * combination of recents if it is visible or home if recents is invisible.
860     * They are handled together to capture them both being long-pressed
861     * at the same time to exit screen pinning (lock task).
862     *
863     * When accessibility mode is on, only a long-press from recents/home
864     * is required to exit.
865     *
866     * In all other circumstances we try to pass through long-press events
867     * for Back, so that apps can still use it.  Which can be from two things.
868     * 1) Not currently in screen pinning (lock task).
869     * 2) Back is long-pressed without recents/home.
870     */
871    private boolean onLongPressNavigationButtons(View v, @IdRes int btnId1, @IdRes int btnId2) {
872        try {
873            boolean sendBackLongPress = false;
874            IActivityManager activityManager = ActivityManagerNative.getDefault();
875            boolean touchExplorationEnabled = mAccessibilityManager.isTouchExplorationEnabled();
876            boolean inLockTaskMode = activityManager.isInLockTaskMode();
877            if (inLockTaskMode && !touchExplorationEnabled) {
878                long time = System.currentTimeMillis();
879
880                // If we recently long-pressed the other button then they were
881                // long-pressed 'together'
882                if ((time - mLastLockToAppLongPress) < LOCK_TO_APP_GESTURE_TOLERENCE) {
883                    activityManager.stopSystemLockTaskMode();
884                    // When exiting refresh disabled flags.
885                    mNavigationBarView.updateNavButtonIcons();
886                    return true;
887                } else if (v.getId() == btnId1) {
888                    ButtonDispatcher button = btnId2 == R.id.recent_apps
889                            ? mNavigationBarView.getRecentsButton()
890                            : mNavigationBarView.getHomeButton();
891                    if (!button.getCurrentView().isPressed()) {
892                        // If we aren't pressing recents/home right now then they presses
893                        // won't be together, so send the standard long-press action.
894                        sendBackLongPress = true;
895                    }
896                }
897                mLastLockToAppLongPress = time;
898            } else {
899                // If this is back still need to handle sending the long-press event.
900                if (v.getId() == btnId1) {
901                    sendBackLongPress = true;
902                } else if (touchExplorationEnabled && inLockTaskMode) {
903                    // When in accessibility mode a long press that is recents/home (not back)
904                    // should stop lock task.
905                    activityManager.stopSystemLockTaskMode();
906                    // When exiting refresh disabled flags.
907                    mNavigationBarView.updateNavButtonIcons();
908                    return true;
909                } else if (v.getId() == btnId2) {
910                    return btnId2 == R.id.recent_apps
911                            ? onLongPressRecents()
912                            : onHomeLongClick(mNavigationBarView.getHomeButton().getCurrentView());
913                }
914            }
915            if (sendBackLongPress) {
916                KeyButtonView keyButtonView = (KeyButtonView) v;
917                keyButtonView.sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS);
918                keyButtonView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
919                return true;
920            }
921        } catch (RemoteException e) {
922            Log.d(TAG, "Unable to reach activity manager", e);
923        }
924        return false;
925    }
926
927    private boolean onLongPressRecents() {
928        if (mRecents == null || !ActivityManager.supportsMultiWindow(getContext())
929                || !mDivider.getView().getSnapAlgorithm().isSplitScreenFeasible()
930                || Recents.getConfiguration().isLowRamDevice
931                // If we are connected to the overview service, then disable the recents button
932                || mOverviewProxyService.getProxy() != null) {
933            return false;
934        }
935
936        return mStatusBar.toggleSplitScreenMode(MetricsEvent.ACTION_WINDOW_DOCK_LONGPRESS,
937                MetricsEvent.ACTION_WINDOW_UNDOCK_LONGPRESS);
938    }
939
940    private void onAccessibilityClick(View v) {
941        mAccessibilityManager.notifyAccessibilityButtonClicked();
942    }
943
944    private boolean onAccessibilityLongClick(View v) {
945        Intent intent = new Intent(AccessibilityManager.ACTION_CHOOSE_ACCESSIBILITY_BUTTON);
946        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
947        v.getContext().startActivityAsUser(intent, UserHandle.CURRENT);
948        return true;
949    }
950
951    private void updateAccessibilityServicesState(AccessibilityManager accessibilityManager) {
952        int requestingServices = 0;
953        try {
954            if (Settings.Secure.getIntForUser(mContentResolver,
955                    Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED,
956                    UserHandle.USER_CURRENT) == 1) {
957                requestingServices++;
958            }
959        } catch (Settings.SettingNotFoundException e) {
960        }
961
962        boolean feedbackEnabled = false;
963        // AccessibilityManagerService resolves services for the current user since the local
964        // AccessibilityManager is created from a Context with the INTERACT_ACROSS_USERS permission
965        final List<AccessibilityServiceInfo> services =
966                accessibilityManager.getEnabledAccessibilityServiceList(
967                        AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
968        for (int i = services.size() - 1; i >= 0; --i) {
969            AccessibilityServiceInfo info = services.get(i);
970            if ((info.flags & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0) {
971                requestingServices++;
972            }
973
974            if (info.feedbackType != 0 && info.feedbackType !=
975                    AccessibilityServiceInfo.FEEDBACK_GENERIC) {
976                feedbackEnabled = true;
977            }
978        }
979
980        mAccessibilityFeedbackEnabled = feedbackEnabled;
981
982        final boolean showAccessibilityButton = requestingServices >= 1;
983        final boolean targetSelection = requestingServices >= 2;
984        mNavigationBarView.setAccessibilityButtonState(showAccessibilityButton, targetSelection);
985    }
986
987    private void onRotateSuggestionClick(View v) {
988        mMetricsLogger.action(MetricsEvent.ACTION_ROTATION_SUGGESTION_ACCEPTED);
989        incrementNumAcceptedRotationSuggestionsIfNeeded();
990        mRotationLockController.setRotationLockedAtAngle(true, mLastRotationSuggestion);
991    }
992
993    private boolean onRotateSuggestionHover(View v, MotionEvent event) {
994        final int action = event.getActionMasked();
995        mHoveringRotationSuggestion = (action == MotionEvent.ACTION_HOVER_ENTER)
996                || (action == MotionEvent.ACTION_HOVER_MOVE);
997        rescheduleRotationTimeout(true);
998        return false; // Must return false so a11y hover events are dispatched correctly.
999    }
1000
1001    // ----- Methods that StatusBar talks to (should be minimized) -----
1002
1003    public void setLightBarController(LightBarController lightBarController) {
1004        mLightBarController = lightBarController;
1005        mLightBarController.setNavigationBar(mNavigationBarView.getLightTransitionsController());
1006    }
1007
1008    public boolean isSemiTransparent() {
1009        return mNavigationBarMode == MODE_SEMI_TRANSPARENT;
1010    }
1011
1012    public void disableAnimationsDuringHide(long delay) {
1013        mNavigationBarView.setLayoutTransitionsEnabled(false);
1014        mNavigationBarView.postDelayed(() -> mNavigationBarView.setLayoutTransitionsEnabled(true),
1015                delay + StackStateAnimator.ANIMATION_DURATION_GO_TO_FULL_SHADE);
1016    }
1017
1018    public BarTransitions getBarTransitions() {
1019        return mNavigationBarView.getBarTransitions();
1020    }
1021
1022    public void checkNavBarModes() {
1023        mStatusBar.checkBarMode(mNavigationBarMode,
1024                mNavigationBarWindowState, mNavigationBarView.getBarTransitions());
1025    }
1026
1027    public void finishBarAnimations() {
1028        mNavigationBarView.getBarTransitions().finishAnimations();
1029    }
1030
1031    private final AccessibilityServicesStateChangeListener mAccessibilityListener =
1032            this::updateAccessibilityServicesState;
1033
1034    private class MagnificationContentObserver extends ContentObserver {
1035
1036        public MagnificationContentObserver(Handler handler) {
1037            super(handler);
1038        }
1039
1040        @Override
1041        public void onChange(boolean selfChange) {
1042            NavigationBarFragment.this.updateAccessibilityServicesState(mAccessibilityManager);
1043        }
1044    }
1045
1046    private final Stub mRotationWatcher = new Stub() {
1047        @Override
1048        public void onRotationChanged(final int rotation) throws RemoteException {
1049            // We need this to be scheduled as early as possible to beat the redrawing of
1050            // window in response to the orientation change.
1051            Handler h = getView().getHandler();
1052            Message msg = Message.obtain(h, () -> {
1053
1054                // If the screen rotation changes while locked, potentially update lock to flow with
1055                // new screen rotation and hide any showing suggestions.
1056                if (mRotationLockController.isRotationLocked()) {
1057                    if (shouldOverrideUserLockPrefs(rotation)) {
1058                        mRotationLockController.setRotationLockedAtAngle(true, rotation);
1059                    }
1060                    setRotateSuggestionButtonState(false, true);
1061                }
1062
1063                if (mNavigationBarView != null
1064                        && mNavigationBarView.needsReorient(rotation)) {
1065                    repositionNavigationBar();
1066                }
1067            });
1068            msg.setAsynchronous(true);
1069            h.sendMessageAtFrontOfQueue(msg);
1070        }
1071
1072        private boolean shouldOverrideUserLockPrefs(final int rotation) {
1073            // Only override user prefs when returning to the natural rotation (normally portrait).
1074            // Don't let apps that force landscape or 180 alter user lock.
1075            return rotation == NATURAL_ROTATION;
1076        }
1077    };
1078
1079    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1080        @Override
1081        public void onReceive(Context context, Intent intent) {
1082            String action = intent.getAction();
1083            if (Intent.ACTION_SCREEN_OFF.equals(action)
1084                    || Intent.ACTION_SCREEN_ON.equals(action)) {
1085                notifyNavigationBarScreenOn();
1086            }
1087        }
1088    };
1089
1090    class TaskStackListenerImpl extends SysUiTaskStackChangeListener {
1091        // Invalidate any rotation suggestion on task change or activity orientation change
1092        // Note: all callbacks happen on main thread
1093
1094        @Override
1095        public void onTaskStackChanged() {
1096            setRotateSuggestionButtonState(false);
1097        }
1098
1099        @Override
1100        public void onTaskRemoved(int taskId) {
1101            setRotateSuggestionButtonState(false);
1102        }
1103
1104        @Override
1105        public void onTaskMovedToFront(int taskId) {
1106            setRotateSuggestionButtonState(false);
1107        }
1108
1109        @Override
1110        public void onActivityRequestedOrientationChanged(int taskId, int requestedOrientation) {
1111            // Only hide the icon if the top task changes its requestedOrientation
1112            // Launcher can alter its requestedOrientation while it's not on top, don't hide on this
1113            Optional.ofNullable(ActivityManagerWrapper.getInstance())
1114                    .map(ActivityManagerWrapper::getRunningTask)
1115                    .ifPresent(a -> {
1116                        if (a.id == taskId) setRotateSuggestionButtonState(false);
1117                    });
1118        }
1119    }
1120
1121    private class ViewRippler {
1122        private static final int RIPPLE_OFFSET_MS = 50;
1123        private static final int RIPPLE_INTERVAL_MS = 2000;
1124        private View mRoot;
1125
1126        public void start(View root) {
1127            stop(); // Stop any pending ripple animations
1128
1129            mRoot = root;
1130
1131            // Schedule pending ripples, offset the 1st to avoid problems with visibility change
1132            mRoot.postOnAnimationDelayed(mRipple, RIPPLE_OFFSET_MS);
1133            mRoot.postOnAnimationDelayed(mRipple, RIPPLE_INTERVAL_MS);
1134            mRoot.postOnAnimationDelayed(mRipple, 2*RIPPLE_INTERVAL_MS);
1135        }
1136
1137        public void stop() {
1138            if (mRoot != null) mRoot.removeCallbacks(mRipple);
1139        }
1140
1141        private final Runnable mRipple = new Runnable() {
1142            @Override
1143            public void run() { // Cause the ripple to fire via false presses
1144                if (!mRoot.isAttachedToWindow()) return;
1145                mRoot.setPressed(true);
1146                mRoot.setPressed(false);
1147            }
1148        };
1149    }
1150
1151    public static View create(Context context, FragmentListener listener) {
1152        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
1153                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
1154                WindowManager.LayoutParams.TYPE_NAVIGATION_BAR,
1155                WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
1156                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1157                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
1158                        | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1159                        | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
1160                        | WindowManager.LayoutParams.FLAG_SLIPPERY,
1161                PixelFormat.TRANSLUCENT);
1162        lp.token = new Binder();
1163        lp.setTitle("NavigationBar");
1164        lp.accessibilityTitle = context.getString(R.string.nav_bar);
1165        lp.windowAnimations = 0;
1166
1167        View navigationBarView = LayoutInflater.from(context).inflate(
1168                R.layout.navigation_bar_window, null);
1169
1170        if (DEBUG) Log.v(TAG, "addNavigationBar: about to add " + navigationBarView);
1171        if (navigationBarView == null) return null;
1172
1173        context.getSystemService(WindowManager.class).addView(navigationBarView, lp);
1174        FragmentHostManager fragmentHost = FragmentHostManager.get(navigationBarView);
1175        NavigationBarFragment fragment = new NavigationBarFragment();
1176        fragmentHost.getFragmentManager().beginTransaction()
1177                .replace(R.id.navigation_bar_frame, fragment, TAG)
1178                .commit();
1179        fragmentHost.addTagListener(TAG, listener);
1180        return navigationBarView;
1181    }
1182}
1183