blob: ca6d5968a43c399426907aef583b78d44598b390 [file] [log] [blame]
Jason Monk49fa0162017-01-11 09:21:56 -05001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.statusbar.phone;
16
17import static android.app.StatusBarManager.NAVIGATION_HINT_BACK_ALT;
18import static android.app.StatusBarManager.NAVIGATION_HINT_IME_SHOWN;
19import static android.app.StatusBarManager.WINDOW_STATE_SHOWING;
20import static android.app.StatusBarManager.windowStateToString;
21
Mike Digmanab650252018-03-06 11:01:41 -080022import static com.android.internal.view.RotationPolicy.NATURAL_ROTATION;
23
Matthew Ng8f25fb962018-01-16 17:17:24 -080024import static com.android.systemui.shared.system.NavigationBarCompat.InteractionType;
Jason Monk49fa0162017-01-11 09:21:56 -050025import static com.android.systemui.statusbar.phone.BarTransitions.MODE_SEMI_TRANSPARENT;
Jason Monk2a6ea9c2017-01-26 11:14:51 -050026import static com.android.systemui.statusbar.phone.StatusBar.DEBUG_WINDOW_STATE;
27import static com.android.systemui.statusbar.phone.StatusBar.dumpBarTransitions;
Matthew Ng9c3bce52018-02-01 22:00:31 +000028import static com.android.systemui.OverviewProxyService.OverviewProxyListener;
Jason Monk49fa0162017-01-11 09:21:56 -050029
Casey Burkhardt048c2bc2016-12-08 16:09:20 -080030import android.accessibilityservice.AccessibilityServiceInfo;
Mike Digman7d092772018-01-11 12:10:32 -080031import android.animation.Animator;
32import android.animation.AnimatorListenerAdapter;
Mike Digman7d092772018-01-11 12:10:32 -080033import android.animation.ObjectAnimator;
Matthew Ng9c3bce52018-02-01 22:00:31 +000034import android.annotation.IdRes;
Jason Monk49fa0162017-01-11 09:21:56 -050035import android.annotation.Nullable;
36import android.app.ActivityManager;
37import android.app.ActivityManagerNative;
38import android.app.Fragment;
39import android.app.IActivityManager;
40import android.app.StatusBarManager;
41import android.content.BroadcastReceiver;
Casey Burkhardtb9dcd662017-03-20 15:10:16 -070042import android.content.ContentResolver;
Jason Monk49fa0162017-01-11 09:21:56 -050043import android.content.Context;
44import android.content.Intent;
45import android.content.IntentFilter;
46import android.content.res.Configuration;
Casey Burkhardt74922c62017-02-13 12:43:16 -080047import android.database.ContentObserver;
Jason Monk49fa0162017-01-11 09:21:56 -050048import android.graphics.PixelFormat;
49import android.graphics.Rect;
Mike Digman7d092772018-01-11 12:10:32 -080050import android.graphics.drawable.AnimatedVectorDrawable;
Jason Monk49fa0162017-01-11 09:21:56 -050051import android.inputmethodservice.InputMethodService;
52import android.os.Binder;
53import android.os.Bundle;
54import android.os.Handler;
55import android.os.IBinder;
56import android.os.Message;
Jason Monk49fa0162017-01-11 09:21:56 -050057import android.os.RemoteException;
58import android.os.UserHandle;
Casey Burkhardt74922c62017-02-13 12:43:16 -080059import android.provider.Settings;
Jason Monk865246d2017-01-19 08:27:01 -050060import android.support.annotation.VisibleForTesting;
Jason Monk49fa0162017-01-11 09:21:56 -050061import android.telecom.TelecomManager;
62import android.text.TextUtils;
63import android.util.Log;
64import android.view.IRotationWatcher.Stub;
65import android.view.KeyEvent;
66import android.view.LayoutInflater;
67import android.view.MotionEvent;
Mike Digman85ff7fa2018-01-23 14:59:52 -080068import android.view.Surface;
Jason Monk49fa0162017-01-11 09:21:56 -050069import android.view.View;
70import android.view.ViewGroup;
71import android.view.WindowManager;
72import android.view.WindowManager.LayoutParams;
73import android.view.WindowManagerGlobal;
74import android.view.accessibility.AccessibilityEvent;
75import android.view.accessibility.AccessibilityManager;
Jason Monk91e587e2017-04-13 13:41:23 -040076import android.view.accessibility.AccessibilityManager.AccessibilityServicesStateChangeListener;
Jason Monk49fa0162017-01-11 09:21:56 -050077
78import com.android.internal.logging.MetricsLogger;
79import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monkea03be12017-12-04 11:08:41 -050080import com.android.internal.util.LatencyTracker;
Jason Monk9c7844c2017-01-18 15:21:53 -050081import com.android.systemui.Dependency;
Mike Digman7d092772018-01-11 12:10:32 -080082import com.android.systemui.Interpolators;
Mike Digman1e28a5a2018-02-14 10:49:19 -080083import com.android.systemui.OverviewProxyService;
Jason Monk49fa0162017-01-11 09:21:56 -050084import com.android.systemui.R;
Jason Monk9c7844c2017-01-18 15:21:53 -050085import com.android.systemui.SysUiServiceProvider;
Jason Monk49fa0162017-01-11 09:21:56 -050086import com.android.systemui.assist.AssistManager;
87import com.android.systemui.fragments.FragmentHostManager;
88import com.android.systemui.fragments.FragmentHostManager.FragmentListener;
89import com.android.systemui.recents.Recents;
Mike Digman7d092772018-01-11 12:10:32 -080090import com.android.systemui.recents.misc.SysUiTaskStackChangeListener;
91import com.android.systemui.shared.system.ActivityManagerWrapper;
Matthew Ng64543e62018-02-28 17:35:10 -080092import com.android.systemui.shared.system.WindowManagerWrapper;
Jason Monk49fa0162017-01-11 09:21:56 -050093import com.android.systemui.stackdivider.Divider;
94import com.android.systemui.statusbar.CommandQueue;
95import com.android.systemui.statusbar.CommandQueue.Callbacks;
Jason Monk91e587e2017-04-13 13:41:23 -040096import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper;
Mike Digman7d092772018-01-11 12:10:32 -080097import com.android.systemui.statusbar.policy.KeyButtonDrawable;
Jason Monk49fa0162017-01-11 09:21:56 -050098import com.android.systemui.statusbar.policy.KeyButtonView;
Mike Digman7d092772018-01-11 12:10:32 -080099import com.android.systemui.statusbar.policy.RotationLockController;
Jason Monk49fa0162017-01-11 09:21:56 -0500100import com.android.systemui.statusbar.stack.StackStateAnimator;
101
102import java.io.FileDescriptor;
103import java.io.PrintWriter;
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800104import java.util.List;
Jason Monk49fa0162017-01-11 09:21:56 -0500105import java.util.Locale;
Mike Digman121b11f2018-04-04 12:45:24 -0700106import java.util.Optional;
Jason Monk49fa0162017-01-11 09:21:56 -0500107
108/**
109 * Fragment containing the NavigationBarFragment. Contains logic for what happens
110 * on clicks and view states of the nav bar.
111 */
112public class NavigationBarFragment extends Fragment implements Callbacks {
113
Jason Monkd4afe152017-05-01 15:37:43 -0400114 public static final String TAG = "NavigationBar";
Jason Monk49fa0162017-01-11 09:21:56 -0500115 private static final boolean DEBUG = false;
Mike Digman3e33da62018-03-14 16:25:11 -0700116 private static final boolean DEBUG_ROTATION = true;
Jason Monk49fa0162017-01-11 09:21:56 -0500117 private static final String EXTRA_DISABLE_STATE = "disabled_state";
Mike Digmandd2f49e2018-03-16 10:54:22 -0700118 private static final String EXTRA_DISABLE2_STATE = "disabled2_state";
Jason Monk49fa0162017-01-11 09:21:56 -0500119
Mike Digman1e28a5a2018-02-14 10:49:19 -0800120 private final static int BUTTON_FADE_IN_OUT_DURATION_MS = 100;
Mike Digman5aeca792018-03-05 11:14:39 -0800121 private final static int NAVBAR_HIDDEN_PENDING_ICON_TIMEOUT_MS = 20000;
Mike Digman1e28a5a2018-02-14 10:49:19 -0800122
Mike Digman50752642018-02-15 13:36:09 -0800123 private static final int NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION = 3;
124
Jason Monk49fa0162017-01-11 09:21:56 -0500125 /** Allow some time inbetween the long press for back and recents. */
126 private static final int LOCK_TO_APP_GESTURE_TOLERENCE = 200;
127
128 protected NavigationBarView mNavigationBarView = null;
129 protected AssistManager mAssistManager;
130
131 private int mNavigationBarWindowState = WINDOW_STATE_SHOWING;
132
133 private int mNavigationIconHints = 0;
134 private int mNavigationBarMode;
Mike Digman90402952018-01-22 16:05:51 -0800135 private boolean mAccessibilityFeedbackEnabled;
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800136 private AccessibilityManager mAccessibilityManager;
Casey Burkhardt74922c62017-02-13 12:43:16 -0800137 private MagnificationContentObserver mMagnificationObserver;
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700138 private ContentResolver mContentResolver;
Mike Digmanc94759d2018-01-23 11:01:21 -0800139 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500140
141 private int mDisabledFlags1;
Mike Digmandd2f49e2018-03-16 10:54:22 -0700142 private int mDisabledFlags2;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500143 private StatusBar mStatusBar;
Jason Monk49fa0162017-01-11 09:21:56 -0500144 private Recents mRecents;
145 private Divider mDivider;
146 private WindowManager mWindowManager;
147 private CommandQueue mCommandQueue;
148 private long mLastLockToAppLongPress;
149
150 private Locale mLocale;
151 private int mLayoutDirection;
152
153 private int mSystemUiVisibility;
154 private LightBarController mLightBarController;
Jason Monk49fa0162017-01-11 09:21:56 -0500155
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800156 private OverviewProxyService mOverviewProxyService;
157
Jason Monk49fa0162017-01-11 09:21:56 -0500158 public boolean mHomeBlockedThisTouch;
159
Mike Digman7d092772018-01-11 12:10:32 -0800160 private int mLastRotationSuggestion;
Mike Digman5aeca792018-03-05 11:14:39 -0800161 private boolean mPendingRotationSuggestion;
Mike Digman90402952018-01-22 16:05:51 -0800162 private boolean mHoveringRotationSuggestion;
Mike Digman7d092772018-01-11 12:10:32 -0800163 private RotationLockController mRotationLockController;
164 private TaskStackListenerImpl mTaskStackListener;
165
Mike Digman1e28a5a2018-02-14 10:49:19 -0800166 private final Runnable mRemoveRotationProposal = () -> setRotateSuggestionButtonState(false);
Mike Digman5aeca792018-03-05 11:14:39 -0800167 private final Runnable mCancelPendingRotationProposal =
168 () -> mPendingRotationSuggestion = false;
Mike Digman7d092772018-01-11 12:10:32 -0800169 private Animator mRotateHideAnimator;
Mike Digman50752642018-02-15 13:36:09 -0800170 private ViewRippler mViewRippler = new ViewRippler();
Mike Digman7d092772018-01-11 12:10:32 -0800171
Matthew Ng9c3bce52018-02-01 22:00:31 +0000172 private final OverviewProxyListener mOverviewProxyListener = new OverviewProxyListener() {
173 @Override
174 public void onConnectionChanged(boolean isConnected) {
Winson Chungf9e30272018-03-26 17:25:36 -0700175 mNavigationBarView.updateStates();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000176 updateScreenPinningGestures();
Matthew Ng64543e62018-02-28 17:35:10 -0800177 WindowManagerWrapper.getInstance()
178 .setNavBarVirtualKeyHapticFeedbackEnabled(!isConnected);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000179 }
180
181 @Override
Matthew Ng2ea93b72018-03-14 19:43:18 +0000182 public void onQuickStepStarted() {
183 mNavigationBarView.onQuickStepStarted();
Mike Digman85a9bea2018-02-23 15:08:53 -0800184
185 // Use navbar dragging as a signal to hide the rotate button
186 setRotateSuggestionButtonState(false);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000187 }
Matthew Ng8f25fb962018-01-16 17:17:24 -0800188
189 @Override
190 public void onInteractionFlagsChanged(@InteractionType int flags) {
191 mNavigationBarView.updateStates();
Winson Chungf9e30272018-03-26 17:25:36 -0700192 updateScreenPinningGestures();
Matthew Ng8f25fb962018-01-16 17:17:24 -0800193 }
Matthew Ng9c3bce52018-02-01 22:00:31 +0000194 };
Mike Digman7d092772018-01-11 12:10:32 -0800195
Jason Monk49fa0162017-01-11 09:21:56 -0500196 // ----- Fragment Lifecycle Callbacks -----
197
198 @Override
199 public void onCreate(@Nullable Bundle savedInstanceState) {
200 super.onCreate(savedInstanceState);
Jason Monk9c7844c2017-01-18 15:21:53 -0500201 mCommandQueue = SysUiServiceProvider.getComponent(getContext(), CommandQueue.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500202 mCommandQueue.addCallbacks(this);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500203 mStatusBar = SysUiServiceProvider.getComponent(getContext(), StatusBar.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500204 mRecents = SysUiServiceProvider.getComponent(getContext(), Recents.class);
205 mDivider = SysUiServiceProvider.getComponent(getContext(), Divider.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500206 mWindowManager = getContext().getSystemService(WindowManager.class);
207 mAccessibilityManager = getContext().getSystemService(AccessibilityManager.class);
Jason Monk91e587e2017-04-13 13:41:23 -0400208 Dependency.get(AccessibilityManagerWrapper.class).addCallback(
209 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700210 mContentResolver = getContext().getContentResolver();
Casey Burkhardt74922c62017-02-13 12:43:16 -0800211 mMagnificationObserver = new MagnificationContentObserver(
212 getContext().getMainThreadHandler());
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700213 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(
Casey Burkhardt74922c62017-02-13 12:43:16 -0800214 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED), false,
Casey Burkhardt5d614402017-04-06 13:46:50 -0700215 mMagnificationObserver, UserHandle.USER_ALL);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800216
Jason Monk49fa0162017-01-11 09:21:56 -0500217 if (savedInstanceState != null) {
218 mDisabledFlags1 = savedInstanceState.getInt(EXTRA_DISABLE_STATE, 0);
Mike Digmandd2f49e2018-03-16 10:54:22 -0700219 mDisabledFlags2 = savedInstanceState.getInt(EXTRA_DISABLE2_STATE, 0);
Jason Monk49fa0162017-01-11 09:21:56 -0500220 }
Jason Monk9c7844c2017-01-18 15:21:53 -0500221 mAssistManager = Dependency.get(AssistManager.class);
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800222 mOverviewProxyService = Dependency.get(OverviewProxyService.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500223
224 try {
225 WindowManagerGlobal.getWindowManagerService()
Andrii Kulian35fa3c22017-03-11 09:37:28 -0800226 .watchRotation(mRotationWatcher, getContext().getDisplay().getDisplayId());
Jason Monk49fa0162017-01-11 09:21:56 -0500227 } catch (RemoteException e) {
228 throw e.rethrowFromSystemServer();
229 }
Mike Digman7d092772018-01-11 12:10:32 -0800230
231 mRotationLockController = Dependency.get(RotationLockController.class);
232
Mike Digmanab650252018-03-06 11:01:41 -0800233 // Reset user rotation pref to match that of the WindowManager if starting in locked mode
234 // This will automatically happen when switching from auto-rotate to locked mode
235 if (mRotationLockController.isRotationLocked()) {
236 final int winRotation = mWindowManager.getDefaultDisplay().getRotation();
237 mRotationLockController.setRotationLockedAtAngle(true, winRotation);
238 }
239
Mike Digman7d092772018-01-11 12:10:32 -0800240 // Register the task stack listener
241 mTaskStackListener = new TaskStackListenerImpl();
242 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500243 }
244
245 @Override
246 public void onDestroy() {
247 super.onDestroy();
248 mCommandQueue.removeCallbacks(this);
Jason Monk91e587e2017-04-13 13:41:23 -0400249 Dependency.get(AccessibilityManagerWrapper.class).removeCallback(
250 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700251 mContentResolver.unregisterContentObserver(mMagnificationObserver);
Jason Monk49fa0162017-01-11 09:21:56 -0500252 try {
253 WindowManagerGlobal.getWindowManagerService()
254 .removeRotationWatcher(mRotationWatcher);
255 } catch (RemoteException e) {
256 throw e.rethrowFromSystemServer();
257 }
Mike Digman7d092772018-01-11 12:10:32 -0800258
259 // Unregister the task stack listener
260 ActivityManagerWrapper.getInstance().unregisterTaskStackListener(mTaskStackListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500261 }
262
263 @Override
264 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
265 Bundle savedInstanceState) {
266 return inflater.inflate(R.layout.navigation_bar, container, false);
267 }
268
269 @Override
270 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
271 super.onViewCreated(view, savedInstanceState);
272 mNavigationBarView = (NavigationBarView) view;
273
274 mNavigationBarView.setDisabledFlags(mDisabledFlags1);
Matthew Ng78f88d12018-01-23 12:39:55 -0800275 mNavigationBarView.setComponents(mRecents, mDivider, mStatusBar.getPanel());
Jason Monk49fa0162017-01-11 09:21:56 -0500276 mNavigationBarView.setOnVerticalChangedListener(this::onVerticalChanged);
277 mNavigationBarView.setOnTouchListener(this::onNavigationTouch);
278 if (savedInstanceState != null) {
279 mNavigationBarView.getLightTransitionsController().restoreState(savedInstanceState);
280 }
281
282 prepareNavigationBarView();
283 checkNavBarModes();
284
Mike Digmandd2f49e2018-03-16 10:54:22 -0700285 setDisabled2Flags(mDisabledFlags2);
286
Jason Monk49fa0162017-01-11 09:21:56 -0500287 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
288 filter.addAction(Intent.ACTION_SCREEN_ON);
289 getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100290 notifyNavigationBarScreenOn();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000291 mOverviewProxyService.addCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500292 }
293
294 @Override
295 public void onDestroyView() {
296 super.onDestroyView();
Jason Monkaa573e92017-01-27 17:00:29 -0500297 mNavigationBarView.getLightTransitionsController().destroy(getContext());
Matthew Ng9c3bce52018-02-01 22:00:31 +0000298 mOverviewProxyService.removeCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500299 getContext().unregisterReceiver(mBroadcastReceiver);
300 }
301
302 @Override
303 public void onSaveInstanceState(Bundle outState) {
304 super.onSaveInstanceState(outState);
305 outState.putInt(EXTRA_DISABLE_STATE, mDisabledFlags1);
Mike Digmandd2f49e2018-03-16 10:54:22 -0700306 outState.putInt(EXTRA_DISABLE2_STATE, mDisabledFlags2);
Jason Monk49fa0162017-01-11 09:21:56 -0500307 if (mNavigationBarView != null) {
308 mNavigationBarView.getLightTransitionsController().saveState(outState);
309 }
310 }
311
312 @Override
313 public void onConfigurationChanged(Configuration newConfig) {
314 super.onConfigurationChanged(newConfig);
315 final Locale locale = getContext().getResources().getConfiguration().locale;
316 final int ld = TextUtils.getLayoutDirectionFromLocale(locale);
317 if (!locale.equals(mLocale) || ld != mLayoutDirection) {
318 if (DEBUG) {
319 Log.v(TAG, String.format(
320 "config changed locale/LD: %s (%d) -> %s (%d)", mLocale, mLayoutDirection,
321 locale, ld));
322 }
323 mLocale = locale;
324 mLayoutDirection = ld;
325 refreshLayout(ld);
326 }
327 repositionNavigationBar();
328 }
329
330 @Override
331 public void dump(String prefix, FileDescriptor fd, PrintWriter pw, String[] args) {
332 if (mNavigationBarView != null) {
333 pw.print(" mNavigationBarWindowState=");
334 pw.println(windowStateToString(mNavigationBarWindowState));
335 pw.print(" mNavigationBarMode=");
336 pw.println(BarTransitions.modeToString(mNavigationBarMode));
337 dumpBarTransitions(pw, "mNavigationBarView", mNavigationBarView.getBarTransitions());
338 }
339
340 pw.print(" mNavigationBarView=");
341 if (mNavigationBarView == null) {
342 pw.println("null");
343 } else {
344 mNavigationBarView.dump(fd, pw, args);
345 }
346 }
347
348 // ----- CommandQueue Callbacks -----
349
350 @Override
351 public void setImeWindowStatus(IBinder token, int vis, int backDisposition,
352 boolean showImeSwitcher) {
353 boolean imeShown = (vis & InputMethodService.IME_VISIBLE) != 0;
354 int hints = mNavigationIconHints;
Yohei Yukawa386f50e2018-03-14 13:03:42 -0700355 switch (backDisposition) {
356 case InputMethodService.BACK_DISPOSITION_DEFAULT:
357 case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
358 case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
359 if (imeShown) {
360 hints |= NAVIGATION_HINT_BACK_ALT;
361 } else {
362 hints &= ~NAVIGATION_HINT_BACK_ALT;
363 }
364 break;
365 case InputMethodService.BACK_DISPOSITION_ADJUST_NOTHING:
366 hints &= ~NAVIGATION_HINT_BACK_ALT;
367 break;
Jason Monk49fa0162017-01-11 09:21:56 -0500368 }
369 if (showImeSwitcher) {
370 hints |= NAVIGATION_HINT_IME_SHOWN;
371 } else {
372 hints &= ~NAVIGATION_HINT_IME_SHOWN;
373 }
374 if (hints == mNavigationIconHints) return;
375
376 mNavigationIconHints = hints;
377
378 if (mNavigationBarView != null) {
379 mNavigationBarView.setNavigationIconHints(hints);
380 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500381 mStatusBar.checkBarModes();
Jason Monk49fa0162017-01-11 09:21:56 -0500382 }
383
384 @Override
385 public void topAppWindowChanged(boolean showMenu) {
386 if (mNavigationBarView != null) {
387 mNavigationBarView.setMenuVisibility(showMenu);
388 }
389 }
390
391 @Override
392 public void setWindowState(int window, int state) {
393 if (mNavigationBarView != null
394 && window == StatusBarManager.WINDOW_NAVIGATION_BAR
395 && mNavigationBarWindowState != state) {
396 mNavigationBarWindowState = state;
397 if (DEBUG_WINDOW_STATE) Log.d(TAG, "Navigation bar " + windowStateToString(state));
Mike Digman5aeca792018-03-05 11:14:39 -0800398
399 // If the navbar is visible, show the rotate button if there's a pending suggestion
400 if (state == WINDOW_STATE_SHOWING && mPendingRotationSuggestion) {
401 showAndLogRotationSuggestion();
402 }
Jason Monk49fa0162017-01-11 09:21:56 -0500403 }
404 }
405
Mike Digman7d092772018-01-11 12:10:32 -0800406 @Override
Mike Digmane0777312018-01-19 12:41:51 -0800407 public void onRotationProposal(final int rotation, boolean isValid) {
Mike Digman3e33da62018-03-14 16:25:11 -0700408 final int winRotation = mWindowManager.getDefaultDisplay().getRotation();
Mike Digmandd2f49e2018-03-16 10:54:22 -0700409 final boolean rotateSuggestionsDisabled = hasDisable2RotateSuggestionFlag(mDisabledFlags2);
Mike Digman3e33da62018-03-14 16:25:11 -0700410 if (DEBUG_ROTATION) {
411 Log.v(TAG, "onRotationProposal proposedRotation=" + Surface.rotationToString(rotation)
412 + ", winRotation=" + Surface.rotationToString(winRotation)
413 + ", isValid=" + isValid + ", mNavBarWindowState="
414 + StatusBarManager.windowStateToString(mNavigationBarWindowState)
Mike Digmandd2f49e2018-03-16 10:54:22 -0700415 + ", rotateSuggestionsDisabled=" + rotateSuggestionsDisabled
Mike Digman3e33da62018-03-14 16:25:11 -0700416 + ", isRotateButtonVisible=" + (mNavigationBarView == null ? "null" :
417 mNavigationBarView.isRotateButtonVisible()));
418 }
419
Mike Digmandd2f49e2018-03-16 10:54:22 -0700420 // Respect the disabled flag, no need for action as flag change callback will handle hiding
421 if (rotateSuggestionsDisabled) return;
422
Mike Digmane0777312018-01-19 12:41:51 -0800423 // This method will be called on rotation suggestion changes even if the proposed rotation
424 // is not valid for the top app. Use invalid rotation choices as a signal to remove the
425 // rotate button if shown.
Mike Digmane0777312018-01-19 12:41:51 -0800426 if (!isValid) {
Mike Digman1e28a5a2018-02-14 10:49:19 -0800427 setRotateSuggestionButtonState(false);
Mike Digmane0777312018-01-19 12:41:51 -0800428 return;
429 }
430
Mike Digman5aeca792018-03-05 11:14:39 -0800431 // If window rotation matches suggested rotation, remove any current suggestions
Mike Digman1e28a5a2018-02-14 10:49:19 -0800432 if (rotation == winRotation) {
Mike Digman5aeca792018-03-05 11:14:39 -0800433 getView().removeCallbacks(mRemoveRotationProposal);
Mike Digman1e28a5a2018-02-14 10:49:19 -0800434 setRotateSuggestionButtonState(false);
Mike Digman5aeca792018-03-05 11:14:39 -0800435 return;
Mike Digman7d092772018-01-11 12:10:32 -0800436 }
Mike Digman5aeca792018-03-05 11:14:39 -0800437
438 // Prepare to show the navbar icon by updating the icon style to change anim params
439 mLastRotationSuggestion = rotation; // Remember rotation for click
440 if (mNavigationBarView != null) {
441 final boolean rotationCCW = isRotationAnimationCCW(winRotation, rotation);
442 int style;
443 if (winRotation == Surface.ROTATION_0 || winRotation == Surface.ROTATION_180) {
444 style = rotationCCW ? R.style.RotateButtonCCWStart90 :
445 R.style.RotateButtonCWStart90;
446 } else { // 90 or 270
447 style = rotationCCW ? R.style.RotateButtonCCWStart0 :
448 R.style.RotateButtonCWStart0;
449 }
450 mNavigationBarView.updateRotateSuggestionButtonStyle(style, true);
451 }
452
453 if (mNavigationBarWindowState != WINDOW_STATE_SHOWING) {
454 // If the navbar isn't shown, flag the rotate icon to be shown should the navbar become
455 // visible given some time limit.
456 mPendingRotationSuggestion = true;
457 getView().removeCallbacks(mCancelPendingRotationProposal);
458 getView().postDelayed(mCancelPendingRotationProposal,
459 NAVBAR_HIDDEN_PENDING_ICON_TIMEOUT_MS);
460
461 } else { // The navbar is visible so show the icon right away
462 showAndLogRotationSuggestion();
463 }
464 }
465
Mike Digmandd2f49e2018-03-16 10:54:22 -0700466 private void onRotationSuggestionsDisabled() {
467 // Immediately hide the rotate button and clear any planned removal
468 setRotateSuggestionButtonState(false, true);
Mike Digmane14c4752018-03-20 12:14:54 -0700469
470 // This method can be called before view setup is done, ensure getView isn't null
471 final View v = getView();
472 if (v != null) v.removeCallbacks(mRemoveRotationProposal);
Mike Digmandd2f49e2018-03-16 10:54:22 -0700473 }
474
Mike Digman5aeca792018-03-05 11:14:39 -0800475 private void showAndLogRotationSuggestion() {
476 setRotateSuggestionButtonState(true);
477 rescheduleRotationTimeout(false);
478 mMetricsLogger.visible(MetricsEvent.ROTATION_SUGGESTION_SHOWN);
Mike Digman7d092772018-01-11 12:10:32 -0800479 }
480
Mike Digman1e28a5a2018-02-14 10:49:19 -0800481 private boolean isRotationAnimationCCW(int from, int to) {
482 // All 180deg WM rotation animations are CCW, match that
483 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_90) return false;
484 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_180) return true; //180d so CCW
485 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_270) return true;
486 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_0) return true;
487 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_180) return false;
488 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_270) return true; //180d so CCW
489 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_0) return true; //180d so CCW
490 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_90) return true;
491 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_270) return false;
492 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_0) return false;
493 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_90) return true; //180d so CCW
494 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_180) return true;
495 return false; // Default
Mike Digmana48cf192018-02-12 17:52:48 -0800496 }
497
Mike Digman1e28a5a2018-02-14 10:49:19 -0800498 public void setRotateSuggestionButtonState(final boolean visible) {
499 setRotateSuggestionButtonState(visible, false);
500 }
501
502 public void setRotateSuggestionButtonState(final boolean visible, final boolean force) {
503 if (mNavigationBarView == null) return;
504
505 // At any point the the button can become invisible because an a11y service became active.
506 // Similarly, a call to make the button visible may be rejected because an a11y service is
507 // active. Must account for this.
508
509 ButtonDispatcher rotBtn = mNavigationBarView.getRotateSuggestionButton();
510 final boolean currentlyVisible = mNavigationBarView.isRotateButtonVisible();
511
512 // Rerun a show animation to indicate change but don't rerun a hide animation
513 if (!visible && !currentlyVisible) return;
514
515 View view = rotBtn.getCurrentView();
516 if (view == null) return;
517
518 KeyButtonDrawable kbd = rotBtn.getImageDrawable();
519 if (kbd == null) return;
520
521 // The KBD and AVD is recreated every new valid suggestion because of style changes.
522 AnimatedVectorDrawable animIcon = null;
523 if (kbd.getDrawable(0) instanceof AnimatedVectorDrawable) {
524 animIcon = (AnimatedVectorDrawable) kbd.getDrawable(0);
525 }
526
Mike Digman5aeca792018-03-05 11:14:39 -0800527 // Clear any pending suggestion flag as it has either been nullified or is being shown
528 mPendingRotationSuggestion = false;
529 getView().removeCallbacks(mCancelPendingRotationProposal);
530
531 // Handle the visibility change and animation
Mike Digman1e28a5a2018-02-14 10:49:19 -0800532 if (visible) { // Appear and change (cannot force)
Mike Digman85a9bea2018-02-23 15:08:53 -0800533 // Stop and clear any currently running hide animations
Mike Digman1e28a5a2018-02-14 10:49:19 -0800534 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
Mike Digman85a9bea2018-02-23 15:08:53 -0800535 mRotateHideAnimator.cancel();
Mike Digman1e28a5a2018-02-14 10:49:19 -0800536 }
Mike Digman85a9bea2018-02-23 15:08:53 -0800537 mRotateHideAnimator = null;
Mike Digman1e28a5a2018-02-14 10:49:19 -0800538
539 // Reset the alpha if any has changed due to hide animation
540 view.setAlpha(1f);
541
542 // Run the rotate icon's animation if it has one
543 if (animIcon != null) {
544 animIcon.reset();
545 animIcon.start();
546 }
547
Mike Digman50752642018-02-15 13:36:09 -0800548 if (!isRotateSuggestionIntroduced()) mViewRippler.start(view);
549
Mike Digman1e28a5a2018-02-14 10:49:19 -0800550 // Set visibility, may fail if a11y service is active.
551 // If invisible, call will stop animation.
552 mNavigationBarView.setRotateButtonVisibility(true);
553
554 } else { // Hide
555
Mike Digman50752642018-02-15 13:36:09 -0800556 mViewRippler.stop(); // Prevent any pending ripples, force hide or not
557
Mike Digman1e28a5a2018-02-14 10:49:19 -0800558 if (force) {
559 // If a hide animator is running stop it and make invisible
560 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
561 mRotateHideAnimator.pause();
562 }
563 mNavigationBarView.setRotateButtonVisibility(false);
564 return;
565 }
566
567 // Don't start any new hide animations if one is running
568 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
569
570 ObjectAnimator fadeOut = ObjectAnimator.ofFloat(view, "alpha",
571 0f);
572 fadeOut.setDuration(BUTTON_FADE_IN_OUT_DURATION_MS);
573 fadeOut.setInterpolator(Interpolators.LINEAR);
574 fadeOut.addListener(new AnimatorListenerAdapter() {
575 @Override
576 public void onAnimationEnd(Animator animation) {
577 mNavigationBarView.setRotateButtonVisibility(false);
578 }
579 });
580
581 mRotateHideAnimator = fadeOut;
582 fadeOut.start();
Mike Digmana48cf192018-02-12 17:52:48 -0800583 }
584 }
585
Mike Digman90402952018-01-22 16:05:51 -0800586 private void rescheduleRotationTimeout(final boolean reasonHover) {
587 // May be called due to a new rotation proposal or a change in hover state
588 if (reasonHover) {
589 // Don't reschedule if a hide animator is running
Mike Digman1e28a5a2018-02-14 10:49:19 -0800590 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
Mike Digman90402952018-01-22 16:05:51 -0800591 // Don't reschedule if not visible
Mike Digman1e28a5a2018-02-14 10:49:19 -0800592 if (!mNavigationBarView.isRotateButtonVisible()) return;
Mike Digman90402952018-01-22 16:05:51 -0800593 }
594
Mike Digman5aeca792018-03-05 11:14:39 -0800595 getView().removeCallbacks(mRemoveRotationProposal); // Stop any pending removal
596 getView().postDelayed(mRemoveRotationProposal,
Mike Digman90402952018-01-22 16:05:51 -0800597 computeRotationProposalTimeout()); // Schedule timeout
598 }
599
600 private int computeRotationProposalTimeout() {
601 if (mAccessibilityFeedbackEnabled) return 20000;
602 if (mHoveringRotationSuggestion) return 16000;
603 return 6000;
604 }
605
Mike Digman50752642018-02-15 13:36:09 -0800606 private boolean isRotateSuggestionIntroduced() {
607 ContentResolver cr = getContext().getContentResolver();
608 return Settings.Secure.getInt(cr, Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED, 0)
609 >= NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION;
610 }
611
612 private void incrementNumAcceptedRotationSuggestionsIfNeeded() {
613 // Get the number of accepted suggestions
614 ContentResolver cr = getContext().getContentResolver();
615 final int numSuggestions = Settings.Secure.getInt(cr,
616 Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED, 0);
617
618 // Increment the number of accepted suggestions only if it would change intro mode
619 if (numSuggestions < NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION) {
620 Settings.Secure.putInt(cr, Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED,
621 numSuggestions + 1);
622 }
623 }
624
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500625 // Injected from StatusBar at creation.
Jason Monk49fa0162017-01-11 09:21:56 -0500626 public void setCurrentSysuiVisibility(int systemUiVisibility) {
627 mSystemUiVisibility = systemUiVisibility;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500628 mNavigationBarMode = mStatusBar.computeBarMode(0, mSystemUiVisibility,
Jason Monk49fa0162017-01-11 09:21:56 -0500629 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
630 View.NAVIGATION_BAR_TRANSPARENT);
631 checkNavBarModes();
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500632 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500633 mLightBarController.onNavigationVisibilityChanged(mSystemUiVisibility, 0 /* mask */,
634 true /* nbModeChanged */, mNavigationBarMode);
635 }
636
637 @Override
638 public void setSystemUiVisibility(int vis, int fullscreenStackVis, int dockedStackVis,
639 int mask, Rect fullscreenStackBounds, Rect dockedStackBounds) {
640 final int oldVal = mSystemUiVisibility;
641 final int newVal = (oldVal & ~mask) | (vis & mask);
642 final int diff = newVal ^ oldVal;
643 boolean nbModeChanged = false;
644 if (diff != 0) {
645 mSystemUiVisibility = newVal;
646
647 // update navigation bar mode
648 final int nbMode = getView() == null
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500649 ? -1 : mStatusBar.computeBarMode(oldVal, newVal,
Jason Monk49fa0162017-01-11 09:21:56 -0500650 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
651 View.NAVIGATION_BAR_TRANSPARENT);
652 nbModeChanged = nbMode != -1;
653 if (nbModeChanged) {
654 if (mNavigationBarMode != nbMode) {
655 mNavigationBarMode = nbMode;
656 checkNavBarModes();
657 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500658 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500659 }
660 }
661
662 mLightBarController.onNavigationVisibilityChanged(vis, mask, nbModeChanged,
663 mNavigationBarMode);
664 }
665
666 @Override
667 public void disable(int state1, int state2, boolean animate) {
Mike Digmandd2f49e2018-03-16 10:54:22 -0700668 // Navigation bar flags are in both state1 and state2.
669 final int masked = state1 & (StatusBarManager.DISABLE_HOME
Jason Monk49fa0162017-01-11 09:21:56 -0500670 | StatusBarManager.DISABLE_RECENT
671 | StatusBarManager.DISABLE_BACK
672 | StatusBarManager.DISABLE_SEARCH);
673 if (masked != mDisabledFlags1) {
674 mDisabledFlags1 = masked;
675 if (mNavigationBarView != null) mNavigationBarView.setDisabledFlags(state1);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000676 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500677 }
Mike Digmandd2f49e2018-03-16 10:54:22 -0700678
679 final int masked2 = state2 & (StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS);
680 if (masked2 != mDisabledFlags2) {
681 mDisabledFlags2 = masked2;
682 setDisabled2Flags(masked2);
683 }
684 }
685
686 private void setDisabled2Flags(int state2) {
687 // Method only called on change of disable2 flags
688 final boolean rotateSuggestionsDisabled = hasDisable2RotateSuggestionFlag(state2);
689 if (rotateSuggestionsDisabled) onRotationSuggestionsDisabled();
690 }
691
692 private boolean hasDisable2RotateSuggestionFlag(int disable2Flags) {
693 return (disable2Flags & StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS) != 0;
Jason Monk49fa0162017-01-11 09:21:56 -0500694 }
695
696 // ----- Internal stuffz -----
697
698 private void refreshLayout(int layoutDirection) {
699 if (mNavigationBarView != null) {
700 mNavigationBarView.setLayoutDirection(layoutDirection);
701 }
702 }
703
704 private boolean shouldDisableNavbarGestures() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500705 return !mStatusBar.isDeviceProvisioned()
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800706 || (mDisabledFlags1 & StatusBarManager.DISABLE_SEARCH) != 0;
Jason Monk49fa0162017-01-11 09:21:56 -0500707 }
708
709 private void repositionNavigationBar() {
710 if (mNavigationBarView == null || !mNavigationBarView.isAttachedToWindow()) return;
711
712 prepareNavigationBarView();
713
714 mWindowManager.updateViewLayout((View) mNavigationBarView.getParent(),
715 ((View) mNavigationBarView.getParent()).getLayoutParams());
716 }
717
Matthew Ng9c3bce52018-02-01 22:00:31 +0000718 private void updateScreenPinningGestures() {
719 if (mNavigationBarView == null) {
720 return;
721 }
722
723 // Change the cancel pin gesture to home and back if recents button is invisible
724 boolean recentsVisible = mNavigationBarView.isRecentsButtonVisible();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000725 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
726 if (recentsVisible) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000727 backButton.setOnLongClickListener(this::onLongPressBackRecents);
728 } else {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000729 backButton.setOnLongClickListener(this::onLongPressBackHome);
730 }
731 }
732
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100733 private void notifyNavigationBarScreenOn() {
Matthew Ngd0a73e72018-03-02 15:16:03 -0800734 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500735 }
736
737 private void prepareNavigationBarView() {
738 mNavigationBarView.reorient();
739
740 ButtonDispatcher recentsButton = mNavigationBarView.getRecentsButton();
741 recentsButton.setOnClickListener(this::onRecentsClick);
742 recentsButton.setOnTouchListener(this::onRecentsTouch);
743 recentsButton.setLongClickable(true);
744 recentsButton.setOnLongClickListener(this::onLongPressBackRecents);
745
746 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
747 backButton.setLongClickable(true);
Jason Monk49fa0162017-01-11 09:21:56 -0500748
749 ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();
750 homeButton.setOnTouchListener(this::onHomeTouch);
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800751 homeButton.setOnLongClickListener(this::onHomeLongClick);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800752
753 ButtonDispatcher accessibilityButton = mNavigationBarView.getAccessibilityButton();
754 accessibilityButton.setOnClickListener(this::onAccessibilityClick);
755 accessibilityButton.setOnLongClickListener(this::onAccessibilityLongClick);
Phil Weaverdb9a7742017-04-18 08:15:06 -0700756 updateAccessibilityServicesState(mAccessibilityManager);
Mike Digman7d092772018-01-11 12:10:32 -0800757
758 ButtonDispatcher rotateSuggestionButton = mNavigationBarView.getRotateSuggestionButton();
759 rotateSuggestionButton.setOnClickListener(this::onRotateSuggestionClick);
Mike Digman90402952018-01-22 16:05:51 -0800760 rotateSuggestionButton.setOnHoverListener(this::onRotateSuggestionHover);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000761 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500762 }
763
764 private boolean onHomeTouch(View v, MotionEvent event) {
765 if (mHomeBlockedThisTouch && event.getActionMasked() != MotionEvent.ACTION_DOWN) {
766 return true;
767 }
768 // If an incoming call is ringing, HOME is totally disabled.
769 // (The user is already on the InCallUI at this point,
770 // and his ONLY options are to answer or reject the call.)
771 switch (event.getAction()) {
772 case MotionEvent.ACTION_DOWN:
773 mHomeBlockedThisTouch = false;
774 TelecomManager telecomManager =
775 getContext().getSystemService(TelecomManager.class);
776 if (telecomManager != null && telecomManager.isRinging()) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500777 if (mStatusBar.isKeyguardShowing()) {
Jason Monk49fa0162017-01-11 09:21:56 -0500778 Log.i(TAG, "Ignoring HOME; there's a ringing incoming call. " +
779 "No heads up");
780 mHomeBlockedThisTouch = true;
781 return true;
782 }
783 }
784 break;
785 case MotionEvent.ACTION_UP:
786 case MotionEvent.ACTION_CANCEL:
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500787 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500788 break;
789 }
790 return false;
791 }
792
793 private void onVerticalChanged(boolean isVertical) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500794 mStatusBar.setQsScrimEnabled(!isVertical);
Jason Monk49fa0162017-01-11 09:21:56 -0500795 }
796
797 private boolean onNavigationTouch(View v, MotionEvent event) {
Eliot Courtneycb5d3162017-08-09 16:53:15 +0900798 mStatusBar.checkUserAutohide(event);
Jason Monk49fa0162017-01-11 09:21:56 -0500799 return false;
800 }
801
Jason Monk865246d2017-01-19 08:27:01 -0500802 @VisibleForTesting
803 boolean onHomeLongClick(View v) {
Matthew Ng6ff33b72018-02-27 13:47:38 -0800804 if (!mNavigationBarView.isRecentsButtonVisible()
805 && ActivityManagerWrapper.getInstance().isScreenPinningActive()) {
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800806 return onLongPressBackHome(v);
807 }
Jason Monk49fa0162017-01-11 09:21:56 -0500808 if (shouldDisableNavbarGestures()) {
809 return false;
810 }
Matthew Ng2ea93b72018-03-14 19:43:18 +0000811 mNavigationBarView.onNavigationButtonLongPress(v);
Mike Digmanc94759d2018-01-23 11:01:21 -0800812 mMetricsLogger.action(MetricsEvent.ACTION_ASSIST_LONG_PRESS);
Jason Monk49fa0162017-01-11 09:21:56 -0500813 mAssistManager.startAssist(new Bundle() /* args */);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500814 mStatusBar.awakenDreams();
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800815
Jason Monk49fa0162017-01-11 09:21:56 -0500816 if (mNavigationBarView != null) {
817 mNavigationBarView.abortCurrentGesture();
818 }
819 return true;
820 }
821
822 // additional optimization when we have software system buttons - start loading the recent
823 // tasks on touch down
824 private boolean onRecentsTouch(View v, MotionEvent event) {
825 int action = event.getAction() & MotionEvent.ACTION_MASK;
826 if (action == MotionEvent.ACTION_DOWN) {
827 mCommandQueue.preloadRecentApps();
828 } else if (action == MotionEvent.ACTION_CANCEL) {
829 mCommandQueue.cancelPreloadRecentApps();
830 } else if (action == MotionEvent.ACTION_UP) {
831 if (!v.isPressed()) {
832 mCommandQueue.cancelPreloadRecentApps();
833 }
834 }
835 return false;
836 }
837
838 private void onRecentsClick(View v) {
839 if (LatencyTracker.isEnabled(getContext())) {
840 LatencyTracker.getInstance(getContext()).onActionStart(
841 LatencyTracker.ACTION_TOGGLE_RECENTS);
842 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500843 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500844 mCommandQueue.toggleRecentApps();
845 }
846
Matthew Ng9c3bce52018-02-01 22:00:31 +0000847 private boolean onLongPressBackHome(View v) {
Matthew Ng2ea93b72018-03-14 19:43:18 +0000848 mNavigationBarView.onNavigationButtonLongPress(v);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000849 return onLongPressNavigationButtons(v, R.id.back, R.id.home);
850 }
851
852 private boolean onLongPressBackRecents(View v) {
Matthew Ng2ea93b72018-03-14 19:43:18 +0000853 mNavigationBarView.onNavigationButtonLongPress(v);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000854 return onLongPressNavigationButtons(v, R.id.back, R.id.recent_apps);
855 }
856
Jason Monk49fa0162017-01-11 09:21:56 -0500857 /**
Matthew Ng9c3bce52018-02-01 22:00:31 +0000858 * This handles long-press of both back and recents/home. Back is the common button with
859 * combination of recents if it is visible or home if recents is invisible.
860 * They are handled together to capture them both being long-pressed
Jason Monk49fa0162017-01-11 09:21:56 -0500861 * at the same time to exit screen pinning (lock task).
862 *
Matthew Ng9c3bce52018-02-01 22:00:31 +0000863 * When accessibility mode is on, only a long-press from recents/home
Jason Monk49fa0162017-01-11 09:21:56 -0500864 * is required to exit.
865 *
866 * In all other circumstances we try to pass through long-press events
867 * for Back, so that apps can still use it. Which can be from two things.
868 * 1) Not currently in screen pinning (lock task).
Matthew Ng9c3bce52018-02-01 22:00:31 +0000869 * 2) Back is long-pressed without recents/home.
Jason Monk49fa0162017-01-11 09:21:56 -0500870 */
Matthew Ng9c3bce52018-02-01 22:00:31 +0000871 private boolean onLongPressNavigationButtons(View v, @IdRes int btnId1, @IdRes int btnId2) {
Jason Monk49fa0162017-01-11 09:21:56 -0500872 try {
873 boolean sendBackLongPress = false;
874 IActivityManager activityManager = ActivityManagerNative.getDefault();
875 boolean touchExplorationEnabled = mAccessibilityManager.isTouchExplorationEnabled();
876 boolean inLockTaskMode = activityManager.isInLockTaskMode();
877 if (inLockTaskMode && !touchExplorationEnabled) {
878 long time = System.currentTimeMillis();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000879
Jason Monk49fa0162017-01-11 09:21:56 -0500880 // If we recently long-pressed the other button then they were
881 // long-pressed 'together'
882 if ((time - mLastLockToAppLongPress) < LOCK_TO_APP_GESTURE_TOLERENCE) {
Benjamin Franza83859f2017-07-03 16:34:14 +0100883 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500884 // When exiting refresh disabled flags.
Matthew Ngd0a73e72018-03-02 15:16:03 -0800885 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500886 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000887 } else if (v.getId() == btnId1) {
888 ButtonDispatcher button = btnId2 == R.id.recent_apps
889 ? mNavigationBarView.getRecentsButton()
890 : mNavigationBarView.getHomeButton();
891 if (!button.getCurrentView().isPressed()) {
892 // If we aren't pressing recents/home right now then they presses
893 // won't be together, so send the standard long-press action.
894 sendBackLongPress = true;
895 }
Jason Monk49fa0162017-01-11 09:21:56 -0500896 }
897 mLastLockToAppLongPress = time;
898 } else {
899 // If this is back still need to handle sending the long-press event.
Matthew Ng9c3bce52018-02-01 22:00:31 +0000900 if (v.getId() == btnId1) {
Jason Monk49fa0162017-01-11 09:21:56 -0500901 sendBackLongPress = true;
902 } else if (touchExplorationEnabled && inLockTaskMode) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000903 // When in accessibility mode a long press that is recents/home (not back)
Jason Monk49fa0162017-01-11 09:21:56 -0500904 // should stop lock task.
Benjamin Franza83859f2017-07-03 16:34:14 +0100905 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500906 // When exiting refresh disabled flags.
Matthew Ngd0a73e72018-03-02 15:16:03 -0800907 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500908 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000909 } else if (v.getId() == btnId2) {
910 return btnId2 == R.id.recent_apps
911 ? onLongPressRecents()
912 : onHomeLongClick(mNavigationBarView.getHomeButton().getCurrentView());
Jason Monk49fa0162017-01-11 09:21:56 -0500913 }
914 }
915 if (sendBackLongPress) {
916 KeyButtonView keyButtonView = (KeyButtonView) v;
917 keyButtonView.sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS);
918 keyButtonView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
919 return true;
920 }
921 } catch (RemoteException e) {
922 Log.d(TAG, "Unable to reach activity manager", e);
923 }
924 return false;
925 }
926
927 private boolean onLongPressRecents() {
Erik Wolsheimer9be3a062017-05-31 14:59:57 -0700928 if (mRecents == null || !ActivityManager.supportsMultiWindow(getContext())
Matthew Ng43db6d22017-06-27 15:29:39 -0700929 || !mDivider.getView().getSnapAlgorithm().isSplitScreenFeasible()
Winson Chung074c4342018-03-26 17:27:19 -0700930 || Recents.getConfiguration().isLowRamDevice
931 // If we are connected to the overview service, then disable the recents button
932 || mOverviewProxyService.getProxy() != null) {
Jason Monk49fa0162017-01-11 09:21:56 -0500933 return false;
934 }
935
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500936 return mStatusBar.toggleSplitScreenMode(MetricsEvent.ACTION_WINDOW_DOCK_LONGPRESS,
Jason Monk49fa0162017-01-11 09:21:56 -0500937 MetricsEvent.ACTION_WINDOW_UNDOCK_LONGPRESS);
938 }
939
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800940 private void onAccessibilityClick(View v) {
941 mAccessibilityManager.notifyAccessibilityButtonClicked();
942 }
943
944 private boolean onAccessibilityLongClick(View v) {
Casey Burkhardt5e8b9802017-03-24 10:07:20 -0700945 Intent intent = new Intent(AccessibilityManager.ACTION_CHOOSE_ACCESSIBILITY_BUTTON);
946 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Casey Burkhardt5d614402017-04-06 13:46:50 -0700947 v.getContext().startActivityAsUser(intent, UserHandle.CURRENT);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800948 return true;
949 }
950
Phil Weaverdb9a7742017-04-18 08:15:06 -0700951 private void updateAccessibilityServicesState(AccessibilityManager accessibilityManager) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800952 int requestingServices = 0;
953 try {
Casey Burkhardt5d614402017-04-06 13:46:50 -0700954 if (Settings.Secure.getIntForUser(mContentResolver,
955 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED,
956 UserHandle.USER_CURRENT) == 1) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800957 requestingServices++;
958 }
959 } catch (Settings.SettingNotFoundException e) {
960 }
961
Mike Digman90402952018-01-22 16:05:51 -0800962 boolean feedbackEnabled = false;
Casey Burkhardt5d614402017-04-06 13:46:50 -0700963 // AccessibilityManagerService resolves services for the current user since the local
964 // AccessibilityManager is created from a Context with the INTERACT_ACROSS_USERS permission
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800965 final List<AccessibilityServiceInfo> services =
Phil Weaverdb9a7742017-04-18 08:15:06 -0700966 accessibilityManager.getEnabledAccessibilityServiceList(
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800967 AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800968 for (int i = services.size() - 1; i >= 0; --i) {
969 AccessibilityServiceInfo info = services.get(i);
970 if ((info.flags & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0) {
971 requestingServices++;
972 }
Mike Digman90402952018-01-22 16:05:51 -0800973
974 if (info.feedbackType != 0 && info.feedbackType !=
975 AccessibilityServiceInfo.FEEDBACK_GENERIC) {
976 feedbackEnabled = true;
977 }
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800978 }
979
Mike Digman90402952018-01-22 16:05:51 -0800980 mAccessibilityFeedbackEnabled = feedbackEnabled;
981
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800982 final boolean showAccessibilityButton = requestingServices >= 1;
983 final boolean targetSelection = requestingServices >= 2;
984 mNavigationBarView.setAccessibilityButtonState(showAccessibilityButton, targetSelection);
985 }
986
Mike Digman7d092772018-01-11 12:10:32 -0800987 private void onRotateSuggestionClick(View v) {
Mike Digmanc94759d2018-01-23 11:01:21 -0800988 mMetricsLogger.action(MetricsEvent.ACTION_ROTATION_SUGGESTION_ACCEPTED);
Mike Digman50752642018-02-15 13:36:09 -0800989 incrementNumAcceptedRotationSuggestionsIfNeeded();
Mike Digman7d092772018-01-11 12:10:32 -0800990 mRotationLockController.setRotationLockedAtAngle(true, mLastRotationSuggestion);
991 }
992
Mike Digman90402952018-01-22 16:05:51 -0800993 private boolean onRotateSuggestionHover(View v, MotionEvent event) {
994 final int action = event.getActionMasked();
995 mHoveringRotationSuggestion = (action == MotionEvent.ACTION_HOVER_ENTER)
996 || (action == MotionEvent.ACTION_HOVER_MOVE);
997 rescheduleRotationTimeout(true);
998 return false; // Must return false so a11y hover events are dispatched correctly.
999 }
1000
Jason Monk2a6ea9c2017-01-26 11:14:51 -05001001 // ----- Methods that StatusBar talks to (should be minimized) -----
Jason Monk49fa0162017-01-11 09:21:56 -05001002
Jason Monk49fa0162017-01-11 09:21:56 -05001003 public void setLightBarController(LightBarController lightBarController) {
1004 mLightBarController = lightBarController;
1005 mLightBarController.setNavigationBar(mNavigationBarView.getLightTransitionsController());
1006 }
1007
1008 public boolean isSemiTransparent() {
1009 return mNavigationBarMode == MODE_SEMI_TRANSPARENT;
1010 }
1011
Jason Monk49fa0162017-01-11 09:21:56 -05001012 public void disableAnimationsDuringHide(long delay) {
1013 mNavigationBarView.setLayoutTransitionsEnabled(false);
1014 mNavigationBarView.postDelayed(() -> mNavigationBarView.setLayoutTransitionsEnabled(true),
1015 delay + StackStateAnimator.ANIMATION_DURATION_GO_TO_FULL_SHADE);
1016 }
1017
Jason Monk49fa0162017-01-11 09:21:56 -05001018 public BarTransitions getBarTransitions() {
1019 return mNavigationBarView.getBarTransitions();
1020 }
1021
1022 public void checkNavBarModes() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -05001023 mStatusBar.checkBarMode(mNavigationBarMode,
Jason Monk49fa0162017-01-11 09:21:56 -05001024 mNavigationBarWindowState, mNavigationBarView.getBarTransitions());
1025 }
1026
1027 public void finishBarAnimations() {
1028 mNavigationBarView.getBarTransitions().finishAnimations();
1029 }
1030
Jason Monk91e587e2017-04-13 13:41:23 -04001031 private final AccessibilityServicesStateChangeListener mAccessibilityListener =
1032 this::updateAccessibilityServicesState;
1033
Casey Burkhardt74922c62017-02-13 12:43:16 -08001034 private class MagnificationContentObserver extends ContentObserver {
1035
1036 public MagnificationContentObserver(Handler handler) {
1037 super(handler);
1038 }
1039
1040 @Override
1041 public void onChange(boolean selfChange) {
Phil Weaverdb9a7742017-04-18 08:15:06 -07001042 NavigationBarFragment.this.updateAccessibilityServicesState(mAccessibilityManager);
Casey Burkhardt74922c62017-02-13 12:43:16 -08001043 }
1044 }
1045
Jason Monk49fa0162017-01-11 09:21:56 -05001046 private final Stub mRotationWatcher = new Stub() {
1047 @Override
Mike Digman90402952018-01-22 16:05:51 -08001048 public void onRotationChanged(final int rotation) throws RemoteException {
Jason Monk49fa0162017-01-11 09:21:56 -05001049 // We need this to be scheduled as early as possible to beat the redrawing of
1050 // window in response to the orientation change.
1051 Handler h = getView().getHandler();
1052 Message msg = Message.obtain(h, () -> {
Mike Digman85ff7fa2018-01-23 14:59:52 -08001053
1054 // If the screen rotation changes while locked, potentially update lock to flow with
Mike Digman90402952018-01-22 16:05:51 -08001055 // new screen rotation and hide any showing suggestions.
1056 if (mRotationLockController.isRotationLocked()) {
Mike Digman85ff7fa2018-01-23 14:59:52 -08001057 if (shouldOverrideUserLockPrefs(rotation)) {
1058 mRotationLockController.setRotationLockedAtAngle(true, rotation);
1059 }
Mike Digman1e28a5a2018-02-14 10:49:19 -08001060 setRotateSuggestionButtonState(false, true);
Mike Digman90402952018-01-22 16:05:51 -08001061 }
1062
Jason Monk49fa0162017-01-11 09:21:56 -05001063 if (mNavigationBarView != null
1064 && mNavigationBarView.needsReorient(rotation)) {
1065 repositionNavigationBar();
1066 }
1067 });
1068 msg.setAsynchronous(true);
1069 h.sendMessageAtFrontOfQueue(msg);
1070 }
Mike Digman85ff7fa2018-01-23 14:59:52 -08001071
1072 private boolean shouldOverrideUserLockPrefs(final int rotation) {
Mike Digmanab650252018-03-06 11:01:41 -08001073 // Only override user prefs when returning to the natural rotation (normally portrait).
Mike Digman85ff7fa2018-01-23 14:59:52 -08001074 // Don't let apps that force landscape or 180 alter user lock.
Mike Digmanab650252018-03-06 11:01:41 -08001075 return rotation == NATURAL_ROTATION;
Mike Digman85ff7fa2018-01-23 14:59:52 -08001076 }
Jason Monk49fa0162017-01-11 09:21:56 -05001077 };
1078
1079 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1080 @Override
1081 public void onReceive(Context context, Intent intent) {
1082 String action = intent.getAction();
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +01001083 if (Intent.ACTION_SCREEN_OFF.equals(action)
1084 || Intent.ACTION_SCREEN_ON.equals(action)) {
1085 notifyNavigationBarScreenOn();
Jason Monk49fa0162017-01-11 09:21:56 -05001086 }
1087 }
1088 };
1089
Mike Digman7d092772018-01-11 12:10:32 -08001090 class TaskStackListenerImpl extends SysUiTaskStackChangeListener {
1091 // Invalidate any rotation suggestion on task change or activity orientation change
1092 // Note: all callbacks happen on main thread
1093
1094 @Override
1095 public void onTaskStackChanged() {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001096 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001097 }
1098
1099 @Override
1100 public void onTaskRemoved(int taskId) {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001101 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001102 }
1103
1104 @Override
1105 public void onTaskMovedToFront(int taskId) {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001106 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001107 }
1108
1109 @Override
1110 public void onActivityRequestedOrientationChanged(int taskId, int requestedOrientation) {
Mike Digman85a9bea2018-02-23 15:08:53 -08001111 // Only hide the icon if the top task changes its requestedOrientation
1112 // Launcher can alter its requestedOrientation while it's not on top, don't hide on this
Mike Digman121b11f2018-04-04 12:45:24 -07001113 Optional.ofNullable(ActivityManagerWrapper.getInstance())
1114 .map(ActivityManagerWrapper::getRunningTask)
1115 .ifPresent(a -> {
1116 if (a.id == taskId) setRotateSuggestionButtonState(false);
1117 });
Mike Digman7d092772018-01-11 12:10:32 -08001118 }
1119 }
1120
Mike Digman50752642018-02-15 13:36:09 -08001121 private class ViewRippler {
1122 private static final int RIPPLE_OFFSET_MS = 50;
1123 private static final int RIPPLE_INTERVAL_MS = 2000;
1124 private View mRoot;
1125
1126 public void start(View root) {
1127 stop(); // Stop any pending ripple animations
1128
1129 mRoot = root;
1130
1131 // Schedule pending ripples, offset the 1st to avoid problems with visibility change
1132 mRoot.postOnAnimationDelayed(mRipple, RIPPLE_OFFSET_MS);
1133 mRoot.postOnAnimationDelayed(mRipple, RIPPLE_INTERVAL_MS);
1134 mRoot.postOnAnimationDelayed(mRipple, 2*RIPPLE_INTERVAL_MS);
1135 }
1136
1137 public void stop() {
1138 if (mRoot != null) mRoot.removeCallbacks(mRipple);
1139 }
1140
1141 private final Runnable mRipple = new Runnable() {
1142 @Override
1143 public void run() { // Cause the ripple to fire via false presses
1144 mRoot.setPressed(true);
1145 mRoot.setPressed(false);
1146 }
1147 };
1148 }
1149
Jason Monk49fa0162017-01-11 09:21:56 -05001150 public static View create(Context context, FragmentListener listener) {
1151 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
1152 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
1153 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR,
1154 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
1155 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1156 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
1157 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1158 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
1159 | WindowManager.LayoutParams.FLAG_SLIPPERY,
1160 PixelFormat.TRANSLUCENT);
1161 lp.token = new Binder();
Jason Monk49fa0162017-01-11 09:21:56 -05001162 lp.setTitle("NavigationBar");
Phil Weaver8583ae82018-02-13 11:01:24 -08001163 lp.accessibilityTitle = context.getString(R.string.nav_bar);
Jason Monk49fa0162017-01-11 09:21:56 -05001164 lp.windowAnimations = 0;
1165
1166 View navigationBarView = LayoutInflater.from(context).inflate(
1167 R.layout.navigation_bar_window, null);
1168
1169 if (DEBUG) Log.v(TAG, "addNavigationBar: about to add " + navigationBarView);
1170 if (navigationBarView == null) return null;
1171
1172 context.getSystemService(WindowManager.class).addView(navigationBarView, lp);
1173 FragmentHostManager fragmentHost = FragmentHostManager.get(navigationBarView);
1174 NavigationBarFragment fragment = new NavigationBarFragment();
1175 fragmentHost.getFragmentManager().beginTransaction()
1176 .replace(R.id.navigation_bar_frame, fragment, TAG)
1177 .commit();
1178 fragmentHost.addTagListener(TAG, listener);
1179 return navigationBarView;
1180 }
1181}