blob: b493d7a0e7edb55828b0c2ce6c700bfafbb65a8c [file] [log] [blame]
Jason Monk49fa0162017-01-11 09:21:56 -05001/*
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
Mike Digmanab650252018-03-06 11:01:41 -080022import static com.android.internal.view.RotationPolicy.NATURAL_ROTATION;
23
Matthew Ng8f25fb962018-01-16 17:17:24 -080024import static com.android.systemui.shared.system.NavigationBarCompat.InteractionType;
Jason Monk49fa0162017-01-11 09:21:56 -050025import static com.android.systemui.statusbar.phone.BarTransitions.MODE_SEMI_TRANSPARENT;
Jason Monk2a6ea9c2017-01-26 11:14:51 -050026import static com.android.systemui.statusbar.phone.StatusBar.DEBUG_WINDOW_STATE;
27import static com.android.systemui.statusbar.phone.StatusBar.dumpBarTransitions;
Matthew Ng9c3bce52018-02-01 22:00:31 +000028import static com.android.systemui.OverviewProxyService.OverviewProxyListener;
Jason Monk49fa0162017-01-11 09:21:56 -050029
Casey Burkhardt048c2bc2016-12-08 16:09:20 -080030import android.accessibilityservice.AccessibilityServiceInfo;
Mike Digman7d092772018-01-11 12:10:32 -080031import android.animation.Animator;
32import android.animation.AnimatorListenerAdapter;
Mike Digman7d092772018-01-11 12:10:32 -080033import android.animation.ObjectAnimator;
Matthew Ng9c3bce52018-02-01 22:00:31 +000034import android.annotation.IdRes;
Jason Monk49fa0162017-01-11 09:21:56 -050035import 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;
Casey Burkhardtb9dcd662017-03-20 15:10:16 -070042import android.content.ContentResolver;
Jason Monk49fa0162017-01-11 09:21:56 -050043import android.content.Context;
44import android.content.Intent;
45import android.content.IntentFilter;
46import android.content.res.Configuration;
Casey Burkhardt74922c62017-02-13 12:43:16 -080047import android.database.ContentObserver;
Jason Monk49fa0162017-01-11 09:21:56 -050048import android.graphics.PixelFormat;
49import android.graphics.Rect;
Mike Digman7d092772018-01-11 12:10:32 -080050import android.graphics.drawable.AnimatedVectorDrawable;
Jason Monk49fa0162017-01-11 09:21:56 -050051import android.inputmethodservice.InputMethodService;
52import android.os.Binder;
53import android.os.Bundle;
54import android.os.Handler;
55import android.os.IBinder;
56import android.os.Message;
Jason Monk49fa0162017-01-11 09:21:56 -050057import android.os.RemoteException;
58import android.os.UserHandle;
Casey Burkhardt74922c62017-02-13 12:43:16 -080059import android.provider.Settings;
Jason Monk865246d2017-01-19 08:27:01 -050060import android.support.annotation.VisibleForTesting;
Jason Monk49fa0162017-01-11 09:21:56 -050061import 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;
Mike Digman85ff7fa2018-01-23 14:59:52 -080068import android.view.Surface;
Jason Monk49fa0162017-01-11 09:21:56 -050069import 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;
Jason Monk91e587e2017-04-13 13:41:23 -040076import android.view.accessibility.AccessibilityManager.AccessibilityServicesStateChangeListener;
Jason Monk49fa0162017-01-11 09:21:56 -050077
78import com.android.internal.logging.MetricsLogger;
79import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monkea03be12017-12-04 11:08:41 -050080import com.android.internal.util.LatencyTracker;
Jason Monk9c7844c2017-01-18 15:21:53 -050081import com.android.systemui.Dependency;
Mike Digman7d092772018-01-11 12:10:32 -080082import com.android.systemui.Interpolators;
Mike Digman1e28a5a2018-02-14 10:49:19 -080083import com.android.systemui.OverviewProxyService;
Jason Monk49fa0162017-01-11 09:21:56 -050084import com.android.systemui.R;
Jason Monk9c7844c2017-01-18 15:21:53 -050085import com.android.systemui.SysUiServiceProvider;
Jason Monk49fa0162017-01-11 09:21:56 -050086import com.android.systemui.assist.AssistManager;
87import com.android.systemui.fragments.FragmentHostManager;
88import com.android.systemui.fragments.FragmentHostManager.FragmentListener;
89import com.android.systemui.recents.Recents;
Mike Digman7d092772018-01-11 12:10:32 -080090import com.android.systemui.recents.misc.SysUiTaskStackChangeListener;
91import com.android.systemui.shared.system.ActivityManagerWrapper;
Matthew Ng64543e62018-02-28 17:35:10 -080092import com.android.systemui.shared.system.WindowManagerWrapper;
Jason Monk49fa0162017-01-11 09:21:56 -050093import com.android.systemui.stackdivider.Divider;
94import com.android.systemui.statusbar.CommandQueue;
95import com.android.systemui.statusbar.CommandQueue.Callbacks;
Jason Monk91e587e2017-04-13 13:41:23 -040096import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper;
Mike Digman7d092772018-01-11 12:10:32 -080097import com.android.systemui.statusbar.policy.KeyButtonDrawable;
Jason Monk49fa0162017-01-11 09:21:56 -050098import com.android.systemui.statusbar.policy.KeyButtonView;
Mike Digman7d092772018-01-11 12:10:32 -080099import com.android.systemui.statusbar.policy.RotationLockController;
Jason Monk49fa0162017-01-11 09:21:56 -0500100import com.android.systemui.statusbar.stack.StackStateAnimator;
101
102import java.io.FileDescriptor;
103import java.io.PrintWriter;
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800104import java.util.List;
Jason Monk49fa0162017-01-11 09:21:56 -0500105import java.util.Locale;
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
Jason Monkd4afe152017-05-01 15:37:43 -0400113 public static final String TAG = "NavigationBar";
Jason Monk49fa0162017-01-11 09:21:56 -0500114 private static final boolean DEBUG = false;
Mike Digman3e33da62018-03-14 16:25:11 -0700115 private static final boolean DEBUG_ROTATION = true;
Jason Monk49fa0162017-01-11 09:21:56 -0500116 private static final String EXTRA_DISABLE_STATE = "disabled_state";
Mike Digmandd2f49e2018-03-16 10:54:22 -0700117 private static final String EXTRA_DISABLE2_STATE = "disabled2_state";
Jason Monk49fa0162017-01-11 09:21:56 -0500118
Mike Digman1e28a5a2018-02-14 10:49:19 -0800119 private final static int BUTTON_FADE_IN_OUT_DURATION_MS = 100;
Mike Digman5aeca792018-03-05 11:14:39 -0800120 private final static int NAVBAR_HIDDEN_PENDING_ICON_TIMEOUT_MS = 20000;
Mike Digman1e28a5a2018-02-14 10:49:19 -0800121
Mike Digman50752642018-02-15 13:36:09 -0800122 private static final int NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION = 3;
123
Jason Monk49fa0162017-01-11 09:21:56 -0500124 /** 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;
Mike Digman90402952018-01-22 16:05:51 -0800134 private boolean mAccessibilityFeedbackEnabled;
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800135 private AccessibilityManager mAccessibilityManager;
Casey Burkhardt74922c62017-02-13 12:43:16 -0800136 private MagnificationContentObserver mMagnificationObserver;
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700137 private ContentResolver mContentResolver;
Mike Digmanc94759d2018-01-23 11:01:21 -0800138 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500139
140 private int mDisabledFlags1;
Mike Digmandd2f49e2018-03-16 10:54:22 -0700141 private int mDisabledFlags2;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500142 private StatusBar mStatusBar;
Jason Monk49fa0162017-01-11 09:21:56 -0500143 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;
Jason Monk49fa0162017-01-11 09:21:56 -0500154
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800155 private OverviewProxyService mOverviewProxyService;
156
Jason Monk49fa0162017-01-11 09:21:56 -0500157 public boolean mHomeBlockedThisTouch;
158
Mike Digman7d092772018-01-11 12:10:32 -0800159 private int mLastRotationSuggestion;
Mike Digman5aeca792018-03-05 11:14:39 -0800160 private boolean mPendingRotationSuggestion;
Mike Digman90402952018-01-22 16:05:51 -0800161 private boolean mHoveringRotationSuggestion;
Mike Digman7d092772018-01-11 12:10:32 -0800162 private RotationLockController mRotationLockController;
163 private TaskStackListenerImpl mTaskStackListener;
164
Mike Digman1e28a5a2018-02-14 10:49:19 -0800165 private final Runnable mRemoveRotationProposal = () -> setRotateSuggestionButtonState(false);
Mike Digman5aeca792018-03-05 11:14:39 -0800166 private final Runnable mCancelPendingRotationProposal =
167 () -> mPendingRotationSuggestion = false;
Mike Digman7d092772018-01-11 12:10:32 -0800168 private Animator mRotateHideAnimator;
Mike Digman50752642018-02-15 13:36:09 -0800169 private ViewRippler mViewRippler = new ViewRippler();
Mike Digman7d092772018-01-11 12:10:32 -0800170
Matthew Ng9c3bce52018-02-01 22:00:31 +0000171 private final OverviewProxyListener mOverviewProxyListener = new OverviewProxyListener() {
172 @Override
173 public void onConnectionChanged(boolean isConnected) {
174 mNavigationBarView.onOverviewProxyConnectionChanged(isConnected);
175 updateScreenPinningGestures();
Matthew Ng64543e62018-02-28 17:35:10 -0800176 WindowManagerWrapper.getInstance()
177 .setNavBarVirtualKeyHapticFeedbackEnabled(!isConnected);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000178 }
179
180 @Override
Matthew Ng2ea93b72018-03-14 19:43:18 +0000181 public void onQuickStepStarted() {
182 mNavigationBarView.onQuickStepStarted();
Mike Digman85a9bea2018-02-23 15:08:53 -0800183
184 // Use navbar dragging as a signal to hide the rotate button
185 setRotateSuggestionButtonState(false);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000186 }
Matthew Ng8f25fb962018-01-16 17:17:24 -0800187
188 @Override
189 public void onInteractionFlagsChanged(@InteractionType int flags) {
190 mNavigationBarView.updateStates();
191 }
Matthew Ng9c3bce52018-02-01 22:00:31 +0000192 };
Mike Digman7d092772018-01-11 12:10:32 -0800193
Jason Monk49fa0162017-01-11 09:21:56 -0500194 // ----- Fragment Lifecycle Callbacks -----
195
196 @Override
197 public void onCreate(@Nullable Bundle savedInstanceState) {
198 super.onCreate(savedInstanceState);
Jason Monk9c7844c2017-01-18 15:21:53 -0500199 mCommandQueue = SysUiServiceProvider.getComponent(getContext(), CommandQueue.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500200 mCommandQueue.addCallbacks(this);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500201 mStatusBar = SysUiServiceProvider.getComponent(getContext(), StatusBar.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500202 mRecents = SysUiServiceProvider.getComponent(getContext(), Recents.class);
203 mDivider = SysUiServiceProvider.getComponent(getContext(), Divider.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500204 mWindowManager = getContext().getSystemService(WindowManager.class);
205 mAccessibilityManager = getContext().getSystemService(AccessibilityManager.class);
Jason Monk91e587e2017-04-13 13:41:23 -0400206 Dependency.get(AccessibilityManagerWrapper.class).addCallback(
207 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700208 mContentResolver = getContext().getContentResolver();
Casey Burkhardt74922c62017-02-13 12:43:16 -0800209 mMagnificationObserver = new MagnificationContentObserver(
210 getContext().getMainThreadHandler());
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700211 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(
Casey Burkhardt74922c62017-02-13 12:43:16 -0800212 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED), false,
Casey Burkhardt5d614402017-04-06 13:46:50 -0700213 mMagnificationObserver, UserHandle.USER_ALL);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800214
Jason Monk49fa0162017-01-11 09:21:56 -0500215 if (savedInstanceState != null) {
216 mDisabledFlags1 = savedInstanceState.getInt(EXTRA_DISABLE_STATE, 0);
Mike Digmandd2f49e2018-03-16 10:54:22 -0700217 mDisabledFlags2 = savedInstanceState.getInt(EXTRA_DISABLE2_STATE, 0);
Jason Monk49fa0162017-01-11 09:21:56 -0500218 }
Jason Monk9c7844c2017-01-18 15:21:53 -0500219 mAssistManager = Dependency.get(AssistManager.class);
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800220 mOverviewProxyService = Dependency.get(OverviewProxyService.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500221
222 try {
223 WindowManagerGlobal.getWindowManagerService()
Andrii Kulian35fa3c22017-03-11 09:37:28 -0800224 .watchRotation(mRotationWatcher, getContext().getDisplay().getDisplayId());
Jason Monk49fa0162017-01-11 09:21:56 -0500225 } catch (RemoteException e) {
226 throw e.rethrowFromSystemServer();
227 }
Mike Digman7d092772018-01-11 12:10:32 -0800228
229 mRotationLockController = Dependency.get(RotationLockController.class);
230
Mike Digmanab650252018-03-06 11:01:41 -0800231 // Reset user rotation pref to match that of the WindowManager if starting in locked mode
232 // This will automatically happen when switching from auto-rotate to locked mode
233 if (mRotationLockController.isRotationLocked()) {
234 final int winRotation = mWindowManager.getDefaultDisplay().getRotation();
235 mRotationLockController.setRotationLockedAtAngle(true, winRotation);
236 }
237
Mike Digman7d092772018-01-11 12:10:32 -0800238 // Register the task stack listener
239 mTaskStackListener = new TaskStackListenerImpl();
240 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500241 }
242
243 @Override
244 public void onDestroy() {
245 super.onDestroy();
246 mCommandQueue.removeCallbacks(this);
Jason Monk91e587e2017-04-13 13:41:23 -0400247 Dependency.get(AccessibilityManagerWrapper.class).removeCallback(
248 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700249 mContentResolver.unregisterContentObserver(mMagnificationObserver);
Jason Monk49fa0162017-01-11 09:21:56 -0500250 try {
251 WindowManagerGlobal.getWindowManagerService()
252 .removeRotationWatcher(mRotationWatcher);
253 } catch (RemoteException e) {
254 throw e.rethrowFromSystemServer();
255 }
Mike Digman7d092772018-01-11 12:10:32 -0800256
257 // Unregister the task stack listener
258 ActivityManagerWrapper.getInstance().unregisterTaskStackListener(mTaskStackListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500259 }
260
261 @Override
262 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
263 Bundle savedInstanceState) {
264 return inflater.inflate(R.layout.navigation_bar, container, false);
265 }
266
267 @Override
268 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
269 super.onViewCreated(view, savedInstanceState);
270 mNavigationBarView = (NavigationBarView) view;
271
272 mNavigationBarView.setDisabledFlags(mDisabledFlags1);
Matthew Ng78f88d12018-01-23 12:39:55 -0800273 mNavigationBarView.setComponents(mRecents, mDivider, mStatusBar.getPanel());
Jason Monk49fa0162017-01-11 09:21:56 -0500274 mNavigationBarView.setOnVerticalChangedListener(this::onVerticalChanged);
275 mNavigationBarView.setOnTouchListener(this::onNavigationTouch);
276 if (savedInstanceState != null) {
277 mNavigationBarView.getLightTransitionsController().restoreState(savedInstanceState);
278 }
279
280 prepareNavigationBarView();
281 checkNavBarModes();
282
Mike Digmandd2f49e2018-03-16 10:54:22 -0700283 setDisabled2Flags(mDisabledFlags2);
284
Jason Monk49fa0162017-01-11 09:21:56 -0500285 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
286 filter.addAction(Intent.ACTION_SCREEN_ON);
287 getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100288 notifyNavigationBarScreenOn();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000289 mOverviewProxyService.addCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500290 }
291
292 @Override
293 public void onDestroyView() {
294 super.onDestroyView();
Jason Monkaa573e92017-01-27 17:00:29 -0500295 mNavigationBarView.getLightTransitionsController().destroy(getContext());
Matthew Ng9c3bce52018-02-01 22:00:31 +0000296 mOverviewProxyService.removeCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500297 getContext().unregisterReceiver(mBroadcastReceiver);
298 }
299
300 @Override
301 public void onSaveInstanceState(Bundle outState) {
302 super.onSaveInstanceState(outState);
303 outState.putInt(EXTRA_DISABLE_STATE, mDisabledFlags1);
Mike Digmandd2f49e2018-03-16 10:54:22 -0700304 outState.putInt(EXTRA_DISABLE2_STATE, mDisabledFlags2);
Jason Monk49fa0162017-01-11 09:21:56 -0500305 if (mNavigationBarView != null) {
306 mNavigationBarView.getLightTransitionsController().saveState(outState);
307 }
308 }
309
310 @Override
311 public void onConfigurationChanged(Configuration newConfig) {
312 super.onConfigurationChanged(newConfig);
313 final Locale locale = getContext().getResources().getConfiguration().locale;
314 final int ld = TextUtils.getLayoutDirectionFromLocale(locale);
315 if (!locale.equals(mLocale) || ld != mLayoutDirection) {
316 if (DEBUG) {
317 Log.v(TAG, String.format(
318 "config changed locale/LD: %s (%d) -> %s (%d)", mLocale, mLayoutDirection,
319 locale, ld));
320 }
321 mLocale = locale;
322 mLayoutDirection = ld;
323 refreshLayout(ld);
324 }
325 repositionNavigationBar();
326 }
327
328 @Override
329 public void dump(String prefix, FileDescriptor fd, PrintWriter pw, String[] args) {
330 if (mNavigationBarView != null) {
331 pw.print(" mNavigationBarWindowState=");
332 pw.println(windowStateToString(mNavigationBarWindowState));
333 pw.print(" mNavigationBarMode=");
334 pw.println(BarTransitions.modeToString(mNavigationBarMode));
335 dumpBarTransitions(pw, "mNavigationBarView", mNavigationBarView.getBarTransitions());
336 }
337
338 pw.print(" mNavigationBarView=");
339 if (mNavigationBarView == null) {
340 pw.println("null");
341 } else {
342 mNavigationBarView.dump(fd, pw, args);
343 }
344 }
345
346 // ----- CommandQueue Callbacks -----
347
348 @Override
349 public void setImeWindowStatus(IBinder token, int vis, int backDisposition,
350 boolean showImeSwitcher) {
351 boolean imeShown = (vis & InputMethodService.IME_VISIBLE) != 0;
352 int hints = mNavigationIconHints;
Yohei Yukawa386f50e2018-03-14 13:03:42 -0700353 switch (backDisposition) {
354 case InputMethodService.BACK_DISPOSITION_DEFAULT:
355 case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
356 case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
357 if (imeShown) {
358 hints |= NAVIGATION_HINT_BACK_ALT;
359 } else {
360 hints &= ~NAVIGATION_HINT_BACK_ALT;
361 }
362 break;
363 case InputMethodService.BACK_DISPOSITION_ADJUST_NOTHING:
364 hints &= ~NAVIGATION_HINT_BACK_ALT;
365 break;
Jason Monk49fa0162017-01-11 09:21:56 -0500366 }
367 if (showImeSwitcher) {
368 hints |= NAVIGATION_HINT_IME_SHOWN;
369 } else {
370 hints &= ~NAVIGATION_HINT_IME_SHOWN;
371 }
372 if (hints == mNavigationIconHints) return;
373
374 mNavigationIconHints = hints;
375
376 if (mNavigationBarView != null) {
377 mNavigationBarView.setNavigationIconHints(hints);
378 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500379 mStatusBar.checkBarModes();
Jason Monk49fa0162017-01-11 09:21:56 -0500380 }
381
382 @Override
383 public void topAppWindowChanged(boolean showMenu) {
384 if (mNavigationBarView != null) {
385 mNavigationBarView.setMenuVisibility(showMenu);
386 }
387 }
388
389 @Override
390 public void setWindowState(int window, int state) {
391 if (mNavigationBarView != null
392 && window == StatusBarManager.WINDOW_NAVIGATION_BAR
393 && mNavigationBarWindowState != state) {
394 mNavigationBarWindowState = state;
395 if (DEBUG_WINDOW_STATE) Log.d(TAG, "Navigation bar " + windowStateToString(state));
Mike Digman5aeca792018-03-05 11:14:39 -0800396
397 // If the navbar is visible, show the rotate button if there's a pending suggestion
398 if (state == WINDOW_STATE_SHOWING && mPendingRotationSuggestion) {
399 showAndLogRotationSuggestion();
400 }
Jason Monk49fa0162017-01-11 09:21:56 -0500401 }
402 }
403
Mike Digman7d092772018-01-11 12:10:32 -0800404 @Override
Mike Digmane0777312018-01-19 12:41:51 -0800405 public void onRotationProposal(final int rotation, boolean isValid) {
Mike Digman3e33da62018-03-14 16:25:11 -0700406 final int winRotation = mWindowManager.getDefaultDisplay().getRotation();
Mike Digmandd2f49e2018-03-16 10:54:22 -0700407 final boolean rotateSuggestionsDisabled = hasDisable2RotateSuggestionFlag(mDisabledFlags2);
Mike Digman3e33da62018-03-14 16:25:11 -0700408 if (DEBUG_ROTATION) {
409 Log.v(TAG, "onRotationProposal proposedRotation=" + Surface.rotationToString(rotation)
410 + ", winRotation=" + Surface.rotationToString(winRotation)
411 + ", isValid=" + isValid + ", mNavBarWindowState="
412 + StatusBarManager.windowStateToString(mNavigationBarWindowState)
Mike Digmandd2f49e2018-03-16 10:54:22 -0700413 + ", rotateSuggestionsDisabled=" + rotateSuggestionsDisabled
Mike Digman3e33da62018-03-14 16:25:11 -0700414 + ", isRotateButtonVisible=" + (mNavigationBarView == null ? "null" :
415 mNavigationBarView.isRotateButtonVisible()));
416 }
417
Mike Digmandd2f49e2018-03-16 10:54:22 -0700418 // Respect the disabled flag, no need for action as flag change callback will handle hiding
419 if (rotateSuggestionsDisabled) return;
420
Mike Digmane0777312018-01-19 12:41:51 -0800421 // This method will be called on rotation suggestion changes even if the proposed rotation
422 // is not valid for the top app. Use invalid rotation choices as a signal to remove the
423 // rotate button if shown.
Mike Digmane0777312018-01-19 12:41:51 -0800424 if (!isValid) {
Mike Digman1e28a5a2018-02-14 10:49:19 -0800425 setRotateSuggestionButtonState(false);
Mike Digmane0777312018-01-19 12:41:51 -0800426 return;
427 }
428
Mike Digman5aeca792018-03-05 11:14:39 -0800429 // If window rotation matches suggested rotation, remove any current suggestions
Mike Digman1e28a5a2018-02-14 10:49:19 -0800430 if (rotation == winRotation) {
Mike Digman5aeca792018-03-05 11:14:39 -0800431 getView().removeCallbacks(mRemoveRotationProposal);
Mike Digman1e28a5a2018-02-14 10:49:19 -0800432 setRotateSuggestionButtonState(false);
Mike Digman5aeca792018-03-05 11:14:39 -0800433 return;
Mike Digman7d092772018-01-11 12:10:32 -0800434 }
Mike Digman5aeca792018-03-05 11:14:39 -0800435
436 // Prepare to show the navbar icon by updating the icon style to change anim params
437 mLastRotationSuggestion = rotation; // Remember rotation for click
438 if (mNavigationBarView != null) {
439 final boolean rotationCCW = isRotationAnimationCCW(winRotation, rotation);
440 int style;
441 if (winRotation == Surface.ROTATION_0 || winRotation == Surface.ROTATION_180) {
442 style = rotationCCW ? R.style.RotateButtonCCWStart90 :
443 R.style.RotateButtonCWStart90;
444 } else { // 90 or 270
445 style = rotationCCW ? R.style.RotateButtonCCWStart0 :
446 R.style.RotateButtonCWStart0;
447 }
448 mNavigationBarView.updateRotateSuggestionButtonStyle(style, true);
449 }
450
451 if (mNavigationBarWindowState != WINDOW_STATE_SHOWING) {
452 // If the navbar isn't shown, flag the rotate icon to be shown should the navbar become
453 // visible given some time limit.
454 mPendingRotationSuggestion = true;
455 getView().removeCallbacks(mCancelPendingRotationProposal);
456 getView().postDelayed(mCancelPendingRotationProposal,
457 NAVBAR_HIDDEN_PENDING_ICON_TIMEOUT_MS);
458
459 } else { // The navbar is visible so show the icon right away
460 showAndLogRotationSuggestion();
461 }
462 }
463
Mike Digmandd2f49e2018-03-16 10:54:22 -0700464 private void onRotationSuggestionsDisabled() {
465 // Immediately hide the rotate button and clear any planned removal
466 setRotateSuggestionButtonState(false, true);
467 getView().removeCallbacks(mRemoveRotationProposal);
468 }
469
Mike Digman5aeca792018-03-05 11:14:39 -0800470 private void showAndLogRotationSuggestion() {
471 setRotateSuggestionButtonState(true);
472 rescheduleRotationTimeout(false);
473 mMetricsLogger.visible(MetricsEvent.ROTATION_SUGGESTION_SHOWN);
Mike Digman7d092772018-01-11 12:10:32 -0800474 }
475
Mike Digman1e28a5a2018-02-14 10:49:19 -0800476 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
Mike Digmana48cf192018-02-12 17:52:48 -0800491 }
492
Mike Digman1e28a5a2018-02-14 10:49:19 -0800493 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
Mike Digman5aeca792018-03-05 11:14:39 -0800522 // Clear any pending suggestion flag as it has either been nullified or is being shown
523 mPendingRotationSuggestion = false;
524 getView().removeCallbacks(mCancelPendingRotationProposal);
525
526 // Handle the visibility change and animation
Mike Digman1e28a5a2018-02-14 10:49:19 -0800527 if (visible) { // Appear and change (cannot force)
Mike Digman85a9bea2018-02-23 15:08:53 -0800528 // Stop and clear any currently running hide animations
Mike Digman1e28a5a2018-02-14 10:49:19 -0800529 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
Mike Digman85a9bea2018-02-23 15:08:53 -0800530 mRotateHideAnimator.cancel();
Mike Digman1e28a5a2018-02-14 10:49:19 -0800531 }
Mike Digman85a9bea2018-02-23 15:08:53 -0800532 mRotateHideAnimator = null;
Mike Digman1e28a5a2018-02-14 10:49:19 -0800533
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
Mike Digman50752642018-02-15 13:36:09 -0800543 if (!isRotateSuggestionIntroduced()) mViewRippler.start(view);
544
Mike Digman1e28a5a2018-02-14 10:49:19 -0800545 // Set visibility, may fail if a11y service is active.
546 // If invisible, call will stop animation.
547 mNavigationBarView.setRotateButtonVisibility(true);
548
549 } else { // Hide
550
Mike Digman50752642018-02-15 13:36:09 -0800551 mViewRippler.stop(); // Prevent any pending ripples, force hide or not
552
Mike Digman1e28a5a2018-02-14 10:49:19 -0800553 if (force) {
554 // If a hide animator is running stop it and make invisible
555 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
556 mRotateHideAnimator.pause();
557 }
558 mNavigationBarView.setRotateButtonVisibility(false);
559 return;
560 }
561
562 // Don't start any new hide animations if one is running
563 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
564
565 ObjectAnimator fadeOut = ObjectAnimator.ofFloat(view, "alpha",
566 0f);
567 fadeOut.setDuration(BUTTON_FADE_IN_OUT_DURATION_MS);
568 fadeOut.setInterpolator(Interpolators.LINEAR);
569 fadeOut.addListener(new AnimatorListenerAdapter() {
570 @Override
571 public void onAnimationEnd(Animator animation) {
572 mNavigationBarView.setRotateButtonVisibility(false);
573 }
574 });
575
576 mRotateHideAnimator = fadeOut;
577 fadeOut.start();
Mike Digmana48cf192018-02-12 17:52:48 -0800578 }
579 }
580
Mike Digman90402952018-01-22 16:05:51 -0800581 private void rescheduleRotationTimeout(final boolean reasonHover) {
582 // May be called due to a new rotation proposal or a change in hover state
583 if (reasonHover) {
584 // Don't reschedule if a hide animator is running
Mike Digman1e28a5a2018-02-14 10:49:19 -0800585 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
Mike Digman90402952018-01-22 16:05:51 -0800586 // Don't reschedule if not visible
Mike Digman1e28a5a2018-02-14 10:49:19 -0800587 if (!mNavigationBarView.isRotateButtonVisible()) return;
Mike Digman90402952018-01-22 16:05:51 -0800588 }
589
Mike Digman5aeca792018-03-05 11:14:39 -0800590 getView().removeCallbacks(mRemoveRotationProposal); // Stop any pending removal
591 getView().postDelayed(mRemoveRotationProposal,
Mike Digman90402952018-01-22 16:05:51 -0800592 computeRotationProposalTimeout()); // Schedule timeout
593 }
594
595 private int computeRotationProposalTimeout() {
596 if (mAccessibilityFeedbackEnabled) return 20000;
597 if (mHoveringRotationSuggestion) return 16000;
598 return 6000;
599 }
600
Mike Digman50752642018-02-15 13:36:09 -0800601 private boolean isRotateSuggestionIntroduced() {
602 ContentResolver cr = getContext().getContentResolver();
603 return Settings.Secure.getInt(cr, Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED, 0)
604 >= NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION;
605 }
606
607 private void incrementNumAcceptedRotationSuggestionsIfNeeded() {
608 // Get the number of accepted suggestions
609 ContentResolver cr = getContext().getContentResolver();
610 final int numSuggestions = Settings.Secure.getInt(cr,
611 Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED, 0);
612
613 // Increment the number of accepted suggestions only if it would change intro mode
614 if (numSuggestions < NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION) {
615 Settings.Secure.putInt(cr, Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED,
616 numSuggestions + 1);
617 }
618 }
619
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500620 // Injected from StatusBar at creation.
Jason Monk49fa0162017-01-11 09:21:56 -0500621 public void setCurrentSysuiVisibility(int systemUiVisibility) {
622 mSystemUiVisibility = systemUiVisibility;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500623 mNavigationBarMode = mStatusBar.computeBarMode(0, mSystemUiVisibility,
Jason Monk49fa0162017-01-11 09:21:56 -0500624 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
625 View.NAVIGATION_BAR_TRANSPARENT);
626 checkNavBarModes();
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500627 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500628 mLightBarController.onNavigationVisibilityChanged(mSystemUiVisibility, 0 /* mask */,
629 true /* nbModeChanged */, mNavigationBarMode);
630 }
631
632 @Override
633 public void setSystemUiVisibility(int vis, int fullscreenStackVis, int dockedStackVis,
634 int mask, Rect fullscreenStackBounds, Rect dockedStackBounds) {
635 final int oldVal = mSystemUiVisibility;
636 final int newVal = (oldVal & ~mask) | (vis & mask);
637 final int diff = newVal ^ oldVal;
638 boolean nbModeChanged = false;
639 if (diff != 0) {
640 mSystemUiVisibility = newVal;
641
642 // update navigation bar mode
643 final int nbMode = getView() == null
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500644 ? -1 : mStatusBar.computeBarMode(oldVal, newVal,
Jason Monk49fa0162017-01-11 09:21:56 -0500645 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
646 View.NAVIGATION_BAR_TRANSPARENT);
647 nbModeChanged = nbMode != -1;
648 if (nbModeChanged) {
649 if (mNavigationBarMode != nbMode) {
650 mNavigationBarMode = nbMode;
651 checkNavBarModes();
652 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500653 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500654 }
655 }
656
657 mLightBarController.onNavigationVisibilityChanged(vis, mask, nbModeChanged,
658 mNavigationBarMode);
659 }
660
661 @Override
662 public void disable(int state1, int state2, boolean animate) {
Mike Digmandd2f49e2018-03-16 10:54:22 -0700663 // Navigation bar flags are in both state1 and state2.
664 final int masked = state1 & (StatusBarManager.DISABLE_HOME
Jason Monk49fa0162017-01-11 09:21:56 -0500665 | StatusBarManager.DISABLE_RECENT
666 | StatusBarManager.DISABLE_BACK
667 | StatusBarManager.DISABLE_SEARCH);
668 if (masked != mDisabledFlags1) {
669 mDisabledFlags1 = masked;
670 if (mNavigationBarView != null) mNavigationBarView.setDisabledFlags(state1);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000671 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500672 }
Mike Digmandd2f49e2018-03-16 10:54:22 -0700673
674 final int masked2 = state2 & (StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS);
675 if (masked2 != mDisabledFlags2) {
676 mDisabledFlags2 = masked2;
677 setDisabled2Flags(masked2);
678 }
679 }
680
681 private void setDisabled2Flags(int state2) {
682 // Method only called on change of disable2 flags
683 final boolean rotateSuggestionsDisabled = hasDisable2RotateSuggestionFlag(state2);
684 if (rotateSuggestionsDisabled) onRotationSuggestionsDisabled();
685 }
686
687 private boolean hasDisable2RotateSuggestionFlag(int disable2Flags) {
688 return (disable2Flags & StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS) != 0;
Jason Monk49fa0162017-01-11 09:21:56 -0500689 }
690
691 // ----- Internal stuffz -----
692
693 private void refreshLayout(int layoutDirection) {
694 if (mNavigationBarView != null) {
695 mNavigationBarView.setLayoutDirection(layoutDirection);
696 }
697 }
698
699 private boolean shouldDisableNavbarGestures() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500700 return !mStatusBar.isDeviceProvisioned()
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800701 || (mDisabledFlags1 & StatusBarManager.DISABLE_SEARCH) != 0;
Jason Monk49fa0162017-01-11 09:21:56 -0500702 }
703
704 private void repositionNavigationBar() {
705 if (mNavigationBarView == null || !mNavigationBarView.isAttachedToWindow()) return;
706
707 prepareNavigationBarView();
708
709 mWindowManager.updateViewLayout((View) mNavigationBarView.getParent(),
710 ((View) mNavigationBarView.getParent()).getLayoutParams());
711 }
712
Matthew Ng9c3bce52018-02-01 22:00:31 +0000713 private void updateScreenPinningGestures() {
714 if (mNavigationBarView == null) {
715 return;
716 }
717
718 // Change the cancel pin gesture to home and back if recents button is invisible
719 boolean recentsVisible = mNavigationBarView.isRecentsButtonVisible();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000720 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
721 if (recentsVisible) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000722 backButton.setOnLongClickListener(this::onLongPressBackRecents);
723 } else {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000724 backButton.setOnLongClickListener(this::onLongPressBackHome);
725 }
726 }
727
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100728 private void notifyNavigationBarScreenOn() {
Matthew Ngd0a73e72018-03-02 15:16:03 -0800729 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500730 }
731
732 private void prepareNavigationBarView() {
733 mNavigationBarView.reorient();
734
735 ButtonDispatcher recentsButton = mNavigationBarView.getRecentsButton();
736 recentsButton.setOnClickListener(this::onRecentsClick);
737 recentsButton.setOnTouchListener(this::onRecentsTouch);
738 recentsButton.setLongClickable(true);
739 recentsButton.setOnLongClickListener(this::onLongPressBackRecents);
740
741 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
742 backButton.setLongClickable(true);
Jason Monk49fa0162017-01-11 09:21:56 -0500743
744 ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();
745 homeButton.setOnTouchListener(this::onHomeTouch);
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800746 homeButton.setOnLongClickListener(this::onHomeLongClick);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800747
748 ButtonDispatcher accessibilityButton = mNavigationBarView.getAccessibilityButton();
749 accessibilityButton.setOnClickListener(this::onAccessibilityClick);
750 accessibilityButton.setOnLongClickListener(this::onAccessibilityLongClick);
Phil Weaverdb9a7742017-04-18 08:15:06 -0700751 updateAccessibilityServicesState(mAccessibilityManager);
Mike Digman7d092772018-01-11 12:10:32 -0800752
753 ButtonDispatcher rotateSuggestionButton = mNavigationBarView.getRotateSuggestionButton();
754 rotateSuggestionButton.setOnClickListener(this::onRotateSuggestionClick);
Mike Digman90402952018-01-22 16:05:51 -0800755 rotateSuggestionButton.setOnHoverListener(this::onRotateSuggestionHover);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000756 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500757 }
758
759 private boolean onHomeTouch(View v, MotionEvent event) {
760 if (mHomeBlockedThisTouch && event.getActionMasked() != MotionEvent.ACTION_DOWN) {
761 return true;
762 }
763 // If an incoming call is ringing, HOME is totally disabled.
764 // (The user is already on the InCallUI at this point,
765 // and his ONLY options are to answer or reject the call.)
766 switch (event.getAction()) {
767 case MotionEvent.ACTION_DOWN:
768 mHomeBlockedThisTouch = false;
769 TelecomManager telecomManager =
770 getContext().getSystemService(TelecomManager.class);
771 if (telecomManager != null && telecomManager.isRinging()) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500772 if (mStatusBar.isKeyguardShowing()) {
Jason Monk49fa0162017-01-11 09:21:56 -0500773 Log.i(TAG, "Ignoring HOME; there's a ringing incoming call. " +
774 "No heads up");
775 mHomeBlockedThisTouch = true;
776 return true;
777 }
778 }
779 break;
780 case MotionEvent.ACTION_UP:
781 case MotionEvent.ACTION_CANCEL:
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500782 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500783 break;
784 }
785 return false;
786 }
787
788 private void onVerticalChanged(boolean isVertical) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500789 mStatusBar.setQsScrimEnabled(!isVertical);
Jason Monk49fa0162017-01-11 09:21:56 -0500790 }
791
792 private boolean onNavigationTouch(View v, MotionEvent event) {
Eliot Courtneycb5d3162017-08-09 16:53:15 +0900793 mStatusBar.checkUserAutohide(event);
Jason Monk49fa0162017-01-11 09:21:56 -0500794 return false;
795 }
796
Jason Monk865246d2017-01-19 08:27:01 -0500797 @VisibleForTesting
798 boolean onHomeLongClick(View v) {
Matthew Ng6ff33b72018-02-27 13:47:38 -0800799 if (!mNavigationBarView.isRecentsButtonVisible()
800 && ActivityManagerWrapper.getInstance().isScreenPinningActive()) {
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800801 return onLongPressBackHome(v);
802 }
Jason Monk49fa0162017-01-11 09:21:56 -0500803 if (shouldDisableNavbarGestures()) {
804 return false;
805 }
Matthew Ng2ea93b72018-03-14 19:43:18 +0000806 mNavigationBarView.onNavigationButtonLongPress(v);
Mike Digmanc94759d2018-01-23 11:01:21 -0800807 mMetricsLogger.action(MetricsEvent.ACTION_ASSIST_LONG_PRESS);
Jason Monk49fa0162017-01-11 09:21:56 -0500808 mAssistManager.startAssist(new Bundle() /* args */);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500809 mStatusBar.awakenDreams();
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800810
Jason Monk49fa0162017-01-11 09:21:56 -0500811 if (mNavigationBarView != null) {
812 mNavigationBarView.abortCurrentGesture();
813 }
814 return true;
815 }
816
817 // additional optimization when we have software system buttons - start loading the recent
818 // tasks on touch down
819 private boolean onRecentsTouch(View v, MotionEvent event) {
820 int action = event.getAction() & MotionEvent.ACTION_MASK;
821 if (action == MotionEvent.ACTION_DOWN) {
822 mCommandQueue.preloadRecentApps();
823 } else if (action == MotionEvent.ACTION_CANCEL) {
824 mCommandQueue.cancelPreloadRecentApps();
825 } else if (action == MotionEvent.ACTION_UP) {
826 if (!v.isPressed()) {
827 mCommandQueue.cancelPreloadRecentApps();
828 }
829 }
830 return false;
831 }
832
833 private void onRecentsClick(View v) {
834 if (LatencyTracker.isEnabled(getContext())) {
835 LatencyTracker.getInstance(getContext()).onActionStart(
836 LatencyTracker.ACTION_TOGGLE_RECENTS);
837 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500838 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500839 mCommandQueue.toggleRecentApps();
840 }
841
Matthew Ng9c3bce52018-02-01 22:00:31 +0000842 private boolean onLongPressBackHome(View v) {
Matthew Ng2ea93b72018-03-14 19:43:18 +0000843 mNavigationBarView.onNavigationButtonLongPress(v);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000844 return onLongPressNavigationButtons(v, R.id.back, R.id.home);
845 }
846
847 private boolean onLongPressBackRecents(View v) {
Matthew Ng2ea93b72018-03-14 19:43:18 +0000848 mNavigationBarView.onNavigationButtonLongPress(v);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000849 return onLongPressNavigationButtons(v, R.id.back, R.id.recent_apps);
850 }
851
Jason Monk49fa0162017-01-11 09:21:56 -0500852 /**
Matthew Ng9c3bce52018-02-01 22:00:31 +0000853 * This handles long-press of both back and recents/home. Back is the common button with
854 * combination of recents if it is visible or home if recents is invisible.
855 * They are handled together to capture them both being long-pressed
Jason Monk49fa0162017-01-11 09:21:56 -0500856 * at the same time to exit screen pinning (lock task).
857 *
Matthew Ng9c3bce52018-02-01 22:00:31 +0000858 * When accessibility mode is on, only a long-press from recents/home
Jason Monk49fa0162017-01-11 09:21:56 -0500859 * is required to exit.
860 *
861 * In all other circumstances we try to pass through long-press events
862 * for Back, so that apps can still use it. Which can be from two things.
863 * 1) Not currently in screen pinning (lock task).
Matthew Ng9c3bce52018-02-01 22:00:31 +0000864 * 2) Back is long-pressed without recents/home.
Jason Monk49fa0162017-01-11 09:21:56 -0500865 */
Matthew Ng9c3bce52018-02-01 22:00:31 +0000866 private boolean onLongPressNavigationButtons(View v, @IdRes int btnId1, @IdRes int btnId2) {
Jason Monk49fa0162017-01-11 09:21:56 -0500867 try {
868 boolean sendBackLongPress = false;
869 IActivityManager activityManager = ActivityManagerNative.getDefault();
870 boolean touchExplorationEnabled = mAccessibilityManager.isTouchExplorationEnabled();
871 boolean inLockTaskMode = activityManager.isInLockTaskMode();
872 if (inLockTaskMode && !touchExplorationEnabled) {
873 long time = System.currentTimeMillis();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000874
Jason Monk49fa0162017-01-11 09:21:56 -0500875 // If we recently long-pressed the other button then they were
876 // long-pressed 'together'
877 if ((time - mLastLockToAppLongPress) < LOCK_TO_APP_GESTURE_TOLERENCE) {
Benjamin Franza83859f2017-07-03 16:34:14 +0100878 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500879 // When exiting refresh disabled flags.
Matthew Ngd0a73e72018-03-02 15:16:03 -0800880 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500881 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000882 } else if (v.getId() == btnId1) {
883 ButtonDispatcher button = btnId2 == R.id.recent_apps
884 ? mNavigationBarView.getRecentsButton()
885 : mNavigationBarView.getHomeButton();
886 if (!button.getCurrentView().isPressed()) {
887 // If we aren't pressing recents/home right now then they presses
888 // won't be together, so send the standard long-press action.
889 sendBackLongPress = true;
890 }
Jason Monk49fa0162017-01-11 09:21:56 -0500891 }
892 mLastLockToAppLongPress = time;
893 } else {
894 // If this is back still need to handle sending the long-press event.
Matthew Ng9c3bce52018-02-01 22:00:31 +0000895 if (v.getId() == btnId1) {
Jason Monk49fa0162017-01-11 09:21:56 -0500896 sendBackLongPress = true;
897 } else if (touchExplorationEnabled && inLockTaskMode) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000898 // When in accessibility mode a long press that is recents/home (not back)
Jason Monk49fa0162017-01-11 09:21:56 -0500899 // should stop lock task.
Benjamin Franza83859f2017-07-03 16:34:14 +0100900 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500901 // When exiting refresh disabled flags.
Matthew Ngd0a73e72018-03-02 15:16:03 -0800902 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500903 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000904 } else if (v.getId() == btnId2) {
905 return btnId2 == R.id.recent_apps
906 ? onLongPressRecents()
907 : onHomeLongClick(mNavigationBarView.getHomeButton().getCurrentView());
Jason Monk49fa0162017-01-11 09:21:56 -0500908 }
909 }
910 if (sendBackLongPress) {
911 KeyButtonView keyButtonView = (KeyButtonView) v;
912 keyButtonView.sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS);
913 keyButtonView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
914 return true;
915 }
916 } catch (RemoteException e) {
917 Log.d(TAG, "Unable to reach activity manager", e);
918 }
919 return false;
920 }
921
922 private boolean onLongPressRecents() {
Erik Wolsheimer9be3a062017-05-31 14:59:57 -0700923 if (mRecents == null || !ActivityManager.supportsMultiWindow(getContext())
Matthew Ng43db6d22017-06-27 15:29:39 -0700924 || !mDivider.getView().getSnapAlgorithm().isSplitScreenFeasible()
925 || Recents.getConfiguration().isLowRamDevice) {
Jason Monk49fa0162017-01-11 09:21:56 -0500926 return false;
927 }
928
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500929 return mStatusBar.toggleSplitScreenMode(MetricsEvent.ACTION_WINDOW_DOCK_LONGPRESS,
Jason Monk49fa0162017-01-11 09:21:56 -0500930 MetricsEvent.ACTION_WINDOW_UNDOCK_LONGPRESS);
931 }
932
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800933 private void onAccessibilityClick(View v) {
934 mAccessibilityManager.notifyAccessibilityButtonClicked();
935 }
936
937 private boolean onAccessibilityLongClick(View v) {
Casey Burkhardt5e8b9802017-03-24 10:07:20 -0700938 Intent intent = new Intent(AccessibilityManager.ACTION_CHOOSE_ACCESSIBILITY_BUTTON);
939 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Casey Burkhardt5d614402017-04-06 13:46:50 -0700940 v.getContext().startActivityAsUser(intent, UserHandle.CURRENT);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800941 return true;
942 }
943
Phil Weaverdb9a7742017-04-18 08:15:06 -0700944 private void updateAccessibilityServicesState(AccessibilityManager accessibilityManager) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800945 int requestingServices = 0;
946 try {
Casey Burkhardt5d614402017-04-06 13:46:50 -0700947 if (Settings.Secure.getIntForUser(mContentResolver,
948 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED,
949 UserHandle.USER_CURRENT) == 1) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800950 requestingServices++;
951 }
952 } catch (Settings.SettingNotFoundException e) {
953 }
954
Mike Digman90402952018-01-22 16:05:51 -0800955 boolean feedbackEnabled = false;
Casey Burkhardt5d614402017-04-06 13:46:50 -0700956 // AccessibilityManagerService resolves services for the current user since the local
957 // AccessibilityManager is created from a Context with the INTERACT_ACROSS_USERS permission
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800958 final List<AccessibilityServiceInfo> services =
Phil Weaverdb9a7742017-04-18 08:15:06 -0700959 accessibilityManager.getEnabledAccessibilityServiceList(
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800960 AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800961 for (int i = services.size() - 1; i >= 0; --i) {
962 AccessibilityServiceInfo info = services.get(i);
963 if ((info.flags & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0) {
964 requestingServices++;
965 }
Mike Digman90402952018-01-22 16:05:51 -0800966
967 if (info.feedbackType != 0 && info.feedbackType !=
968 AccessibilityServiceInfo.FEEDBACK_GENERIC) {
969 feedbackEnabled = true;
970 }
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800971 }
972
Mike Digman90402952018-01-22 16:05:51 -0800973 mAccessibilityFeedbackEnabled = feedbackEnabled;
974
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800975 final boolean showAccessibilityButton = requestingServices >= 1;
976 final boolean targetSelection = requestingServices >= 2;
977 mNavigationBarView.setAccessibilityButtonState(showAccessibilityButton, targetSelection);
978 }
979
Mike Digman7d092772018-01-11 12:10:32 -0800980 private void onRotateSuggestionClick(View v) {
Mike Digmanc94759d2018-01-23 11:01:21 -0800981 mMetricsLogger.action(MetricsEvent.ACTION_ROTATION_SUGGESTION_ACCEPTED);
Mike Digman50752642018-02-15 13:36:09 -0800982 incrementNumAcceptedRotationSuggestionsIfNeeded();
Mike Digman7d092772018-01-11 12:10:32 -0800983 mRotationLockController.setRotationLockedAtAngle(true, mLastRotationSuggestion);
984 }
985
Mike Digman90402952018-01-22 16:05:51 -0800986 private boolean onRotateSuggestionHover(View v, MotionEvent event) {
987 final int action = event.getActionMasked();
988 mHoveringRotationSuggestion = (action == MotionEvent.ACTION_HOVER_ENTER)
989 || (action == MotionEvent.ACTION_HOVER_MOVE);
990 rescheduleRotationTimeout(true);
991 return false; // Must return false so a11y hover events are dispatched correctly.
992 }
993
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500994 // ----- Methods that StatusBar talks to (should be minimized) -----
Jason Monk49fa0162017-01-11 09:21:56 -0500995
Jason Monk49fa0162017-01-11 09:21:56 -0500996 public void setLightBarController(LightBarController lightBarController) {
997 mLightBarController = lightBarController;
998 mLightBarController.setNavigationBar(mNavigationBarView.getLightTransitionsController());
999 }
1000
1001 public boolean isSemiTransparent() {
1002 return mNavigationBarMode == MODE_SEMI_TRANSPARENT;
1003 }
1004
Jason Monk49fa0162017-01-11 09:21:56 -05001005 public void disableAnimationsDuringHide(long delay) {
1006 mNavigationBarView.setLayoutTransitionsEnabled(false);
1007 mNavigationBarView.postDelayed(() -> mNavigationBarView.setLayoutTransitionsEnabled(true),
1008 delay + StackStateAnimator.ANIMATION_DURATION_GO_TO_FULL_SHADE);
1009 }
1010
Jason Monk49fa0162017-01-11 09:21:56 -05001011 public BarTransitions getBarTransitions() {
1012 return mNavigationBarView.getBarTransitions();
1013 }
1014
1015 public void checkNavBarModes() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -05001016 mStatusBar.checkBarMode(mNavigationBarMode,
Jason Monk49fa0162017-01-11 09:21:56 -05001017 mNavigationBarWindowState, mNavigationBarView.getBarTransitions());
1018 }
1019
1020 public void finishBarAnimations() {
1021 mNavigationBarView.getBarTransitions().finishAnimations();
1022 }
1023
Jason Monk91e587e2017-04-13 13:41:23 -04001024 private final AccessibilityServicesStateChangeListener mAccessibilityListener =
1025 this::updateAccessibilityServicesState;
1026
Casey Burkhardt74922c62017-02-13 12:43:16 -08001027 private class MagnificationContentObserver extends ContentObserver {
1028
1029 public MagnificationContentObserver(Handler handler) {
1030 super(handler);
1031 }
1032
1033 @Override
1034 public void onChange(boolean selfChange) {
Phil Weaverdb9a7742017-04-18 08:15:06 -07001035 NavigationBarFragment.this.updateAccessibilityServicesState(mAccessibilityManager);
Casey Burkhardt74922c62017-02-13 12:43:16 -08001036 }
1037 }
1038
Jason Monk49fa0162017-01-11 09:21:56 -05001039 private final Stub mRotationWatcher = new Stub() {
1040 @Override
Mike Digman90402952018-01-22 16:05:51 -08001041 public void onRotationChanged(final int rotation) throws RemoteException {
Jason Monk49fa0162017-01-11 09:21:56 -05001042 // We need this to be scheduled as early as possible to beat the redrawing of
1043 // window in response to the orientation change.
1044 Handler h = getView().getHandler();
1045 Message msg = Message.obtain(h, () -> {
Mike Digman85ff7fa2018-01-23 14:59:52 -08001046
1047 // If the screen rotation changes while locked, potentially update lock to flow with
Mike Digman90402952018-01-22 16:05:51 -08001048 // new screen rotation and hide any showing suggestions.
1049 if (mRotationLockController.isRotationLocked()) {
Mike Digman85ff7fa2018-01-23 14:59:52 -08001050 if (shouldOverrideUserLockPrefs(rotation)) {
1051 mRotationLockController.setRotationLockedAtAngle(true, rotation);
1052 }
Mike Digman1e28a5a2018-02-14 10:49:19 -08001053 setRotateSuggestionButtonState(false, true);
Mike Digman90402952018-01-22 16:05:51 -08001054 }
1055
Jason Monk49fa0162017-01-11 09:21:56 -05001056 if (mNavigationBarView != null
1057 && mNavigationBarView.needsReorient(rotation)) {
1058 repositionNavigationBar();
1059 }
1060 });
1061 msg.setAsynchronous(true);
1062 h.sendMessageAtFrontOfQueue(msg);
1063 }
Mike Digman85ff7fa2018-01-23 14:59:52 -08001064
1065 private boolean shouldOverrideUserLockPrefs(final int rotation) {
Mike Digmanab650252018-03-06 11:01:41 -08001066 // Only override user prefs when returning to the natural rotation (normally portrait).
Mike Digman85ff7fa2018-01-23 14:59:52 -08001067 // Don't let apps that force landscape or 180 alter user lock.
Mike Digmanab650252018-03-06 11:01:41 -08001068 return rotation == NATURAL_ROTATION;
Mike Digman85ff7fa2018-01-23 14:59:52 -08001069 }
Jason Monk49fa0162017-01-11 09:21:56 -05001070 };
1071
1072 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1073 @Override
1074 public void onReceive(Context context, Intent intent) {
1075 String action = intent.getAction();
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +01001076 if (Intent.ACTION_SCREEN_OFF.equals(action)
1077 || Intent.ACTION_SCREEN_ON.equals(action)) {
1078 notifyNavigationBarScreenOn();
Jason Monk49fa0162017-01-11 09:21:56 -05001079 }
1080 }
1081 };
1082
Mike Digman7d092772018-01-11 12:10:32 -08001083 class TaskStackListenerImpl extends SysUiTaskStackChangeListener {
1084 // Invalidate any rotation suggestion on task change or activity orientation change
1085 // Note: all callbacks happen on main thread
1086
1087 @Override
1088 public void onTaskStackChanged() {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001089 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001090 }
1091
1092 @Override
1093 public void onTaskRemoved(int taskId) {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001094 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001095 }
1096
1097 @Override
1098 public void onTaskMovedToFront(int taskId) {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001099 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001100 }
1101
1102 @Override
1103 public void onActivityRequestedOrientationChanged(int taskId, int requestedOrientation) {
Mike Digman85a9bea2018-02-23 15:08:53 -08001104 // Only hide the icon if the top task changes its requestedOrientation
1105 // Launcher can alter its requestedOrientation while it's not on top, don't hide on this
1106 final boolean top = ActivityManagerWrapper.getInstance().getRunningTask().id == taskId;
1107 if (top) setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001108 }
1109 }
1110
Mike Digman50752642018-02-15 13:36:09 -08001111 private class ViewRippler {
1112 private static final int RIPPLE_OFFSET_MS = 50;
1113 private static final int RIPPLE_INTERVAL_MS = 2000;
1114 private View mRoot;
1115
1116 public void start(View root) {
1117 stop(); // Stop any pending ripple animations
1118
1119 mRoot = root;
1120
1121 // Schedule pending ripples, offset the 1st to avoid problems with visibility change
1122 mRoot.postOnAnimationDelayed(mRipple, RIPPLE_OFFSET_MS);
1123 mRoot.postOnAnimationDelayed(mRipple, RIPPLE_INTERVAL_MS);
1124 mRoot.postOnAnimationDelayed(mRipple, 2*RIPPLE_INTERVAL_MS);
1125 }
1126
1127 public void stop() {
1128 if (mRoot != null) mRoot.removeCallbacks(mRipple);
1129 }
1130
1131 private final Runnable mRipple = new Runnable() {
1132 @Override
1133 public void run() { // Cause the ripple to fire via false presses
1134 mRoot.setPressed(true);
1135 mRoot.setPressed(false);
1136 }
1137 };
1138 }
1139
Jason Monk49fa0162017-01-11 09:21:56 -05001140 public static View create(Context context, FragmentListener listener) {
1141 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
1142 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
1143 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR,
1144 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
1145 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1146 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
1147 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1148 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
1149 | WindowManager.LayoutParams.FLAG_SLIPPERY,
1150 PixelFormat.TRANSLUCENT);
1151 lp.token = new Binder();
Jason Monk49fa0162017-01-11 09:21:56 -05001152 lp.setTitle("NavigationBar");
Phil Weaver8583ae82018-02-13 11:01:24 -08001153 lp.accessibilityTitle = context.getString(R.string.nav_bar);
Jason Monk49fa0162017-01-11 09:21:56 -05001154 lp.windowAnimations = 0;
1155
1156 View navigationBarView = LayoutInflater.from(context).inflate(
1157 R.layout.navigation_bar_window, null);
1158
1159 if (DEBUG) Log.v(TAG, "addNavigationBar: about to add " + navigationBarView);
1160 if (navigationBarView == null) return null;
1161
1162 context.getSystemService(WindowManager.class).addView(navigationBarView, lp);
1163 FragmentHostManager fragmentHost = FragmentHostManager.get(navigationBarView);
1164 NavigationBarFragment fragment = new NavigationBarFragment();
1165 fragmentHost.getFragmentManager().beginTransaction()
1166 .replace(R.id.navigation_bar_frame, fragment, TAG)
1167 .commit();
1168 fragmentHost.addTagListener(TAG, listener);
1169 return navigationBarView;
1170 }
1171}