blob: e8d85a84139dbb804df557e86ae5b3407d6826db [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;
Mike Digman1e28a5a2018-02-14 10:49:19 -080049import android.graphics.drawable.Drawable;
Jason Monk49fa0162017-01-11 09:21:56 -050050import android.inputmethodservice.InputMethodService;
51import android.os.Binder;
52import android.os.Bundle;
53import android.os.Handler;
54import android.os.IBinder;
55import android.os.Message;
Jason Monk49fa0162017-01-11 09:21:56 -050056import android.os.RemoteException;
57import android.os.UserHandle;
Casey Burkhardt74922c62017-02-13 12:43:16 -080058import android.provider.Settings;
Jason Monk865246d2017-01-19 08:27:01 -050059import android.support.annotation.VisibleForTesting;
Jason Monk49fa0162017-01-11 09:21:56 -050060import android.telecom.TelecomManager;
61import android.text.TextUtils;
62import android.util.Log;
63import android.view.IRotationWatcher.Stub;
64import android.view.KeyEvent;
65import android.view.LayoutInflater;
66import android.view.MotionEvent;
Mike Digman85ff7fa2018-01-23 14:59:52 -080067import android.view.Surface;
Jason Monk49fa0162017-01-11 09:21:56 -050068import android.view.View;
69import android.view.ViewGroup;
70import android.view.WindowManager;
71import android.view.WindowManager.LayoutParams;
72import android.view.WindowManagerGlobal;
73import android.view.accessibility.AccessibilityEvent;
74import android.view.accessibility.AccessibilityManager;
Jason Monk91e587e2017-04-13 13:41:23 -040075import android.view.accessibility.AccessibilityManager.AccessibilityServicesStateChangeListener;
Jason Monk49fa0162017-01-11 09:21:56 -050076
77import com.android.internal.logging.MetricsLogger;
78import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monkea03be12017-12-04 11:08:41 -050079import com.android.internal.util.LatencyTracker;
Jason Monk9c7844c2017-01-18 15:21:53 -050080import com.android.systemui.Dependency;
Mike Digman7d092772018-01-11 12:10:32 -080081import com.android.systemui.Interpolators;
Mike Digman1e28a5a2018-02-14 10:49:19 -080082import com.android.systemui.OverviewProxyService;
Jason Monk49fa0162017-01-11 09:21:56 -050083import com.android.systemui.R;
Jason Monk9c7844c2017-01-18 15:21:53 -050084import com.android.systemui.SysUiServiceProvider;
Jason Monk49fa0162017-01-11 09:21:56 -050085import com.android.systemui.assist.AssistManager;
86import com.android.systemui.fragments.FragmentHostManager;
87import com.android.systemui.fragments.FragmentHostManager.FragmentListener;
88import com.android.systemui.recents.Recents;
Mike Digman7d092772018-01-11 12:10:32 -080089import com.android.systemui.recents.misc.SysUiTaskStackChangeListener;
90import com.android.systemui.shared.system.ActivityManagerWrapper;
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;
116 private final static int ROTATE_BUTTON_LOOP_DURATION_MS = 2000;
117
Jason Monk49fa0162017-01-11 09:21:56 -0500118 /** Allow some time inbetween the long press for back and recents. */
119 private static final int LOCK_TO_APP_GESTURE_TOLERENCE = 200;
120
121 protected NavigationBarView mNavigationBarView = null;
122 protected AssistManager mAssistManager;
123
124 private int mNavigationBarWindowState = WINDOW_STATE_SHOWING;
125
126 private int mNavigationIconHints = 0;
127 private int mNavigationBarMode;
Mike Digman90402952018-01-22 16:05:51 -0800128 private boolean mAccessibilityFeedbackEnabled;
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800129 private AccessibilityManager mAccessibilityManager;
Casey Burkhardt74922c62017-02-13 12:43:16 -0800130 private MagnificationContentObserver mMagnificationObserver;
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700131 private ContentResolver mContentResolver;
Mike Digmanc94759d2018-01-23 11:01:21 -0800132 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500133
134 private int mDisabledFlags1;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500135 private StatusBar mStatusBar;
Jason Monk49fa0162017-01-11 09:21:56 -0500136 private Recents mRecents;
137 private Divider mDivider;
138 private WindowManager mWindowManager;
139 private CommandQueue mCommandQueue;
140 private long mLastLockToAppLongPress;
141
142 private Locale mLocale;
143 private int mLayoutDirection;
144
145 private int mSystemUiVisibility;
146 private LightBarController mLightBarController;
Jason Monk49fa0162017-01-11 09:21:56 -0500147
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800148 private OverviewProxyService mOverviewProxyService;
149
Jason Monk49fa0162017-01-11 09:21:56 -0500150 public boolean mHomeBlockedThisTouch;
151
Mike Digman7d092772018-01-11 12:10:32 -0800152 private int mLastRotationSuggestion;
Mike Digman90402952018-01-22 16:05:51 -0800153 private boolean mHoveringRotationSuggestion;
Mike Digman7d092772018-01-11 12:10:32 -0800154 private RotationLockController mRotationLockController;
155 private TaskStackListenerImpl mTaskStackListener;
156
Mike Digman1e28a5a2018-02-14 10:49:19 -0800157 private final Runnable mRemoveRotationProposal = () -> setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -0800158 private Animator mRotateHideAnimator;
159
Matthew Ng9c3bce52018-02-01 22:00:31 +0000160 private final OverviewProxyListener mOverviewProxyListener = new OverviewProxyListener() {
161 @Override
162 public void onConnectionChanged(boolean isConnected) {
163 mNavigationBarView.onOverviewProxyConnectionChanged(isConnected);
164 updateScreenPinningGestures();
165 }
166
167 @Override
168 public void onRecentsAnimationStarted() {
169 mNavigationBarView.setRecentsAnimationStarted(true);
Mike Digman85a9bea2018-02-23 15:08:53 -0800170
171 // Use navbar dragging as a signal to hide the rotate button
172 setRotateSuggestionButtonState(false);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000173 }
Matthew Ng8f25fb962018-01-16 17:17:24 -0800174
175 @Override
176 public void onInteractionFlagsChanged(@InteractionType int flags) {
177 mNavigationBarView.updateStates();
178 }
Matthew Ng9c3bce52018-02-01 22:00:31 +0000179 };
Mike Digman7d092772018-01-11 12:10:32 -0800180
Jason Monk49fa0162017-01-11 09:21:56 -0500181 // ----- Fragment Lifecycle Callbacks -----
182
183 @Override
184 public void onCreate(@Nullable Bundle savedInstanceState) {
185 super.onCreate(savedInstanceState);
Jason Monk9c7844c2017-01-18 15:21:53 -0500186 mCommandQueue = SysUiServiceProvider.getComponent(getContext(), CommandQueue.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500187 mCommandQueue.addCallbacks(this);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500188 mStatusBar = SysUiServiceProvider.getComponent(getContext(), StatusBar.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500189 mRecents = SysUiServiceProvider.getComponent(getContext(), Recents.class);
190 mDivider = SysUiServiceProvider.getComponent(getContext(), Divider.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500191 mWindowManager = getContext().getSystemService(WindowManager.class);
192 mAccessibilityManager = getContext().getSystemService(AccessibilityManager.class);
Jason Monk91e587e2017-04-13 13:41:23 -0400193 Dependency.get(AccessibilityManagerWrapper.class).addCallback(
194 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700195 mContentResolver = getContext().getContentResolver();
Casey Burkhardt74922c62017-02-13 12:43:16 -0800196 mMagnificationObserver = new MagnificationContentObserver(
197 getContext().getMainThreadHandler());
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700198 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(
Casey Burkhardt74922c62017-02-13 12:43:16 -0800199 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED), false,
Casey Burkhardt5d614402017-04-06 13:46:50 -0700200 mMagnificationObserver, UserHandle.USER_ALL);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800201
Jason Monk49fa0162017-01-11 09:21:56 -0500202 if (savedInstanceState != null) {
203 mDisabledFlags1 = savedInstanceState.getInt(EXTRA_DISABLE_STATE, 0);
204 }
Jason Monk9c7844c2017-01-18 15:21:53 -0500205 mAssistManager = Dependency.get(AssistManager.class);
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800206 mOverviewProxyService = Dependency.get(OverviewProxyService.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500207
208 try {
209 WindowManagerGlobal.getWindowManagerService()
Andrii Kulian35fa3c22017-03-11 09:37:28 -0800210 .watchRotation(mRotationWatcher, getContext().getDisplay().getDisplayId());
Jason Monk49fa0162017-01-11 09:21:56 -0500211 } catch (RemoteException e) {
212 throw e.rethrowFromSystemServer();
213 }
Mike Digman7d092772018-01-11 12:10:32 -0800214
215 mRotationLockController = Dependency.get(RotationLockController.class);
216
217 // Register the task stack listener
218 mTaskStackListener = new TaskStackListenerImpl();
219 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500220 }
221
222 @Override
223 public void onDestroy() {
224 super.onDestroy();
225 mCommandQueue.removeCallbacks(this);
Jason Monk91e587e2017-04-13 13:41:23 -0400226 Dependency.get(AccessibilityManagerWrapper.class).removeCallback(
227 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700228 mContentResolver.unregisterContentObserver(mMagnificationObserver);
Jason Monk49fa0162017-01-11 09:21:56 -0500229 try {
230 WindowManagerGlobal.getWindowManagerService()
231 .removeRotationWatcher(mRotationWatcher);
232 } catch (RemoteException e) {
233 throw e.rethrowFromSystemServer();
234 }
Mike Digman7d092772018-01-11 12:10:32 -0800235
236 // Unregister the task stack listener
237 ActivityManagerWrapper.getInstance().unregisterTaskStackListener(mTaskStackListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500238 }
239
240 @Override
241 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
242 Bundle savedInstanceState) {
243 return inflater.inflate(R.layout.navigation_bar, container, false);
244 }
245
246 @Override
247 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
248 super.onViewCreated(view, savedInstanceState);
249 mNavigationBarView = (NavigationBarView) view;
250
251 mNavigationBarView.setDisabledFlags(mDisabledFlags1);
Matthew Ng78f88d12018-01-23 12:39:55 -0800252 mNavigationBarView.setComponents(mRecents, mDivider, mStatusBar.getPanel());
Jason Monk49fa0162017-01-11 09:21:56 -0500253 mNavigationBarView.setOnVerticalChangedListener(this::onVerticalChanged);
254 mNavigationBarView.setOnTouchListener(this::onNavigationTouch);
255 if (savedInstanceState != null) {
256 mNavigationBarView.getLightTransitionsController().restoreState(savedInstanceState);
257 }
258
259 prepareNavigationBarView();
260 checkNavBarModes();
261
262 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
263 filter.addAction(Intent.ACTION_SCREEN_ON);
264 getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100265 notifyNavigationBarScreenOn();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000266 mOverviewProxyService.addCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500267 }
268
269 @Override
270 public void onDestroyView() {
271 super.onDestroyView();
Jason Monkaa573e92017-01-27 17:00:29 -0500272 mNavigationBarView.getLightTransitionsController().destroy(getContext());
Matthew Ng9c3bce52018-02-01 22:00:31 +0000273 mOverviewProxyService.removeCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500274 getContext().unregisterReceiver(mBroadcastReceiver);
275 }
276
277 @Override
278 public void onSaveInstanceState(Bundle outState) {
279 super.onSaveInstanceState(outState);
280 outState.putInt(EXTRA_DISABLE_STATE, mDisabledFlags1);
281 if (mNavigationBarView != null) {
282 mNavigationBarView.getLightTransitionsController().saveState(outState);
283 }
284 }
285
286 @Override
287 public void onConfigurationChanged(Configuration newConfig) {
288 super.onConfigurationChanged(newConfig);
289 final Locale locale = getContext().getResources().getConfiguration().locale;
290 final int ld = TextUtils.getLayoutDirectionFromLocale(locale);
291 if (!locale.equals(mLocale) || ld != mLayoutDirection) {
292 if (DEBUG) {
293 Log.v(TAG, String.format(
294 "config changed locale/LD: %s (%d) -> %s (%d)", mLocale, mLayoutDirection,
295 locale, ld));
296 }
297 mLocale = locale;
298 mLayoutDirection = ld;
299 refreshLayout(ld);
300 }
301 repositionNavigationBar();
302 }
303
304 @Override
305 public void dump(String prefix, FileDescriptor fd, PrintWriter pw, String[] args) {
306 if (mNavigationBarView != null) {
307 pw.print(" mNavigationBarWindowState=");
308 pw.println(windowStateToString(mNavigationBarWindowState));
309 pw.print(" mNavigationBarMode=");
310 pw.println(BarTransitions.modeToString(mNavigationBarMode));
311 dumpBarTransitions(pw, "mNavigationBarView", mNavigationBarView.getBarTransitions());
312 }
313
314 pw.print(" mNavigationBarView=");
315 if (mNavigationBarView == null) {
316 pw.println("null");
317 } else {
318 mNavigationBarView.dump(fd, pw, args);
319 }
320 }
321
322 // ----- CommandQueue Callbacks -----
323
324 @Override
325 public void setImeWindowStatus(IBinder token, int vis, int backDisposition,
326 boolean showImeSwitcher) {
327 boolean imeShown = (vis & InputMethodService.IME_VISIBLE) != 0;
328 int hints = mNavigationIconHints;
Tarandeep Singh3fecef12018-01-22 14:33:33 -0800329 if (imeShown && backDisposition != InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS) {
Jason Monk49fa0162017-01-11 09:21:56 -0500330 hints |= NAVIGATION_HINT_BACK_ALT;
331 } else {
332 hints &= ~NAVIGATION_HINT_BACK_ALT;
333 }
334 if (showImeSwitcher) {
335 hints |= NAVIGATION_HINT_IME_SHOWN;
336 } else {
337 hints &= ~NAVIGATION_HINT_IME_SHOWN;
338 }
339 if (hints == mNavigationIconHints) return;
340
341 mNavigationIconHints = hints;
342
343 if (mNavigationBarView != null) {
344 mNavigationBarView.setNavigationIconHints(hints);
345 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500346 mStatusBar.checkBarModes();
Jason Monk49fa0162017-01-11 09:21:56 -0500347 }
348
349 @Override
350 public void topAppWindowChanged(boolean showMenu) {
351 if (mNavigationBarView != null) {
352 mNavigationBarView.setMenuVisibility(showMenu);
353 }
354 }
355
356 @Override
357 public void setWindowState(int window, int state) {
358 if (mNavigationBarView != null
359 && window == StatusBarManager.WINDOW_NAVIGATION_BAR
360 && mNavigationBarWindowState != state) {
361 mNavigationBarWindowState = state;
362 if (DEBUG_WINDOW_STATE) Log.d(TAG, "Navigation bar " + windowStateToString(state));
363 }
364 }
365
Mike Digman7d092772018-01-11 12:10:32 -0800366 @Override
Mike Digmane0777312018-01-19 12:41:51 -0800367 public void onRotationProposal(final int rotation, boolean isValid) {
368 // This method will be called on rotation suggestion changes even if the proposed rotation
369 // is not valid for the top app. Use invalid rotation choices as a signal to remove the
370 // rotate button if shown.
371
372 if (!isValid) {
Mike Digman1e28a5a2018-02-14 10:49:19 -0800373 setRotateSuggestionButtonState(false);
Mike Digmane0777312018-01-19 12:41:51 -0800374 return;
375 }
376
Mike Digman1e28a5a2018-02-14 10:49:19 -0800377 final int winRotation = mWindowManager.getDefaultDisplay().getRotation();
378 if (rotation == winRotation) {
Mike Digman7d092772018-01-11 12:10:32 -0800379 // Use this as a signal to remove any current suggestions
Mike Digman90402952018-01-22 16:05:51 -0800380 getView().getHandler().removeCallbacks(mRemoveRotationProposal);
Mike Digman1e28a5a2018-02-14 10:49:19 -0800381 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -0800382 } else {
383 mLastRotationSuggestion = rotation; // Remember rotation for click
Mike Digman1e28a5a2018-02-14 10:49:19 -0800384
385 // Update the icon style to change animation parameters
386 if (mNavigationBarView != null) {
387 final boolean rotationCCW = isRotationAnimationCCW(winRotation, rotation);
388 int style;
389 if (winRotation == Surface.ROTATION_0 || winRotation == Surface.ROTATION_180) {
390 style = rotationCCW ? R.style.RotateButtonCCWStart90 :
391 R.style.RotateButtonCWStart90;
392 } else { // 90 or 270
393 style = rotationCCW ? R.style.RotateButtonCCWStart0 :
394 R.style.RotateButtonCWStart0;
395 }
396 mNavigationBarView.updateRotateSuggestionButtonStyle(style, true);
397 }
398
399 setRotateSuggestionButtonState(true);
Mike Digman90402952018-01-22 16:05:51 -0800400 rescheduleRotationTimeout(false);
Mike Digmanc94759d2018-01-23 11:01:21 -0800401 mMetricsLogger.visible(MetricsEvent.ROTATION_SUGGESTION_SHOWN);
Mike Digman7d092772018-01-11 12:10:32 -0800402 }
403 }
404
Mike Digman1e28a5a2018-02-14 10:49:19 -0800405 private boolean isRotationAnimationCCW(int from, int to) {
406 // All 180deg WM rotation animations are CCW, match that
407 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_90) return false;
408 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_180) return true; //180d so CCW
409 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_270) return true;
410 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_0) return true;
411 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_180) return false;
412 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_270) return true; //180d so CCW
413 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_0) return true; //180d so CCW
414 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_90) return true;
415 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_270) return false;
416 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_0) return false;
417 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_90) return true; //180d so CCW
418 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_180) return true;
419 return false; // Default
Mike Digmana48cf192018-02-12 17:52:48 -0800420 }
421
Mike Digman1e28a5a2018-02-14 10:49:19 -0800422 public void setRotateSuggestionButtonState(final boolean visible) {
423 setRotateSuggestionButtonState(visible, false);
424 }
425
426 public void setRotateSuggestionButtonState(final boolean visible, final boolean force) {
427 if (mNavigationBarView == null) return;
428
429 // At any point the the button can become invisible because an a11y service became active.
430 // Similarly, a call to make the button visible may be rejected because an a11y service is
431 // active. Must account for this.
432
433 ButtonDispatcher rotBtn = mNavigationBarView.getRotateSuggestionButton();
434 final boolean currentlyVisible = mNavigationBarView.isRotateButtonVisible();
435
436 // Rerun a show animation to indicate change but don't rerun a hide animation
437 if (!visible && !currentlyVisible) return;
438
439 View view = rotBtn.getCurrentView();
440 if (view == null) return;
441
442 KeyButtonDrawable kbd = rotBtn.getImageDrawable();
443 if (kbd == null) return;
444
445 // The KBD and AVD is recreated every new valid suggestion because of style changes.
446 AnimatedVectorDrawable animIcon = null;
447 if (kbd.getDrawable(0) instanceof AnimatedVectorDrawable) {
448 animIcon = (AnimatedVectorDrawable) kbd.getDrawable(0);
449 }
450
451 if (visible) { // Appear and change (cannot force)
Mike Digman85a9bea2018-02-23 15:08:53 -0800452 // Stop and clear any currently running hide animations
Mike Digman1e28a5a2018-02-14 10:49:19 -0800453 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
Mike Digman85a9bea2018-02-23 15:08:53 -0800454 mRotateHideAnimator.cancel();
Mike Digman1e28a5a2018-02-14 10:49:19 -0800455 }
Mike Digman85a9bea2018-02-23 15:08:53 -0800456 mRotateHideAnimator = null;
Mike Digman1e28a5a2018-02-14 10:49:19 -0800457
458 // Reset the alpha if any has changed due to hide animation
459 view.setAlpha(1f);
460
461 // Run the rotate icon's animation if it has one
462 if (animIcon != null) {
463 animIcon.reset();
464 animIcon.start();
465 }
466
467 // Set visibility, may fail if a11y service is active.
468 // If invisible, call will stop animation.
469 mNavigationBarView.setRotateButtonVisibility(true);
470
471 } else { // Hide
472
473 if (force) {
474 // If a hide animator is running stop it and make invisible
475 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
476 mRotateHideAnimator.pause();
477 }
478 mNavigationBarView.setRotateButtonVisibility(false);
479 return;
480 }
481
482 // Don't start any new hide animations if one is running
483 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
484
485 ObjectAnimator fadeOut = ObjectAnimator.ofFloat(view, "alpha",
486 0f);
487 fadeOut.setDuration(BUTTON_FADE_IN_OUT_DURATION_MS);
488 fadeOut.setInterpolator(Interpolators.LINEAR);
489 fadeOut.addListener(new AnimatorListenerAdapter() {
490 @Override
491 public void onAnimationEnd(Animator animation) {
492 mNavigationBarView.setRotateButtonVisibility(false);
493 }
494 });
495
496 mRotateHideAnimator = fadeOut;
497 fadeOut.start();
Mike Digmana48cf192018-02-12 17:52:48 -0800498 }
499 }
500
Mike Digman90402952018-01-22 16:05:51 -0800501 private void rescheduleRotationTimeout(final boolean reasonHover) {
502 // May be called due to a new rotation proposal or a change in hover state
503 if (reasonHover) {
504 // Don't reschedule if a hide animator is running
Mike Digman1e28a5a2018-02-14 10:49:19 -0800505 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
Mike Digman90402952018-01-22 16:05:51 -0800506 // Don't reschedule if not visible
Mike Digman1e28a5a2018-02-14 10:49:19 -0800507 if (!mNavigationBarView.isRotateButtonVisible()) return;
Mike Digman90402952018-01-22 16:05:51 -0800508 }
509
510 Handler h = getView().getHandler();
511 h.removeCallbacks(mRemoveRotationProposal); // Stop any pending removal
512 h.postDelayed(mRemoveRotationProposal,
513 computeRotationProposalTimeout()); // Schedule timeout
514 }
515
516 private int computeRotationProposalTimeout() {
517 if (mAccessibilityFeedbackEnabled) return 20000;
518 if (mHoveringRotationSuggestion) return 16000;
519 return 6000;
520 }
521
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500522 // Injected from StatusBar at creation.
Jason Monk49fa0162017-01-11 09:21:56 -0500523 public void setCurrentSysuiVisibility(int systemUiVisibility) {
524 mSystemUiVisibility = systemUiVisibility;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500525 mNavigationBarMode = mStatusBar.computeBarMode(0, mSystemUiVisibility,
Jason Monk49fa0162017-01-11 09:21:56 -0500526 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
527 View.NAVIGATION_BAR_TRANSPARENT);
528 checkNavBarModes();
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500529 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500530 mLightBarController.onNavigationVisibilityChanged(mSystemUiVisibility, 0 /* mask */,
531 true /* nbModeChanged */, mNavigationBarMode);
532 }
533
534 @Override
535 public void setSystemUiVisibility(int vis, int fullscreenStackVis, int dockedStackVis,
536 int mask, Rect fullscreenStackBounds, Rect dockedStackBounds) {
537 final int oldVal = mSystemUiVisibility;
538 final int newVal = (oldVal & ~mask) | (vis & mask);
539 final int diff = newVal ^ oldVal;
540 boolean nbModeChanged = false;
541 if (diff != 0) {
542 mSystemUiVisibility = newVal;
543
544 // update navigation bar mode
545 final int nbMode = getView() == null
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500546 ? -1 : mStatusBar.computeBarMode(oldVal, newVal,
Jason Monk49fa0162017-01-11 09:21:56 -0500547 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
548 View.NAVIGATION_BAR_TRANSPARENT);
549 nbModeChanged = nbMode != -1;
550 if (nbModeChanged) {
551 if (mNavigationBarMode != nbMode) {
552 mNavigationBarMode = nbMode;
553 checkNavBarModes();
554 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500555 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500556 }
557 }
558
559 mLightBarController.onNavigationVisibilityChanged(vis, mask, nbModeChanged,
560 mNavigationBarMode);
561 }
562
563 @Override
564 public void disable(int state1, int state2, boolean animate) {
565 // All navigation bar flags are in state1.
566 int masked = state1 & (StatusBarManager.DISABLE_HOME
567 | StatusBarManager.DISABLE_RECENT
568 | StatusBarManager.DISABLE_BACK
569 | StatusBarManager.DISABLE_SEARCH);
570 if (masked != mDisabledFlags1) {
571 mDisabledFlags1 = masked;
572 if (mNavigationBarView != null) mNavigationBarView.setDisabledFlags(state1);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000573 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500574 }
575 }
576
577 // ----- Internal stuffz -----
578
579 private void refreshLayout(int layoutDirection) {
580 if (mNavigationBarView != null) {
581 mNavigationBarView.setLayoutDirection(layoutDirection);
582 }
583 }
584
585 private boolean shouldDisableNavbarGestures() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500586 return !mStatusBar.isDeviceProvisioned()
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800587 || (mDisabledFlags1 & StatusBarManager.DISABLE_SEARCH) != 0
Matthew Ng9c3bce52018-02-01 22:00:31 +0000588 || mNavigationBarView.getRecentsButton().getVisibility() != View.VISIBLE;
Jason Monk49fa0162017-01-11 09:21:56 -0500589 }
590
591 private void repositionNavigationBar() {
592 if (mNavigationBarView == null || !mNavigationBarView.isAttachedToWindow()) return;
593
594 prepareNavigationBarView();
595
596 mWindowManager.updateViewLayout((View) mNavigationBarView.getParent(),
597 ((View) mNavigationBarView.getParent()).getLayoutParams());
598 }
599
Matthew Ng9c3bce52018-02-01 22:00:31 +0000600 private void updateScreenPinningGestures() {
601 if (mNavigationBarView == null) {
602 return;
603 }
604
605 // Change the cancel pin gesture to home and back if recents button is invisible
606 boolean recentsVisible = mNavigationBarView.isRecentsButtonVisible();
607 ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();
608 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
609 if (recentsVisible) {
610 homeButton.setOnLongClickListener(this::onHomeLongClick);
611 backButton.setOnLongClickListener(this::onLongPressBackRecents);
612 } else {
613 homeButton.setOnLongClickListener(this::onLongPressBackHome);
614 backButton.setOnLongClickListener(this::onLongPressBackHome);
615 }
616 }
617
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100618 private void notifyNavigationBarScreenOn() {
619 mNavigationBarView.notifyScreenOn();
Jason Monk49fa0162017-01-11 09:21:56 -0500620 }
621
622 private void prepareNavigationBarView() {
623 mNavigationBarView.reorient();
624
625 ButtonDispatcher recentsButton = mNavigationBarView.getRecentsButton();
626 recentsButton.setOnClickListener(this::onRecentsClick);
627 recentsButton.setOnTouchListener(this::onRecentsTouch);
628 recentsButton.setLongClickable(true);
629 recentsButton.setOnLongClickListener(this::onLongPressBackRecents);
630
631 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
632 backButton.setLongClickable(true);
Jason Monk49fa0162017-01-11 09:21:56 -0500633
634 ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();
635 homeButton.setOnTouchListener(this::onHomeTouch);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800636
637 ButtonDispatcher accessibilityButton = mNavigationBarView.getAccessibilityButton();
638 accessibilityButton.setOnClickListener(this::onAccessibilityClick);
639 accessibilityButton.setOnLongClickListener(this::onAccessibilityLongClick);
Phil Weaverdb9a7742017-04-18 08:15:06 -0700640 updateAccessibilityServicesState(mAccessibilityManager);
Mike Digman7d092772018-01-11 12:10:32 -0800641
642 ButtonDispatcher rotateSuggestionButton = mNavigationBarView.getRotateSuggestionButton();
643 rotateSuggestionButton.setOnClickListener(this::onRotateSuggestionClick);
Mike Digman90402952018-01-22 16:05:51 -0800644 rotateSuggestionButton.setOnHoverListener(this::onRotateSuggestionHover);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000645 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500646 }
647
648 private boolean onHomeTouch(View v, MotionEvent event) {
649 if (mHomeBlockedThisTouch && event.getActionMasked() != MotionEvent.ACTION_DOWN) {
650 return true;
651 }
652 // If an incoming call is ringing, HOME is totally disabled.
653 // (The user is already on the InCallUI at this point,
654 // and his ONLY options are to answer or reject the call.)
655 switch (event.getAction()) {
656 case MotionEvent.ACTION_DOWN:
657 mHomeBlockedThisTouch = false;
658 TelecomManager telecomManager =
659 getContext().getSystemService(TelecomManager.class);
660 if (telecomManager != null && telecomManager.isRinging()) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500661 if (mStatusBar.isKeyguardShowing()) {
Jason Monk49fa0162017-01-11 09:21:56 -0500662 Log.i(TAG, "Ignoring HOME; there's a ringing incoming call. " +
663 "No heads up");
664 mHomeBlockedThisTouch = true;
665 return true;
666 }
667 }
668 break;
669 case MotionEvent.ACTION_UP:
670 case MotionEvent.ACTION_CANCEL:
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500671 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500672 break;
673 }
674 return false;
675 }
676
677 private void onVerticalChanged(boolean isVertical) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500678 mStatusBar.setQsScrimEnabled(!isVertical);
Jason Monk49fa0162017-01-11 09:21:56 -0500679 }
680
681 private boolean onNavigationTouch(View v, MotionEvent event) {
Eliot Courtneycb5d3162017-08-09 16:53:15 +0900682 mStatusBar.checkUserAutohide(event);
Jason Monk49fa0162017-01-11 09:21:56 -0500683 return false;
684 }
685
Jason Monk865246d2017-01-19 08:27:01 -0500686 @VisibleForTesting
687 boolean onHomeLongClick(View v) {
Jason Monk49fa0162017-01-11 09:21:56 -0500688 if (shouldDisableNavbarGestures()) {
689 return false;
690 }
Mike Digmanc94759d2018-01-23 11:01:21 -0800691 mMetricsLogger.action(MetricsEvent.ACTION_ASSIST_LONG_PRESS);
Jason Monk49fa0162017-01-11 09:21:56 -0500692 mAssistManager.startAssist(new Bundle() /* args */);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500693 mStatusBar.awakenDreams();
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800694
Jason Monk49fa0162017-01-11 09:21:56 -0500695 if (mNavigationBarView != null) {
696 mNavigationBarView.abortCurrentGesture();
697 }
698 return true;
699 }
700
701 // additional optimization when we have software system buttons - start loading the recent
702 // tasks on touch down
703 private boolean onRecentsTouch(View v, MotionEvent event) {
704 int action = event.getAction() & MotionEvent.ACTION_MASK;
705 if (action == MotionEvent.ACTION_DOWN) {
706 mCommandQueue.preloadRecentApps();
707 } else if (action == MotionEvent.ACTION_CANCEL) {
708 mCommandQueue.cancelPreloadRecentApps();
709 } else if (action == MotionEvent.ACTION_UP) {
710 if (!v.isPressed()) {
711 mCommandQueue.cancelPreloadRecentApps();
712 }
713 }
714 return false;
715 }
716
717 private void onRecentsClick(View v) {
718 if (LatencyTracker.isEnabled(getContext())) {
719 LatencyTracker.getInstance(getContext()).onActionStart(
720 LatencyTracker.ACTION_TOGGLE_RECENTS);
721 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500722 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500723 mCommandQueue.toggleRecentApps();
724 }
725
Matthew Ng9c3bce52018-02-01 22:00:31 +0000726 private boolean onLongPressBackHome(View v) {
727 return onLongPressNavigationButtons(v, R.id.back, R.id.home);
728 }
729
730 private boolean onLongPressBackRecents(View v) {
731 return onLongPressNavigationButtons(v, R.id.back, R.id.recent_apps);
732 }
733
Jason Monk49fa0162017-01-11 09:21:56 -0500734 /**
Matthew Ng9c3bce52018-02-01 22:00:31 +0000735 * This handles long-press of both back and recents/home. Back is the common button with
736 * combination of recents if it is visible or home if recents is invisible.
737 * They are handled together to capture them both being long-pressed
Jason Monk49fa0162017-01-11 09:21:56 -0500738 * at the same time to exit screen pinning (lock task).
739 *
Matthew Ng9c3bce52018-02-01 22:00:31 +0000740 * When accessibility mode is on, only a long-press from recents/home
Jason Monk49fa0162017-01-11 09:21:56 -0500741 * is required to exit.
742 *
743 * In all other circumstances we try to pass through long-press events
744 * for Back, so that apps can still use it. Which can be from two things.
745 * 1) Not currently in screen pinning (lock task).
Matthew Ng9c3bce52018-02-01 22:00:31 +0000746 * 2) Back is long-pressed without recents/home.
Jason Monk49fa0162017-01-11 09:21:56 -0500747 */
Matthew Ng9c3bce52018-02-01 22:00:31 +0000748 private boolean onLongPressNavigationButtons(View v, @IdRes int btnId1, @IdRes int btnId2) {
Jason Monk49fa0162017-01-11 09:21:56 -0500749 try {
750 boolean sendBackLongPress = false;
751 IActivityManager activityManager = ActivityManagerNative.getDefault();
752 boolean touchExplorationEnabled = mAccessibilityManager.isTouchExplorationEnabled();
753 boolean inLockTaskMode = activityManager.isInLockTaskMode();
754 if (inLockTaskMode && !touchExplorationEnabled) {
755 long time = System.currentTimeMillis();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000756
Jason Monk49fa0162017-01-11 09:21:56 -0500757 // If we recently long-pressed the other button then they were
758 // long-pressed 'together'
759 if ((time - mLastLockToAppLongPress) < LOCK_TO_APP_GESTURE_TOLERENCE) {
Benjamin Franza83859f2017-07-03 16:34:14 +0100760 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500761 // When exiting refresh disabled flags.
762 mNavigationBarView.setDisabledFlags(mDisabledFlags1, true);
763 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000764 } else if (v.getId() == btnId1) {
765 ButtonDispatcher button = btnId2 == R.id.recent_apps
766 ? mNavigationBarView.getRecentsButton()
767 : mNavigationBarView.getHomeButton();
768 if (!button.getCurrentView().isPressed()) {
769 // If we aren't pressing recents/home right now then they presses
770 // won't be together, so send the standard long-press action.
771 sendBackLongPress = true;
772 }
Jason Monk49fa0162017-01-11 09:21:56 -0500773 }
774 mLastLockToAppLongPress = time;
775 } else {
776 // If this is back still need to handle sending the long-press event.
Matthew Ng9c3bce52018-02-01 22:00:31 +0000777 if (v.getId() == btnId1) {
Jason Monk49fa0162017-01-11 09:21:56 -0500778 sendBackLongPress = true;
779 } else if (touchExplorationEnabled && inLockTaskMode) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000780 // When in accessibility mode a long press that is recents/home (not back)
Jason Monk49fa0162017-01-11 09:21:56 -0500781 // should stop lock task.
Benjamin Franza83859f2017-07-03 16:34:14 +0100782 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500783 // When exiting refresh disabled flags.
784 mNavigationBarView.setDisabledFlags(mDisabledFlags1, true);
785 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000786 } else if (v.getId() == btnId2) {
787 return btnId2 == R.id.recent_apps
788 ? onLongPressRecents()
789 : onHomeLongClick(mNavigationBarView.getHomeButton().getCurrentView());
Jason Monk49fa0162017-01-11 09:21:56 -0500790 }
791 }
792 if (sendBackLongPress) {
793 KeyButtonView keyButtonView = (KeyButtonView) v;
794 keyButtonView.sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS);
795 keyButtonView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
796 return true;
797 }
798 } catch (RemoteException e) {
799 Log.d(TAG, "Unable to reach activity manager", e);
800 }
801 return false;
802 }
803
804 private boolean onLongPressRecents() {
Erik Wolsheimer9be3a062017-05-31 14:59:57 -0700805 if (mRecents == null || !ActivityManager.supportsMultiWindow(getContext())
Matthew Ng43db6d22017-06-27 15:29:39 -0700806 || !mDivider.getView().getSnapAlgorithm().isSplitScreenFeasible()
807 || Recents.getConfiguration().isLowRamDevice) {
Jason Monk49fa0162017-01-11 09:21:56 -0500808 return false;
809 }
810
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500811 return mStatusBar.toggleSplitScreenMode(MetricsEvent.ACTION_WINDOW_DOCK_LONGPRESS,
Jason Monk49fa0162017-01-11 09:21:56 -0500812 MetricsEvent.ACTION_WINDOW_UNDOCK_LONGPRESS);
813 }
814
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800815 private void onAccessibilityClick(View v) {
816 mAccessibilityManager.notifyAccessibilityButtonClicked();
817 }
818
819 private boolean onAccessibilityLongClick(View v) {
Casey Burkhardt5e8b9802017-03-24 10:07:20 -0700820 Intent intent = new Intent(AccessibilityManager.ACTION_CHOOSE_ACCESSIBILITY_BUTTON);
821 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Casey Burkhardt5d614402017-04-06 13:46:50 -0700822 v.getContext().startActivityAsUser(intent, UserHandle.CURRENT);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800823 return true;
824 }
825
Phil Weaverdb9a7742017-04-18 08:15:06 -0700826 private void updateAccessibilityServicesState(AccessibilityManager accessibilityManager) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800827 int requestingServices = 0;
828 try {
Casey Burkhardt5d614402017-04-06 13:46:50 -0700829 if (Settings.Secure.getIntForUser(mContentResolver,
830 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED,
831 UserHandle.USER_CURRENT) == 1) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800832 requestingServices++;
833 }
834 } catch (Settings.SettingNotFoundException e) {
835 }
836
Mike Digman90402952018-01-22 16:05:51 -0800837 boolean feedbackEnabled = false;
Casey Burkhardt5d614402017-04-06 13:46:50 -0700838 // AccessibilityManagerService resolves services for the current user since the local
839 // AccessibilityManager is created from a Context with the INTERACT_ACROSS_USERS permission
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800840 final List<AccessibilityServiceInfo> services =
Phil Weaverdb9a7742017-04-18 08:15:06 -0700841 accessibilityManager.getEnabledAccessibilityServiceList(
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800842 AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800843 for (int i = services.size() - 1; i >= 0; --i) {
844 AccessibilityServiceInfo info = services.get(i);
845 if ((info.flags & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0) {
846 requestingServices++;
847 }
Mike Digman90402952018-01-22 16:05:51 -0800848
849 if (info.feedbackType != 0 && info.feedbackType !=
850 AccessibilityServiceInfo.FEEDBACK_GENERIC) {
851 feedbackEnabled = true;
852 }
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800853 }
854
Mike Digman90402952018-01-22 16:05:51 -0800855 mAccessibilityFeedbackEnabled = feedbackEnabled;
856
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800857 final boolean showAccessibilityButton = requestingServices >= 1;
858 final boolean targetSelection = requestingServices >= 2;
859 mNavigationBarView.setAccessibilityButtonState(showAccessibilityButton, targetSelection);
860 }
861
Mike Digman7d092772018-01-11 12:10:32 -0800862 private void onRotateSuggestionClick(View v) {
Mike Digmanc94759d2018-01-23 11:01:21 -0800863 mMetricsLogger.action(MetricsEvent.ACTION_ROTATION_SUGGESTION_ACCEPTED);
Mike Digman7d092772018-01-11 12:10:32 -0800864 mRotationLockController.setRotationLockedAtAngle(true, mLastRotationSuggestion);
865 }
866
Mike Digman90402952018-01-22 16:05:51 -0800867 private boolean onRotateSuggestionHover(View v, MotionEvent event) {
868 final int action = event.getActionMasked();
869 mHoveringRotationSuggestion = (action == MotionEvent.ACTION_HOVER_ENTER)
870 || (action == MotionEvent.ACTION_HOVER_MOVE);
871 rescheduleRotationTimeout(true);
872 return false; // Must return false so a11y hover events are dispatched correctly.
873 }
874
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500875 // ----- Methods that StatusBar talks to (should be minimized) -----
Jason Monk49fa0162017-01-11 09:21:56 -0500876
Jason Monk49fa0162017-01-11 09:21:56 -0500877 public void setLightBarController(LightBarController lightBarController) {
878 mLightBarController = lightBarController;
879 mLightBarController.setNavigationBar(mNavigationBarView.getLightTransitionsController());
880 }
881
882 public boolean isSemiTransparent() {
883 return mNavigationBarMode == MODE_SEMI_TRANSPARENT;
884 }
885
Jason Monk49fa0162017-01-11 09:21:56 -0500886 public void disableAnimationsDuringHide(long delay) {
887 mNavigationBarView.setLayoutTransitionsEnabled(false);
888 mNavigationBarView.postDelayed(() -> mNavigationBarView.setLayoutTransitionsEnabled(true),
889 delay + StackStateAnimator.ANIMATION_DURATION_GO_TO_FULL_SHADE);
890 }
891
Jason Monk49fa0162017-01-11 09:21:56 -0500892 public BarTransitions getBarTransitions() {
893 return mNavigationBarView.getBarTransitions();
894 }
895
896 public void checkNavBarModes() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500897 mStatusBar.checkBarMode(mNavigationBarMode,
Jason Monk49fa0162017-01-11 09:21:56 -0500898 mNavigationBarWindowState, mNavigationBarView.getBarTransitions());
899 }
900
901 public void finishBarAnimations() {
902 mNavigationBarView.getBarTransitions().finishAnimations();
903 }
904
Jason Monk91e587e2017-04-13 13:41:23 -0400905 private final AccessibilityServicesStateChangeListener mAccessibilityListener =
906 this::updateAccessibilityServicesState;
907
Casey Burkhardt74922c62017-02-13 12:43:16 -0800908 private class MagnificationContentObserver extends ContentObserver {
909
910 public MagnificationContentObserver(Handler handler) {
911 super(handler);
912 }
913
914 @Override
915 public void onChange(boolean selfChange) {
Phil Weaverdb9a7742017-04-18 08:15:06 -0700916 NavigationBarFragment.this.updateAccessibilityServicesState(mAccessibilityManager);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800917 }
918 }
919
Jason Monk49fa0162017-01-11 09:21:56 -0500920 private final Stub mRotationWatcher = new Stub() {
921 @Override
Mike Digman90402952018-01-22 16:05:51 -0800922 public void onRotationChanged(final int rotation) throws RemoteException {
Jason Monk49fa0162017-01-11 09:21:56 -0500923 // We need this to be scheduled as early as possible to beat the redrawing of
924 // window in response to the orientation change.
925 Handler h = getView().getHandler();
926 Message msg = Message.obtain(h, () -> {
Mike Digman85ff7fa2018-01-23 14:59:52 -0800927
928 // If the screen rotation changes while locked, potentially update lock to flow with
Mike Digman90402952018-01-22 16:05:51 -0800929 // new screen rotation and hide any showing suggestions.
930 if (mRotationLockController.isRotationLocked()) {
Mike Digman85ff7fa2018-01-23 14:59:52 -0800931 if (shouldOverrideUserLockPrefs(rotation)) {
932 mRotationLockController.setRotationLockedAtAngle(true, rotation);
933 }
Mike Digman1e28a5a2018-02-14 10:49:19 -0800934 setRotateSuggestionButtonState(false, true);
Mike Digman90402952018-01-22 16:05:51 -0800935 }
936
Jason Monk49fa0162017-01-11 09:21:56 -0500937 if (mNavigationBarView != null
938 && mNavigationBarView.needsReorient(rotation)) {
939 repositionNavigationBar();
940 }
941 });
942 msg.setAsynchronous(true);
943 h.sendMessageAtFrontOfQueue(msg);
944 }
Mike Digman85ff7fa2018-01-23 14:59:52 -0800945
946 private boolean shouldOverrideUserLockPrefs(final int rotation) {
947 // Only override user prefs when returning to portrait.
948 // Don't let apps that force landscape or 180 alter user lock.
949 return rotation == Surface.ROTATION_0;
950 }
Jason Monk49fa0162017-01-11 09:21:56 -0500951 };
952
953 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
954 @Override
955 public void onReceive(Context context, Intent intent) {
956 String action = intent.getAction();
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100957 if (Intent.ACTION_SCREEN_OFF.equals(action)
958 || Intent.ACTION_SCREEN_ON.equals(action)) {
959 notifyNavigationBarScreenOn();
Jason Monk49fa0162017-01-11 09:21:56 -0500960 }
961 }
962 };
963
Mike Digman7d092772018-01-11 12:10:32 -0800964 class TaskStackListenerImpl extends SysUiTaskStackChangeListener {
965 // Invalidate any rotation suggestion on task change or activity orientation change
966 // Note: all callbacks happen on main thread
967
968 @Override
969 public void onTaskStackChanged() {
Mike Digman1e28a5a2018-02-14 10:49:19 -0800970 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -0800971 }
972
973 @Override
974 public void onTaskRemoved(int taskId) {
Mike Digman1e28a5a2018-02-14 10:49:19 -0800975 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -0800976 }
977
978 @Override
979 public void onTaskMovedToFront(int taskId) {
Mike Digman1e28a5a2018-02-14 10:49:19 -0800980 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -0800981 }
982
983 @Override
984 public void onActivityRequestedOrientationChanged(int taskId, int requestedOrientation) {
Mike Digman85a9bea2018-02-23 15:08:53 -0800985 // Only hide the icon if the top task changes its requestedOrientation
986 // Launcher can alter its requestedOrientation while it's not on top, don't hide on this
987 final boolean top = ActivityManagerWrapper.getInstance().getRunningTask().id == taskId;
988 if (top) setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -0800989 }
990 }
991
Jason Monk49fa0162017-01-11 09:21:56 -0500992 public static View create(Context context, FragmentListener listener) {
993 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
994 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
995 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR,
996 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
997 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
998 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
999 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1000 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
1001 | WindowManager.LayoutParams.FLAG_SLIPPERY,
1002 PixelFormat.TRANSLUCENT);
1003 lp.token = new Binder();
Jason Monk49fa0162017-01-11 09:21:56 -05001004 lp.setTitle("NavigationBar");
Phil Weaver8583ae82018-02-13 11:01:24 -08001005 lp.accessibilityTitle = context.getString(R.string.nav_bar);
Jason Monk49fa0162017-01-11 09:21:56 -05001006 lp.windowAnimations = 0;
1007
1008 View navigationBarView = LayoutInflater.from(context).inflate(
1009 R.layout.navigation_bar_window, null);
1010
1011 if (DEBUG) Log.v(TAG, "addNavigationBar: about to add " + navigationBarView);
1012 if (navigationBarView == null) return null;
1013
1014 context.getSystemService(WindowManager.class).addView(navigationBarView, lp);
1015 FragmentHostManager fragmentHost = FragmentHostManager.get(navigationBarView);
1016 NavigationBarFragment fragment = new NavigationBarFragment();
1017 fragmentHost.getFragmentManager().beginTransaction()
1018 .replace(R.id.navigation_bar_frame, fragment, TAG)
1019 .commit();
1020 fragmentHost.addTagListener(TAG, listener);
1021 return navigationBarView;
1022 }
1023}