blob: 11e57e19b64c9cd116bf137db552ee16e8f0c2b3 [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
Matthew Ng8f25fb962018-01-16 17:17:24 -080022import static com.android.systemui.shared.system.NavigationBarCompat.InteractionType;
Jason Monk49fa0162017-01-11 09:21:56 -050023import static com.android.systemui.statusbar.phone.BarTransitions.MODE_SEMI_TRANSPARENT;
Jason Monk2a6ea9c2017-01-26 11:14:51 -050024import static com.android.systemui.statusbar.phone.StatusBar.DEBUG_WINDOW_STATE;
25import static com.android.systemui.statusbar.phone.StatusBar.dumpBarTransitions;
Matthew Ng9c3bce52018-02-01 22:00:31 +000026import static com.android.systemui.OverviewProxyService.OverviewProxyListener;
Jason Monk49fa0162017-01-11 09:21:56 -050027
Casey Burkhardt048c2bc2016-12-08 16:09:20 -080028import android.accessibilityservice.AccessibilityServiceInfo;
Mike Digman7d092772018-01-11 12:10:32 -080029import android.animation.Animator;
30import android.animation.AnimatorListenerAdapter;
Mike Digman7d092772018-01-11 12:10:32 -080031import android.animation.ObjectAnimator;
Matthew Ng9c3bce52018-02-01 22:00:31 +000032import android.annotation.IdRes;
Jason Monk49fa0162017-01-11 09:21:56 -050033import android.annotation.Nullable;
34import android.app.ActivityManager;
35import android.app.ActivityManagerNative;
36import android.app.Fragment;
37import android.app.IActivityManager;
38import android.app.StatusBarManager;
39import android.content.BroadcastReceiver;
Casey Burkhardtb9dcd662017-03-20 15:10:16 -070040import android.content.ContentResolver;
Jason Monk49fa0162017-01-11 09:21:56 -050041import android.content.Context;
42import android.content.Intent;
43import android.content.IntentFilter;
44import android.content.res.Configuration;
Casey Burkhardt74922c62017-02-13 12:43:16 -080045import android.database.ContentObserver;
Jason Monk49fa0162017-01-11 09:21:56 -050046import android.graphics.PixelFormat;
47import android.graphics.Rect;
Mike Digman7d092772018-01-11 12:10:32 -080048import android.graphics.drawable.AnimatedVectorDrawable;
Jason Monk49fa0162017-01-11 09:21:56 -050049import android.inputmethodservice.InputMethodService;
50import android.os.Binder;
51import android.os.Bundle;
52import android.os.Handler;
53import android.os.IBinder;
54import android.os.Message;
Jason Monk49fa0162017-01-11 09:21:56 -050055import android.os.RemoteException;
56import android.os.UserHandle;
Casey Burkhardt74922c62017-02-13 12:43:16 -080057import android.provider.Settings;
Jason Monk865246d2017-01-19 08:27:01 -050058import android.support.annotation.VisibleForTesting;
Jason Monk49fa0162017-01-11 09:21:56 -050059import android.telecom.TelecomManager;
60import android.text.TextUtils;
61import android.util.Log;
62import android.view.IRotationWatcher.Stub;
63import android.view.KeyEvent;
64import android.view.LayoutInflater;
65import android.view.MotionEvent;
Mike Digman85ff7fa2018-01-23 14:59:52 -080066import android.view.Surface;
Jason Monk49fa0162017-01-11 09:21:56 -050067import android.view.View;
68import android.view.ViewGroup;
69import android.view.WindowManager;
70import android.view.WindowManager.LayoutParams;
71import android.view.WindowManagerGlobal;
72import android.view.accessibility.AccessibilityEvent;
73import android.view.accessibility.AccessibilityManager;
Jason Monk91e587e2017-04-13 13:41:23 -040074import android.view.accessibility.AccessibilityManager.AccessibilityServicesStateChangeListener;
Jason Monk49fa0162017-01-11 09:21:56 -050075
76import com.android.internal.logging.MetricsLogger;
77import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monkea03be12017-12-04 11:08:41 -050078import com.android.internal.util.LatencyTracker;
Jason Monk9c7844c2017-01-18 15:21:53 -050079import com.android.systemui.Dependency;
Mike Digman7d092772018-01-11 12:10:32 -080080import com.android.systemui.Interpolators;
Mike Digman1e28a5a2018-02-14 10:49:19 -080081import com.android.systemui.OverviewProxyService;
Jason Monk49fa0162017-01-11 09:21:56 -050082import com.android.systemui.R;
Jason Monk9c7844c2017-01-18 15:21:53 -050083import com.android.systemui.SysUiServiceProvider;
Jason Monk49fa0162017-01-11 09:21:56 -050084import com.android.systemui.assist.AssistManager;
85import com.android.systemui.fragments.FragmentHostManager;
86import com.android.systemui.fragments.FragmentHostManager.FragmentListener;
87import com.android.systemui.recents.Recents;
Mike Digman7d092772018-01-11 12:10:32 -080088import com.android.systemui.recents.misc.SysUiTaskStackChangeListener;
89import com.android.systemui.shared.system.ActivityManagerWrapper;
Matthew Ng64543e62018-02-28 17:35:10 -080090import com.android.systemui.shared.system.WindowManagerWrapper;
Jason Monk49fa0162017-01-11 09:21:56 -050091import com.android.systemui.stackdivider.Divider;
92import com.android.systemui.statusbar.CommandQueue;
93import com.android.systemui.statusbar.CommandQueue.Callbacks;
Jason Monk91e587e2017-04-13 13:41:23 -040094import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper;
Mike Digman7d092772018-01-11 12:10:32 -080095import com.android.systemui.statusbar.policy.KeyButtonDrawable;
Jason Monk49fa0162017-01-11 09:21:56 -050096import com.android.systemui.statusbar.policy.KeyButtonView;
Mike Digman7d092772018-01-11 12:10:32 -080097import com.android.systemui.statusbar.policy.RotationLockController;
Jason Monk49fa0162017-01-11 09:21:56 -050098import com.android.systemui.statusbar.stack.StackStateAnimator;
99
100import java.io.FileDescriptor;
101import java.io.PrintWriter;
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800102import java.util.List;
Jason Monk49fa0162017-01-11 09:21:56 -0500103import java.util.Locale;
104
105/**
106 * Fragment containing the NavigationBarFragment. Contains logic for what happens
107 * on clicks and view states of the nav bar.
108 */
109public class NavigationBarFragment extends Fragment implements Callbacks {
110
Jason Monkd4afe152017-05-01 15:37:43 -0400111 public static final String TAG = "NavigationBar";
Jason Monk49fa0162017-01-11 09:21:56 -0500112 private static final boolean DEBUG = false;
113 private static final String EXTRA_DISABLE_STATE = "disabled_state";
114
Mike Digman1e28a5a2018-02-14 10:49:19 -0800115 private final static int BUTTON_FADE_IN_OUT_DURATION_MS = 100;
Mike Digman5aeca792018-03-05 11:14:39 -0800116 private final static int NAVBAR_HIDDEN_PENDING_ICON_TIMEOUT_MS = 20000;
Mike Digman1e28a5a2018-02-14 10:49:19 -0800117
Mike Digman50752642018-02-15 13:36:09 -0800118 private static final int NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION = 3;
119
Jason Monk49fa0162017-01-11 09:21:56 -0500120 /** Allow some time inbetween the long press for back and recents. */
121 private static final int LOCK_TO_APP_GESTURE_TOLERENCE = 200;
122
123 protected NavigationBarView mNavigationBarView = null;
124 protected AssistManager mAssistManager;
125
126 private int mNavigationBarWindowState = WINDOW_STATE_SHOWING;
127
128 private int mNavigationIconHints = 0;
129 private int mNavigationBarMode;
Mike Digman90402952018-01-22 16:05:51 -0800130 private boolean mAccessibilityFeedbackEnabled;
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800131 private AccessibilityManager mAccessibilityManager;
Casey Burkhardt74922c62017-02-13 12:43:16 -0800132 private MagnificationContentObserver mMagnificationObserver;
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700133 private ContentResolver mContentResolver;
Mike Digmanc94759d2018-01-23 11:01:21 -0800134 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500135
136 private int mDisabledFlags1;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500137 private StatusBar mStatusBar;
Jason Monk49fa0162017-01-11 09:21:56 -0500138 private Recents mRecents;
139 private Divider mDivider;
140 private WindowManager mWindowManager;
141 private CommandQueue mCommandQueue;
142 private long mLastLockToAppLongPress;
143
144 private Locale mLocale;
145 private int mLayoutDirection;
146
147 private int mSystemUiVisibility;
148 private LightBarController mLightBarController;
Jason Monk49fa0162017-01-11 09:21:56 -0500149
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800150 private OverviewProxyService mOverviewProxyService;
151
Jason Monk49fa0162017-01-11 09:21:56 -0500152 public boolean mHomeBlockedThisTouch;
153
Mike Digman7d092772018-01-11 12:10:32 -0800154 private int mLastRotationSuggestion;
Mike Digman5aeca792018-03-05 11:14:39 -0800155 private boolean mPendingRotationSuggestion;
Mike Digman90402952018-01-22 16:05:51 -0800156 private boolean mHoveringRotationSuggestion;
Mike Digman7d092772018-01-11 12:10:32 -0800157 private RotationLockController mRotationLockController;
158 private TaskStackListenerImpl mTaskStackListener;
159
Mike Digman1e28a5a2018-02-14 10:49:19 -0800160 private final Runnable mRemoveRotationProposal = () -> setRotateSuggestionButtonState(false);
Mike Digman5aeca792018-03-05 11:14:39 -0800161 private final Runnable mCancelPendingRotationProposal =
162 () -> mPendingRotationSuggestion = false;
Mike Digman7d092772018-01-11 12:10:32 -0800163 private Animator mRotateHideAnimator;
Mike Digman50752642018-02-15 13:36:09 -0800164 private ViewRippler mViewRippler = new ViewRippler();
Mike Digman7d092772018-01-11 12:10:32 -0800165
Matthew Ng9c3bce52018-02-01 22:00:31 +0000166 private final OverviewProxyListener mOverviewProxyListener = new OverviewProxyListener() {
167 @Override
168 public void onConnectionChanged(boolean isConnected) {
169 mNavigationBarView.onOverviewProxyConnectionChanged(isConnected);
170 updateScreenPinningGestures();
Matthew Ng64543e62018-02-28 17:35:10 -0800171 WindowManagerWrapper.getInstance()
172 .setNavBarVirtualKeyHapticFeedbackEnabled(!isConnected);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000173 }
174
175 @Override
176 public void onRecentsAnimationStarted() {
177 mNavigationBarView.setRecentsAnimationStarted(true);
Mike Digman85a9bea2018-02-23 15:08:53 -0800178
179 // Use navbar dragging as a signal to hide the rotate button
180 setRotateSuggestionButtonState(false);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000181 }
Matthew Ng8f25fb962018-01-16 17:17:24 -0800182
183 @Override
184 public void onInteractionFlagsChanged(@InteractionType int flags) {
185 mNavigationBarView.updateStates();
186 }
Matthew Ng9c3bce52018-02-01 22:00:31 +0000187 };
Mike Digman7d092772018-01-11 12:10:32 -0800188
Jason Monk49fa0162017-01-11 09:21:56 -0500189 // ----- Fragment Lifecycle Callbacks -----
190
191 @Override
192 public void onCreate(@Nullable Bundle savedInstanceState) {
193 super.onCreate(savedInstanceState);
Jason Monk9c7844c2017-01-18 15:21:53 -0500194 mCommandQueue = SysUiServiceProvider.getComponent(getContext(), CommandQueue.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500195 mCommandQueue.addCallbacks(this);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500196 mStatusBar = SysUiServiceProvider.getComponent(getContext(), StatusBar.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500197 mRecents = SysUiServiceProvider.getComponent(getContext(), Recents.class);
198 mDivider = SysUiServiceProvider.getComponent(getContext(), Divider.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500199 mWindowManager = getContext().getSystemService(WindowManager.class);
200 mAccessibilityManager = getContext().getSystemService(AccessibilityManager.class);
Jason Monk91e587e2017-04-13 13:41:23 -0400201 Dependency.get(AccessibilityManagerWrapper.class).addCallback(
202 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700203 mContentResolver = getContext().getContentResolver();
Casey Burkhardt74922c62017-02-13 12:43:16 -0800204 mMagnificationObserver = new MagnificationContentObserver(
205 getContext().getMainThreadHandler());
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700206 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(
Casey Burkhardt74922c62017-02-13 12:43:16 -0800207 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED), false,
Casey Burkhardt5d614402017-04-06 13:46:50 -0700208 mMagnificationObserver, UserHandle.USER_ALL);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800209
Jason Monk49fa0162017-01-11 09:21:56 -0500210 if (savedInstanceState != null) {
211 mDisabledFlags1 = savedInstanceState.getInt(EXTRA_DISABLE_STATE, 0);
212 }
Jason Monk9c7844c2017-01-18 15:21:53 -0500213 mAssistManager = Dependency.get(AssistManager.class);
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800214 mOverviewProxyService = Dependency.get(OverviewProxyService.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500215
216 try {
217 WindowManagerGlobal.getWindowManagerService()
Andrii Kulian35fa3c22017-03-11 09:37:28 -0800218 .watchRotation(mRotationWatcher, getContext().getDisplay().getDisplayId());
Jason Monk49fa0162017-01-11 09:21:56 -0500219 } catch (RemoteException e) {
220 throw e.rethrowFromSystemServer();
221 }
Mike Digman7d092772018-01-11 12:10:32 -0800222
223 mRotationLockController = Dependency.get(RotationLockController.class);
224
225 // Register the task stack listener
226 mTaskStackListener = new TaskStackListenerImpl();
227 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500228 }
229
230 @Override
231 public void onDestroy() {
232 super.onDestroy();
233 mCommandQueue.removeCallbacks(this);
Jason Monk91e587e2017-04-13 13:41:23 -0400234 Dependency.get(AccessibilityManagerWrapper.class).removeCallback(
235 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700236 mContentResolver.unregisterContentObserver(mMagnificationObserver);
Jason Monk49fa0162017-01-11 09:21:56 -0500237 try {
238 WindowManagerGlobal.getWindowManagerService()
239 .removeRotationWatcher(mRotationWatcher);
240 } catch (RemoteException e) {
241 throw e.rethrowFromSystemServer();
242 }
Mike Digman7d092772018-01-11 12:10:32 -0800243
244 // Unregister the task stack listener
245 ActivityManagerWrapper.getInstance().unregisterTaskStackListener(mTaskStackListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500246 }
247
248 @Override
249 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
250 Bundle savedInstanceState) {
251 return inflater.inflate(R.layout.navigation_bar, container, false);
252 }
253
254 @Override
255 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
256 super.onViewCreated(view, savedInstanceState);
257 mNavigationBarView = (NavigationBarView) view;
258
259 mNavigationBarView.setDisabledFlags(mDisabledFlags1);
Matthew Ng78f88d12018-01-23 12:39:55 -0800260 mNavigationBarView.setComponents(mRecents, mDivider, mStatusBar.getPanel());
Jason Monk49fa0162017-01-11 09:21:56 -0500261 mNavigationBarView.setOnVerticalChangedListener(this::onVerticalChanged);
262 mNavigationBarView.setOnTouchListener(this::onNavigationTouch);
263 if (savedInstanceState != null) {
264 mNavigationBarView.getLightTransitionsController().restoreState(savedInstanceState);
265 }
266
267 prepareNavigationBarView();
268 checkNavBarModes();
269
270 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
271 filter.addAction(Intent.ACTION_SCREEN_ON);
272 getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100273 notifyNavigationBarScreenOn();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000274 mOverviewProxyService.addCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500275 }
276
277 @Override
278 public void onDestroyView() {
279 super.onDestroyView();
Jason Monkaa573e92017-01-27 17:00:29 -0500280 mNavigationBarView.getLightTransitionsController().destroy(getContext());
Matthew Ng9c3bce52018-02-01 22:00:31 +0000281 mOverviewProxyService.removeCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500282 getContext().unregisterReceiver(mBroadcastReceiver);
283 }
284
285 @Override
286 public void onSaveInstanceState(Bundle outState) {
287 super.onSaveInstanceState(outState);
288 outState.putInt(EXTRA_DISABLE_STATE, mDisabledFlags1);
289 if (mNavigationBarView != null) {
290 mNavigationBarView.getLightTransitionsController().saveState(outState);
291 }
292 }
293
294 @Override
295 public void onConfigurationChanged(Configuration newConfig) {
296 super.onConfigurationChanged(newConfig);
297 final Locale locale = getContext().getResources().getConfiguration().locale;
298 final int ld = TextUtils.getLayoutDirectionFromLocale(locale);
299 if (!locale.equals(mLocale) || ld != mLayoutDirection) {
300 if (DEBUG) {
301 Log.v(TAG, String.format(
302 "config changed locale/LD: %s (%d) -> %s (%d)", mLocale, mLayoutDirection,
303 locale, ld));
304 }
305 mLocale = locale;
306 mLayoutDirection = ld;
307 refreshLayout(ld);
308 }
309 repositionNavigationBar();
310 }
311
312 @Override
313 public void dump(String prefix, FileDescriptor fd, PrintWriter pw, String[] args) {
314 if (mNavigationBarView != null) {
315 pw.print(" mNavigationBarWindowState=");
316 pw.println(windowStateToString(mNavigationBarWindowState));
317 pw.print(" mNavigationBarMode=");
318 pw.println(BarTransitions.modeToString(mNavigationBarMode));
319 dumpBarTransitions(pw, "mNavigationBarView", mNavigationBarView.getBarTransitions());
320 }
321
322 pw.print(" mNavigationBarView=");
323 if (mNavigationBarView == null) {
324 pw.println("null");
325 } else {
326 mNavigationBarView.dump(fd, pw, args);
327 }
328 }
329
330 // ----- CommandQueue Callbacks -----
331
332 @Override
333 public void setImeWindowStatus(IBinder token, int vis, int backDisposition,
334 boolean showImeSwitcher) {
335 boolean imeShown = (vis & InputMethodService.IME_VISIBLE) != 0;
336 int hints = mNavigationIconHints;
Tarandeep Singh3fecef12018-01-22 14:33:33 -0800337 if (imeShown && backDisposition != InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS) {
Jason Monk49fa0162017-01-11 09:21:56 -0500338 hints |= NAVIGATION_HINT_BACK_ALT;
339 } else {
340 hints &= ~NAVIGATION_HINT_BACK_ALT;
341 }
342 if (showImeSwitcher) {
343 hints |= NAVIGATION_HINT_IME_SHOWN;
344 } else {
345 hints &= ~NAVIGATION_HINT_IME_SHOWN;
346 }
347 if (hints == mNavigationIconHints) return;
348
349 mNavigationIconHints = hints;
350
351 if (mNavigationBarView != null) {
352 mNavigationBarView.setNavigationIconHints(hints);
353 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500354 mStatusBar.checkBarModes();
Jason Monk49fa0162017-01-11 09:21:56 -0500355 }
356
357 @Override
358 public void topAppWindowChanged(boolean showMenu) {
359 if (mNavigationBarView != null) {
360 mNavigationBarView.setMenuVisibility(showMenu);
361 }
362 }
363
364 @Override
365 public void setWindowState(int window, int state) {
366 if (mNavigationBarView != null
367 && window == StatusBarManager.WINDOW_NAVIGATION_BAR
368 && mNavigationBarWindowState != state) {
369 mNavigationBarWindowState = state;
370 if (DEBUG_WINDOW_STATE) Log.d(TAG, "Navigation bar " + windowStateToString(state));
Mike Digman5aeca792018-03-05 11:14:39 -0800371
372 // If the navbar is visible, show the rotate button if there's a pending suggestion
373 if (state == WINDOW_STATE_SHOWING && mPendingRotationSuggestion) {
374 showAndLogRotationSuggestion();
375 }
Jason Monk49fa0162017-01-11 09:21:56 -0500376 }
377 }
378
Mike Digman7d092772018-01-11 12:10:32 -0800379 @Override
Mike Digmane0777312018-01-19 12:41:51 -0800380 public void onRotationProposal(final int rotation, boolean isValid) {
381 // This method will be called on rotation suggestion changes even if the proposed rotation
382 // is not valid for the top app. Use invalid rotation choices as a signal to remove the
383 // rotate button if shown.
Mike Digmane0777312018-01-19 12:41:51 -0800384 if (!isValid) {
Mike Digman1e28a5a2018-02-14 10:49:19 -0800385 setRotateSuggestionButtonState(false);
Mike Digmane0777312018-01-19 12:41:51 -0800386 return;
387 }
388
Mike Digman5aeca792018-03-05 11:14:39 -0800389 // If window rotation matches suggested rotation, remove any current suggestions
Mike Digman1e28a5a2018-02-14 10:49:19 -0800390 final int winRotation = mWindowManager.getDefaultDisplay().getRotation();
391 if (rotation == winRotation) {
Mike Digman5aeca792018-03-05 11:14:39 -0800392 getView().removeCallbacks(mRemoveRotationProposal);
Mike Digman1e28a5a2018-02-14 10:49:19 -0800393 setRotateSuggestionButtonState(false);
Mike Digman5aeca792018-03-05 11:14:39 -0800394 return;
Mike Digman7d092772018-01-11 12:10:32 -0800395 }
Mike Digman5aeca792018-03-05 11:14:39 -0800396
397 // Prepare to show the navbar icon by updating the icon style to change anim params
398 mLastRotationSuggestion = rotation; // Remember rotation for click
399 if (mNavigationBarView != null) {
400 final boolean rotationCCW = isRotationAnimationCCW(winRotation, rotation);
401 int style;
402 if (winRotation == Surface.ROTATION_0 || winRotation == Surface.ROTATION_180) {
403 style = rotationCCW ? R.style.RotateButtonCCWStart90 :
404 R.style.RotateButtonCWStart90;
405 } else { // 90 or 270
406 style = rotationCCW ? R.style.RotateButtonCCWStart0 :
407 R.style.RotateButtonCWStart0;
408 }
409 mNavigationBarView.updateRotateSuggestionButtonStyle(style, true);
410 }
411
412 if (mNavigationBarWindowState != WINDOW_STATE_SHOWING) {
413 // If the navbar isn't shown, flag the rotate icon to be shown should the navbar become
414 // visible given some time limit.
415 mPendingRotationSuggestion = true;
416 getView().removeCallbacks(mCancelPendingRotationProposal);
417 getView().postDelayed(mCancelPendingRotationProposal,
418 NAVBAR_HIDDEN_PENDING_ICON_TIMEOUT_MS);
419
420 } else { // The navbar is visible so show the icon right away
421 showAndLogRotationSuggestion();
422 }
423 }
424
425 private void showAndLogRotationSuggestion() {
426 setRotateSuggestionButtonState(true);
427 rescheduleRotationTimeout(false);
428 mMetricsLogger.visible(MetricsEvent.ROTATION_SUGGESTION_SHOWN);
Mike Digman7d092772018-01-11 12:10:32 -0800429 }
430
Mike Digman1e28a5a2018-02-14 10:49:19 -0800431 private boolean isRotationAnimationCCW(int from, int to) {
432 // All 180deg WM rotation animations are CCW, match that
433 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_90) return false;
434 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_180) return true; //180d so CCW
435 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_270) return true;
436 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_0) return true;
437 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_180) return false;
438 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_270) return true; //180d so CCW
439 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_0) return true; //180d so CCW
440 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_90) return true;
441 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_270) return false;
442 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_0) return false;
443 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_90) return true; //180d so CCW
444 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_180) return true;
445 return false; // Default
Mike Digmana48cf192018-02-12 17:52:48 -0800446 }
447
Mike Digman1e28a5a2018-02-14 10:49:19 -0800448 public void setRotateSuggestionButtonState(final boolean visible) {
449 setRotateSuggestionButtonState(visible, false);
450 }
451
452 public void setRotateSuggestionButtonState(final boolean visible, final boolean force) {
453 if (mNavigationBarView == null) return;
454
455 // At any point the the button can become invisible because an a11y service became active.
456 // Similarly, a call to make the button visible may be rejected because an a11y service is
457 // active. Must account for this.
458
459 ButtonDispatcher rotBtn = mNavigationBarView.getRotateSuggestionButton();
460 final boolean currentlyVisible = mNavigationBarView.isRotateButtonVisible();
461
462 // Rerun a show animation to indicate change but don't rerun a hide animation
463 if (!visible && !currentlyVisible) return;
464
465 View view = rotBtn.getCurrentView();
466 if (view == null) return;
467
468 KeyButtonDrawable kbd = rotBtn.getImageDrawable();
469 if (kbd == null) return;
470
471 // The KBD and AVD is recreated every new valid suggestion because of style changes.
472 AnimatedVectorDrawable animIcon = null;
473 if (kbd.getDrawable(0) instanceof AnimatedVectorDrawable) {
474 animIcon = (AnimatedVectorDrawable) kbd.getDrawable(0);
475 }
476
Mike Digman5aeca792018-03-05 11:14:39 -0800477 // Clear any pending suggestion flag as it has either been nullified or is being shown
478 mPendingRotationSuggestion = false;
479 getView().removeCallbacks(mCancelPendingRotationProposal);
480
481 // Handle the visibility change and animation
Mike Digman1e28a5a2018-02-14 10:49:19 -0800482 if (visible) { // Appear and change (cannot force)
Mike Digman85a9bea2018-02-23 15:08:53 -0800483 // Stop and clear any currently running hide animations
Mike Digman1e28a5a2018-02-14 10:49:19 -0800484 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
Mike Digman85a9bea2018-02-23 15:08:53 -0800485 mRotateHideAnimator.cancel();
Mike Digman1e28a5a2018-02-14 10:49:19 -0800486 }
Mike Digman85a9bea2018-02-23 15:08:53 -0800487 mRotateHideAnimator = null;
Mike Digman1e28a5a2018-02-14 10:49:19 -0800488
489 // Reset the alpha if any has changed due to hide animation
490 view.setAlpha(1f);
491
492 // Run the rotate icon's animation if it has one
493 if (animIcon != null) {
494 animIcon.reset();
495 animIcon.start();
496 }
497
Mike Digman50752642018-02-15 13:36:09 -0800498 if (!isRotateSuggestionIntroduced()) mViewRippler.start(view);
499
Mike Digman1e28a5a2018-02-14 10:49:19 -0800500 // Set visibility, may fail if a11y service is active.
501 // If invisible, call will stop animation.
502 mNavigationBarView.setRotateButtonVisibility(true);
503
504 } else { // Hide
505
Mike Digman50752642018-02-15 13:36:09 -0800506 mViewRippler.stop(); // Prevent any pending ripples, force hide or not
507
Mike Digman1e28a5a2018-02-14 10:49:19 -0800508 if (force) {
509 // If a hide animator is running stop it and make invisible
510 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
511 mRotateHideAnimator.pause();
512 }
513 mNavigationBarView.setRotateButtonVisibility(false);
514 return;
515 }
516
517 // Don't start any new hide animations if one is running
518 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
519
520 ObjectAnimator fadeOut = ObjectAnimator.ofFloat(view, "alpha",
521 0f);
522 fadeOut.setDuration(BUTTON_FADE_IN_OUT_DURATION_MS);
523 fadeOut.setInterpolator(Interpolators.LINEAR);
524 fadeOut.addListener(new AnimatorListenerAdapter() {
525 @Override
526 public void onAnimationEnd(Animator animation) {
527 mNavigationBarView.setRotateButtonVisibility(false);
528 }
529 });
530
531 mRotateHideAnimator = fadeOut;
532 fadeOut.start();
Mike Digmana48cf192018-02-12 17:52:48 -0800533 }
534 }
535
Mike Digman90402952018-01-22 16:05:51 -0800536 private void rescheduleRotationTimeout(final boolean reasonHover) {
537 // May be called due to a new rotation proposal or a change in hover state
538 if (reasonHover) {
539 // Don't reschedule if a hide animator is running
Mike Digman1e28a5a2018-02-14 10:49:19 -0800540 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
Mike Digman90402952018-01-22 16:05:51 -0800541 // Don't reschedule if not visible
Mike Digman1e28a5a2018-02-14 10:49:19 -0800542 if (!mNavigationBarView.isRotateButtonVisible()) return;
Mike Digman90402952018-01-22 16:05:51 -0800543 }
544
Mike Digman5aeca792018-03-05 11:14:39 -0800545 getView().removeCallbacks(mRemoveRotationProposal); // Stop any pending removal
546 getView().postDelayed(mRemoveRotationProposal,
Mike Digman90402952018-01-22 16:05:51 -0800547 computeRotationProposalTimeout()); // Schedule timeout
548 }
549
550 private int computeRotationProposalTimeout() {
551 if (mAccessibilityFeedbackEnabled) return 20000;
552 if (mHoveringRotationSuggestion) return 16000;
553 return 6000;
554 }
555
Mike Digman50752642018-02-15 13:36:09 -0800556 private boolean isRotateSuggestionIntroduced() {
557 ContentResolver cr = getContext().getContentResolver();
558 return Settings.Secure.getInt(cr, Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED, 0)
559 >= NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION;
560 }
561
562 private void incrementNumAcceptedRotationSuggestionsIfNeeded() {
563 // Get the number of accepted suggestions
564 ContentResolver cr = getContext().getContentResolver();
565 final int numSuggestions = Settings.Secure.getInt(cr,
566 Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED, 0);
567
568 // Increment the number of accepted suggestions only if it would change intro mode
569 if (numSuggestions < NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION) {
570 Settings.Secure.putInt(cr, Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED,
571 numSuggestions + 1);
572 }
573 }
574
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500575 // Injected from StatusBar at creation.
Jason Monk49fa0162017-01-11 09:21:56 -0500576 public void setCurrentSysuiVisibility(int systemUiVisibility) {
577 mSystemUiVisibility = systemUiVisibility;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500578 mNavigationBarMode = mStatusBar.computeBarMode(0, mSystemUiVisibility,
Jason Monk49fa0162017-01-11 09:21:56 -0500579 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
580 View.NAVIGATION_BAR_TRANSPARENT);
581 checkNavBarModes();
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500582 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500583 mLightBarController.onNavigationVisibilityChanged(mSystemUiVisibility, 0 /* mask */,
584 true /* nbModeChanged */, mNavigationBarMode);
585 }
586
587 @Override
588 public void setSystemUiVisibility(int vis, int fullscreenStackVis, int dockedStackVis,
589 int mask, Rect fullscreenStackBounds, Rect dockedStackBounds) {
590 final int oldVal = mSystemUiVisibility;
591 final int newVal = (oldVal & ~mask) | (vis & mask);
592 final int diff = newVal ^ oldVal;
593 boolean nbModeChanged = false;
594 if (diff != 0) {
595 mSystemUiVisibility = newVal;
596
597 // update navigation bar mode
598 final int nbMode = getView() == null
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500599 ? -1 : mStatusBar.computeBarMode(oldVal, newVal,
Jason Monk49fa0162017-01-11 09:21:56 -0500600 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
601 View.NAVIGATION_BAR_TRANSPARENT);
602 nbModeChanged = nbMode != -1;
603 if (nbModeChanged) {
604 if (mNavigationBarMode != nbMode) {
605 mNavigationBarMode = nbMode;
606 checkNavBarModes();
607 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500608 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500609 }
610 }
611
612 mLightBarController.onNavigationVisibilityChanged(vis, mask, nbModeChanged,
613 mNavigationBarMode);
614 }
615
616 @Override
617 public void disable(int state1, int state2, boolean animate) {
618 // All navigation bar flags are in state1.
619 int masked = state1 & (StatusBarManager.DISABLE_HOME
620 | StatusBarManager.DISABLE_RECENT
621 | StatusBarManager.DISABLE_BACK
622 | StatusBarManager.DISABLE_SEARCH);
623 if (masked != mDisabledFlags1) {
624 mDisabledFlags1 = masked;
625 if (mNavigationBarView != null) mNavigationBarView.setDisabledFlags(state1);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000626 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500627 }
628 }
629
630 // ----- Internal stuffz -----
631
632 private void refreshLayout(int layoutDirection) {
633 if (mNavigationBarView != null) {
634 mNavigationBarView.setLayoutDirection(layoutDirection);
635 }
636 }
637
638 private boolean shouldDisableNavbarGestures() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500639 return !mStatusBar.isDeviceProvisioned()
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800640 || (mDisabledFlags1 & StatusBarManager.DISABLE_SEARCH) != 0;
Jason Monk49fa0162017-01-11 09:21:56 -0500641 }
642
643 private void repositionNavigationBar() {
644 if (mNavigationBarView == null || !mNavigationBarView.isAttachedToWindow()) return;
645
646 prepareNavigationBarView();
647
648 mWindowManager.updateViewLayout((View) mNavigationBarView.getParent(),
649 ((View) mNavigationBarView.getParent()).getLayoutParams());
650 }
651
Matthew Ng9c3bce52018-02-01 22:00:31 +0000652 private void updateScreenPinningGestures() {
653 if (mNavigationBarView == null) {
654 return;
655 }
656
657 // Change the cancel pin gesture to home and back if recents button is invisible
658 boolean recentsVisible = mNavigationBarView.isRecentsButtonVisible();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000659 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
660 if (recentsVisible) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000661 backButton.setOnLongClickListener(this::onLongPressBackRecents);
662 } else {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000663 backButton.setOnLongClickListener(this::onLongPressBackHome);
664 }
665 }
666
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100667 private void notifyNavigationBarScreenOn() {
Matthew Ngd0a73e72018-03-02 15:16:03 -0800668 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500669 }
670
671 private void prepareNavigationBarView() {
672 mNavigationBarView.reorient();
673
674 ButtonDispatcher recentsButton = mNavigationBarView.getRecentsButton();
675 recentsButton.setOnClickListener(this::onRecentsClick);
676 recentsButton.setOnTouchListener(this::onRecentsTouch);
677 recentsButton.setLongClickable(true);
678 recentsButton.setOnLongClickListener(this::onLongPressBackRecents);
679
680 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
681 backButton.setLongClickable(true);
Jason Monk49fa0162017-01-11 09:21:56 -0500682
683 ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();
684 homeButton.setOnTouchListener(this::onHomeTouch);
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800685 homeButton.setOnLongClickListener(this::onHomeLongClick);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800686
687 ButtonDispatcher accessibilityButton = mNavigationBarView.getAccessibilityButton();
688 accessibilityButton.setOnClickListener(this::onAccessibilityClick);
689 accessibilityButton.setOnLongClickListener(this::onAccessibilityLongClick);
Phil Weaverdb9a7742017-04-18 08:15:06 -0700690 updateAccessibilityServicesState(mAccessibilityManager);
Mike Digman7d092772018-01-11 12:10:32 -0800691
692 ButtonDispatcher rotateSuggestionButton = mNavigationBarView.getRotateSuggestionButton();
693 rotateSuggestionButton.setOnClickListener(this::onRotateSuggestionClick);
Mike Digman90402952018-01-22 16:05:51 -0800694 rotateSuggestionButton.setOnHoverListener(this::onRotateSuggestionHover);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000695 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500696 }
697
698 private boolean onHomeTouch(View v, MotionEvent event) {
699 if (mHomeBlockedThisTouch && event.getActionMasked() != MotionEvent.ACTION_DOWN) {
700 return true;
701 }
702 // If an incoming call is ringing, HOME is totally disabled.
703 // (The user is already on the InCallUI at this point,
704 // and his ONLY options are to answer or reject the call.)
705 switch (event.getAction()) {
706 case MotionEvent.ACTION_DOWN:
707 mHomeBlockedThisTouch = false;
708 TelecomManager telecomManager =
709 getContext().getSystemService(TelecomManager.class);
710 if (telecomManager != null && telecomManager.isRinging()) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500711 if (mStatusBar.isKeyguardShowing()) {
Jason Monk49fa0162017-01-11 09:21:56 -0500712 Log.i(TAG, "Ignoring HOME; there's a ringing incoming call. " +
713 "No heads up");
714 mHomeBlockedThisTouch = true;
715 return true;
716 }
717 }
718 break;
719 case MotionEvent.ACTION_UP:
720 case MotionEvent.ACTION_CANCEL:
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500721 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500722 break;
723 }
724 return false;
725 }
726
727 private void onVerticalChanged(boolean isVertical) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500728 mStatusBar.setQsScrimEnabled(!isVertical);
Jason Monk49fa0162017-01-11 09:21:56 -0500729 }
730
731 private boolean onNavigationTouch(View v, MotionEvent event) {
Eliot Courtneycb5d3162017-08-09 16:53:15 +0900732 mStatusBar.checkUserAutohide(event);
Jason Monk49fa0162017-01-11 09:21:56 -0500733 return false;
734 }
735
Jason Monk865246d2017-01-19 08:27:01 -0500736 @VisibleForTesting
737 boolean onHomeLongClick(View v) {
Matthew Ng6ff33b72018-02-27 13:47:38 -0800738 if (!mNavigationBarView.isRecentsButtonVisible()
739 && ActivityManagerWrapper.getInstance().isScreenPinningActive()) {
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800740 return onLongPressBackHome(v);
741 }
Jason Monk49fa0162017-01-11 09:21:56 -0500742 if (shouldDisableNavbarGestures()) {
743 return false;
744 }
Mike Digmanc94759d2018-01-23 11:01:21 -0800745 mMetricsLogger.action(MetricsEvent.ACTION_ASSIST_LONG_PRESS);
Jason Monk49fa0162017-01-11 09:21:56 -0500746 mAssistManager.startAssist(new Bundle() /* args */);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500747 mStatusBar.awakenDreams();
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800748
Jason Monk49fa0162017-01-11 09:21:56 -0500749 if (mNavigationBarView != null) {
750 mNavigationBarView.abortCurrentGesture();
751 }
752 return true;
753 }
754
755 // additional optimization when we have software system buttons - start loading the recent
756 // tasks on touch down
757 private boolean onRecentsTouch(View v, MotionEvent event) {
758 int action = event.getAction() & MotionEvent.ACTION_MASK;
759 if (action == MotionEvent.ACTION_DOWN) {
760 mCommandQueue.preloadRecentApps();
761 } else if (action == MotionEvent.ACTION_CANCEL) {
762 mCommandQueue.cancelPreloadRecentApps();
763 } else if (action == MotionEvent.ACTION_UP) {
764 if (!v.isPressed()) {
765 mCommandQueue.cancelPreloadRecentApps();
766 }
767 }
768 return false;
769 }
770
771 private void onRecentsClick(View v) {
772 if (LatencyTracker.isEnabled(getContext())) {
773 LatencyTracker.getInstance(getContext()).onActionStart(
774 LatencyTracker.ACTION_TOGGLE_RECENTS);
775 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500776 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500777 mCommandQueue.toggleRecentApps();
778 }
779
Matthew Ng9c3bce52018-02-01 22:00:31 +0000780 private boolean onLongPressBackHome(View v) {
781 return onLongPressNavigationButtons(v, R.id.back, R.id.home);
782 }
783
784 private boolean onLongPressBackRecents(View v) {
785 return onLongPressNavigationButtons(v, R.id.back, R.id.recent_apps);
786 }
787
Jason Monk49fa0162017-01-11 09:21:56 -0500788 /**
Matthew Ng9c3bce52018-02-01 22:00:31 +0000789 * This handles long-press of both back and recents/home. Back is the common button with
790 * combination of recents if it is visible or home if recents is invisible.
791 * They are handled together to capture them both being long-pressed
Jason Monk49fa0162017-01-11 09:21:56 -0500792 * at the same time to exit screen pinning (lock task).
793 *
Matthew Ng9c3bce52018-02-01 22:00:31 +0000794 * When accessibility mode is on, only a long-press from recents/home
Jason Monk49fa0162017-01-11 09:21:56 -0500795 * is required to exit.
796 *
797 * In all other circumstances we try to pass through long-press events
798 * for Back, so that apps can still use it. Which can be from two things.
799 * 1) Not currently in screen pinning (lock task).
Matthew Ng9c3bce52018-02-01 22:00:31 +0000800 * 2) Back is long-pressed without recents/home.
Jason Monk49fa0162017-01-11 09:21:56 -0500801 */
Matthew Ng9c3bce52018-02-01 22:00:31 +0000802 private boolean onLongPressNavigationButtons(View v, @IdRes int btnId1, @IdRes int btnId2) {
Jason Monk49fa0162017-01-11 09:21:56 -0500803 try {
804 boolean sendBackLongPress = false;
805 IActivityManager activityManager = ActivityManagerNative.getDefault();
806 boolean touchExplorationEnabled = mAccessibilityManager.isTouchExplorationEnabled();
807 boolean inLockTaskMode = activityManager.isInLockTaskMode();
808 if (inLockTaskMode && !touchExplorationEnabled) {
809 long time = System.currentTimeMillis();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000810
Jason Monk49fa0162017-01-11 09:21:56 -0500811 // If we recently long-pressed the other button then they were
812 // long-pressed 'together'
813 if ((time - mLastLockToAppLongPress) < LOCK_TO_APP_GESTURE_TOLERENCE) {
Benjamin Franza83859f2017-07-03 16:34:14 +0100814 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500815 // When exiting refresh disabled flags.
Matthew Ngd0a73e72018-03-02 15:16:03 -0800816 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500817 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000818 } else if (v.getId() == btnId1) {
819 ButtonDispatcher button = btnId2 == R.id.recent_apps
820 ? mNavigationBarView.getRecentsButton()
821 : mNavigationBarView.getHomeButton();
822 if (!button.getCurrentView().isPressed()) {
823 // If we aren't pressing recents/home right now then they presses
824 // won't be together, so send the standard long-press action.
825 sendBackLongPress = true;
826 }
Jason Monk49fa0162017-01-11 09:21:56 -0500827 }
828 mLastLockToAppLongPress = time;
829 } else {
830 // If this is back still need to handle sending the long-press event.
Matthew Ng9c3bce52018-02-01 22:00:31 +0000831 if (v.getId() == btnId1) {
Jason Monk49fa0162017-01-11 09:21:56 -0500832 sendBackLongPress = true;
833 } else if (touchExplorationEnabled && inLockTaskMode) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000834 // When in accessibility mode a long press that is recents/home (not back)
Jason Monk49fa0162017-01-11 09:21:56 -0500835 // should stop lock task.
Benjamin Franza83859f2017-07-03 16:34:14 +0100836 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500837 // When exiting refresh disabled flags.
Matthew Ngd0a73e72018-03-02 15:16:03 -0800838 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500839 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000840 } else if (v.getId() == btnId2) {
841 return btnId2 == R.id.recent_apps
842 ? onLongPressRecents()
843 : onHomeLongClick(mNavigationBarView.getHomeButton().getCurrentView());
Jason Monk49fa0162017-01-11 09:21:56 -0500844 }
845 }
846 if (sendBackLongPress) {
847 KeyButtonView keyButtonView = (KeyButtonView) v;
848 keyButtonView.sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS);
849 keyButtonView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
850 return true;
851 }
852 } catch (RemoteException e) {
853 Log.d(TAG, "Unable to reach activity manager", e);
854 }
855 return false;
856 }
857
858 private boolean onLongPressRecents() {
Erik Wolsheimer9be3a062017-05-31 14:59:57 -0700859 if (mRecents == null || !ActivityManager.supportsMultiWindow(getContext())
Matthew Ng43db6d22017-06-27 15:29:39 -0700860 || !mDivider.getView().getSnapAlgorithm().isSplitScreenFeasible()
861 || Recents.getConfiguration().isLowRamDevice) {
Jason Monk49fa0162017-01-11 09:21:56 -0500862 return false;
863 }
864
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500865 return mStatusBar.toggleSplitScreenMode(MetricsEvent.ACTION_WINDOW_DOCK_LONGPRESS,
Jason Monk49fa0162017-01-11 09:21:56 -0500866 MetricsEvent.ACTION_WINDOW_UNDOCK_LONGPRESS);
867 }
868
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800869 private void onAccessibilityClick(View v) {
870 mAccessibilityManager.notifyAccessibilityButtonClicked();
871 }
872
873 private boolean onAccessibilityLongClick(View v) {
Casey Burkhardt5e8b9802017-03-24 10:07:20 -0700874 Intent intent = new Intent(AccessibilityManager.ACTION_CHOOSE_ACCESSIBILITY_BUTTON);
875 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Casey Burkhardt5d614402017-04-06 13:46:50 -0700876 v.getContext().startActivityAsUser(intent, UserHandle.CURRENT);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800877 return true;
878 }
879
Phil Weaverdb9a7742017-04-18 08:15:06 -0700880 private void updateAccessibilityServicesState(AccessibilityManager accessibilityManager) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800881 int requestingServices = 0;
882 try {
Casey Burkhardt5d614402017-04-06 13:46:50 -0700883 if (Settings.Secure.getIntForUser(mContentResolver,
884 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED,
885 UserHandle.USER_CURRENT) == 1) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800886 requestingServices++;
887 }
888 } catch (Settings.SettingNotFoundException e) {
889 }
890
Mike Digman90402952018-01-22 16:05:51 -0800891 boolean feedbackEnabled = false;
Casey Burkhardt5d614402017-04-06 13:46:50 -0700892 // AccessibilityManagerService resolves services for the current user since the local
893 // AccessibilityManager is created from a Context with the INTERACT_ACROSS_USERS permission
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800894 final List<AccessibilityServiceInfo> services =
Phil Weaverdb9a7742017-04-18 08:15:06 -0700895 accessibilityManager.getEnabledAccessibilityServiceList(
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800896 AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800897 for (int i = services.size() - 1; i >= 0; --i) {
898 AccessibilityServiceInfo info = services.get(i);
899 if ((info.flags & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0) {
900 requestingServices++;
901 }
Mike Digman90402952018-01-22 16:05:51 -0800902
903 if (info.feedbackType != 0 && info.feedbackType !=
904 AccessibilityServiceInfo.FEEDBACK_GENERIC) {
905 feedbackEnabled = true;
906 }
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800907 }
908
Mike Digman90402952018-01-22 16:05:51 -0800909 mAccessibilityFeedbackEnabled = feedbackEnabled;
910
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800911 final boolean showAccessibilityButton = requestingServices >= 1;
912 final boolean targetSelection = requestingServices >= 2;
913 mNavigationBarView.setAccessibilityButtonState(showAccessibilityButton, targetSelection);
914 }
915
Mike Digman7d092772018-01-11 12:10:32 -0800916 private void onRotateSuggestionClick(View v) {
Mike Digmanc94759d2018-01-23 11:01:21 -0800917 mMetricsLogger.action(MetricsEvent.ACTION_ROTATION_SUGGESTION_ACCEPTED);
Mike Digman50752642018-02-15 13:36:09 -0800918 incrementNumAcceptedRotationSuggestionsIfNeeded();
Mike Digman7d092772018-01-11 12:10:32 -0800919 mRotationLockController.setRotationLockedAtAngle(true, mLastRotationSuggestion);
920 }
921
Mike Digman90402952018-01-22 16:05:51 -0800922 private boolean onRotateSuggestionHover(View v, MotionEvent event) {
923 final int action = event.getActionMasked();
924 mHoveringRotationSuggestion = (action == MotionEvent.ACTION_HOVER_ENTER)
925 || (action == MotionEvent.ACTION_HOVER_MOVE);
926 rescheduleRotationTimeout(true);
927 return false; // Must return false so a11y hover events are dispatched correctly.
928 }
929
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500930 // ----- Methods that StatusBar talks to (should be minimized) -----
Jason Monk49fa0162017-01-11 09:21:56 -0500931
Jason Monk49fa0162017-01-11 09:21:56 -0500932 public void setLightBarController(LightBarController lightBarController) {
933 mLightBarController = lightBarController;
934 mLightBarController.setNavigationBar(mNavigationBarView.getLightTransitionsController());
935 }
936
937 public boolean isSemiTransparent() {
938 return mNavigationBarMode == MODE_SEMI_TRANSPARENT;
939 }
940
Jason Monk49fa0162017-01-11 09:21:56 -0500941 public void disableAnimationsDuringHide(long delay) {
942 mNavigationBarView.setLayoutTransitionsEnabled(false);
943 mNavigationBarView.postDelayed(() -> mNavigationBarView.setLayoutTransitionsEnabled(true),
944 delay + StackStateAnimator.ANIMATION_DURATION_GO_TO_FULL_SHADE);
945 }
946
Jason Monk49fa0162017-01-11 09:21:56 -0500947 public BarTransitions getBarTransitions() {
948 return mNavigationBarView.getBarTransitions();
949 }
950
951 public void checkNavBarModes() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500952 mStatusBar.checkBarMode(mNavigationBarMode,
Jason Monk49fa0162017-01-11 09:21:56 -0500953 mNavigationBarWindowState, mNavigationBarView.getBarTransitions());
954 }
955
956 public void finishBarAnimations() {
957 mNavigationBarView.getBarTransitions().finishAnimations();
958 }
959
Jason Monk91e587e2017-04-13 13:41:23 -0400960 private final AccessibilityServicesStateChangeListener mAccessibilityListener =
961 this::updateAccessibilityServicesState;
962
Casey Burkhardt74922c62017-02-13 12:43:16 -0800963 private class MagnificationContentObserver extends ContentObserver {
964
965 public MagnificationContentObserver(Handler handler) {
966 super(handler);
967 }
968
969 @Override
970 public void onChange(boolean selfChange) {
Phil Weaverdb9a7742017-04-18 08:15:06 -0700971 NavigationBarFragment.this.updateAccessibilityServicesState(mAccessibilityManager);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800972 }
973 }
974
Jason Monk49fa0162017-01-11 09:21:56 -0500975 private final Stub mRotationWatcher = new Stub() {
976 @Override
Mike Digman90402952018-01-22 16:05:51 -0800977 public void onRotationChanged(final int rotation) throws RemoteException {
Jason Monk49fa0162017-01-11 09:21:56 -0500978 // We need this to be scheduled as early as possible to beat the redrawing of
979 // window in response to the orientation change.
980 Handler h = getView().getHandler();
981 Message msg = Message.obtain(h, () -> {
Mike Digman85ff7fa2018-01-23 14:59:52 -0800982
983 // If the screen rotation changes while locked, potentially update lock to flow with
Mike Digman90402952018-01-22 16:05:51 -0800984 // new screen rotation and hide any showing suggestions.
985 if (mRotationLockController.isRotationLocked()) {
Mike Digman85ff7fa2018-01-23 14:59:52 -0800986 if (shouldOverrideUserLockPrefs(rotation)) {
987 mRotationLockController.setRotationLockedAtAngle(true, rotation);
988 }
Mike Digman1e28a5a2018-02-14 10:49:19 -0800989 setRotateSuggestionButtonState(false, true);
Mike Digman90402952018-01-22 16:05:51 -0800990 }
991
Jason Monk49fa0162017-01-11 09:21:56 -0500992 if (mNavigationBarView != null
993 && mNavigationBarView.needsReorient(rotation)) {
994 repositionNavigationBar();
995 }
996 });
997 msg.setAsynchronous(true);
998 h.sendMessageAtFrontOfQueue(msg);
999 }
Mike Digman85ff7fa2018-01-23 14:59:52 -08001000
1001 private boolean shouldOverrideUserLockPrefs(final int rotation) {
1002 // Only override user prefs when returning to portrait.
1003 // Don't let apps that force landscape or 180 alter user lock.
1004 return rotation == Surface.ROTATION_0;
1005 }
Jason Monk49fa0162017-01-11 09:21:56 -05001006 };
1007
1008 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1009 @Override
1010 public void onReceive(Context context, Intent intent) {
1011 String action = intent.getAction();
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +01001012 if (Intent.ACTION_SCREEN_OFF.equals(action)
1013 || Intent.ACTION_SCREEN_ON.equals(action)) {
1014 notifyNavigationBarScreenOn();
Jason Monk49fa0162017-01-11 09:21:56 -05001015 }
1016 }
1017 };
1018
Mike Digman7d092772018-01-11 12:10:32 -08001019 class TaskStackListenerImpl extends SysUiTaskStackChangeListener {
1020 // Invalidate any rotation suggestion on task change or activity orientation change
1021 // Note: all callbacks happen on main thread
1022
1023 @Override
1024 public void onTaskStackChanged() {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001025 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001026 }
1027
1028 @Override
1029 public void onTaskRemoved(int taskId) {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001030 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001031 }
1032
1033 @Override
1034 public void onTaskMovedToFront(int taskId) {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001035 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001036 }
1037
1038 @Override
1039 public void onActivityRequestedOrientationChanged(int taskId, int requestedOrientation) {
Mike Digman85a9bea2018-02-23 15:08:53 -08001040 // Only hide the icon if the top task changes its requestedOrientation
1041 // Launcher can alter its requestedOrientation while it's not on top, don't hide on this
1042 final boolean top = ActivityManagerWrapper.getInstance().getRunningTask().id == taskId;
1043 if (top) setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001044 }
1045 }
1046
Mike Digman50752642018-02-15 13:36:09 -08001047 private class ViewRippler {
1048 private static final int RIPPLE_OFFSET_MS = 50;
1049 private static final int RIPPLE_INTERVAL_MS = 2000;
1050 private View mRoot;
1051
1052 public void start(View root) {
1053 stop(); // Stop any pending ripple animations
1054
1055 mRoot = root;
1056
1057 // Schedule pending ripples, offset the 1st to avoid problems with visibility change
1058 mRoot.postOnAnimationDelayed(mRipple, RIPPLE_OFFSET_MS);
1059 mRoot.postOnAnimationDelayed(mRipple, RIPPLE_INTERVAL_MS);
1060 mRoot.postOnAnimationDelayed(mRipple, 2*RIPPLE_INTERVAL_MS);
1061 }
1062
1063 public void stop() {
1064 if (mRoot != null) mRoot.removeCallbacks(mRipple);
1065 }
1066
1067 private final Runnable mRipple = new Runnable() {
1068 @Override
1069 public void run() { // Cause the ripple to fire via false presses
1070 mRoot.setPressed(true);
1071 mRoot.setPressed(false);
1072 }
1073 };
1074 }
1075
Jason Monk49fa0162017-01-11 09:21:56 -05001076 public static View create(Context context, FragmentListener listener) {
1077 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
1078 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
1079 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR,
1080 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
1081 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1082 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
1083 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1084 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
1085 | WindowManager.LayoutParams.FLAG_SLIPPERY,
1086 PixelFormat.TRANSLUCENT);
1087 lp.token = new Binder();
Jason Monk49fa0162017-01-11 09:21:56 -05001088 lp.setTitle("NavigationBar");
Phil Weaver8583ae82018-02-13 11:01:24 -08001089 lp.accessibilityTitle = context.getString(R.string.nav_bar);
Jason Monk49fa0162017-01-11 09:21:56 -05001090 lp.windowAnimations = 0;
1091
1092 View navigationBarView = LayoutInflater.from(context).inflate(
1093 R.layout.navigation_bar_window, null);
1094
1095 if (DEBUG) Log.v(TAG, "addNavigationBar: about to add " + navigationBarView);
1096 if (navigationBarView == null) return null;
1097
1098 context.getSystemService(WindowManager.class).addView(navigationBarView, lp);
1099 FragmentHostManager fragmentHost = FragmentHostManager.get(navigationBarView);
1100 NavigationBarFragment fragment = new NavigationBarFragment();
1101 fragmentHost.getFragmentManager().beginTransaction()
1102 .replace(R.id.navigation_bar_frame, fragment, TAG)
1103 .commit();
1104 fragmentHost.addTagListener(TAG, listener);
1105 return navigationBarView;
1106 }
1107}