blob: 0ed69e66b03eb2bb3fb6672491252e6039128f92 [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
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 Digman90402952018-01-22 16:05:51 -0800155 private boolean mHoveringRotationSuggestion;
Mike Digman7d092772018-01-11 12:10:32 -0800156 private RotationLockController mRotationLockController;
157 private TaskStackListenerImpl mTaskStackListener;
158
Mike Digman1e28a5a2018-02-14 10:49:19 -0800159 private final Runnable mRemoveRotationProposal = () -> setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -0800160 private Animator mRotateHideAnimator;
Mike Digman50752642018-02-15 13:36:09 -0800161 private ViewRippler mViewRippler = new ViewRippler();
Mike Digman7d092772018-01-11 12:10:32 -0800162
Matthew Ng9c3bce52018-02-01 22:00:31 +0000163 private final OverviewProxyListener mOverviewProxyListener = new OverviewProxyListener() {
164 @Override
165 public void onConnectionChanged(boolean isConnected) {
166 mNavigationBarView.onOverviewProxyConnectionChanged(isConnected);
167 updateScreenPinningGestures();
168 }
169
170 @Override
171 public void onRecentsAnimationStarted() {
172 mNavigationBarView.setRecentsAnimationStarted(true);
Mike Digman85a9bea2018-02-23 15:08:53 -0800173
174 // Use navbar dragging as a signal to hide the rotate button
175 setRotateSuggestionButtonState(false);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000176 }
Matthew Ng8f25fb962018-01-16 17:17:24 -0800177
178 @Override
179 public void onInteractionFlagsChanged(@InteractionType int flags) {
180 mNavigationBarView.updateStates();
181 }
Matthew Ng9c3bce52018-02-01 22:00:31 +0000182 };
Mike Digman7d092772018-01-11 12:10:32 -0800183
Jason Monk49fa0162017-01-11 09:21:56 -0500184 // ----- Fragment Lifecycle Callbacks -----
185
186 @Override
187 public void onCreate(@Nullable Bundle savedInstanceState) {
188 super.onCreate(savedInstanceState);
Jason Monk9c7844c2017-01-18 15:21:53 -0500189 mCommandQueue = SysUiServiceProvider.getComponent(getContext(), CommandQueue.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500190 mCommandQueue.addCallbacks(this);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500191 mStatusBar = SysUiServiceProvider.getComponent(getContext(), StatusBar.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500192 mRecents = SysUiServiceProvider.getComponent(getContext(), Recents.class);
193 mDivider = SysUiServiceProvider.getComponent(getContext(), Divider.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500194 mWindowManager = getContext().getSystemService(WindowManager.class);
195 mAccessibilityManager = getContext().getSystemService(AccessibilityManager.class);
Jason Monk91e587e2017-04-13 13:41:23 -0400196 Dependency.get(AccessibilityManagerWrapper.class).addCallback(
197 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700198 mContentResolver = getContext().getContentResolver();
Casey Burkhardt74922c62017-02-13 12:43:16 -0800199 mMagnificationObserver = new MagnificationContentObserver(
200 getContext().getMainThreadHandler());
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700201 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(
Casey Burkhardt74922c62017-02-13 12:43:16 -0800202 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED), false,
Casey Burkhardt5d614402017-04-06 13:46:50 -0700203 mMagnificationObserver, UserHandle.USER_ALL);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800204
Jason Monk49fa0162017-01-11 09:21:56 -0500205 if (savedInstanceState != null) {
206 mDisabledFlags1 = savedInstanceState.getInt(EXTRA_DISABLE_STATE, 0);
207 }
Jason Monk9c7844c2017-01-18 15:21:53 -0500208 mAssistManager = Dependency.get(AssistManager.class);
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800209 mOverviewProxyService = Dependency.get(OverviewProxyService.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500210
211 try {
212 WindowManagerGlobal.getWindowManagerService()
Andrii Kulian35fa3c22017-03-11 09:37:28 -0800213 .watchRotation(mRotationWatcher, getContext().getDisplay().getDisplayId());
Jason Monk49fa0162017-01-11 09:21:56 -0500214 } catch (RemoteException e) {
215 throw e.rethrowFromSystemServer();
216 }
Mike Digman7d092772018-01-11 12:10:32 -0800217
218 mRotationLockController = Dependency.get(RotationLockController.class);
219
220 // Register the task stack listener
221 mTaskStackListener = new TaskStackListenerImpl();
222 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500223 }
224
225 @Override
226 public void onDestroy() {
227 super.onDestroy();
228 mCommandQueue.removeCallbacks(this);
Jason Monk91e587e2017-04-13 13:41:23 -0400229 Dependency.get(AccessibilityManagerWrapper.class).removeCallback(
230 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700231 mContentResolver.unregisterContentObserver(mMagnificationObserver);
Jason Monk49fa0162017-01-11 09:21:56 -0500232 try {
233 WindowManagerGlobal.getWindowManagerService()
234 .removeRotationWatcher(mRotationWatcher);
235 } catch (RemoteException e) {
236 throw e.rethrowFromSystemServer();
237 }
Mike Digman7d092772018-01-11 12:10:32 -0800238
239 // Unregister the task stack listener
240 ActivityManagerWrapper.getInstance().unregisterTaskStackListener(mTaskStackListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500241 }
242
243 @Override
244 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
245 Bundle savedInstanceState) {
246 return inflater.inflate(R.layout.navigation_bar, container, false);
247 }
248
249 @Override
250 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
251 super.onViewCreated(view, savedInstanceState);
252 mNavigationBarView = (NavigationBarView) view;
253
254 mNavigationBarView.setDisabledFlags(mDisabledFlags1);
Matthew Ng78f88d12018-01-23 12:39:55 -0800255 mNavigationBarView.setComponents(mRecents, mDivider, mStatusBar.getPanel());
Jason Monk49fa0162017-01-11 09:21:56 -0500256 mNavigationBarView.setOnVerticalChangedListener(this::onVerticalChanged);
257 mNavigationBarView.setOnTouchListener(this::onNavigationTouch);
258 if (savedInstanceState != null) {
259 mNavigationBarView.getLightTransitionsController().restoreState(savedInstanceState);
260 }
261
262 prepareNavigationBarView();
263 checkNavBarModes();
264
265 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
266 filter.addAction(Intent.ACTION_SCREEN_ON);
267 getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100268 notifyNavigationBarScreenOn();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000269 mOverviewProxyService.addCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500270 }
271
272 @Override
273 public void onDestroyView() {
274 super.onDestroyView();
Jason Monkaa573e92017-01-27 17:00:29 -0500275 mNavigationBarView.getLightTransitionsController().destroy(getContext());
Matthew Ng9c3bce52018-02-01 22:00:31 +0000276 mOverviewProxyService.removeCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500277 getContext().unregisterReceiver(mBroadcastReceiver);
278 }
279
280 @Override
281 public void onSaveInstanceState(Bundle outState) {
282 super.onSaveInstanceState(outState);
283 outState.putInt(EXTRA_DISABLE_STATE, mDisabledFlags1);
284 if (mNavigationBarView != null) {
285 mNavigationBarView.getLightTransitionsController().saveState(outState);
286 }
287 }
288
289 @Override
290 public void onConfigurationChanged(Configuration newConfig) {
291 super.onConfigurationChanged(newConfig);
292 final Locale locale = getContext().getResources().getConfiguration().locale;
293 final int ld = TextUtils.getLayoutDirectionFromLocale(locale);
294 if (!locale.equals(mLocale) || ld != mLayoutDirection) {
295 if (DEBUG) {
296 Log.v(TAG, String.format(
297 "config changed locale/LD: %s (%d) -> %s (%d)", mLocale, mLayoutDirection,
298 locale, ld));
299 }
300 mLocale = locale;
301 mLayoutDirection = ld;
302 refreshLayout(ld);
303 }
304 repositionNavigationBar();
305 }
306
307 @Override
308 public void dump(String prefix, FileDescriptor fd, PrintWriter pw, String[] args) {
309 if (mNavigationBarView != null) {
310 pw.print(" mNavigationBarWindowState=");
311 pw.println(windowStateToString(mNavigationBarWindowState));
312 pw.print(" mNavigationBarMode=");
313 pw.println(BarTransitions.modeToString(mNavigationBarMode));
314 dumpBarTransitions(pw, "mNavigationBarView", mNavigationBarView.getBarTransitions());
315 }
316
317 pw.print(" mNavigationBarView=");
318 if (mNavigationBarView == null) {
319 pw.println("null");
320 } else {
321 mNavigationBarView.dump(fd, pw, args);
322 }
323 }
324
325 // ----- CommandQueue Callbacks -----
326
327 @Override
328 public void setImeWindowStatus(IBinder token, int vis, int backDisposition,
329 boolean showImeSwitcher) {
330 boolean imeShown = (vis & InputMethodService.IME_VISIBLE) != 0;
331 int hints = mNavigationIconHints;
Tarandeep Singh3fecef12018-01-22 14:33:33 -0800332 if (imeShown && backDisposition != InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS) {
Jason Monk49fa0162017-01-11 09:21:56 -0500333 hints |= NAVIGATION_HINT_BACK_ALT;
334 } else {
335 hints &= ~NAVIGATION_HINT_BACK_ALT;
336 }
337 if (showImeSwitcher) {
338 hints |= NAVIGATION_HINT_IME_SHOWN;
339 } else {
340 hints &= ~NAVIGATION_HINT_IME_SHOWN;
341 }
342 if (hints == mNavigationIconHints) return;
343
344 mNavigationIconHints = hints;
345
346 if (mNavigationBarView != null) {
347 mNavigationBarView.setNavigationIconHints(hints);
348 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500349 mStatusBar.checkBarModes();
Jason Monk49fa0162017-01-11 09:21:56 -0500350 }
351
352 @Override
353 public void topAppWindowChanged(boolean showMenu) {
354 if (mNavigationBarView != null) {
355 mNavigationBarView.setMenuVisibility(showMenu);
356 }
357 }
358
359 @Override
360 public void setWindowState(int window, int state) {
361 if (mNavigationBarView != null
362 && window == StatusBarManager.WINDOW_NAVIGATION_BAR
363 && mNavigationBarWindowState != state) {
364 mNavigationBarWindowState = state;
365 if (DEBUG_WINDOW_STATE) Log.d(TAG, "Navigation bar " + windowStateToString(state));
366 }
367 }
368
Mike Digman7d092772018-01-11 12:10:32 -0800369 @Override
Mike Digmane0777312018-01-19 12:41:51 -0800370 public void onRotationProposal(final int rotation, boolean isValid) {
371 // This method will be called on rotation suggestion changes even if the proposed rotation
372 // is not valid for the top app. Use invalid rotation choices as a signal to remove the
373 // rotate button if shown.
374
375 if (!isValid) {
Mike Digman1e28a5a2018-02-14 10:49:19 -0800376 setRotateSuggestionButtonState(false);
Mike Digmane0777312018-01-19 12:41:51 -0800377 return;
378 }
379
Mike Digman1e28a5a2018-02-14 10:49:19 -0800380 final int winRotation = mWindowManager.getDefaultDisplay().getRotation();
381 if (rotation == winRotation) {
Mike Digman7d092772018-01-11 12:10:32 -0800382 // Use this as a signal to remove any current suggestions
Mike Digman90402952018-01-22 16:05:51 -0800383 getView().getHandler().removeCallbacks(mRemoveRotationProposal);
Mike Digman1e28a5a2018-02-14 10:49:19 -0800384 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -0800385 } else {
386 mLastRotationSuggestion = rotation; // Remember rotation for click
Mike Digman1e28a5a2018-02-14 10:49:19 -0800387
388 // Update the icon style to change animation parameters
389 if (mNavigationBarView != null) {
390 final boolean rotationCCW = isRotationAnimationCCW(winRotation, rotation);
391 int style;
392 if (winRotation == Surface.ROTATION_0 || winRotation == Surface.ROTATION_180) {
393 style = rotationCCW ? R.style.RotateButtonCCWStart90 :
394 R.style.RotateButtonCWStart90;
395 } else { // 90 or 270
396 style = rotationCCW ? R.style.RotateButtonCCWStart0 :
397 R.style.RotateButtonCWStart0;
398 }
399 mNavigationBarView.updateRotateSuggestionButtonStyle(style, true);
400 }
401
402 setRotateSuggestionButtonState(true);
Mike Digman90402952018-01-22 16:05:51 -0800403 rescheduleRotationTimeout(false);
Mike Digmanc94759d2018-01-23 11:01:21 -0800404 mMetricsLogger.visible(MetricsEvent.ROTATION_SUGGESTION_SHOWN);
Mike Digman7d092772018-01-11 12:10:32 -0800405 }
406 }
407
Mike Digman1e28a5a2018-02-14 10:49:19 -0800408 private boolean isRotationAnimationCCW(int from, int to) {
409 // All 180deg WM rotation animations are CCW, match that
410 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_90) return false;
411 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_180) return true; //180d so CCW
412 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_270) return true;
413 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_0) return true;
414 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_180) return false;
415 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_270) return true; //180d so CCW
416 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_0) return true; //180d so CCW
417 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_90) return true;
418 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_270) return false;
419 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_0) return false;
420 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_90) return true; //180d so CCW
421 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_180) return true;
422 return false; // Default
Mike Digmana48cf192018-02-12 17:52:48 -0800423 }
424
Mike Digman1e28a5a2018-02-14 10:49:19 -0800425 public void setRotateSuggestionButtonState(final boolean visible) {
426 setRotateSuggestionButtonState(visible, false);
427 }
428
429 public void setRotateSuggestionButtonState(final boolean visible, final boolean force) {
430 if (mNavigationBarView == null) return;
431
432 // At any point the the button can become invisible because an a11y service became active.
433 // Similarly, a call to make the button visible may be rejected because an a11y service is
434 // active. Must account for this.
435
436 ButtonDispatcher rotBtn = mNavigationBarView.getRotateSuggestionButton();
437 final boolean currentlyVisible = mNavigationBarView.isRotateButtonVisible();
438
439 // Rerun a show animation to indicate change but don't rerun a hide animation
440 if (!visible && !currentlyVisible) return;
441
442 View view = rotBtn.getCurrentView();
443 if (view == null) return;
444
445 KeyButtonDrawable kbd = rotBtn.getImageDrawable();
446 if (kbd == null) return;
447
448 // The KBD and AVD is recreated every new valid suggestion because of style changes.
449 AnimatedVectorDrawable animIcon = null;
450 if (kbd.getDrawable(0) instanceof AnimatedVectorDrawable) {
451 animIcon = (AnimatedVectorDrawable) kbd.getDrawable(0);
452 }
453
454 if (visible) { // Appear and change (cannot force)
Mike Digman85a9bea2018-02-23 15:08:53 -0800455 // Stop and clear any currently running hide animations
Mike Digman1e28a5a2018-02-14 10:49:19 -0800456 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
Mike Digman85a9bea2018-02-23 15:08:53 -0800457 mRotateHideAnimator.cancel();
Mike Digman1e28a5a2018-02-14 10:49:19 -0800458 }
Mike Digman85a9bea2018-02-23 15:08:53 -0800459 mRotateHideAnimator = null;
Mike Digman1e28a5a2018-02-14 10:49:19 -0800460
461 // Reset the alpha if any has changed due to hide animation
462 view.setAlpha(1f);
463
464 // Run the rotate icon's animation if it has one
465 if (animIcon != null) {
466 animIcon.reset();
467 animIcon.start();
468 }
469
Mike Digman50752642018-02-15 13:36:09 -0800470 if (!isRotateSuggestionIntroduced()) mViewRippler.start(view);
471
Mike Digman1e28a5a2018-02-14 10:49:19 -0800472 // Set visibility, may fail if a11y service is active.
473 // If invisible, call will stop animation.
474 mNavigationBarView.setRotateButtonVisibility(true);
475
476 } else { // Hide
477
Mike Digman50752642018-02-15 13:36:09 -0800478 mViewRippler.stop(); // Prevent any pending ripples, force hide or not
479
Mike Digman1e28a5a2018-02-14 10:49:19 -0800480 if (force) {
481 // If a hide animator is running stop it and make invisible
482 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
483 mRotateHideAnimator.pause();
484 }
485 mNavigationBarView.setRotateButtonVisibility(false);
486 return;
487 }
488
489 // Don't start any new hide animations if one is running
490 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
491
492 ObjectAnimator fadeOut = ObjectAnimator.ofFloat(view, "alpha",
493 0f);
494 fadeOut.setDuration(BUTTON_FADE_IN_OUT_DURATION_MS);
495 fadeOut.setInterpolator(Interpolators.LINEAR);
496 fadeOut.addListener(new AnimatorListenerAdapter() {
497 @Override
498 public void onAnimationEnd(Animator animation) {
499 mNavigationBarView.setRotateButtonVisibility(false);
500 }
501 });
502
503 mRotateHideAnimator = fadeOut;
504 fadeOut.start();
Mike Digmana48cf192018-02-12 17:52:48 -0800505 }
506 }
507
Mike Digman90402952018-01-22 16:05:51 -0800508 private void rescheduleRotationTimeout(final boolean reasonHover) {
509 // May be called due to a new rotation proposal or a change in hover state
510 if (reasonHover) {
511 // Don't reschedule if a hide animator is running
Mike Digman1e28a5a2018-02-14 10:49:19 -0800512 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
Mike Digman90402952018-01-22 16:05:51 -0800513 // Don't reschedule if not visible
Mike Digman1e28a5a2018-02-14 10:49:19 -0800514 if (!mNavigationBarView.isRotateButtonVisible()) return;
Mike Digman90402952018-01-22 16:05:51 -0800515 }
516
517 Handler h = getView().getHandler();
518 h.removeCallbacks(mRemoveRotationProposal); // Stop any pending removal
519 h.postDelayed(mRemoveRotationProposal,
520 computeRotationProposalTimeout()); // Schedule timeout
521 }
522
523 private int computeRotationProposalTimeout() {
524 if (mAccessibilityFeedbackEnabled) return 20000;
525 if (mHoveringRotationSuggestion) return 16000;
526 return 6000;
527 }
528
Mike Digman50752642018-02-15 13:36:09 -0800529 private boolean isRotateSuggestionIntroduced() {
530 ContentResolver cr = getContext().getContentResolver();
531 return Settings.Secure.getInt(cr, Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED, 0)
532 >= NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION;
533 }
534
535 private void incrementNumAcceptedRotationSuggestionsIfNeeded() {
536 // Get the number of accepted suggestions
537 ContentResolver cr = getContext().getContentResolver();
538 final int numSuggestions = Settings.Secure.getInt(cr,
539 Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED, 0);
540
541 // Increment the number of accepted suggestions only if it would change intro mode
542 if (numSuggestions < NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION) {
543 Settings.Secure.putInt(cr, Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED,
544 numSuggestions + 1);
545 }
546 }
547
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500548 // Injected from StatusBar at creation.
Jason Monk49fa0162017-01-11 09:21:56 -0500549 public void setCurrentSysuiVisibility(int systemUiVisibility) {
550 mSystemUiVisibility = systemUiVisibility;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500551 mNavigationBarMode = mStatusBar.computeBarMode(0, mSystemUiVisibility,
Jason Monk49fa0162017-01-11 09:21:56 -0500552 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
553 View.NAVIGATION_BAR_TRANSPARENT);
554 checkNavBarModes();
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500555 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500556 mLightBarController.onNavigationVisibilityChanged(mSystemUiVisibility, 0 /* mask */,
557 true /* nbModeChanged */, mNavigationBarMode);
558 }
559
560 @Override
561 public void setSystemUiVisibility(int vis, int fullscreenStackVis, int dockedStackVis,
562 int mask, Rect fullscreenStackBounds, Rect dockedStackBounds) {
563 final int oldVal = mSystemUiVisibility;
564 final int newVal = (oldVal & ~mask) | (vis & mask);
565 final int diff = newVal ^ oldVal;
566 boolean nbModeChanged = false;
567 if (diff != 0) {
568 mSystemUiVisibility = newVal;
569
570 // update navigation bar mode
571 final int nbMode = getView() == null
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500572 ? -1 : mStatusBar.computeBarMode(oldVal, newVal,
Jason Monk49fa0162017-01-11 09:21:56 -0500573 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
574 View.NAVIGATION_BAR_TRANSPARENT);
575 nbModeChanged = nbMode != -1;
576 if (nbModeChanged) {
577 if (mNavigationBarMode != nbMode) {
578 mNavigationBarMode = nbMode;
579 checkNavBarModes();
580 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500581 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500582 }
583 }
584
585 mLightBarController.onNavigationVisibilityChanged(vis, mask, nbModeChanged,
586 mNavigationBarMode);
587 }
588
589 @Override
590 public void disable(int state1, int state2, boolean animate) {
591 // All navigation bar flags are in state1.
592 int masked = state1 & (StatusBarManager.DISABLE_HOME
593 | StatusBarManager.DISABLE_RECENT
594 | StatusBarManager.DISABLE_BACK
595 | StatusBarManager.DISABLE_SEARCH);
596 if (masked != mDisabledFlags1) {
597 mDisabledFlags1 = masked;
598 if (mNavigationBarView != null) mNavigationBarView.setDisabledFlags(state1);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000599 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500600 }
601 }
602
603 // ----- Internal stuffz -----
604
605 private void refreshLayout(int layoutDirection) {
606 if (mNavigationBarView != null) {
607 mNavigationBarView.setLayoutDirection(layoutDirection);
608 }
609 }
610
611 private boolean shouldDisableNavbarGestures() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500612 return !mStatusBar.isDeviceProvisioned()
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800613 || (mDisabledFlags1 & StatusBarManager.DISABLE_SEARCH) != 0;
Jason Monk49fa0162017-01-11 09:21:56 -0500614 }
615
616 private void repositionNavigationBar() {
617 if (mNavigationBarView == null || !mNavigationBarView.isAttachedToWindow()) return;
618
619 prepareNavigationBarView();
620
621 mWindowManager.updateViewLayout((View) mNavigationBarView.getParent(),
622 ((View) mNavigationBarView.getParent()).getLayoutParams());
623 }
624
Matthew Ng9c3bce52018-02-01 22:00:31 +0000625 private void updateScreenPinningGestures() {
626 if (mNavigationBarView == null) {
627 return;
628 }
629
630 // Change the cancel pin gesture to home and back if recents button is invisible
631 boolean recentsVisible = mNavigationBarView.isRecentsButtonVisible();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000632 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
633 if (recentsVisible) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000634 backButton.setOnLongClickListener(this::onLongPressBackRecents);
635 } else {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000636 backButton.setOnLongClickListener(this::onLongPressBackHome);
637 }
638 }
639
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100640 private void notifyNavigationBarScreenOn() {
641 mNavigationBarView.notifyScreenOn();
Jason Monk49fa0162017-01-11 09:21:56 -0500642 }
643
644 private void prepareNavigationBarView() {
645 mNavigationBarView.reorient();
646
647 ButtonDispatcher recentsButton = mNavigationBarView.getRecentsButton();
648 recentsButton.setOnClickListener(this::onRecentsClick);
649 recentsButton.setOnTouchListener(this::onRecentsTouch);
650 recentsButton.setLongClickable(true);
651 recentsButton.setOnLongClickListener(this::onLongPressBackRecents);
652
653 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
654 backButton.setLongClickable(true);
Jason Monk49fa0162017-01-11 09:21:56 -0500655
656 ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();
657 homeButton.setOnTouchListener(this::onHomeTouch);
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800658 homeButton.setOnLongClickListener(this::onHomeLongClick);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800659
660 ButtonDispatcher accessibilityButton = mNavigationBarView.getAccessibilityButton();
661 accessibilityButton.setOnClickListener(this::onAccessibilityClick);
662 accessibilityButton.setOnLongClickListener(this::onAccessibilityLongClick);
Phil Weaverdb9a7742017-04-18 08:15:06 -0700663 updateAccessibilityServicesState(mAccessibilityManager);
Mike Digman7d092772018-01-11 12:10:32 -0800664
665 ButtonDispatcher rotateSuggestionButton = mNavigationBarView.getRotateSuggestionButton();
666 rotateSuggestionButton.setOnClickListener(this::onRotateSuggestionClick);
Mike Digman90402952018-01-22 16:05:51 -0800667 rotateSuggestionButton.setOnHoverListener(this::onRotateSuggestionHover);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000668 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500669 }
670
671 private boolean onHomeTouch(View v, MotionEvent event) {
672 if (mHomeBlockedThisTouch && event.getActionMasked() != MotionEvent.ACTION_DOWN) {
673 return true;
674 }
675 // If an incoming call is ringing, HOME is totally disabled.
676 // (The user is already on the InCallUI at this point,
677 // and his ONLY options are to answer or reject the call.)
678 switch (event.getAction()) {
679 case MotionEvent.ACTION_DOWN:
680 mHomeBlockedThisTouch = false;
681 TelecomManager telecomManager =
682 getContext().getSystemService(TelecomManager.class);
683 if (telecomManager != null && telecomManager.isRinging()) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500684 if (mStatusBar.isKeyguardShowing()) {
Jason Monk49fa0162017-01-11 09:21:56 -0500685 Log.i(TAG, "Ignoring HOME; there's a ringing incoming call. " +
686 "No heads up");
687 mHomeBlockedThisTouch = true;
688 return true;
689 }
690 }
691 break;
692 case MotionEvent.ACTION_UP:
693 case MotionEvent.ACTION_CANCEL:
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500694 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500695 break;
696 }
697 return false;
698 }
699
700 private void onVerticalChanged(boolean isVertical) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500701 mStatusBar.setQsScrimEnabled(!isVertical);
Jason Monk49fa0162017-01-11 09:21:56 -0500702 }
703
704 private boolean onNavigationTouch(View v, MotionEvent event) {
Eliot Courtneycb5d3162017-08-09 16:53:15 +0900705 mStatusBar.checkUserAutohide(event);
Jason Monk49fa0162017-01-11 09:21:56 -0500706 return false;
707 }
708
Jason Monk865246d2017-01-19 08:27:01 -0500709 @VisibleForTesting
710 boolean onHomeLongClick(View v) {
Matthew Ng6ff33b72018-02-27 13:47:38 -0800711 if (!mNavigationBarView.isRecentsButtonVisible()
712 && ActivityManagerWrapper.getInstance().isScreenPinningActive()) {
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800713 return onLongPressBackHome(v);
714 }
Jason Monk49fa0162017-01-11 09:21:56 -0500715 if (shouldDisableNavbarGestures()) {
716 return false;
717 }
Mike Digmanc94759d2018-01-23 11:01:21 -0800718 mMetricsLogger.action(MetricsEvent.ACTION_ASSIST_LONG_PRESS);
Jason Monk49fa0162017-01-11 09:21:56 -0500719 mAssistManager.startAssist(new Bundle() /* args */);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500720 mStatusBar.awakenDreams();
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800721
Jason Monk49fa0162017-01-11 09:21:56 -0500722 if (mNavigationBarView != null) {
723 mNavigationBarView.abortCurrentGesture();
724 }
725 return true;
726 }
727
728 // additional optimization when we have software system buttons - start loading the recent
729 // tasks on touch down
730 private boolean onRecentsTouch(View v, MotionEvent event) {
731 int action = event.getAction() & MotionEvent.ACTION_MASK;
732 if (action == MotionEvent.ACTION_DOWN) {
733 mCommandQueue.preloadRecentApps();
734 } else if (action == MotionEvent.ACTION_CANCEL) {
735 mCommandQueue.cancelPreloadRecentApps();
736 } else if (action == MotionEvent.ACTION_UP) {
737 if (!v.isPressed()) {
738 mCommandQueue.cancelPreloadRecentApps();
739 }
740 }
741 return false;
742 }
743
744 private void onRecentsClick(View v) {
745 if (LatencyTracker.isEnabled(getContext())) {
746 LatencyTracker.getInstance(getContext()).onActionStart(
747 LatencyTracker.ACTION_TOGGLE_RECENTS);
748 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500749 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500750 mCommandQueue.toggleRecentApps();
751 }
752
Matthew Ng9c3bce52018-02-01 22:00:31 +0000753 private boolean onLongPressBackHome(View v) {
754 return onLongPressNavigationButtons(v, R.id.back, R.id.home);
755 }
756
757 private boolean onLongPressBackRecents(View v) {
758 return onLongPressNavigationButtons(v, R.id.back, R.id.recent_apps);
759 }
760
Jason Monk49fa0162017-01-11 09:21:56 -0500761 /**
Matthew Ng9c3bce52018-02-01 22:00:31 +0000762 * This handles long-press of both back and recents/home. Back is the common button with
763 * combination of recents if it is visible or home if recents is invisible.
764 * They are handled together to capture them both being long-pressed
Jason Monk49fa0162017-01-11 09:21:56 -0500765 * at the same time to exit screen pinning (lock task).
766 *
Matthew Ng9c3bce52018-02-01 22:00:31 +0000767 * When accessibility mode is on, only a long-press from recents/home
Jason Monk49fa0162017-01-11 09:21:56 -0500768 * is required to exit.
769 *
770 * In all other circumstances we try to pass through long-press events
771 * for Back, so that apps can still use it. Which can be from two things.
772 * 1) Not currently in screen pinning (lock task).
Matthew Ng9c3bce52018-02-01 22:00:31 +0000773 * 2) Back is long-pressed without recents/home.
Jason Monk49fa0162017-01-11 09:21:56 -0500774 */
Matthew Ng9c3bce52018-02-01 22:00:31 +0000775 private boolean onLongPressNavigationButtons(View v, @IdRes int btnId1, @IdRes int btnId2) {
Jason Monk49fa0162017-01-11 09:21:56 -0500776 try {
777 boolean sendBackLongPress = false;
778 IActivityManager activityManager = ActivityManagerNative.getDefault();
779 boolean touchExplorationEnabled = mAccessibilityManager.isTouchExplorationEnabled();
780 boolean inLockTaskMode = activityManager.isInLockTaskMode();
781 if (inLockTaskMode && !touchExplorationEnabled) {
782 long time = System.currentTimeMillis();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000783
Jason Monk49fa0162017-01-11 09:21:56 -0500784 // If we recently long-pressed the other button then they were
785 // long-pressed 'together'
786 if ((time - mLastLockToAppLongPress) < LOCK_TO_APP_GESTURE_TOLERENCE) {
Benjamin Franza83859f2017-07-03 16:34:14 +0100787 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500788 // When exiting refresh disabled flags.
789 mNavigationBarView.setDisabledFlags(mDisabledFlags1, true);
790 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000791 } else if (v.getId() == btnId1) {
792 ButtonDispatcher button = btnId2 == R.id.recent_apps
793 ? mNavigationBarView.getRecentsButton()
794 : mNavigationBarView.getHomeButton();
795 if (!button.getCurrentView().isPressed()) {
796 // If we aren't pressing recents/home right now then they presses
797 // won't be together, so send the standard long-press action.
798 sendBackLongPress = true;
799 }
Jason Monk49fa0162017-01-11 09:21:56 -0500800 }
801 mLastLockToAppLongPress = time;
802 } else {
803 // If this is back still need to handle sending the long-press event.
Matthew Ng9c3bce52018-02-01 22:00:31 +0000804 if (v.getId() == btnId1) {
Jason Monk49fa0162017-01-11 09:21:56 -0500805 sendBackLongPress = true;
806 } else if (touchExplorationEnabled && inLockTaskMode) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000807 // When in accessibility mode a long press that is recents/home (not back)
Jason Monk49fa0162017-01-11 09:21:56 -0500808 // should stop lock task.
Benjamin Franza83859f2017-07-03 16:34:14 +0100809 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500810 // When exiting refresh disabled flags.
811 mNavigationBarView.setDisabledFlags(mDisabledFlags1, true);
812 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000813 } else if (v.getId() == btnId2) {
814 return btnId2 == R.id.recent_apps
815 ? onLongPressRecents()
816 : onHomeLongClick(mNavigationBarView.getHomeButton().getCurrentView());
Jason Monk49fa0162017-01-11 09:21:56 -0500817 }
818 }
819 if (sendBackLongPress) {
820 KeyButtonView keyButtonView = (KeyButtonView) v;
821 keyButtonView.sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS);
822 keyButtonView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
823 return true;
824 }
825 } catch (RemoteException e) {
826 Log.d(TAG, "Unable to reach activity manager", e);
827 }
828 return false;
829 }
830
831 private boolean onLongPressRecents() {
Erik Wolsheimer9be3a062017-05-31 14:59:57 -0700832 if (mRecents == null || !ActivityManager.supportsMultiWindow(getContext())
Matthew Ng43db6d22017-06-27 15:29:39 -0700833 || !mDivider.getView().getSnapAlgorithm().isSplitScreenFeasible()
834 || Recents.getConfiguration().isLowRamDevice) {
Jason Monk49fa0162017-01-11 09:21:56 -0500835 return false;
836 }
837
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500838 return mStatusBar.toggleSplitScreenMode(MetricsEvent.ACTION_WINDOW_DOCK_LONGPRESS,
Jason Monk49fa0162017-01-11 09:21:56 -0500839 MetricsEvent.ACTION_WINDOW_UNDOCK_LONGPRESS);
840 }
841
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800842 private void onAccessibilityClick(View v) {
843 mAccessibilityManager.notifyAccessibilityButtonClicked();
844 }
845
846 private boolean onAccessibilityLongClick(View v) {
Casey Burkhardt5e8b9802017-03-24 10:07:20 -0700847 Intent intent = new Intent(AccessibilityManager.ACTION_CHOOSE_ACCESSIBILITY_BUTTON);
848 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Casey Burkhardt5d614402017-04-06 13:46:50 -0700849 v.getContext().startActivityAsUser(intent, UserHandle.CURRENT);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800850 return true;
851 }
852
Phil Weaverdb9a7742017-04-18 08:15:06 -0700853 private void updateAccessibilityServicesState(AccessibilityManager accessibilityManager) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800854 int requestingServices = 0;
855 try {
Casey Burkhardt5d614402017-04-06 13:46:50 -0700856 if (Settings.Secure.getIntForUser(mContentResolver,
857 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED,
858 UserHandle.USER_CURRENT) == 1) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800859 requestingServices++;
860 }
861 } catch (Settings.SettingNotFoundException e) {
862 }
863
Mike Digman90402952018-01-22 16:05:51 -0800864 boolean feedbackEnabled = false;
Casey Burkhardt5d614402017-04-06 13:46:50 -0700865 // AccessibilityManagerService resolves services for the current user since the local
866 // AccessibilityManager is created from a Context with the INTERACT_ACROSS_USERS permission
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800867 final List<AccessibilityServiceInfo> services =
Phil Weaverdb9a7742017-04-18 08:15:06 -0700868 accessibilityManager.getEnabledAccessibilityServiceList(
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800869 AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800870 for (int i = services.size() - 1; i >= 0; --i) {
871 AccessibilityServiceInfo info = services.get(i);
872 if ((info.flags & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0) {
873 requestingServices++;
874 }
Mike Digman90402952018-01-22 16:05:51 -0800875
876 if (info.feedbackType != 0 && info.feedbackType !=
877 AccessibilityServiceInfo.FEEDBACK_GENERIC) {
878 feedbackEnabled = true;
879 }
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800880 }
881
Mike Digman90402952018-01-22 16:05:51 -0800882 mAccessibilityFeedbackEnabled = feedbackEnabled;
883
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800884 final boolean showAccessibilityButton = requestingServices >= 1;
885 final boolean targetSelection = requestingServices >= 2;
886 mNavigationBarView.setAccessibilityButtonState(showAccessibilityButton, targetSelection);
887 }
888
Mike Digman7d092772018-01-11 12:10:32 -0800889 private void onRotateSuggestionClick(View v) {
Mike Digmanc94759d2018-01-23 11:01:21 -0800890 mMetricsLogger.action(MetricsEvent.ACTION_ROTATION_SUGGESTION_ACCEPTED);
Mike Digman50752642018-02-15 13:36:09 -0800891 incrementNumAcceptedRotationSuggestionsIfNeeded();
Mike Digman7d092772018-01-11 12:10:32 -0800892 mRotationLockController.setRotationLockedAtAngle(true, mLastRotationSuggestion);
893 }
894
Mike Digman90402952018-01-22 16:05:51 -0800895 private boolean onRotateSuggestionHover(View v, MotionEvent event) {
896 final int action = event.getActionMasked();
897 mHoveringRotationSuggestion = (action == MotionEvent.ACTION_HOVER_ENTER)
898 || (action == MotionEvent.ACTION_HOVER_MOVE);
899 rescheduleRotationTimeout(true);
900 return false; // Must return false so a11y hover events are dispatched correctly.
901 }
902
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500903 // ----- Methods that StatusBar talks to (should be minimized) -----
Jason Monk49fa0162017-01-11 09:21:56 -0500904
Jason Monk49fa0162017-01-11 09:21:56 -0500905 public void setLightBarController(LightBarController lightBarController) {
906 mLightBarController = lightBarController;
907 mLightBarController.setNavigationBar(mNavigationBarView.getLightTransitionsController());
908 }
909
910 public boolean isSemiTransparent() {
911 return mNavigationBarMode == MODE_SEMI_TRANSPARENT;
912 }
913
Jason Monk49fa0162017-01-11 09:21:56 -0500914 public void disableAnimationsDuringHide(long delay) {
915 mNavigationBarView.setLayoutTransitionsEnabled(false);
916 mNavigationBarView.postDelayed(() -> mNavigationBarView.setLayoutTransitionsEnabled(true),
917 delay + StackStateAnimator.ANIMATION_DURATION_GO_TO_FULL_SHADE);
918 }
919
Jason Monk49fa0162017-01-11 09:21:56 -0500920 public BarTransitions getBarTransitions() {
921 return mNavigationBarView.getBarTransitions();
922 }
923
924 public void checkNavBarModes() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500925 mStatusBar.checkBarMode(mNavigationBarMode,
Jason Monk49fa0162017-01-11 09:21:56 -0500926 mNavigationBarWindowState, mNavigationBarView.getBarTransitions());
927 }
928
929 public void finishBarAnimations() {
930 mNavigationBarView.getBarTransitions().finishAnimations();
931 }
932
Jason Monk91e587e2017-04-13 13:41:23 -0400933 private final AccessibilityServicesStateChangeListener mAccessibilityListener =
934 this::updateAccessibilityServicesState;
935
Casey Burkhardt74922c62017-02-13 12:43:16 -0800936 private class MagnificationContentObserver extends ContentObserver {
937
938 public MagnificationContentObserver(Handler handler) {
939 super(handler);
940 }
941
942 @Override
943 public void onChange(boolean selfChange) {
Phil Weaverdb9a7742017-04-18 08:15:06 -0700944 NavigationBarFragment.this.updateAccessibilityServicesState(mAccessibilityManager);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800945 }
946 }
947
Jason Monk49fa0162017-01-11 09:21:56 -0500948 private final Stub mRotationWatcher = new Stub() {
949 @Override
Mike Digman90402952018-01-22 16:05:51 -0800950 public void onRotationChanged(final int rotation) throws RemoteException {
Jason Monk49fa0162017-01-11 09:21:56 -0500951 // We need this to be scheduled as early as possible to beat the redrawing of
952 // window in response to the orientation change.
953 Handler h = getView().getHandler();
954 Message msg = Message.obtain(h, () -> {
Mike Digman85ff7fa2018-01-23 14:59:52 -0800955
956 // If the screen rotation changes while locked, potentially update lock to flow with
Mike Digman90402952018-01-22 16:05:51 -0800957 // new screen rotation and hide any showing suggestions.
958 if (mRotationLockController.isRotationLocked()) {
Mike Digman85ff7fa2018-01-23 14:59:52 -0800959 if (shouldOverrideUserLockPrefs(rotation)) {
960 mRotationLockController.setRotationLockedAtAngle(true, rotation);
961 }
Mike Digman1e28a5a2018-02-14 10:49:19 -0800962 setRotateSuggestionButtonState(false, true);
Mike Digman90402952018-01-22 16:05:51 -0800963 }
964
Jason Monk49fa0162017-01-11 09:21:56 -0500965 if (mNavigationBarView != null
966 && mNavigationBarView.needsReorient(rotation)) {
967 repositionNavigationBar();
968 }
969 });
970 msg.setAsynchronous(true);
971 h.sendMessageAtFrontOfQueue(msg);
972 }
Mike Digman85ff7fa2018-01-23 14:59:52 -0800973
974 private boolean shouldOverrideUserLockPrefs(final int rotation) {
975 // Only override user prefs when returning to portrait.
976 // Don't let apps that force landscape or 180 alter user lock.
977 return rotation == Surface.ROTATION_0;
978 }
Jason Monk49fa0162017-01-11 09:21:56 -0500979 };
980
981 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
982 @Override
983 public void onReceive(Context context, Intent intent) {
984 String action = intent.getAction();
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100985 if (Intent.ACTION_SCREEN_OFF.equals(action)
986 || Intent.ACTION_SCREEN_ON.equals(action)) {
987 notifyNavigationBarScreenOn();
Jason Monk49fa0162017-01-11 09:21:56 -0500988 }
989 }
990 };
991
Mike Digman7d092772018-01-11 12:10:32 -0800992 class TaskStackListenerImpl extends SysUiTaskStackChangeListener {
993 // Invalidate any rotation suggestion on task change or activity orientation change
994 // Note: all callbacks happen on main thread
995
996 @Override
997 public void onTaskStackChanged() {
Mike Digman1e28a5a2018-02-14 10:49:19 -0800998 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -0800999 }
1000
1001 @Override
1002 public void onTaskRemoved(int taskId) {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001003 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001004 }
1005
1006 @Override
1007 public void onTaskMovedToFront(int taskId) {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001008 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001009 }
1010
1011 @Override
1012 public void onActivityRequestedOrientationChanged(int taskId, int requestedOrientation) {
Mike Digman85a9bea2018-02-23 15:08:53 -08001013 // Only hide the icon if the top task changes its requestedOrientation
1014 // Launcher can alter its requestedOrientation while it's not on top, don't hide on this
1015 final boolean top = ActivityManagerWrapper.getInstance().getRunningTask().id == taskId;
1016 if (top) setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001017 }
1018 }
1019
Mike Digman50752642018-02-15 13:36:09 -08001020 private class ViewRippler {
1021 private static final int RIPPLE_OFFSET_MS = 50;
1022 private static final int RIPPLE_INTERVAL_MS = 2000;
1023 private View mRoot;
1024
1025 public void start(View root) {
1026 stop(); // Stop any pending ripple animations
1027
1028 mRoot = root;
1029
1030 // Schedule pending ripples, offset the 1st to avoid problems with visibility change
1031 mRoot.postOnAnimationDelayed(mRipple, RIPPLE_OFFSET_MS);
1032 mRoot.postOnAnimationDelayed(mRipple, RIPPLE_INTERVAL_MS);
1033 mRoot.postOnAnimationDelayed(mRipple, 2*RIPPLE_INTERVAL_MS);
1034 }
1035
1036 public void stop() {
1037 if (mRoot != null) mRoot.removeCallbacks(mRipple);
1038 }
1039
1040 private final Runnable mRipple = new Runnable() {
1041 @Override
1042 public void run() { // Cause the ripple to fire via false presses
1043 mRoot.setPressed(true);
1044 mRoot.setPressed(false);
1045 }
1046 };
1047 }
1048
Jason Monk49fa0162017-01-11 09:21:56 -05001049 public static View create(Context context, FragmentListener listener) {
1050 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
1051 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
1052 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR,
1053 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
1054 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1055 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
1056 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1057 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
1058 | WindowManager.LayoutParams.FLAG_SLIPPERY,
1059 PixelFormat.TRANSLUCENT);
1060 lp.token = new Binder();
Jason Monk49fa0162017-01-11 09:21:56 -05001061 lp.setTitle("NavigationBar");
Phil Weaver8583ae82018-02-13 11:01:24 -08001062 lp.accessibilityTitle = context.getString(R.string.nav_bar);
Jason Monk49fa0162017-01-11 09:21:56 -05001063 lp.windowAnimations = 0;
1064
1065 View navigationBarView = LayoutInflater.from(context).inflate(
1066 R.layout.navigation_bar_window, null);
1067
1068 if (DEBUG) Log.v(TAG, "addNavigationBar: about to add " + navigationBarView);
1069 if (navigationBarView == null) return null;
1070
1071 context.getSystemService(WindowManager.class).addView(navigationBarView, lp);
1072 FragmentHostManager fragmentHost = FragmentHostManager.get(navigationBarView);
1073 NavigationBarFragment fragment = new NavigationBarFragment();
1074 fragmentHost.getFragmentManager().beginTransaction()
1075 .replace(R.id.navigation_bar_frame, fragment, TAG)
1076 .commit();
1077 fragmentHost.addTagListener(TAG, listener);
1078 return navigationBarView;
1079 }
1080}