blob: 91483bcd358c1372898d515b816f20b165f3172e [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";
117
Mike Digman1e28a5a2018-02-14 10:49:19 -0800118 private final static int BUTTON_FADE_IN_OUT_DURATION_MS = 100;
Mike Digman5aeca792018-03-05 11:14:39 -0800119 private final static int NAVBAR_HIDDEN_PENDING_ICON_TIMEOUT_MS = 20000;
Mike Digman1e28a5a2018-02-14 10:49:19 -0800120
Mike Digman50752642018-02-15 13:36:09 -0800121 private static final int NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION = 3;
122
Jason Monk49fa0162017-01-11 09:21:56 -0500123 /** Allow some time inbetween the long press for back and recents. */
124 private static final int LOCK_TO_APP_GESTURE_TOLERENCE = 200;
125
126 protected NavigationBarView mNavigationBarView = null;
127 protected AssistManager mAssistManager;
128
129 private int mNavigationBarWindowState = WINDOW_STATE_SHOWING;
130
131 private int mNavigationIconHints = 0;
132 private int mNavigationBarMode;
Mike Digman90402952018-01-22 16:05:51 -0800133 private boolean mAccessibilityFeedbackEnabled;
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800134 private AccessibilityManager mAccessibilityManager;
Casey Burkhardt74922c62017-02-13 12:43:16 -0800135 private MagnificationContentObserver mMagnificationObserver;
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700136 private ContentResolver mContentResolver;
Mike Digmanc94759d2018-01-23 11:01:21 -0800137 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500138
139 private int mDisabledFlags1;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500140 private StatusBar mStatusBar;
Jason Monk49fa0162017-01-11 09:21:56 -0500141 private Recents mRecents;
142 private Divider mDivider;
143 private WindowManager mWindowManager;
144 private CommandQueue mCommandQueue;
145 private long mLastLockToAppLongPress;
146
147 private Locale mLocale;
148 private int mLayoutDirection;
149
150 private int mSystemUiVisibility;
151 private LightBarController mLightBarController;
Jason Monk49fa0162017-01-11 09:21:56 -0500152
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800153 private OverviewProxyService mOverviewProxyService;
154
Jason Monk49fa0162017-01-11 09:21:56 -0500155 public boolean mHomeBlockedThisTouch;
156
Mike Digman7d092772018-01-11 12:10:32 -0800157 private int mLastRotationSuggestion;
Mike Digman5aeca792018-03-05 11:14:39 -0800158 private boolean mPendingRotationSuggestion;
Mike Digman90402952018-01-22 16:05:51 -0800159 private boolean mHoveringRotationSuggestion;
Mike Digman7d092772018-01-11 12:10:32 -0800160 private RotationLockController mRotationLockController;
161 private TaskStackListenerImpl mTaskStackListener;
162
Mike Digman1e28a5a2018-02-14 10:49:19 -0800163 private final Runnable mRemoveRotationProposal = () -> setRotateSuggestionButtonState(false);
Mike Digman5aeca792018-03-05 11:14:39 -0800164 private final Runnable mCancelPendingRotationProposal =
165 () -> mPendingRotationSuggestion = false;
Mike Digman7d092772018-01-11 12:10:32 -0800166 private Animator mRotateHideAnimator;
Mike Digman50752642018-02-15 13:36:09 -0800167 private ViewRippler mViewRippler = new ViewRippler();
Mike Digman7d092772018-01-11 12:10:32 -0800168
Matthew Ng9c3bce52018-02-01 22:00:31 +0000169 private final OverviewProxyListener mOverviewProxyListener = new OverviewProxyListener() {
170 @Override
171 public void onConnectionChanged(boolean isConnected) {
172 mNavigationBarView.onOverviewProxyConnectionChanged(isConnected);
173 updateScreenPinningGestures();
Matthew Ng64543e62018-02-28 17:35:10 -0800174 WindowManagerWrapper.getInstance()
175 .setNavBarVirtualKeyHapticFeedbackEnabled(!isConnected);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000176 }
177
178 @Override
Matthew Ng2ea93b72018-03-14 19:43:18 +0000179 public void onQuickStepStarted() {
180 mNavigationBarView.onQuickStepStarted();
Mike Digman85a9bea2018-02-23 15:08:53 -0800181
182 // Use navbar dragging as a signal to hide the rotate button
183 setRotateSuggestionButtonState(false);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000184 }
Matthew Ng8f25fb962018-01-16 17:17:24 -0800185
186 @Override
187 public void onInteractionFlagsChanged(@InteractionType int flags) {
188 mNavigationBarView.updateStates();
189 }
Matthew Ng9c3bce52018-02-01 22:00:31 +0000190 };
Mike Digman7d092772018-01-11 12:10:32 -0800191
Jason Monk49fa0162017-01-11 09:21:56 -0500192 // ----- Fragment Lifecycle Callbacks -----
193
194 @Override
195 public void onCreate(@Nullable Bundle savedInstanceState) {
196 super.onCreate(savedInstanceState);
Jason Monk9c7844c2017-01-18 15:21:53 -0500197 mCommandQueue = SysUiServiceProvider.getComponent(getContext(), CommandQueue.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500198 mCommandQueue.addCallbacks(this);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500199 mStatusBar = SysUiServiceProvider.getComponent(getContext(), StatusBar.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500200 mRecents = SysUiServiceProvider.getComponent(getContext(), Recents.class);
201 mDivider = SysUiServiceProvider.getComponent(getContext(), Divider.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500202 mWindowManager = getContext().getSystemService(WindowManager.class);
203 mAccessibilityManager = getContext().getSystemService(AccessibilityManager.class);
Jason Monk91e587e2017-04-13 13:41:23 -0400204 Dependency.get(AccessibilityManagerWrapper.class).addCallback(
205 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700206 mContentResolver = getContext().getContentResolver();
Casey Burkhardt74922c62017-02-13 12:43:16 -0800207 mMagnificationObserver = new MagnificationContentObserver(
208 getContext().getMainThreadHandler());
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700209 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(
Casey Burkhardt74922c62017-02-13 12:43:16 -0800210 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED), false,
Casey Burkhardt5d614402017-04-06 13:46:50 -0700211 mMagnificationObserver, UserHandle.USER_ALL);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800212
Jason Monk49fa0162017-01-11 09:21:56 -0500213 if (savedInstanceState != null) {
214 mDisabledFlags1 = savedInstanceState.getInt(EXTRA_DISABLE_STATE, 0);
215 }
Jason Monk9c7844c2017-01-18 15:21:53 -0500216 mAssistManager = Dependency.get(AssistManager.class);
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800217 mOverviewProxyService = Dependency.get(OverviewProxyService.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500218
219 try {
220 WindowManagerGlobal.getWindowManagerService()
Andrii Kulian35fa3c22017-03-11 09:37:28 -0800221 .watchRotation(mRotationWatcher, getContext().getDisplay().getDisplayId());
Jason Monk49fa0162017-01-11 09:21:56 -0500222 } catch (RemoteException e) {
223 throw e.rethrowFromSystemServer();
224 }
Mike Digman7d092772018-01-11 12:10:32 -0800225
226 mRotationLockController = Dependency.get(RotationLockController.class);
227
Mike Digmanab650252018-03-06 11:01:41 -0800228 // 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
Mike Digman7d092772018-01-11 12:10:32 -0800235 // Register the task stack listener
236 mTaskStackListener = new TaskStackListenerImpl();
237 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500238 }
239
240 @Override
241 public void onDestroy() {
242 super.onDestroy();
243 mCommandQueue.removeCallbacks(this);
Jason Monk91e587e2017-04-13 13:41:23 -0400244 Dependency.get(AccessibilityManagerWrapper.class).removeCallback(
245 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700246 mContentResolver.unregisterContentObserver(mMagnificationObserver);
Jason Monk49fa0162017-01-11 09:21:56 -0500247 try {
248 WindowManagerGlobal.getWindowManagerService()
249 .removeRotationWatcher(mRotationWatcher);
250 } catch (RemoteException e) {
251 throw e.rethrowFromSystemServer();
252 }
Mike Digman7d092772018-01-11 12:10:32 -0800253
254 // Unregister the task stack listener
255 ActivityManagerWrapper.getInstance().unregisterTaskStackListener(mTaskStackListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500256 }
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);
Matthew Ng78f88d12018-01-23 12:39:55 -0800270 mNavigationBarView.setComponents(mRecents, mDivider, mStatusBar.getPanel());
Jason Monk49fa0162017-01-11 09:21:56 -0500271 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 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
281 filter.addAction(Intent.ACTION_SCREEN_ON);
282 getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100283 notifyNavigationBarScreenOn();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000284 mOverviewProxyService.addCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500285 }
286
287 @Override
288 public void onDestroyView() {
289 super.onDestroyView();
Jason Monkaa573e92017-01-27 17:00:29 -0500290 mNavigationBarView.getLightTransitionsController().destroy(getContext());
Matthew Ng9c3bce52018-02-01 22:00:31 +0000291 mOverviewProxyService.removeCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500292 getContext().unregisterReceiver(mBroadcastReceiver);
293 }
294
295 @Override
296 public void onSaveInstanceState(Bundle outState) {
297 super.onSaveInstanceState(outState);
298 outState.putInt(EXTRA_DISABLE_STATE, mDisabledFlags1);
299 if (mNavigationBarView != null) {
300 mNavigationBarView.getLightTransitionsController().saveState(outState);
301 }
302 }
303
304 @Override
305 public void onConfigurationChanged(Configuration newConfig) {
306 super.onConfigurationChanged(newConfig);
307 final Locale locale = getContext().getResources().getConfiguration().locale;
308 final int ld = TextUtils.getLayoutDirectionFromLocale(locale);
309 if (!locale.equals(mLocale) || ld != mLayoutDirection) {
310 if (DEBUG) {
311 Log.v(TAG, String.format(
312 "config changed locale/LD: %s (%d) -> %s (%d)", mLocale, mLayoutDirection,
313 locale, ld));
314 }
315 mLocale = locale;
316 mLayoutDirection = ld;
317 refreshLayout(ld);
318 }
319 repositionNavigationBar();
320 }
321
322 @Override
323 public void dump(String prefix, FileDescriptor fd, PrintWriter pw, String[] args) {
324 if (mNavigationBarView != null) {
325 pw.print(" mNavigationBarWindowState=");
326 pw.println(windowStateToString(mNavigationBarWindowState));
327 pw.print(" mNavigationBarMode=");
328 pw.println(BarTransitions.modeToString(mNavigationBarMode));
329 dumpBarTransitions(pw, "mNavigationBarView", mNavigationBarView.getBarTransitions());
330 }
331
332 pw.print(" mNavigationBarView=");
333 if (mNavigationBarView == null) {
334 pw.println("null");
335 } else {
336 mNavigationBarView.dump(fd, pw, args);
337 }
338 }
339
340 // ----- CommandQueue Callbacks -----
341
342 @Override
343 public void setImeWindowStatus(IBinder token, int vis, int backDisposition,
344 boolean showImeSwitcher) {
345 boolean imeShown = (vis & InputMethodService.IME_VISIBLE) != 0;
346 int hints = mNavigationIconHints;
Yohei Yukawa386f50e2018-03-14 13:03:42 -0700347 switch (backDisposition) {
348 case InputMethodService.BACK_DISPOSITION_DEFAULT:
349 case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
350 case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
351 if (imeShown) {
352 hints |= NAVIGATION_HINT_BACK_ALT;
353 } else {
354 hints &= ~NAVIGATION_HINT_BACK_ALT;
355 }
356 break;
357 case InputMethodService.BACK_DISPOSITION_ADJUST_NOTHING:
358 hints &= ~NAVIGATION_HINT_BACK_ALT;
359 break;
Jason Monk49fa0162017-01-11 09:21:56 -0500360 }
361 if (showImeSwitcher) {
362 hints |= NAVIGATION_HINT_IME_SHOWN;
363 } else {
364 hints &= ~NAVIGATION_HINT_IME_SHOWN;
365 }
366 if (hints == mNavigationIconHints) return;
367
368 mNavigationIconHints = hints;
369
370 if (mNavigationBarView != null) {
371 mNavigationBarView.setNavigationIconHints(hints);
372 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500373 mStatusBar.checkBarModes();
Jason Monk49fa0162017-01-11 09:21:56 -0500374 }
375
376 @Override
377 public void topAppWindowChanged(boolean showMenu) {
378 if (mNavigationBarView != null) {
379 mNavigationBarView.setMenuVisibility(showMenu);
380 }
381 }
382
383 @Override
384 public void setWindowState(int window, int state) {
385 if (mNavigationBarView != null
386 && window == StatusBarManager.WINDOW_NAVIGATION_BAR
387 && mNavigationBarWindowState != state) {
388 mNavigationBarWindowState = state;
389 if (DEBUG_WINDOW_STATE) Log.d(TAG, "Navigation bar " + windowStateToString(state));
Mike Digman5aeca792018-03-05 11:14:39 -0800390
391 // If the navbar is visible, show the rotate button if there's a pending suggestion
392 if (state == WINDOW_STATE_SHOWING && mPendingRotationSuggestion) {
393 showAndLogRotationSuggestion();
394 }
Jason Monk49fa0162017-01-11 09:21:56 -0500395 }
396 }
397
Mike Digman7d092772018-01-11 12:10:32 -0800398 @Override
Mike Digmane0777312018-01-19 12:41:51 -0800399 public void onRotationProposal(final int rotation, boolean isValid) {
Mike Digman3e33da62018-03-14 16:25:11 -0700400 final int winRotation = mWindowManager.getDefaultDisplay().getRotation();
401 if (DEBUG_ROTATION) {
402 Log.v(TAG, "onRotationProposal proposedRotation=" + Surface.rotationToString(rotation)
403 + ", winRotation=" + Surface.rotationToString(winRotation)
404 + ", isValid=" + isValid + ", mNavBarWindowState="
405 + StatusBarManager.windowStateToString(mNavigationBarWindowState)
406 + ", isRotateButtonVisible=" + (mNavigationBarView == null ? "null" :
407 mNavigationBarView.isRotateButtonVisible()));
408 }
409
Mike Digmane0777312018-01-19 12:41:51 -0800410 // This method will be called on rotation suggestion changes even if the proposed rotation
411 // is not valid for the top app. Use invalid rotation choices as a signal to remove the
412 // rotate button if shown.
Mike Digmane0777312018-01-19 12:41:51 -0800413 if (!isValid) {
Mike Digman1e28a5a2018-02-14 10:49:19 -0800414 setRotateSuggestionButtonState(false);
Mike Digmane0777312018-01-19 12:41:51 -0800415 return;
416 }
417
Mike Digman5aeca792018-03-05 11:14:39 -0800418 // If window rotation matches suggested rotation, remove any current suggestions
Mike Digman1e28a5a2018-02-14 10:49:19 -0800419 if (rotation == winRotation) {
Mike Digman5aeca792018-03-05 11:14:39 -0800420 getView().removeCallbacks(mRemoveRotationProposal);
Mike Digman1e28a5a2018-02-14 10:49:19 -0800421 setRotateSuggestionButtonState(false);
Mike Digman5aeca792018-03-05 11:14:39 -0800422 return;
Mike Digman7d092772018-01-11 12:10:32 -0800423 }
Mike Digman5aeca792018-03-05 11:14:39 -0800424
425 // Prepare to show the navbar icon by updating the icon style to change anim params
426 mLastRotationSuggestion = rotation; // Remember rotation for click
427 if (mNavigationBarView != null) {
428 final boolean rotationCCW = isRotationAnimationCCW(winRotation, rotation);
429 int style;
430 if (winRotation == Surface.ROTATION_0 || winRotation == Surface.ROTATION_180) {
431 style = rotationCCW ? R.style.RotateButtonCCWStart90 :
432 R.style.RotateButtonCWStart90;
433 } else { // 90 or 270
434 style = rotationCCW ? R.style.RotateButtonCCWStart0 :
435 R.style.RotateButtonCWStart0;
436 }
437 mNavigationBarView.updateRotateSuggestionButtonStyle(style, true);
438 }
439
440 if (mNavigationBarWindowState != WINDOW_STATE_SHOWING) {
441 // If the navbar isn't shown, flag the rotate icon to be shown should the navbar become
442 // visible given some time limit.
443 mPendingRotationSuggestion = true;
444 getView().removeCallbacks(mCancelPendingRotationProposal);
445 getView().postDelayed(mCancelPendingRotationProposal,
446 NAVBAR_HIDDEN_PENDING_ICON_TIMEOUT_MS);
447
448 } else { // The navbar is visible so show the icon right away
449 showAndLogRotationSuggestion();
450 }
451 }
452
453 private void showAndLogRotationSuggestion() {
454 setRotateSuggestionButtonState(true);
455 rescheduleRotationTimeout(false);
456 mMetricsLogger.visible(MetricsEvent.ROTATION_SUGGESTION_SHOWN);
Mike Digman7d092772018-01-11 12:10:32 -0800457 }
458
Mike Digman1e28a5a2018-02-14 10:49:19 -0800459 private boolean isRotationAnimationCCW(int from, int to) {
460 // All 180deg WM rotation animations are CCW, match that
461 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_90) return false;
462 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_180) return true; //180d so CCW
463 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_270) return true;
464 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_0) return true;
465 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_180) return false;
466 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_270) return true; //180d so CCW
467 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_0) return true; //180d so CCW
468 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_90) return true;
469 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_270) return false;
470 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_0) return false;
471 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_90) return true; //180d so CCW
472 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_180) return true;
473 return false; // Default
Mike Digmana48cf192018-02-12 17:52:48 -0800474 }
475
Mike Digman1e28a5a2018-02-14 10:49:19 -0800476 public void setRotateSuggestionButtonState(final boolean visible) {
477 setRotateSuggestionButtonState(visible, false);
478 }
479
480 public void setRotateSuggestionButtonState(final boolean visible, final boolean force) {
481 if (mNavigationBarView == null) return;
482
483 // At any point the the button can become invisible because an a11y service became active.
484 // Similarly, a call to make the button visible may be rejected because an a11y service is
485 // active. Must account for this.
486
487 ButtonDispatcher rotBtn = mNavigationBarView.getRotateSuggestionButton();
488 final boolean currentlyVisible = mNavigationBarView.isRotateButtonVisible();
489
490 // Rerun a show animation to indicate change but don't rerun a hide animation
491 if (!visible && !currentlyVisible) return;
492
493 View view = rotBtn.getCurrentView();
494 if (view == null) return;
495
496 KeyButtonDrawable kbd = rotBtn.getImageDrawable();
497 if (kbd == null) return;
498
499 // The KBD and AVD is recreated every new valid suggestion because of style changes.
500 AnimatedVectorDrawable animIcon = null;
501 if (kbd.getDrawable(0) instanceof AnimatedVectorDrawable) {
502 animIcon = (AnimatedVectorDrawable) kbd.getDrawable(0);
503 }
504
Mike Digman5aeca792018-03-05 11:14:39 -0800505 // Clear any pending suggestion flag as it has either been nullified or is being shown
506 mPendingRotationSuggestion = false;
507 getView().removeCallbacks(mCancelPendingRotationProposal);
508
509 // Handle the visibility change and animation
Mike Digman1e28a5a2018-02-14 10:49:19 -0800510 if (visible) { // Appear and change (cannot force)
Mike Digman85a9bea2018-02-23 15:08:53 -0800511 // Stop and clear any currently running hide animations
Mike Digman1e28a5a2018-02-14 10:49:19 -0800512 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
Mike Digman85a9bea2018-02-23 15:08:53 -0800513 mRotateHideAnimator.cancel();
Mike Digman1e28a5a2018-02-14 10:49:19 -0800514 }
Mike Digman85a9bea2018-02-23 15:08:53 -0800515 mRotateHideAnimator = null;
Mike Digman1e28a5a2018-02-14 10:49:19 -0800516
517 // Reset the alpha if any has changed due to hide animation
518 view.setAlpha(1f);
519
520 // Run the rotate icon's animation if it has one
521 if (animIcon != null) {
522 animIcon.reset();
523 animIcon.start();
524 }
525
Mike Digman50752642018-02-15 13:36:09 -0800526 if (!isRotateSuggestionIntroduced()) mViewRippler.start(view);
527
Mike Digman1e28a5a2018-02-14 10:49:19 -0800528 // Set visibility, may fail if a11y service is active.
529 // If invisible, call will stop animation.
530 mNavigationBarView.setRotateButtonVisibility(true);
531
532 } else { // Hide
533
Mike Digman50752642018-02-15 13:36:09 -0800534 mViewRippler.stop(); // Prevent any pending ripples, force hide or not
535
Mike Digman1e28a5a2018-02-14 10:49:19 -0800536 if (force) {
537 // If a hide animator is running stop it and make invisible
538 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
539 mRotateHideAnimator.pause();
540 }
541 mNavigationBarView.setRotateButtonVisibility(false);
542 return;
543 }
544
545 // Don't start any new hide animations if one is running
546 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
547
548 ObjectAnimator fadeOut = ObjectAnimator.ofFloat(view, "alpha",
549 0f);
550 fadeOut.setDuration(BUTTON_FADE_IN_OUT_DURATION_MS);
551 fadeOut.setInterpolator(Interpolators.LINEAR);
552 fadeOut.addListener(new AnimatorListenerAdapter() {
553 @Override
554 public void onAnimationEnd(Animator animation) {
555 mNavigationBarView.setRotateButtonVisibility(false);
556 }
557 });
558
559 mRotateHideAnimator = fadeOut;
560 fadeOut.start();
Mike Digmana48cf192018-02-12 17:52:48 -0800561 }
562 }
563
Mike Digman90402952018-01-22 16:05:51 -0800564 private void rescheduleRotationTimeout(final boolean reasonHover) {
565 // May be called due to a new rotation proposal or a change in hover state
566 if (reasonHover) {
567 // Don't reschedule if a hide animator is running
Mike Digman1e28a5a2018-02-14 10:49:19 -0800568 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
Mike Digman90402952018-01-22 16:05:51 -0800569 // Don't reschedule if not visible
Mike Digman1e28a5a2018-02-14 10:49:19 -0800570 if (!mNavigationBarView.isRotateButtonVisible()) return;
Mike Digman90402952018-01-22 16:05:51 -0800571 }
572
Mike Digman5aeca792018-03-05 11:14:39 -0800573 getView().removeCallbacks(mRemoveRotationProposal); // Stop any pending removal
574 getView().postDelayed(mRemoveRotationProposal,
Mike Digman90402952018-01-22 16:05:51 -0800575 computeRotationProposalTimeout()); // Schedule timeout
576 }
577
578 private int computeRotationProposalTimeout() {
579 if (mAccessibilityFeedbackEnabled) return 20000;
580 if (mHoveringRotationSuggestion) return 16000;
581 return 6000;
582 }
583
Mike Digman50752642018-02-15 13:36:09 -0800584 private boolean isRotateSuggestionIntroduced() {
585 ContentResolver cr = getContext().getContentResolver();
586 return Settings.Secure.getInt(cr, Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED, 0)
587 >= NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION;
588 }
589
590 private void incrementNumAcceptedRotationSuggestionsIfNeeded() {
591 // Get the number of accepted suggestions
592 ContentResolver cr = getContext().getContentResolver();
593 final int numSuggestions = Settings.Secure.getInt(cr,
594 Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED, 0);
595
596 // Increment the number of accepted suggestions only if it would change intro mode
597 if (numSuggestions < NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION) {
598 Settings.Secure.putInt(cr, Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED,
599 numSuggestions + 1);
600 }
601 }
602
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500603 // Injected from StatusBar at creation.
Jason Monk49fa0162017-01-11 09:21:56 -0500604 public void setCurrentSysuiVisibility(int systemUiVisibility) {
605 mSystemUiVisibility = systemUiVisibility;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500606 mNavigationBarMode = mStatusBar.computeBarMode(0, mSystemUiVisibility,
Jason Monk49fa0162017-01-11 09:21:56 -0500607 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
608 View.NAVIGATION_BAR_TRANSPARENT);
609 checkNavBarModes();
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500610 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500611 mLightBarController.onNavigationVisibilityChanged(mSystemUiVisibility, 0 /* mask */,
612 true /* nbModeChanged */, mNavigationBarMode);
613 }
614
615 @Override
616 public void setSystemUiVisibility(int vis, int fullscreenStackVis, int dockedStackVis,
617 int mask, Rect fullscreenStackBounds, Rect dockedStackBounds) {
618 final int oldVal = mSystemUiVisibility;
619 final int newVal = (oldVal & ~mask) | (vis & mask);
620 final int diff = newVal ^ oldVal;
621 boolean nbModeChanged = false;
622 if (diff != 0) {
623 mSystemUiVisibility = newVal;
624
625 // update navigation bar mode
626 final int nbMode = getView() == null
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500627 ? -1 : mStatusBar.computeBarMode(oldVal, newVal,
Jason Monk49fa0162017-01-11 09:21:56 -0500628 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
629 View.NAVIGATION_BAR_TRANSPARENT);
630 nbModeChanged = nbMode != -1;
631 if (nbModeChanged) {
632 if (mNavigationBarMode != nbMode) {
633 mNavigationBarMode = nbMode;
634 checkNavBarModes();
635 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500636 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500637 }
638 }
639
640 mLightBarController.onNavigationVisibilityChanged(vis, mask, nbModeChanged,
641 mNavigationBarMode);
642 }
643
644 @Override
645 public void disable(int state1, int state2, boolean animate) {
646 // All navigation bar flags are in state1.
647 int masked = state1 & (StatusBarManager.DISABLE_HOME
648 | StatusBarManager.DISABLE_RECENT
649 | StatusBarManager.DISABLE_BACK
650 | StatusBarManager.DISABLE_SEARCH);
651 if (masked != mDisabledFlags1) {
652 mDisabledFlags1 = masked;
653 if (mNavigationBarView != null) mNavigationBarView.setDisabledFlags(state1);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000654 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500655 }
656 }
657
658 // ----- Internal stuffz -----
659
660 private void refreshLayout(int layoutDirection) {
661 if (mNavigationBarView != null) {
662 mNavigationBarView.setLayoutDirection(layoutDirection);
663 }
664 }
665
666 private boolean shouldDisableNavbarGestures() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500667 return !mStatusBar.isDeviceProvisioned()
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800668 || (mDisabledFlags1 & StatusBarManager.DISABLE_SEARCH) != 0;
Jason Monk49fa0162017-01-11 09:21:56 -0500669 }
670
671 private void repositionNavigationBar() {
672 if (mNavigationBarView == null || !mNavigationBarView.isAttachedToWindow()) return;
673
674 prepareNavigationBarView();
675
676 mWindowManager.updateViewLayout((View) mNavigationBarView.getParent(),
677 ((View) mNavigationBarView.getParent()).getLayoutParams());
678 }
679
Matthew Ng9c3bce52018-02-01 22:00:31 +0000680 private void updateScreenPinningGestures() {
681 if (mNavigationBarView == null) {
682 return;
683 }
684
685 // Change the cancel pin gesture to home and back if recents button is invisible
686 boolean recentsVisible = mNavigationBarView.isRecentsButtonVisible();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000687 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
688 if (recentsVisible) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000689 backButton.setOnLongClickListener(this::onLongPressBackRecents);
690 } else {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000691 backButton.setOnLongClickListener(this::onLongPressBackHome);
692 }
693 }
694
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100695 private void notifyNavigationBarScreenOn() {
Matthew Ngd0a73e72018-03-02 15:16:03 -0800696 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500697 }
698
699 private void prepareNavigationBarView() {
700 mNavigationBarView.reorient();
701
702 ButtonDispatcher recentsButton = mNavigationBarView.getRecentsButton();
703 recentsButton.setOnClickListener(this::onRecentsClick);
704 recentsButton.setOnTouchListener(this::onRecentsTouch);
705 recentsButton.setLongClickable(true);
706 recentsButton.setOnLongClickListener(this::onLongPressBackRecents);
707
708 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
709 backButton.setLongClickable(true);
Jason Monk49fa0162017-01-11 09:21:56 -0500710
711 ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();
712 homeButton.setOnTouchListener(this::onHomeTouch);
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800713 homeButton.setOnLongClickListener(this::onHomeLongClick);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800714
715 ButtonDispatcher accessibilityButton = mNavigationBarView.getAccessibilityButton();
716 accessibilityButton.setOnClickListener(this::onAccessibilityClick);
717 accessibilityButton.setOnLongClickListener(this::onAccessibilityLongClick);
Phil Weaverdb9a7742017-04-18 08:15:06 -0700718 updateAccessibilityServicesState(mAccessibilityManager);
Mike Digman7d092772018-01-11 12:10:32 -0800719
720 ButtonDispatcher rotateSuggestionButton = mNavigationBarView.getRotateSuggestionButton();
721 rotateSuggestionButton.setOnClickListener(this::onRotateSuggestionClick);
Mike Digman90402952018-01-22 16:05:51 -0800722 rotateSuggestionButton.setOnHoverListener(this::onRotateSuggestionHover);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000723 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500724 }
725
726 private boolean onHomeTouch(View v, MotionEvent event) {
727 if (mHomeBlockedThisTouch && event.getActionMasked() != MotionEvent.ACTION_DOWN) {
728 return true;
729 }
730 // If an incoming call is ringing, HOME is totally disabled.
731 // (The user is already on the InCallUI at this point,
732 // and his ONLY options are to answer or reject the call.)
733 switch (event.getAction()) {
734 case MotionEvent.ACTION_DOWN:
735 mHomeBlockedThisTouch = false;
736 TelecomManager telecomManager =
737 getContext().getSystemService(TelecomManager.class);
738 if (telecomManager != null && telecomManager.isRinging()) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500739 if (mStatusBar.isKeyguardShowing()) {
Jason Monk49fa0162017-01-11 09:21:56 -0500740 Log.i(TAG, "Ignoring HOME; there's a ringing incoming call. " +
741 "No heads up");
742 mHomeBlockedThisTouch = true;
743 return true;
744 }
745 }
746 break;
747 case MotionEvent.ACTION_UP:
748 case MotionEvent.ACTION_CANCEL:
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500749 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500750 break;
751 }
752 return false;
753 }
754
755 private void onVerticalChanged(boolean isVertical) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500756 mStatusBar.setQsScrimEnabled(!isVertical);
Jason Monk49fa0162017-01-11 09:21:56 -0500757 }
758
759 private boolean onNavigationTouch(View v, MotionEvent event) {
Eliot Courtneycb5d3162017-08-09 16:53:15 +0900760 mStatusBar.checkUserAutohide(event);
Jason Monk49fa0162017-01-11 09:21:56 -0500761 return false;
762 }
763
Jason Monk865246d2017-01-19 08:27:01 -0500764 @VisibleForTesting
765 boolean onHomeLongClick(View v) {
Matthew Ng6ff33b72018-02-27 13:47:38 -0800766 if (!mNavigationBarView.isRecentsButtonVisible()
767 && ActivityManagerWrapper.getInstance().isScreenPinningActive()) {
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800768 return onLongPressBackHome(v);
769 }
Jason Monk49fa0162017-01-11 09:21:56 -0500770 if (shouldDisableNavbarGestures()) {
771 return false;
772 }
Matthew Ng2ea93b72018-03-14 19:43:18 +0000773 mNavigationBarView.onNavigationButtonLongPress(v);
Mike Digmanc94759d2018-01-23 11:01:21 -0800774 mMetricsLogger.action(MetricsEvent.ACTION_ASSIST_LONG_PRESS);
Jason Monk49fa0162017-01-11 09:21:56 -0500775 mAssistManager.startAssist(new Bundle() /* args */);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500776 mStatusBar.awakenDreams();
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800777
Jason Monk49fa0162017-01-11 09:21:56 -0500778 if (mNavigationBarView != null) {
779 mNavigationBarView.abortCurrentGesture();
780 }
781 return true;
782 }
783
784 // additional optimization when we have software system buttons - start loading the recent
785 // tasks on touch down
786 private boolean onRecentsTouch(View v, MotionEvent event) {
787 int action = event.getAction() & MotionEvent.ACTION_MASK;
788 if (action == MotionEvent.ACTION_DOWN) {
789 mCommandQueue.preloadRecentApps();
790 } else if (action == MotionEvent.ACTION_CANCEL) {
791 mCommandQueue.cancelPreloadRecentApps();
792 } else if (action == MotionEvent.ACTION_UP) {
793 if (!v.isPressed()) {
794 mCommandQueue.cancelPreloadRecentApps();
795 }
796 }
797 return false;
798 }
799
800 private void onRecentsClick(View v) {
801 if (LatencyTracker.isEnabled(getContext())) {
802 LatencyTracker.getInstance(getContext()).onActionStart(
803 LatencyTracker.ACTION_TOGGLE_RECENTS);
804 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500805 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500806 mCommandQueue.toggleRecentApps();
807 }
808
Matthew Ng9c3bce52018-02-01 22:00:31 +0000809 private boolean onLongPressBackHome(View v) {
Matthew Ng2ea93b72018-03-14 19:43:18 +0000810 mNavigationBarView.onNavigationButtonLongPress(v);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000811 return onLongPressNavigationButtons(v, R.id.back, R.id.home);
812 }
813
814 private boolean onLongPressBackRecents(View v) {
Matthew Ng2ea93b72018-03-14 19:43:18 +0000815 mNavigationBarView.onNavigationButtonLongPress(v);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000816 return onLongPressNavigationButtons(v, R.id.back, R.id.recent_apps);
817 }
818
Jason Monk49fa0162017-01-11 09:21:56 -0500819 /**
Matthew Ng9c3bce52018-02-01 22:00:31 +0000820 * This handles long-press of both back and recents/home. Back is the common button with
821 * combination of recents if it is visible or home if recents is invisible.
822 * They are handled together to capture them both being long-pressed
Jason Monk49fa0162017-01-11 09:21:56 -0500823 * at the same time to exit screen pinning (lock task).
824 *
Matthew Ng9c3bce52018-02-01 22:00:31 +0000825 * When accessibility mode is on, only a long-press from recents/home
Jason Monk49fa0162017-01-11 09:21:56 -0500826 * is required to exit.
827 *
828 * In all other circumstances we try to pass through long-press events
829 * for Back, so that apps can still use it. Which can be from two things.
830 * 1) Not currently in screen pinning (lock task).
Matthew Ng9c3bce52018-02-01 22:00:31 +0000831 * 2) Back is long-pressed without recents/home.
Jason Monk49fa0162017-01-11 09:21:56 -0500832 */
Matthew Ng9c3bce52018-02-01 22:00:31 +0000833 private boolean onLongPressNavigationButtons(View v, @IdRes int btnId1, @IdRes int btnId2) {
Jason Monk49fa0162017-01-11 09:21:56 -0500834 try {
835 boolean sendBackLongPress = false;
836 IActivityManager activityManager = ActivityManagerNative.getDefault();
837 boolean touchExplorationEnabled = mAccessibilityManager.isTouchExplorationEnabled();
838 boolean inLockTaskMode = activityManager.isInLockTaskMode();
839 if (inLockTaskMode && !touchExplorationEnabled) {
840 long time = System.currentTimeMillis();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000841
Jason Monk49fa0162017-01-11 09:21:56 -0500842 // If we recently long-pressed the other button then they were
843 // long-pressed 'together'
844 if ((time - mLastLockToAppLongPress) < LOCK_TO_APP_GESTURE_TOLERENCE) {
Benjamin Franza83859f2017-07-03 16:34:14 +0100845 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500846 // When exiting refresh disabled flags.
Matthew Ngd0a73e72018-03-02 15:16:03 -0800847 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500848 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000849 } else if (v.getId() == btnId1) {
850 ButtonDispatcher button = btnId2 == R.id.recent_apps
851 ? mNavigationBarView.getRecentsButton()
852 : mNavigationBarView.getHomeButton();
853 if (!button.getCurrentView().isPressed()) {
854 // If we aren't pressing recents/home right now then they presses
855 // won't be together, so send the standard long-press action.
856 sendBackLongPress = true;
857 }
Jason Monk49fa0162017-01-11 09:21:56 -0500858 }
859 mLastLockToAppLongPress = time;
860 } else {
861 // If this is back still need to handle sending the long-press event.
Matthew Ng9c3bce52018-02-01 22:00:31 +0000862 if (v.getId() == btnId1) {
Jason Monk49fa0162017-01-11 09:21:56 -0500863 sendBackLongPress = true;
864 } else if (touchExplorationEnabled && inLockTaskMode) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000865 // When in accessibility mode a long press that is recents/home (not back)
Jason Monk49fa0162017-01-11 09:21:56 -0500866 // should stop lock task.
Benjamin Franza83859f2017-07-03 16:34:14 +0100867 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500868 // When exiting refresh disabled flags.
Matthew Ngd0a73e72018-03-02 15:16:03 -0800869 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500870 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000871 } else if (v.getId() == btnId2) {
872 return btnId2 == R.id.recent_apps
873 ? onLongPressRecents()
874 : onHomeLongClick(mNavigationBarView.getHomeButton().getCurrentView());
Jason Monk49fa0162017-01-11 09:21:56 -0500875 }
876 }
877 if (sendBackLongPress) {
878 KeyButtonView keyButtonView = (KeyButtonView) v;
879 keyButtonView.sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS);
880 keyButtonView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
881 return true;
882 }
883 } catch (RemoteException e) {
884 Log.d(TAG, "Unable to reach activity manager", e);
885 }
886 return false;
887 }
888
889 private boolean onLongPressRecents() {
Erik Wolsheimer9be3a062017-05-31 14:59:57 -0700890 if (mRecents == null || !ActivityManager.supportsMultiWindow(getContext())
Matthew Ng43db6d22017-06-27 15:29:39 -0700891 || !mDivider.getView().getSnapAlgorithm().isSplitScreenFeasible()
892 || Recents.getConfiguration().isLowRamDevice) {
Jason Monk49fa0162017-01-11 09:21:56 -0500893 return false;
894 }
895
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500896 return mStatusBar.toggleSplitScreenMode(MetricsEvent.ACTION_WINDOW_DOCK_LONGPRESS,
Jason Monk49fa0162017-01-11 09:21:56 -0500897 MetricsEvent.ACTION_WINDOW_UNDOCK_LONGPRESS);
898 }
899
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800900 private void onAccessibilityClick(View v) {
901 mAccessibilityManager.notifyAccessibilityButtonClicked();
902 }
903
904 private boolean onAccessibilityLongClick(View v) {
Casey Burkhardt5e8b9802017-03-24 10:07:20 -0700905 Intent intent = new Intent(AccessibilityManager.ACTION_CHOOSE_ACCESSIBILITY_BUTTON);
906 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Casey Burkhardt5d614402017-04-06 13:46:50 -0700907 v.getContext().startActivityAsUser(intent, UserHandle.CURRENT);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800908 return true;
909 }
910
Phil Weaverdb9a7742017-04-18 08:15:06 -0700911 private void updateAccessibilityServicesState(AccessibilityManager accessibilityManager) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800912 int requestingServices = 0;
913 try {
Casey Burkhardt5d614402017-04-06 13:46:50 -0700914 if (Settings.Secure.getIntForUser(mContentResolver,
915 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED,
916 UserHandle.USER_CURRENT) == 1) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800917 requestingServices++;
918 }
919 } catch (Settings.SettingNotFoundException e) {
920 }
921
Mike Digman90402952018-01-22 16:05:51 -0800922 boolean feedbackEnabled = false;
Casey Burkhardt5d614402017-04-06 13:46:50 -0700923 // AccessibilityManagerService resolves services for the current user since the local
924 // AccessibilityManager is created from a Context with the INTERACT_ACROSS_USERS permission
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800925 final List<AccessibilityServiceInfo> services =
Phil Weaverdb9a7742017-04-18 08:15:06 -0700926 accessibilityManager.getEnabledAccessibilityServiceList(
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800927 AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800928 for (int i = services.size() - 1; i >= 0; --i) {
929 AccessibilityServiceInfo info = services.get(i);
930 if ((info.flags & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0) {
931 requestingServices++;
932 }
Mike Digman90402952018-01-22 16:05:51 -0800933
934 if (info.feedbackType != 0 && info.feedbackType !=
935 AccessibilityServiceInfo.FEEDBACK_GENERIC) {
936 feedbackEnabled = true;
937 }
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800938 }
939
Mike Digman90402952018-01-22 16:05:51 -0800940 mAccessibilityFeedbackEnabled = feedbackEnabled;
941
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800942 final boolean showAccessibilityButton = requestingServices >= 1;
943 final boolean targetSelection = requestingServices >= 2;
944 mNavigationBarView.setAccessibilityButtonState(showAccessibilityButton, targetSelection);
945 }
946
Mike Digman7d092772018-01-11 12:10:32 -0800947 private void onRotateSuggestionClick(View v) {
Mike Digmanc94759d2018-01-23 11:01:21 -0800948 mMetricsLogger.action(MetricsEvent.ACTION_ROTATION_SUGGESTION_ACCEPTED);
Mike Digman50752642018-02-15 13:36:09 -0800949 incrementNumAcceptedRotationSuggestionsIfNeeded();
Mike Digman7d092772018-01-11 12:10:32 -0800950 mRotationLockController.setRotationLockedAtAngle(true, mLastRotationSuggestion);
951 }
952
Mike Digman90402952018-01-22 16:05:51 -0800953 private boolean onRotateSuggestionHover(View v, MotionEvent event) {
954 final int action = event.getActionMasked();
955 mHoveringRotationSuggestion = (action == MotionEvent.ACTION_HOVER_ENTER)
956 || (action == MotionEvent.ACTION_HOVER_MOVE);
957 rescheduleRotationTimeout(true);
958 return false; // Must return false so a11y hover events are dispatched correctly.
959 }
960
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500961 // ----- Methods that StatusBar talks to (should be minimized) -----
Jason Monk49fa0162017-01-11 09:21:56 -0500962
Jason Monk49fa0162017-01-11 09:21:56 -0500963 public void setLightBarController(LightBarController lightBarController) {
964 mLightBarController = lightBarController;
965 mLightBarController.setNavigationBar(mNavigationBarView.getLightTransitionsController());
966 }
967
968 public boolean isSemiTransparent() {
969 return mNavigationBarMode == MODE_SEMI_TRANSPARENT;
970 }
971
Jason Monk49fa0162017-01-11 09:21:56 -0500972 public void disableAnimationsDuringHide(long delay) {
973 mNavigationBarView.setLayoutTransitionsEnabled(false);
974 mNavigationBarView.postDelayed(() -> mNavigationBarView.setLayoutTransitionsEnabled(true),
975 delay + StackStateAnimator.ANIMATION_DURATION_GO_TO_FULL_SHADE);
976 }
977
Jason Monk49fa0162017-01-11 09:21:56 -0500978 public BarTransitions getBarTransitions() {
979 return mNavigationBarView.getBarTransitions();
980 }
981
982 public void checkNavBarModes() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500983 mStatusBar.checkBarMode(mNavigationBarMode,
Jason Monk49fa0162017-01-11 09:21:56 -0500984 mNavigationBarWindowState, mNavigationBarView.getBarTransitions());
985 }
986
987 public void finishBarAnimations() {
988 mNavigationBarView.getBarTransitions().finishAnimations();
989 }
990
Jason Monk91e587e2017-04-13 13:41:23 -0400991 private final AccessibilityServicesStateChangeListener mAccessibilityListener =
992 this::updateAccessibilityServicesState;
993
Casey Burkhardt74922c62017-02-13 12:43:16 -0800994 private class MagnificationContentObserver extends ContentObserver {
995
996 public MagnificationContentObserver(Handler handler) {
997 super(handler);
998 }
999
1000 @Override
1001 public void onChange(boolean selfChange) {
Phil Weaverdb9a7742017-04-18 08:15:06 -07001002 NavigationBarFragment.this.updateAccessibilityServicesState(mAccessibilityManager);
Casey Burkhardt74922c62017-02-13 12:43:16 -08001003 }
1004 }
1005
Jason Monk49fa0162017-01-11 09:21:56 -05001006 private final Stub mRotationWatcher = new Stub() {
1007 @Override
Mike Digman90402952018-01-22 16:05:51 -08001008 public void onRotationChanged(final int rotation) throws RemoteException {
Jason Monk49fa0162017-01-11 09:21:56 -05001009 // We need this to be scheduled as early as possible to beat the redrawing of
1010 // window in response to the orientation change.
1011 Handler h = getView().getHandler();
1012 Message msg = Message.obtain(h, () -> {
Mike Digman85ff7fa2018-01-23 14:59:52 -08001013
1014 // If the screen rotation changes while locked, potentially update lock to flow with
Mike Digman90402952018-01-22 16:05:51 -08001015 // new screen rotation and hide any showing suggestions.
1016 if (mRotationLockController.isRotationLocked()) {
Mike Digman85ff7fa2018-01-23 14:59:52 -08001017 if (shouldOverrideUserLockPrefs(rotation)) {
1018 mRotationLockController.setRotationLockedAtAngle(true, rotation);
1019 }
Mike Digman1e28a5a2018-02-14 10:49:19 -08001020 setRotateSuggestionButtonState(false, true);
Mike Digman90402952018-01-22 16:05:51 -08001021 }
1022
Jason Monk49fa0162017-01-11 09:21:56 -05001023 if (mNavigationBarView != null
1024 && mNavigationBarView.needsReorient(rotation)) {
1025 repositionNavigationBar();
1026 }
1027 });
1028 msg.setAsynchronous(true);
1029 h.sendMessageAtFrontOfQueue(msg);
1030 }
Mike Digman85ff7fa2018-01-23 14:59:52 -08001031
1032 private boolean shouldOverrideUserLockPrefs(final int rotation) {
Mike Digmanab650252018-03-06 11:01:41 -08001033 // Only override user prefs when returning to the natural rotation (normally portrait).
Mike Digman85ff7fa2018-01-23 14:59:52 -08001034 // Don't let apps that force landscape or 180 alter user lock.
Mike Digmanab650252018-03-06 11:01:41 -08001035 return rotation == NATURAL_ROTATION;
Mike Digman85ff7fa2018-01-23 14:59:52 -08001036 }
Jason Monk49fa0162017-01-11 09:21:56 -05001037 };
1038
1039 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1040 @Override
1041 public void onReceive(Context context, Intent intent) {
1042 String action = intent.getAction();
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +01001043 if (Intent.ACTION_SCREEN_OFF.equals(action)
1044 || Intent.ACTION_SCREEN_ON.equals(action)) {
1045 notifyNavigationBarScreenOn();
Jason Monk49fa0162017-01-11 09:21:56 -05001046 }
1047 }
1048 };
1049
Mike Digman7d092772018-01-11 12:10:32 -08001050 class TaskStackListenerImpl extends SysUiTaskStackChangeListener {
1051 // Invalidate any rotation suggestion on task change or activity orientation change
1052 // Note: all callbacks happen on main thread
1053
1054 @Override
1055 public void onTaskStackChanged() {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001056 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001057 }
1058
1059 @Override
1060 public void onTaskRemoved(int taskId) {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001061 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001062 }
1063
1064 @Override
1065 public void onTaskMovedToFront(int taskId) {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001066 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001067 }
1068
1069 @Override
1070 public void onActivityRequestedOrientationChanged(int taskId, int requestedOrientation) {
Mike Digman85a9bea2018-02-23 15:08:53 -08001071 // Only hide the icon if the top task changes its requestedOrientation
1072 // Launcher can alter its requestedOrientation while it's not on top, don't hide on this
1073 final boolean top = ActivityManagerWrapper.getInstance().getRunningTask().id == taskId;
1074 if (top) setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001075 }
1076 }
1077
Mike Digman50752642018-02-15 13:36:09 -08001078 private class ViewRippler {
1079 private static final int RIPPLE_OFFSET_MS = 50;
1080 private static final int RIPPLE_INTERVAL_MS = 2000;
1081 private View mRoot;
1082
1083 public void start(View root) {
1084 stop(); // Stop any pending ripple animations
1085
1086 mRoot = root;
1087
1088 // Schedule pending ripples, offset the 1st to avoid problems with visibility change
1089 mRoot.postOnAnimationDelayed(mRipple, RIPPLE_OFFSET_MS);
1090 mRoot.postOnAnimationDelayed(mRipple, RIPPLE_INTERVAL_MS);
1091 mRoot.postOnAnimationDelayed(mRipple, 2*RIPPLE_INTERVAL_MS);
1092 }
1093
1094 public void stop() {
1095 if (mRoot != null) mRoot.removeCallbacks(mRipple);
1096 }
1097
1098 private final Runnable mRipple = new Runnable() {
1099 @Override
1100 public void run() { // Cause the ripple to fire via false presses
1101 mRoot.setPressed(true);
1102 mRoot.setPressed(false);
1103 }
1104 };
1105 }
1106
Jason Monk49fa0162017-01-11 09:21:56 -05001107 public static View create(Context context, FragmentListener listener) {
1108 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
1109 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
1110 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR,
1111 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
1112 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1113 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
1114 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1115 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
1116 | WindowManager.LayoutParams.FLAG_SLIPPERY,
1117 PixelFormat.TRANSLUCENT);
1118 lp.token = new Binder();
Jason Monk49fa0162017-01-11 09:21:56 -05001119 lp.setTitle("NavigationBar");
Phil Weaver8583ae82018-02-13 11:01:24 -08001120 lp.accessibilityTitle = context.getString(R.string.nav_bar);
Jason Monk49fa0162017-01-11 09:21:56 -05001121 lp.windowAnimations = 0;
1122
1123 View navigationBarView = LayoutInflater.from(context).inflate(
1124 R.layout.navigation_bar_window, null);
1125
1126 if (DEBUG) Log.v(TAG, "addNavigationBar: about to add " + navigationBarView);
1127 if (navigationBarView == null) return null;
1128
1129 context.getSystemService(WindowManager.class).addView(navigationBarView, lp);
1130 FragmentHostManager fragmentHost = FragmentHostManager.get(navigationBarView);
1131 NavigationBarFragment fragment = new NavigationBarFragment();
1132 fragmentHost.getFragmentManager().beginTransaction()
1133 .replace(R.id.navigation_bar_frame, fragment, TAG)
1134 .commit();
1135 fragmentHost.addTagListener(TAG, listener);
1136 return navigationBarView;
1137 }
1138}