blob: ce8e746ce3957c9042367e47fb94f68bf3cd04be [file] [log] [blame]
Jason Monk49fa0162017-01-11 09:21:56 -05001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.statusbar.phone;
16
17import static android.app.StatusBarManager.NAVIGATION_HINT_BACK_ALT;
18import static android.app.StatusBarManager.NAVIGATION_HINT_IME_SHOWN;
19import static android.app.StatusBarManager.WINDOW_STATE_SHOWING;
20import static android.app.StatusBarManager.windowStateToString;
21
Mike Digmanab650252018-03-06 11:01:41 -080022import static com.android.internal.view.RotationPolicy.NATURAL_ROTATION;
23
Matthew Ng8f25fb962018-01-16 17:17:24 -080024import static com.android.systemui.shared.system.NavigationBarCompat.InteractionType;
Jason Monk49fa0162017-01-11 09:21:56 -050025import static com.android.systemui.statusbar.phone.BarTransitions.MODE_SEMI_TRANSPARENT;
Jason Monk2a6ea9c2017-01-26 11:14:51 -050026import static com.android.systemui.statusbar.phone.StatusBar.DEBUG_WINDOW_STATE;
27import static com.android.systemui.statusbar.phone.StatusBar.dumpBarTransitions;
Matthew Ng9c3bce52018-02-01 22:00:31 +000028import static com.android.systemui.OverviewProxyService.OverviewProxyListener;
Jason Monk49fa0162017-01-11 09:21:56 -050029
Casey Burkhardt048c2bc2016-12-08 16:09:20 -080030import android.accessibilityservice.AccessibilityServiceInfo;
Mike Digman7d092772018-01-11 12:10:32 -080031import android.animation.Animator;
32import android.animation.AnimatorListenerAdapter;
Mike Digman7d092772018-01-11 12:10:32 -080033import android.animation.ObjectAnimator;
Matthew Ng9c3bce52018-02-01 22:00:31 +000034import android.annotation.IdRes;
Jason Monk49fa0162017-01-11 09:21:56 -050035import android.annotation.Nullable;
36import android.app.ActivityManager;
37import android.app.ActivityManagerNative;
38import android.app.Fragment;
39import android.app.IActivityManager;
40import android.app.StatusBarManager;
41import android.content.BroadcastReceiver;
Casey Burkhardtb9dcd662017-03-20 15:10:16 -070042import android.content.ContentResolver;
Jason Monk49fa0162017-01-11 09:21:56 -050043import android.content.Context;
44import android.content.Intent;
45import android.content.IntentFilter;
46import android.content.res.Configuration;
Casey Burkhardt74922c62017-02-13 12:43:16 -080047import android.database.ContentObserver;
Jason Monk49fa0162017-01-11 09:21:56 -050048import android.graphics.PixelFormat;
49import android.graphics.Rect;
Mike Digman7d092772018-01-11 12:10:32 -080050import android.graphics.drawable.AnimatedVectorDrawable;
Jason Monk49fa0162017-01-11 09:21:56 -050051import android.inputmethodservice.InputMethodService;
52import android.os.Binder;
53import android.os.Bundle;
54import android.os.Handler;
55import android.os.IBinder;
56import android.os.Message;
Jason Monk49fa0162017-01-11 09:21:56 -050057import android.os.RemoteException;
58import android.os.UserHandle;
Casey Burkhardt74922c62017-02-13 12:43:16 -080059import android.provider.Settings;
Jason Monk865246d2017-01-19 08:27:01 -050060import android.support.annotation.VisibleForTesting;
Jason Monk49fa0162017-01-11 09:21:56 -050061import android.telecom.TelecomManager;
62import android.text.TextUtils;
63import android.util.Log;
64import android.view.IRotationWatcher.Stub;
65import android.view.KeyEvent;
66import android.view.LayoutInflater;
67import android.view.MotionEvent;
Mike Digman85ff7fa2018-01-23 14:59:52 -080068import android.view.Surface;
Jason Monk49fa0162017-01-11 09:21:56 -050069import android.view.View;
70import android.view.ViewGroup;
71import android.view.WindowManager;
72import android.view.WindowManager.LayoutParams;
73import android.view.WindowManagerGlobal;
74import android.view.accessibility.AccessibilityEvent;
75import android.view.accessibility.AccessibilityManager;
Jason Monk91e587e2017-04-13 13:41:23 -040076import android.view.accessibility.AccessibilityManager.AccessibilityServicesStateChangeListener;
Jason Monk49fa0162017-01-11 09:21:56 -050077
78import com.android.internal.logging.MetricsLogger;
79import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monkea03be12017-12-04 11:08:41 -050080import com.android.internal.util.LatencyTracker;
Jason Monk9c7844c2017-01-18 15:21:53 -050081import com.android.systemui.Dependency;
Mike Digman7d092772018-01-11 12:10:32 -080082import com.android.systemui.Interpolators;
Mike Digman1e28a5a2018-02-14 10:49:19 -080083import com.android.systemui.OverviewProxyService;
Jason Monk49fa0162017-01-11 09:21:56 -050084import com.android.systemui.R;
Jason Monk9c7844c2017-01-18 15:21:53 -050085import com.android.systemui.SysUiServiceProvider;
Jason Monk49fa0162017-01-11 09:21:56 -050086import com.android.systemui.assist.AssistManager;
87import com.android.systemui.fragments.FragmentHostManager;
88import com.android.systemui.fragments.FragmentHostManager.FragmentListener;
89import com.android.systemui.recents.Recents;
Mike Digman7d092772018-01-11 12:10:32 -080090import com.android.systemui.recents.misc.SysUiTaskStackChangeListener;
91import com.android.systemui.shared.system.ActivityManagerWrapper;
Matthew Ng64543e62018-02-28 17:35:10 -080092import com.android.systemui.shared.system.WindowManagerWrapper;
Jason Monk49fa0162017-01-11 09:21:56 -050093import com.android.systemui.stackdivider.Divider;
94import com.android.systemui.statusbar.CommandQueue;
95import com.android.systemui.statusbar.CommandQueue.Callbacks;
Jason Monk91e587e2017-04-13 13:41:23 -040096import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper;
Mike Digman7d092772018-01-11 12:10:32 -080097import com.android.systemui.statusbar.policy.KeyButtonDrawable;
Jason Monk49fa0162017-01-11 09:21:56 -050098import com.android.systemui.statusbar.policy.KeyButtonView;
Mike Digman7d092772018-01-11 12:10:32 -080099import com.android.systemui.statusbar.policy.RotationLockController;
Jason Monk49fa0162017-01-11 09:21:56 -0500100import com.android.systemui.statusbar.stack.StackStateAnimator;
101
102import java.io.FileDescriptor;
103import java.io.PrintWriter;
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800104import java.util.List;
Jason Monk49fa0162017-01-11 09:21:56 -0500105import java.util.Locale;
106
107/**
108 * Fragment containing the NavigationBarFragment. Contains logic for what happens
109 * on clicks and view states of the nav bar.
110 */
111public class NavigationBarFragment extends Fragment implements Callbacks {
112
Jason Monkd4afe152017-05-01 15:37:43 -0400113 public static final String TAG = "NavigationBar";
Jason Monk49fa0162017-01-11 09:21:56 -0500114 private static final boolean DEBUG = false;
115 private static final String EXTRA_DISABLE_STATE = "disabled_state";
116
Mike Digman1e28a5a2018-02-14 10:49:19 -0800117 private final static int BUTTON_FADE_IN_OUT_DURATION_MS = 100;
Mike Digman5aeca792018-03-05 11:14:39 -0800118 private final static int NAVBAR_HIDDEN_PENDING_ICON_TIMEOUT_MS = 20000;
Mike Digman1e28a5a2018-02-14 10:49:19 -0800119
Mike Digman50752642018-02-15 13:36:09 -0800120 private static final int NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION = 3;
121
Jason Monk49fa0162017-01-11 09:21:56 -0500122 /** Allow some time inbetween the long press for back and recents. */
123 private static final int LOCK_TO_APP_GESTURE_TOLERENCE = 200;
124
125 protected NavigationBarView mNavigationBarView = null;
126 protected AssistManager mAssistManager;
127
128 private int mNavigationBarWindowState = WINDOW_STATE_SHOWING;
129
130 private int mNavigationIconHints = 0;
131 private int mNavigationBarMode;
Mike Digman90402952018-01-22 16:05:51 -0800132 private boolean mAccessibilityFeedbackEnabled;
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800133 private AccessibilityManager mAccessibilityManager;
Casey Burkhardt74922c62017-02-13 12:43:16 -0800134 private MagnificationContentObserver mMagnificationObserver;
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700135 private ContentResolver mContentResolver;
Mike Digmanc94759d2018-01-23 11:01:21 -0800136 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500137
138 private int mDisabledFlags1;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500139 private StatusBar mStatusBar;
Jason Monk49fa0162017-01-11 09:21:56 -0500140 private Recents mRecents;
141 private Divider mDivider;
142 private WindowManager mWindowManager;
143 private CommandQueue mCommandQueue;
144 private long mLastLockToAppLongPress;
145
146 private Locale mLocale;
147 private int mLayoutDirection;
148
149 private int mSystemUiVisibility;
150 private LightBarController mLightBarController;
Jason Monk49fa0162017-01-11 09:21:56 -0500151
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800152 private OverviewProxyService mOverviewProxyService;
153
Jason Monk49fa0162017-01-11 09:21:56 -0500154 public boolean mHomeBlockedThisTouch;
155
Mike Digman7d092772018-01-11 12:10:32 -0800156 private int mLastRotationSuggestion;
Mike Digman5aeca792018-03-05 11:14:39 -0800157 private boolean mPendingRotationSuggestion;
Mike Digman90402952018-01-22 16:05:51 -0800158 private boolean mHoveringRotationSuggestion;
Mike Digman7d092772018-01-11 12:10:32 -0800159 private RotationLockController mRotationLockController;
160 private TaskStackListenerImpl mTaskStackListener;
161
Mike Digman1e28a5a2018-02-14 10:49:19 -0800162 private final Runnable mRemoveRotationProposal = () -> setRotateSuggestionButtonState(false);
Mike Digman5aeca792018-03-05 11:14:39 -0800163 private final Runnable mCancelPendingRotationProposal =
164 () -> mPendingRotationSuggestion = false;
Mike Digman7d092772018-01-11 12:10:32 -0800165 private Animator mRotateHideAnimator;
Mike Digman50752642018-02-15 13:36:09 -0800166 private ViewRippler mViewRippler = new ViewRippler();
Mike Digman7d092772018-01-11 12:10:32 -0800167
Matthew Ng9c3bce52018-02-01 22:00:31 +0000168 private final OverviewProxyListener mOverviewProxyListener = new OverviewProxyListener() {
169 @Override
170 public void onConnectionChanged(boolean isConnected) {
171 mNavigationBarView.onOverviewProxyConnectionChanged(isConnected);
172 updateScreenPinningGestures();
Matthew Ng64543e62018-02-28 17:35:10 -0800173 WindowManagerWrapper.getInstance()
174 .setNavBarVirtualKeyHapticFeedbackEnabled(!isConnected);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000175 }
176
177 @Override
Matthew Ng2ea93b72018-03-14 19:43:18 +0000178 public void onQuickStepStarted() {
179 mNavigationBarView.onQuickStepStarted();
Mike Digman85a9bea2018-02-23 15:08:53 -0800180
181 // Use navbar dragging as a signal to hide the rotate button
182 setRotateSuggestionButtonState(false);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000183 }
Matthew Ng8f25fb962018-01-16 17:17:24 -0800184
185 @Override
186 public void onInteractionFlagsChanged(@InteractionType int flags) {
187 mNavigationBarView.updateStates();
188 }
Matthew Ng9c3bce52018-02-01 22:00:31 +0000189 };
Mike Digman7d092772018-01-11 12:10:32 -0800190
Jason Monk49fa0162017-01-11 09:21:56 -0500191 // ----- Fragment Lifecycle Callbacks -----
192
193 @Override
194 public void onCreate(@Nullable Bundle savedInstanceState) {
195 super.onCreate(savedInstanceState);
Jason Monk9c7844c2017-01-18 15:21:53 -0500196 mCommandQueue = SysUiServiceProvider.getComponent(getContext(), CommandQueue.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500197 mCommandQueue.addCallbacks(this);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500198 mStatusBar = SysUiServiceProvider.getComponent(getContext(), StatusBar.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500199 mRecents = SysUiServiceProvider.getComponent(getContext(), Recents.class);
200 mDivider = SysUiServiceProvider.getComponent(getContext(), Divider.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500201 mWindowManager = getContext().getSystemService(WindowManager.class);
202 mAccessibilityManager = getContext().getSystemService(AccessibilityManager.class);
Jason Monk91e587e2017-04-13 13:41:23 -0400203 Dependency.get(AccessibilityManagerWrapper.class).addCallback(
204 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700205 mContentResolver = getContext().getContentResolver();
Casey Burkhardt74922c62017-02-13 12:43:16 -0800206 mMagnificationObserver = new MagnificationContentObserver(
207 getContext().getMainThreadHandler());
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700208 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(
Casey Burkhardt74922c62017-02-13 12:43:16 -0800209 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED), false,
Casey Burkhardt5d614402017-04-06 13:46:50 -0700210 mMagnificationObserver, UserHandle.USER_ALL);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800211
Jason Monk49fa0162017-01-11 09:21:56 -0500212 if (savedInstanceState != null) {
213 mDisabledFlags1 = savedInstanceState.getInt(EXTRA_DISABLE_STATE, 0);
214 }
Jason Monk9c7844c2017-01-18 15:21:53 -0500215 mAssistManager = Dependency.get(AssistManager.class);
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800216 mOverviewProxyService = Dependency.get(OverviewProxyService.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500217
218 try {
219 WindowManagerGlobal.getWindowManagerService()
Andrii Kulian35fa3c22017-03-11 09:37:28 -0800220 .watchRotation(mRotationWatcher, getContext().getDisplay().getDisplayId());
Jason Monk49fa0162017-01-11 09:21:56 -0500221 } catch (RemoteException e) {
222 throw e.rethrowFromSystemServer();
223 }
Mike Digman7d092772018-01-11 12:10:32 -0800224
225 mRotationLockController = Dependency.get(RotationLockController.class);
226
Mike Digmanab650252018-03-06 11:01:41 -0800227 // Reset user rotation pref to match that of the WindowManager if starting in locked mode
228 // This will automatically happen when switching from auto-rotate to locked mode
229 if (mRotationLockController.isRotationLocked()) {
230 final int winRotation = mWindowManager.getDefaultDisplay().getRotation();
231 mRotationLockController.setRotationLockedAtAngle(true, winRotation);
232 }
233
Mike Digman7d092772018-01-11 12:10:32 -0800234 // Register the task stack listener
235 mTaskStackListener = new TaskStackListenerImpl();
236 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskStackListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500237 }
238
239 @Override
240 public void onDestroy() {
241 super.onDestroy();
242 mCommandQueue.removeCallbacks(this);
Jason Monk91e587e2017-04-13 13:41:23 -0400243 Dependency.get(AccessibilityManagerWrapper.class).removeCallback(
244 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700245 mContentResolver.unregisterContentObserver(mMagnificationObserver);
Jason Monk49fa0162017-01-11 09:21:56 -0500246 try {
247 WindowManagerGlobal.getWindowManagerService()
248 .removeRotationWatcher(mRotationWatcher);
249 } catch (RemoteException e) {
250 throw e.rethrowFromSystemServer();
251 }
Mike Digman7d092772018-01-11 12:10:32 -0800252
253 // Unregister the task stack listener
254 ActivityManagerWrapper.getInstance().unregisterTaskStackListener(mTaskStackListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500255 }
256
257 @Override
258 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
259 Bundle savedInstanceState) {
260 return inflater.inflate(R.layout.navigation_bar, container, false);
261 }
262
263 @Override
264 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
265 super.onViewCreated(view, savedInstanceState);
266 mNavigationBarView = (NavigationBarView) view;
267
268 mNavigationBarView.setDisabledFlags(mDisabledFlags1);
Matthew Ng78f88d12018-01-23 12:39:55 -0800269 mNavigationBarView.setComponents(mRecents, mDivider, mStatusBar.getPanel());
Jason Monk49fa0162017-01-11 09:21:56 -0500270 mNavigationBarView.setOnVerticalChangedListener(this::onVerticalChanged);
271 mNavigationBarView.setOnTouchListener(this::onNavigationTouch);
272 if (savedInstanceState != null) {
273 mNavigationBarView.getLightTransitionsController().restoreState(savedInstanceState);
274 }
275
276 prepareNavigationBarView();
277 checkNavBarModes();
278
279 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
280 filter.addAction(Intent.ACTION_SCREEN_ON);
281 getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100282 notifyNavigationBarScreenOn();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000283 mOverviewProxyService.addCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500284 }
285
286 @Override
287 public void onDestroyView() {
288 super.onDestroyView();
Jason Monkaa573e92017-01-27 17:00:29 -0500289 mNavigationBarView.getLightTransitionsController().destroy(getContext());
Matthew Ng9c3bce52018-02-01 22:00:31 +0000290 mOverviewProxyService.removeCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500291 getContext().unregisterReceiver(mBroadcastReceiver);
292 }
293
294 @Override
295 public void onSaveInstanceState(Bundle outState) {
296 super.onSaveInstanceState(outState);
297 outState.putInt(EXTRA_DISABLE_STATE, mDisabledFlags1);
298 if (mNavigationBarView != null) {
299 mNavigationBarView.getLightTransitionsController().saveState(outState);
300 }
301 }
302
303 @Override
304 public void onConfigurationChanged(Configuration newConfig) {
305 super.onConfigurationChanged(newConfig);
306 final Locale locale = getContext().getResources().getConfiguration().locale;
307 final int ld = TextUtils.getLayoutDirectionFromLocale(locale);
308 if (!locale.equals(mLocale) || ld != mLayoutDirection) {
309 if (DEBUG) {
310 Log.v(TAG, String.format(
311 "config changed locale/LD: %s (%d) -> %s (%d)", mLocale, mLayoutDirection,
312 locale, ld));
313 }
314 mLocale = locale;
315 mLayoutDirection = ld;
316 refreshLayout(ld);
317 }
318 repositionNavigationBar();
319 }
320
321 @Override
322 public void dump(String prefix, FileDescriptor fd, PrintWriter pw, String[] args) {
323 if (mNavigationBarView != null) {
324 pw.print(" mNavigationBarWindowState=");
325 pw.println(windowStateToString(mNavigationBarWindowState));
326 pw.print(" mNavigationBarMode=");
327 pw.println(BarTransitions.modeToString(mNavigationBarMode));
328 dumpBarTransitions(pw, "mNavigationBarView", mNavigationBarView.getBarTransitions());
329 }
330
331 pw.print(" mNavigationBarView=");
332 if (mNavigationBarView == null) {
333 pw.println("null");
334 } else {
335 mNavigationBarView.dump(fd, pw, args);
336 }
337 }
338
339 // ----- CommandQueue Callbacks -----
340
341 @Override
342 public void setImeWindowStatus(IBinder token, int vis, int backDisposition,
343 boolean showImeSwitcher) {
344 boolean imeShown = (vis & InputMethodService.IME_VISIBLE) != 0;
345 int hints = mNavigationIconHints;
Tarandeep Singh3fecef12018-01-22 14:33:33 -0800346 if (imeShown && backDisposition != InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS) {
Jason Monk49fa0162017-01-11 09:21:56 -0500347 hints |= NAVIGATION_HINT_BACK_ALT;
348 } else {
349 hints &= ~NAVIGATION_HINT_BACK_ALT;
350 }
351 if (showImeSwitcher) {
352 hints |= NAVIGATION_HINT_IME_SHOWN;
353 } else {
354 hints &= ~NAVIGATION_HINT_IME_SHOWN;
355 }
356 if (hints == mNavigationIconHints) return;
357
358 mNavigationIconHints = hints;
359
360 if (mNavigationBarView != null) {
361 mNavigationBarView.setNavigationIconHints(hints);
362 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500363 mStatusBar.checkBarModes();
Jason Monk49fa0162017-01-11 09:21:56 -0500364 }
365
366 @Override
367 public void topAppWindowChanged(boolean showMenu) {
368 if (mNavigationBarView != null) {
369 mNavigationBarView.setMenuVisibility(showMenu);
370 }
371 }
372
373 @Override
374 public void setWindowState(int window, int state) {
375 if (mNavigationBarView != null
376 && window == StatusBarManager.WINDOW_NAVIGATION_BAR
377 && mNavigationBarWindowState != state) {
378 mNavigationBarWindowState = state;
379 if (DEBUG_WINDOW_STATE) Log.d(TAG, "Navigation bar " + windowStateToString(state));
Mike Digman5aeca792018-03-05 11:14:39 -0800380
381 // If the navbar is visible, show the rotate button if there's a pending suggestion
382 if (state == WINDOW_STATE_SHOWING && mPendingRotationSuggestion) {
383 showAndLogRotationSuggestion();
384 }
Jason Monk49fa0162017-01-11 09:21:56 -0500385 }
386 }
387
Mike Digman7d092772018-01-11 12:10:32 -0800388 @Override
Mike Digmane0777312018-01-19 12:41:51 -0800389 public void onRotationProposal(final int rotation, boolean isValid) {
390 // This method will be called on rotation suggestion changes even if the proposed rotation
391 // is not valid for the top app. Use invalid rotation choices as a signal to remove the
392 // rotate button if shown.
Mike Digmane0777312018-01-19 12:41:51 -0800393 if (!isValid) {
Mike Digman1e28a5a2018-02-14 10:49:19 -0800394 setRotateSuggestionButtonState(false);
Mike Digmane0777312018-01-19 12:41:51 -0800395 return;
396 }
397
Mike Digman5aeca792018-03-05 11:14:39 -0800398 // If window rotation matches suggested rotation, remove any current suggestions
Mike Digman1e28a5a2018-02-14 10:49:19 -0800399 final int winRotation = mWindowManager.getDefaultDisplay().getRotation();
400 if (rotation == winRotation) {
Mike Digman5aeca792018-03-05 11:14:39 -0800401 getView().removeCallbacks(mRemoveRotationProposal);
Mike Digman1e28a5a2018-02-14 10:49:19 -0800402 setRotateSuggestionButtonState(false);
Mike Digman5aeca792018-03-05 11:14:39 -0800403 return;
Mike Digman7d092772018-01-11 12:10:32 -0800404 }
Mike Digman5aeca792018-03-05 11:14:39 -0800405
406 // Prepare to show the navbar icon by updating the icon style to change anim params
407 mLastRotationSuggestion = rotation; // Remember rotation for click
408 if (mNavigationBarView != null) {
409 final boolean rotationCCW = isRotationAnimationCCW(winRotation, rotation);
410 int style;
411 if (winRotation == Surface.ROTATION_0 || winRotation == Surface.ROTATION_180) {
412 style = rotationCCW ? R.style.RotateButtonCCWStart90 :
413 R.style.RotateButtonCWStart90;
414 } else { // 90 or 270
415 style = rotationCCW ? R.style.RotateButtonCCWStart0 :
416 R.style.RotateButtonCWStart0;
417 }
418 mNavigationBarView.updateRotateSuggestionButtonStyle(style, true);
419 }
420
421 if (mNavigationBarWindowState != WINDOW_STATE_SHOWING) {
422 // If the navbar isn't shown, flag the rotate icon to be shown should the navbar become
423 // visible given some time limit.
424 mPendingRotationSuggestion = true;
425 getView().removeCallbacks(mCancelPendingRotationProposal);
426 getView().postDelayed(mCancelPendingRotationProposal,
427 NAVBAR_HIDDEN_PENDING_ICON_TIMEOUT_MS);
428
429 } else { // The navbar is visible so show the icon right away
430 showAndLogRotationSuggestion();
431 }
432 }
433
434 private void showAndLogRotationSuggestion() {
435 setRotateSuggestionButtonState(true);
436 rescheduleRotationTimeout(false);
437 mMetricsLogger.visible(MetricsEvent.ROTATION_SUGGESTION_SHOWN);
Mike Digman7d092772018-01-11 12:10:32 -0800438 }
439
Mike Digman1e28a5a2018-02-14 10:49:19 -0800440 private boolean isRotationAnimationCCW(int from, int to) {
441 // All 180deg WM rotation animations are CCW, match that
442 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_90) return false;
443 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_180) return true; //180d so CCW
444 if (from == Surface.ROTATION_0 && to == Surface.ROTATION_270) return true;
445 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_0) return true;
446 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_180) return false;
447 if (from == Surface.ROTATION_90 && to == Surface.ROTATION_270) return true; //180d so CCW
448 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_0) return true; //180d so CCW
449 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_90) return true;
450 if (from == Surface.ROTATION_180 && to == Surface.ROTATION_270) return false;
451 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_0) return false;
452 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_90) return true; //180d so CCW
453 if (from == Surface.ROTATION_270 && to == Surface.ROTATION_180) return true;
454 return false; // Default
Mike Digmana48cf192018-02-12 17:52:48 -0800455 }
456
Mike Digman1e28a5a2018-02-14 10:49:19 -0800457 public void setRotateSuggestionButtonState(final boolean visible) {
458 setRotateSuggestionButtonState(visible, false);
459 }
460
461 public void setRotateSuggestionButtonState(final boolean visible, final boolean force) {
462 if (mNavigationBarView == null) return;
463
464 // At any point the the button can become invisible because an a11y service became active.
465 // Similarly, a call to make the button visible may be rejected because an a11y service is
466 // active. Must account for this.
467
468 ButtonDispatcher rotBtn = mNavigationBarView.getRotateSuggestionButton();
469 final boolean currentlyVisible = mNavigationBarView.isRotateButtonVisible();
470
471 // Rerun a show animation to indicate change but don't rerun a hide animation
472 if (!visible && !currentlyVisible) return;
473
474 View view = rotBtn.getCurrentView();
475 if (view == null) return;
476
477 KeyButtonDrawable kbd = rotBtn.getImageDrawable();
478 if (kbd == null) return;
479
480 // The KBD and AVD is recreated every new valid suggestion because of style changes.
481 AnimatedVectorDrawable animIcon = null;
482 if (kbd.getDrawable(0) instanceof AnimatedVectorDrawable) {
483 animIcon = (AnimatedVectorDrawable) kbd.getDrawable(0);
484 }
485
Mike Digman5aeca792018-03-05 11:14:39 -0800486 // Clear any pending suggestion flag as it has either been nullified or is being shown
487 mPendingRotationSuggestion = false;
488 getView().removeCallbacks(mCancelPendingRotationProposal);
489
490 // Handle the visibility change and animation
Mike Digman1e28a5a2018-02-14 10:49:19 -0800491 if (visible) { // Appear and change (cannot force)
Mike Digman85a9bea2018-02-23 15:08:53 -0800492 // Stop and clear any currently running hide animations
Mike Digman1e28a5a2018-02-14 10:49:19 -0800493 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
Mike Digman85a9bea2018-02-23 15:08:53 -0800494 mRotateHideAnimator.cancel();
Mike Digman1e28a5a2018-02-14 10:49:19 -0800495 }
Mike Digman85a9bea2018-02-23 15:08:53 -0800496 mRotateHideAnimator = null;
Mike Digman1e28a5a2018-02-14 10:49:19 -0800497
498 // Reset the alpha if any has changed due to hide animation
499 view.setAlpha(1f);
500
501 // Run the rotate icon's animation if it has one
502 if (animIcon != null) {
503 animIcon.reset();
504 animIcon.start();
505 }
506
Mike Digman50752642018-02-15 13:36:09 -0800507 if (!isRotateSuggestionIntroduced()) mViewRippler.start(view);
508
Mike Digman1e28a5a2018-02-14 10:49:19 -0800509 // Set visibility, may fail if a11y service is active.
510 // If invisible, call will stop animation.
511 mNavigationBarView.setRotateButtonVisibility(true);
512
513 } else { // Hide
514
Mike Digman50752642018-02-15 13:36:09 -0800515 mViewRippler.stop(); // Prevent any pending ripples, force hide or not
516
Mike Digman1e28a5a2018-02-14 10:49:19 -0800517 if (force) {
518 // If a hide animator is running stop it and make invisible
519 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) {
520 mRotateHideAnimator.pause();
521 }
522 mNavigationBarView.setRotateButtonVisibility(false);
523 return;
524 }
525
526 // Don't start any new hide animations if one is running
527 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
528
529 ObjectAnimator fadeOut = ObjectAnimator.ofFloat(view, "alpha",
530 0f);
531 fadeOut.setDuration(BUTTON_FADE_IN_OUT_DURATION_MS);
532 fadeOut.setInterpolator(Interpolators.LINEAR);
533 fadeOut.addListener(new AnimatorListenerAdapter() {
534 @Override
535 public void onAnimationEnd(Animator animation) {
536 mNavigationBarView.setRotateButtonVisibility(false);
537 }
538 });
539
540 mRotateHideAnimator = fadeOut;
541 fadeOut.start();
Mike Digmana48cf192018-02-12 17:52:48 -0800542 }
543 }
544
Mike Digman90402952018-01-22 16:05:51 -0800545 private void rescheduleRotationTimeout(final boolean reasonHover) {
546 // May be called due to a new rotation proposal or a change in hover state
547 if (reasonHover) {
548 // Don't reschedule if a hide animator is running
Mike Digman1e28a5a2018-02-14 10:49:19 -0800549 if (mRotateHideAnimator != null && mRotateHideAnimator.isRunning()) return;
Mike Digman90402952018-01-22 16:05:51 -0800550 // Don't reschedule if not visible
Mike Digman1e28a5a2018-02-14 10:49:19 -0800551 if (!mNavigationBarView.isRotateButtonVisible()) return;
Mike Digman90402952018-01-22 16:05:51 -0800552 }
553
Mike Digman5aeca792018-03-05 11:14:39 -0800554 getView().removeCallbacks(mRemoveRotationProposal); // Stop any pending removal
555 getView().postDelayed(mRemoveRotationProposal,
Mike Digman90402952018-01-22 16:05:51 -0800556 computeRotationProposalTimeout()); // Schedule timeout
557 }
558
559 private int computeRotationProposalTimeout() {
560 if (mAccessibilityFeedbackEnabled) return 20000;
561 if (mHoveringRotationSuggestion) return 16000;
562 return 6000;
563 }
564
Mike Digman50752642018-02-15 13:36:09 -0800565 private boolean isRotateSuggestionIntroduced() {
566 ContentResolver cr = getContext().getContentResolver();
567 return Settings.Secure.getInt(cr, Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED, 0)
568 >= NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION;
569 }
570
571 private void incrementNumAcceptedRotationSuggestionsIfNeeded() {
572 // Get the number of accepted suggestions
573 ContentResolver cr = getContext().getContentResolver();
574 final int numSuggestions = Settings.Secure.getInt(cr,
575 Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED, 0);
576
577 // Increment the number of accepted suggestions only if it would change intro mode
578 if (numSuggestions < NUM_ACCEPTED_ROTATION_SUGGESTIONS_FOR_INTRODUCTION) {
579 Settings.Secure.putInt(cr, Settings.Secure.NUM_ROTATION_SUGGESTIONS_ACCEPTED,
580 numSuggestions + 1);
581 }
582 }
583
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500584 // Injected from StatusBar at creation.
Jason Monk49fa0162017-01-11 09:21:56 -0500585 public void setCurrentSysuiVisibility(int systemUiVisibility) {
586 mSystemUiVisibility = systemUiVisibility;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500587 mNavigationBarMode = mStatusBar.computeBarMode(0, mSystemUiVisibility,
Jason Monk49fa0162017-01-11 09:21:56 -0500588 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
589 View.NAVIGATION_BAR_TRANSPARENT);
590 checkNavBarModes();
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500591 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500592 mLightBarController.onNavigationVisibilityChanged(mSystemUiVisibility, 0 /* mask */,
593 true /* nbModeChanged */, mNavigationBarMode);
594 }
595
596 @Override
597 public void setSystemUiVisibility(int vis, int fullscreenStackVis, int dockedStackVis,
598 int mask, Rect fullscreenStackBounds, Rect dockedStackBounds) {
599 final int oldVal = mSystemUiVisibility;
600 final int newVal = (oldVal & ~mask) | (vis & mask);
601 final int diff = newVal ^ oldVal;
602 boolean nbModeChanged = false;
603 if (diff != 0) {
604 mSystemUiVisibility = newVal;
605
606 // update navigation bar mode
607 final int nbMode = getView() == null
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500608 ? -1 : mStatusBar.computeBarMode(oldVal, newVal,
Jason Monk49fa0162017-01-11 09:21:56 -0500609 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
610 View.NAVIGATION_BAR_TRANSPARENT);
611 nbModeChanged = nbMode != -1;
612 if (nbModeChanged) {
613 if (mNavigationBarMode != nbMode) {
614 mNavigationBarMode = nbMode;
615 checkNavBarModes();
616 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500617 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500618 }
619 }
620
621 mLightBarController.onNavigationVisibilityChanged(vis, mask, nbModeChanged,
622 mNavigationBarMode);
623 }
624
625 @Override
626 public void disable(int state1, int state2, boolean animate) {
627 // All navigation bar flags are in state1.
628 int masked = state1 & (StatusBarManager.DISABLE_HOME
629 | StatusBarManager.DISABLE_RECENT
630 | StatusBarManager.DISABLE_BACK
631 | StatusBarManager.DISABLE_SEARCH);
632 if (masked != mDisabledFlags1) {
633 mDisabledFlags1 = masked;
634 if (mNavigationBarView != null) mNavigationBarView.setDisabledFlags(state1);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000635 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500636 }
637 }
638
639 // ----- Internal stuffz -----
640
641 private void refreshLayout(int layoutDirection) {
642 if (mNavigationBarView != null) {
643 mNavigationBarView.setLayoutDirection(layoutDirection);
644 }
645 }
646
647 private boolean shouldDisableNavbarGestures() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500648 return !mStatusBar.isDeviceProvisioned()
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800649 || (mDisabledFlags1 & StatusBarManager.DISABLE_SEARCH) != 0;
Jason Monk49fa0162017-01-11 09:21:56 -0500650 }
651
652 private void repositionNavigationBar() {
653 if (mNavigationBarView == null || !mNavigationBarView.isAttachedToWindow()) return;
654
655 prepareNavigationBarView();
656
657 mWindowManager.updateViewLayout((View) mNavigationBarView.getParent(),
658 ((View) mNavigationBarView.getParent()).getLayoutParams());
659 }
660
Matthew Ng9c3bce52018-02-01 22:00:31 +0000661 private void updateScreenPinningGestures() {
662 if (mNavigationBarView == null) {
663 return;
664 }
665
666 // Change the cancel pin gesture to home and back if recents button is invisible
667 boolean recentsVisible = mNavigationBarView.isRecentsButtonVisible();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000668 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
669 if (recentsVisible) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000670 backButton.setOnLongClickListener(this::onLongPressBackRecents);
671 } else {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000672 backButton.setOnLongClickListener(this::onLongPressBackHome);
673 }
674 }
675
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100676 private void notifyNavigationBarScreenOn() {
Matthew Ngd0a73e72018-03-02 15:16:03 -0800677 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500678 }
679
680 private void prepareNavigationBarView() {
681 mNavigationBarView.reorient();
682
683 ButtonDispatcher recentsButton = mNavigationBarView.getRecentsButton();
684 recentsButton.setOnClickListener(this::onRecentsClick);
685 recentsButton.setOnTouchListener(this::onRecentsTouch);
686 recentsButton.setLongClickable(true);
687 recentsButton.setOnLongClickListener(this::onLongPressBackRecents);
688
689 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
690 backButton.setLongClickable(true);
Jason Monk49fa0162017-01-11 09:21:56 -0500691
692 ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();
693 homeButton.setOnTouchListener(this::onHomeTouch);
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800694 homeButton.setOnLongClickListener(this::onHomeLongClick);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800695
696 ButtonDispatcher accessibilityButton = mNavigationBarView.getAccessibilityButton();
697 accessibilityButton.setOnClickListener(this::onAccessibilityClick);
698 accessibilityButton.setOnLongClickListener(this::onAccessibilityLongClick);
Phil Weaverdb9a7742017-04-18 08:15:06 -0700699 updateAccessibilityServicesState(mAccessibilityManager);
Mike Digman7d092772018-01-11 12:10:32 -0800700
701 ButtonDispatcher rotateSuggestionButton = mNavigationBarView.getRotateSuggestionButton();
702 rotateSuggestionButton.setOnClickListener(this::onRotateSuggestionClick);
Mike Digman90402952018-01-22 16:05:51 -0800703 rotateSuggestionButton.setOnHoverListener(this::onRotateSuggestionHover);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000704 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500705 }
706
707 private boolean onHomeTouch(View v, MotionEvent event) {
708 if (mHomeBlockedThisTouch && event.getActionMasked() != MotionEvent.ACTION_DOWN) {
709 return true;
710 }
711 // If an incoming call is ringing, HOME is totally disabled.
712 // (The user is already on the InCallUI at this point,
713 // and his ONLY options are to answer or reject the call.)
714 switch (event.getAction()) {
715 case MotionEvent.ACTION_DOWN:
716 mHomeBlockedThisTouch = false;
717 TelecomManager telecomManager =
718 getContext().getSystemService(TelecomManager.class);
719 if (telecomManager != null && telecomManager.isRinging()) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500720 if (mStatusBar.isKeyguardShowing()) {
Jason Monk49fa0162017-01-11 09:21:56 -0500721 Log.i(TAG, "Ignoring HOME; there's a ringing incoming call. " +
722 "No heads up");
723 mHomeBlockedThisTouch = true;
724 return true;
725 }
726 }
727 break;
728 case MotionEvent.ACTION_UP:
729 case MotionEvent.ACTION_CANCEL:
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500730 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500731 break;
732 }
733 return false;
734 }
735
736 private void onVerticalChanged(boolean isVertical) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500737 mStatusBar.setQsScrimEnabled(!isVertical);
Jason Monk49fa0162017-01-11 09:21:56 -0500738 }
739
740 private boolean onNavigationTouch(View v, MotionEvent event) {
Eliot Courtneycb5d3162017-08-09 16:53:15 +0900741 mStatusBar.checkUserAutohide(event);
Jason Monk49fa0162017-01-11 09:21:56 -0500742 return false;
743 }
744
Jason Monk865246d2017-01-19 08:27:01 -0500745 @VisibleForTesting
746 boolean onHomeLongClick(View v) {
Matthew Ng6ff33b72018-02-27 13:47:38 -0800747 if (!mNavigationBarView.isRecentsButtonVisible()
748 && ActivityManagerWrapper.getInstance().isScreenPinningActive()) {
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800749 return onLongPressBackHome(v);
750 }
Jason Monk49fa0162017-01-11 09:21:56 -0500751 if (shouldDisableNavbarGestures()) {
752 return false;
753 }
Matthew Ng2ea93b72018-03-14 19:43:18 +0000754 mNavigationBarView.onNavigationButtonLongPress(v);
Mike Digmanc94759d2018-01-23 11:01:21 -0800755 mMetricsLogger.action(MetricsEvent.ACTION_ASSIST_LONG_PRESS);
Jason Monk49fa0162017-01-11 09:21:56 -0500756 mAssistManager.startAssist(new Bundle() /* args */);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500757 mStatusBar.awakenDreams();
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800758
Jason Monk49fa0162017-01-11 09:21:56 -0500759 if (mNavigationBarView != null) {
760 mNavigationBarView.abortCurrentGesture();
761 }
762 return true;
763 }
764
765 // additional optimization when we have software system buttons - start loading the recent
766 // tasks on touch down
767 private boolean onRecentsTouch(View v, MotionEvent event) {
768 int action = event.getAction() & MotionEvent.ACTION_MASK;
769 if (action == MotionEvent.ACTION_DOWN) {
770 mCommandQueue.preloadRecentApps();
771 } else if (action == MotionEvent.ACTION_CANCEL) {
772 mCommandQueue.cancelPreloadRecentApps();
773 } else if (action == MotionEvent.ACTION_UP) {
774 if (!v.isPressed()) {
775 mCommandQueue.cancelPreloadRecentApps();
776 }
777 }
778 return false;
779 }
780
781 private void onRecentsClick(View v) {
782 if (LatencyTracker.isEnabled(getContext())) {
783 LatencyTracker.getInstance(getContext()).onActionStart(
784 LatencyTracker.ACTION_TOGGLE_RECENTS);
785 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500786 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500787 mCommandQueue.toggleRecentApps();
788 }
789
Matthew Ng9c3bce52018-02-01 22:00:31 +0000790 private boolean onLongPressBackHome(View v) {
Matthew Ng2ea93b72018-03-14 19:43:18 +0000791 mNavigationBarView.onNavigationButtonLongPress(v);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000792 return onLongPressNavigationButtons(v, R.id.back, R.id.home);
793 }
794
795 private boolean onLongPressBackRecents(View v) {
Matthew Ng2ea93b72018-03-14 19:43:18 +0000796 mNavigationBarView.onNavigationButtonLongPress(v);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000797 return onLongPressNavigationButtons(v, R.id.back, R.id.recent_apps);
798 }
799
Jason Monk49fa0162017-01-11 09:21:56 -0500800 /**
Matthew Ng9c3bce52018-02-01 22:00:31 +0000801 * This handles long-press of both back and recents/home. Back is the common button with
802 * combination of recents if it is visible or home if recents is invisible.
803 * They are handled together to capture them both being long-pressed
Jason Monk49fa0162017-01-11 09:21:56 -0500804 * at the same time to exit screen pinning (lock task).
805 *
Matthew Ng9c3bce52018-02-01 22:00:31 +0000806 * When accessibility mode is on, only a long-press from recents/home
Jason Monk49fa0162017-01-11 09:21:56 -0500807 * is required to exit.
808 *
809 * In all other circumstances we try to pass through long-press events
810 * for Back, so that apps can still use it. Which can be from two things.
811 * 1) Not currently in screen pinning (lock task).
Matthew Ng9c3bce52018-02-01 22:00:31 +0000812 * 2) Back is long-pressed without recents/home.
Jason Monk49fa0162017-01-11 09:21:56 -0500813 */
Matthew Ng9c3bce52018-02-01 22:00:31 +0000814 private boolean onLongPressNavigationButtons(View v, @IdRes int btnId1, @IdRes int btnId2) {
Jason Monk49fa0162017-01-11 09:21:56 -0500815 try {
816 boolean sendBackLongPress = false;
817 IActivityManager activityManager = ActivityManagerNative.getDefault();
818 boolean touchExplorationEnabled = mAccessibilityManager.isTouchExplorationEnabled();
819 boolean inLockTaskMode = activityManager.isInLockTaskMode();
820 if (inLockTaskMode && !touchExplorationEnabled) {
821 long time = System.currentTimeMillis();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000822
Jason Monk49fa0162017-01-11 09:21:56 -0500823 // If we recently long-pressed the other button then they were
824 // long-pressed 'together'
825 if ((time - mLastLockToAppLongPress) < LOCK_TO_APP_GESTURE_TOLERENCE) {
Benjamin Franza83859f2017-07-03 16:34:14 +0100826 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500827 // When exiting refresh disabled flags.
Matthew Ngd0a73e72018-03-02 15:16:03 -0800828 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500829 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000830 } else if (v.getId() == btnId1) {
831 ButtonDispatcher button = btnId2 == R.id.recent_apps
832 ? mNavigationBarView.getRecentsButton()
833 : mNavigationBarView.getHomeButton();
834 if (!button.getCurrentView().isPressed()) {
835 // If we aren't pressing recents/home right now then they presses
836 // won't be together, so send the standard long-press action.
837 sendBackLongPress = true;
838 }
Jason Monk49fa0162017-01-11 09:21:56 -0500839 }
840 mLastLockToAppLongPress = time;
841 } else {
842 // If this is back still need to handle sending the long-press event.
Matthew Ng9c3bce52018-02-01 22:00:31 +0000843 if (v.getId() == btnId1) {
Jason Monk49fa0162017-01-11 09:21:56 -0500844 sendBackLongPress = true;
845 } else if (touchExplorationEnabled && inLockTaskMode) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000846 // When in accessibility mode a long press that is recents/home (not back)
Jason Monk49fa0162017-01-11 09:21:56 -0500847 // should stop lock task.
Benjamin Franza83859f2017-07-03 16:34:14 +0100848 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500849 // When exiting refresh disabled flags.
Matthew Ngd0a73e72018-03-02 15:16:03 -0800850 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500851 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000852 } else if (v.getId() == btnId2) {
853 return btnId2 == R.id.recent_apps
854 ? onLongPressRecents()
855 : onHomeLongClick(mNavigationBarView.getHomeButton().getCurrentView());
Jason Monk49fa0162017-01-11 09:21:56 -0500856 }
857 }
858 if (sendBackLongPress) {
859 KeyButtonView keyButtonView = (KeyButtonView) v;
860 keyButtonView.sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS);
861 keyButtonView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
862 return true;
863 }
864 } catch (RemoteException e) {
865 Log.d(TAG, "Unable to reach activity manager", e);
866 }
867 return false;
868 }
869
870 private boolean onLongPressRecents() {
Erik Wolsheimer9be3a062017-05-31 14:59:57 -0700871 if (mRecents == null || !ActivityManager.supportsMultiWindow(getContext())
Matthew Ng43db6d22017-06-27 15:29:39 -0700872 || !mDivider.getView().getSnapAlgorithm().isSplitScreenFeasible()
873 || Recents.getConfiguration().isLowRamDevice) {
Jason Monk49fa0162017-01-11 09:21:56 -0500874 return false;
875 }
876
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500877 return mStatusBar.toggleSplitScreenMode(MetricsEvent.ACTION_WINDOW_DOCK_LONGPRESS,
Jason Monk49fa0162017-01-11 09:21:56 -0500878 MetricsEvent.ACTION_WINDOW_UNDOCK_LONGPRESS);
879 }
880
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800881 private void onAccessibilityClick(View v) {
882 mAccessibilityManager.notifyAccessibilityButtonClicked();
883 }
884
885 private boolean onAccessibilityLongClick(View v) {
Casey Burkhardt5e8b9802017-03-24 10:07:20 -0700886 Intent intent = new Intent(AccessibilityManager.ACTION_CHOOSE_ACCESSIBILITY_BUTTON);
887 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Casey Burkhardt5d614402017-04-06 13:46:50 -0700888 v.getContext().startActivityAsUser(intent, UserHandle.CURRENT);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800889 return true;
890 }
891
Phil Weaverdb9a7742017-04-18 08:15:06 -0700892 private void updateAccessibilityServicesState(AccessibilityManager accessibilityManager) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800893 int requestingServices = 0;
894 try {
Casey Burkhardt5d614402017-04-06 13:46:50 -0700895 if (Settings.Secure.getIntForUser(mContentResolver,
896 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED,
897 UserHandle.USER_CURRENT) == 1) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800898 requestingServices++;
899 }
900 } catch (Settings.SettingNotFoundException e) {
901 }
902
Mike Digman90402952018-01-22 16:05:51 -0800903 boolean feedbackEnabled = false;
Casey Burkhardt5d614402017-04-06 13:46:50 -0700904 // AccessibilityManagerService resolves services for the current user since the local
905 // AccessibilityManager is created from a Context with the INTERACT_ACROSS_USERS permission
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800906 final List<AccessibilityServiceInfo> services =
Phil Weaverdb9a7742017-04-18 08:15:06 -0700907 accessibilityManager.getEnabledAccessibilityServiceList(
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800908 AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800909 for (int i = services.size() - 1; i >= 0; --i) {
910 AccessibilityServiceInfo info = services.get(i);
911 if ((info.flags & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0) {
912 requestingServices++;
913 }
Mike Digman90402952018-01-22 16:05:51 -0800914
915 if (info.feedbackType != 0 && info.feedbackType !=
916 AccessibilityServiceInfo.FEEDBACK_GENERIC) {
917 feedbackEnabled = true;
918 }
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800919 }
920
Mike Digman90402952018-01-22 16:05:51 -0800921 mAccessibilityFeedbackEnabled = feedbackEnabled;
922
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800923 final boolean showAccessibilityButton = requestingServices >= 1;
924 final boolean targetSelection = requestingServices >= 2;
925 mNavigationBarView.setAccessibilityButtonState(showAccessibilityButton, targetSelection);
926 }
927
Mike Digman7d092772018-01-11 12:10:32 -0800928 private void onRotateSuggestionClick(View v) {
Mike Digmanc94759d2018-01-23 11:01:21 -0800929 mMetricsLogger.action(MetricsEvent.ACTION_ROTATION_SUGGESTION_ACCEPTED);
Mike Digman50752642018-02-15 13:36:09 -0800930 incrementNumAcceptedRotationSuggestionsIfNeeded();
Mike Digman7d092772018-01-11 12:10:32 -0800931 mRotationLockController.setRotationLockedAtAngle(true, mLastRotationSuggestion);
932 }
933
Mike Digman90402952018-01-22 16:05:51 -0800934 private boolean onRotateSuggestionHover(View v, MotionEvent event) {
935 final int action = event.getActionMasked();
936 mHoveringRotationSuggestion = (action == MotionEvent.ACTION_HOVER_ENTER)
937 || (action == MotionEvent.ACTION_HOVER_MOVE);
938 rescheduleRotationTimeout(true);
939 return false; // Must return false so a11y hover events are dispatched correctly.
940 }
941
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500942 // ----- Methods that StatusBar talks to (should be minimized) -----
Jason Monk49fa0162017-01-11 09:21:56 -0500943
Jason Monk49fa0162017-01-11 09:21:56 -0500944 public void setLightBarController(LightBarController lightBarController) {
945 mLightBarController = lightBarController;
946 mLightBarController.setNavigationBar(mNavigationBarView.getLightTransitionsController());
947 }
948
949 public boolean isSemiTransparent() {
950 return mNavigationBarMode == MODE_SEMI_TRANSPARENT;
951 }
952
Jason Monk49fa0162017-01-11 09:21:56 -0500953 public void disableAnimationsDuringHide(long delay) {
954 mNavigationBarView.setLayoutTransitionsEnabled(false);
955 mNavigationBarView.postDelayed(() -> mNavigationBarView.setLayoutTransitionsEnabled(true),
956 delay + StackStateAnimator.ANIMATION_DURATION_GO_TO_FULL_SHADE);
957 }
958
Jason Monk49fa0162017-01-11 09:21:56 -0500959 public BarTransitions getBarTransitions() {
960 return mNavigationBarView.getBarTransitions();
961 }
962
963 public void checkNavBarModes() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500964 mStatusBar.checkBarMode(mNavigationBarMode,
Jason Monk49fa0162017-01-11 09:21:56 -0500965 mNavigationBarWindowState, mNavigationBarView.getBarTransitions());
966 }
967
968 public void finishBarAnimations() {
969 mNavigationBarView.getBarTransitions().finishAnimations();
970 }
971
Jason Monk91e587e2017-04-13 13:41:23 -0400972 private final AccessibilityServicesStateChangeListener mAccessibilityListener =
973 this::updateAccessibilityServicesState;
974
Casey Burkhardt74922c62017-02-13 12:43:16 -0800975 private class MagnificationContentObserver extends ContentObserver {
976
977 public MagnificationContentObserver(Handler handler) {
978 super(handler);
979 }
980
981 @Override
982 public void onChange(boolean selfChange) {
Phil Weaverdb9a7742017-04-18 08:15:06 -0700983 NavigationBarFragment.this.updateAccessibilityServicesState(mAccessibilityManager);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800984 }
985 }
986
Jason Monk49fa0162017-01-11 09:21:56 -0500987 private final Stub mRotationWatcher = new Stub() {
988 @Override
Mike Digman90402952018-01-22 16:05:51 -0800989 public void onRotationChanged(final int rotation) throws RemoteException {
Jason Monk49fa0162017-01-11 09:21:56 -0500990 // We need this to be scheduled as early as possible to beat the redrawing of
991 // window in response to the orientation change.
992 Handler h = getView().getHandler();
993 Message msg = Message.obtain(h, () -> {
Mike Digman85ff7fa2018-01-23 14:59:52 -0800994
995 // If the screen rotation changes while locked, potentially update lock to flow with
Mike Digman90402952018-01-22 16:05:51 -0800996 // new screen rotation and hide any showing suggestions.
997 if (mRotationLockController.isRotationLocked()) {
Mike Digman85ff7fa2018-01-23 14:59:52 -0800998 if (shouldOverrideUserLockPrefs(rotation)) {
999 mRotationLockController.setRotationLockedAtAngle(true, rotation);
1000 }
Mike Digman1e28a5a2018-02-14 10:49:19 -08001001 setRotateSuggestionButtonState(false, true);
Mike Digman90402952018-01-22 16:05:51 -08001002 }
1003
Jason Monk49fa0162017-01-11 09:21:56 -05001004 if (mNavigationBarView != null
1005 && mNavigationBarView.needsReorient(rotation)) {
1006 repositionNavigationBar();
1007 }
1008 });
1009 msg.setAsynchronous(true);
1010 h.sendMessageAtFrontOfQueue(msg);
1011 }
Mike Digman85ff7fa2018-01-23 14:59:52 -08001012
1013 private boolean shouldOverrideUserLockPrefs(final int rotation) {
Mike Digmanab650252018-03-06 11:01:41 -08001014 // Only override user prefs when returning to the natural rotation (normally portrait).
Mike Digman85ff7fa2018-01-23 14:59:52 -08001015 // Don't let apps that force landscape or 180 alter user lock.
Mike Digmanab650252018-03-06 11:01:41 -08001016 return rotation == NATURAL_ROTATION;
Mike Digman85ff7fa2018-01-23 14:59:52 -08001017 }
Jason Monk49fa0162017-01-11 09:21:56 -05001018 };
1019
1020 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1021 @Override
1022 public void onReceive(Context context, Intent intent) {
1023 String action = intent.getAction();
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +01001024 if (Intent.ACTION_SCREEN_OFF.equals(action)
1025 || Intent.ACTION_SCREEN_ON.equals(action)) {
1026 notifyNavigationBarScreenOn();
Jason Monk49fa0162017-01-11 09:21:56 -05001027 }
1028 }
1029 };
1030
Mike Digman7d092772018-01-11 12:10:32 -08001031 class TaskStackListenerImpl extends SysUiTaskStackChangeListener {
1032 // Invalidate any rotation suggestion on task change or activity orientation change
1033 // Note: all callbacks happen on main thread
1034
1035 @Override
1036 public void onTaskStackChanged() {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001037 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001038 }
1039
1040 @Override
1041 public void onTaskRemoved(int taskId) {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001042 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001043 }
1044
1045 @Override
1046 public void onTaskMovedToFront(int taskId) {
Mike Digman1e28a5a2018-02-14 10:49:19 -08001047 setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001048 }
1049
1050 @Override
1051 public void onActivityRequestedOrientationChanged(int taskId, int requestedOrientation) {
Mike Digman85a9bea2018-02-23 15:08:53 -08001052 // Only hide the icon if the top task changes its requestedOrientation
1053 // Launcher can alter its requestedOrientation while it's not on top, don't hide on this
1054 final boolean top = ActivityManagerWrapper.getInstance().getRunningTask().id == taskId;
1055 if (top) setRotateSuggestionButtonState(false);
Mike Digman7d092772018-01-11 12:10:32 -08001056 }
1057 }
1058
Mike Digman50752642018-02-15 13:36:09 -08001059 private class ViewRippler {
1060 private static final int RIPPLE_OFFSET_MS = 50;
1061 private static final int RIPPLE_INTERVAL_MS = 2000;
1062 private View mRoot;
1063
1064 public void start(View root) {
1065 stop(); // Stop any pending ripple animations
1066
1067 mRoot = root;
1068
1069 // Schedule pending ripples, offset the 1st to avoid problems with visibility change
1070 mRoot.postOnAnimationDelayed(mRipple, RIPPLE_OFFSET_MS);
1071 mRoot.postOnAnimationDelayed(mRipple, RIPPLE_INTERVAL_MS);
1072 mRoot.postOnAnimationDelayed(mRipple, 2*RIPPLE_INTERVAL_MS);
1073 }
1074
1075 public void stop() {
1076 if (mRoot != null) mRoot.removeCallbacks(mRipple);
1077 }
1078
1079 private final Runnable mRipple = new Runnable() {
1080 @Override
1081 public void run() { // Cause the ripple to fire via false presses
1082 mRoot.setPressed(true);
1083 mRoot.setPressed(false);
1084 }
1085 };
1086 }
1087
Jason Monk49fa0162017-01-11 09:21:56 -05001088 public static View create(Context context, FragmentListener listener) {
1089 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
1090 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
1091 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR,
1092 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
1093 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1094 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
1095 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1096 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
1097 | WindowManager.LayoutParams.FLAG_SLIPPERY,
1098 PixelFormat.TRANSLUCENT);
1099 lp.token = new Binder();
Jason Monk49fa0162017-01-11 09:21:56 -05001100 lp.setTitle("NavigationBar");
Phil Weaver8583ae82018-02-13 11:01:24 -08001101 lp.accessibilityTitle = context.getString(R.string.nav_bar);
Jason Monk49fa0162017-01-11 09:21:56 -05001102 lp.windowAnimations = 0;
1103
1104 View navigationBarView = LayoutInflater.from(context).inflate(
1105 R.layout.navigation_bar_window, null);
1106
1107 if (DEBUG) Log.v(TAG, "addNavigationBar: about to add " + navigationBarView);
1108 if (navigationBarView == null) return null;
1109
1110 context.getSystemService(WindowManager.class).addView(navigationBarView, lp);
1111 FragmentHostManager fragmentHost = FragmentHostManager.get(navigationBarView);
1112 NavigationBarFragment fragment = new NavigationBarFragment();
1113 fragmentHost.getFragmentManager().beginTransaction()
1114 .replace(R.id.navigation_bar_frame, fragment, TAG)
1115 .commit();
1116 fragmentHost.addTagListener(TAG, listener);
1117 return navigationBarView;
1118 }
1119}