blob: 3dcadf1d5071cb19f13514f63523e18b8931c254 [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;
Charles Chen10ca70b2018-11-28 00:03:38 +080019import static android.app.StatusBarManager.WINDOW_STATE_HIDDEN;
Jason Monk49fa0162017-01-11 09:21:56 -050020import static android.app.StatusBarManager.WINDOW_STATE_SHOWING;
Charles Chenf3d295c2018-11-30 18:15:21 +080021import static android.app.StatusBarManager.WindowType;
22import static android.app.StatusBarManager.WindowVisibleState;
Jason Monk49fa0162017-01-11 09:21:56 -050023import static android.app.StatusBarManager.windowStateToString;
24
Winson Chung2dbcf092018-10-24 13:00:41 -070025import static com.android.systemui.recents.OverviewProxyService.OverviewProxyListener;
Matthew Ng8f25fb962018-01-16 17:17:24 -080026import static com.android.systemui.shared.system.NavigationBarCompat.InteractionType;
Charles Chen10ca70b2018-11-28 00:03:38 +080027import static com.android.systemui.statusbar.phone.BarTransitions.MODE_LIGHTS_OUT;
Tracy Zhou1ac592c2018-07-25 13:47:37 -070028import static com.android.systemui.statusbar.phone.BarTransitions.MODE_LIGHTS_OUT_TRANSPARENT;
Charles Chen10ca70b2018-11-28 00:03:38 +080029import static com.android.systemui.statusbar.phone.BarTransitions.MODE_OPAQUE;
Jason Monk49fa0162017-01-11 09:21:56 -050030import static com.android.systemui.statusbar.phone.BarTransitions.MODE_SEMI_TRANSPARENT;
Charles Chen10ca70b2018-11-28 00:03:38 +080031import static com.android.systemui.statusbar.phone.BarTransitions.MODE_TRANSLUCENT;
Tracy Zhou1ac592c2018-07-25 13:47:37 -070032import static com.android.systemui.statusbar.phone.BarTransitions.MODE_TRANSPARENT;
Charles Chen10ca70b2018-11-28 00:03:38 +080033import static com.android.systemui.statusbar.phone.BarTransitions.TransitionMode;
Jason Monk2a6ea9c2017-01-26 11:14:51 -050034import static com.android.systemui.statusbar.phone.StatusBar.DEBUG_WINDOW_STATE;
35import static com.android.systemui.statusbar.phone.StatusBar.dumpBarTransitions;
Jason Monk49fa0162017-01-11 09:21:56 -050036
Casey Burkhardt048c2bc2016-12-08 16:09:20 -080037import android.accessibilityservice.AccessibilityServiceInfo;
Matthew Ng9c3bce52018-02-01 22:00:31 +000038import android.annotation.IdRes;
Jason Monk49fa0162017-01-11 09:21:56 -050039import android.annotation.Nullable;
Winson Chung67f5c8b2018-09-24 12:09:19 -070040import android.app.ActivityManager;
Wale Ogunwale65ebd952018-04-25 15:41:44 -070041import android.app.ActivityTaskManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070042import android.app.IActivityTaskManager;
Jason Monk49fa0162017-01-11 09:21:56 -050043import android.app.StatusBarManager;
44import android.content.BroadcastReceiver;
Casey Burkhardtb9dcd662017-03-20 15:10:16 -070045import android.content.ContentResolver;
Jason Monk49fa0162017-01-11 09:21:56 -050046import android.content.Context;
47import android.content.Intent;
48import android.content.IntentFilter;
49import android.content.res.Configuration;
Casey Burkhardt74922c62017-02-13 12:43:16 -080050import android.database.ContentObserver;
Jason Monk49fa0162017-01-11 09:21:56 -050051import android.graphics.PixelFormat;
52import android.graphics.Rect;
53import android.inputmethodservice.InputMethodService;
Matthew Ngadd4c392019-03-01 16:02:31 -080054import android.net.Uri;
Jason Monk49fa0162017-01-11 09:21:56 -050055import android.os.Binder;
56import android.os.Bundle;
57import android.os.Handler;
58import android.os.IBinder;
Matthew Ngadd4c392019-03-01 16:02:31 -080059import android.os.Looper;
Jason Monk49fa0162017-01-11 09:21:56 -050060import android.os.RemoteException;
61import android.os.UserHandle;
Casey Burkhardt74922c62017-02-13 12:43:16 -080062import android.provider.Settings;
Jason Monk49fa0162017-01-11 09:21:56 -050063import android.telecom.TelecomManager;
64import android.text.TextUtils;
65import android.util.Log;
Andrii Kulian3ddd7de2018-10-10 00:33:19 -070066import android.view.Display;
Jason Monk49fa0162017-01-11 09:21:56 -050067import android.view.KeyEvent;
68import android.view.LayoutInflater;
69import android.view.MotionEvent;
Mike Digman85ff7fa2018-01-23 14:59:52 -080070import android.view.Surface;
Jason Monk49fa0162017-01-11 09:21:56 -050071import android.view.View;
72import android.view.ViewGroup;
73import android.view.WindowManager;
74import android.view.WindowManager.LayoutParams;
Jason Monk49fa0162017-01-11 09:21:56 -050075import android.view.accessibility.AccessibilityEvent;
76import android.view.accessibility.AccessibilityManager;
Jason Monk91e587e2017-04-13 13:41:23 -040077import android.view.accessibility.AccessibilityManager.AccessibilityServicesStateChangeListener;
Jason Monk49fa0162017-01-11 09:21:56 -050078
Matthew Ng761562d2018-09-17 11:13:21 -070079import androidx.annotation.VisibleForTesting;
80
Jason Monk49fa0162017-01-11 09:21:56 -050081import com.android.internal.logging.MetricsLogger;
82import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jason Monkea03be12017-12-04 11:08:41 -050083import com.android.internal.util.LatencyTracker;
Charles Chen10ca70b2018-11-28 00:03:38 +080084import com.android.systemui.Dependency;
Jason Monk49fa0162017-01-11 09:21:56 -050085import com.android.systemui.R;
Jason Monk9c7844c2017-01-18 15:21:53 -050086import com.android.systemui.SysUiServiceProvider;
Jason Monk49fa0162017-01-11 09:21:56 -050087import com.android.systemui.assist.AssistManager;
88import com.android.systemui.fragments.FragmentHostManager;
89import com.android.systemui.fragments.FragmentHostManager.FragmentListener;
Beverly8fdb5332019-02-04 14:29:49 -050090import com.android.systemui.plugins.statusbar.StatusBarStateController;
Andrii Kulian3ddd7de2018-10-10 00:33:19 -070091import com.android.systemui.recents.OverviewProxyService;
Jason Monk49fa0162017-01-11 09:21:56 -050092import com.android.systemui.recents.Recents;
Mike Digman7d092772018-01-11 12:10:32 -080093import com.android.systemui.shared.system.ActivityManagerWrapper;
Sunny Goyala6a58122019-04-02 10:20:29 -070094import com.android.systemui.shared.system.QuickStepContract;
Jason Monk49fa0162017-01-11 09:21:56 -050095import com.android.systemui.stackdivider.Divider;
96import com.android.systemui.statusbar.CommandQueue;
97import com.android.systemui.statusbar.CommandQueue.Callbacks;
Charles Chen10ca70b2018-11-28 00:03:38 +080098import com.android.systemui.statusbar.StatusBarState;
Matthew Ng761562d2018-09-17 11:13:21 -070099import com.android.systemui.statusbar.notification.stack.StackStateAnimator;
100import com.android.systemui.statusbar.phone.ContextualButton.ContextButtonListener;
Jason Monk91e587e2017-04-13 13:41:23 -0400101import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper;
Jason Monk297c04e2018-08-23 17:16:59 -0400102import com.android.systemui.statusbar.policy.DeviceProvisionedController;
Jason Monk49fa0162017-01-11 09:21:56 -0500103import com.android.systemui.statusbar.policy.KeyButtonView;
Jason Monk8e4e4cb2018-12-04 11:10:24 -0500104import com.android.systemui.util.LifecycleFragment;
Jason Monk49fa0162017-01-11 09:21:56 -0500105
106import java.io.FileDescriptor;
107import java.io.PrintWriter;
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800108import java.util.List;
Jason Monk49fa0162017-01-11 09:21:56 -0500109import java.util.Locale;
Matthew Ng761562d2018-09-17 11:13:21 -0700110import java.util.function.Consumer;
Jason Monk49fa0162017-01-11 09:21:56 -0500111
Jason Monk9424af72018-12-19 14:17:26 -0500112import javax.inject.Inject;
113
Jason Monk49fa0162017-01-11 09:21:56 -0500114/**
115 * Fragment containing the NavigationBarFragment. Contains logic for what happens
116 * on clicks and view states of the nav bar.
117 */
Jason Monk8e4e4cb2018-12-04 11:10:24 -0500118public class NavigationBarFragment extends LifecycleFragment implements Callbacks {
Jason Monk49fa0162017-01-11 09:21:56 -0500119
Jason Monkd4afe152017-05-01 15:37:43 -0400120 public static final String TAG = "NavigationBar";
Jason Monk49fa0162017-01-11 09:21:56 -0500121 private static final boolean DEBUG = false;
122 private static final String EXTRA_DISABLE_STATE = "disabled_state";
Mike Digmandd2f49e2018-03-16 10:54:22 -0700123 private static final String EXTRA_DISABLE2_STATE = "disabled2_state";
Jason Monk49fa0162017-01-11 09:21:56 -0500124
125 /** Allow some time inbetween the long press for back and recents. */
126 private static final int LOCK_TO_APP_GESTURE_TOLERENCE = 200;
Charles Chen8c9a83f2018-12-18 17:44:10 +0800127 private static final long AUTODIM_TIMEOUT_MS = 2250;
Jason Monk49fa0162017-01-11 09:21:56 -0500128
Jason Monk9424af72018-12-19 14:17:26 -0500129 private final AccessibilityManagerWrapper mAccessibilityManagerWrapper;
130 protected final AssistManager mAssistManager;
131 private final MetricsLogger mMetricsLogger;
132 private final DeviceProvisionedController mDeviceProvisionedController;
Jason Monk297c04e2018-08-23 17:16:59 -0400133
Jason Monk49fa0162017-01-11 09:21:56 -0500134 protected NavigationBarView mNavigationBarView = null;
Jason Monk49fa0162017-01-11 09:21:56 -0500135
Charles Chenf3d295c2018-11-30 18:15:21 +0800136 private @WindowVisibleState int mNavigationBarWindowState = WINDOW_STATE_SHOWING;
Jason Monk49fa0162017-01-11 09:21:56 -0500137
138 private int mNavigationIconHints = 0;
Charles Chen10ca70b2018-11-28 00:03:38 +0800139 private @TransitionMode int mNavigationBarMode;
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800140 private AccessibilityManager mAccessibilityManager;
Casey Burkhardt74922c62017-02-13 12:43:16 -0800141 private MagnificationContentObserver mMagnificationObserver;
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700142 private ContentResolver mContentResolver;
Matthew Ngadd4c392019-03-01 16:02:31 -0800143 private boolean mAssistantAvailable;
Jason Monk49fa0162017-01-11 09:21:56 -0500144
145 private int mDisabledFlags1;
Mike Digmandd2f49e2018-03-16 10:54:22 -0700146 private int mDisabledFlags2;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500147 private StatusBar mStatusBar;
Jason Monk49fa0162017-01-11 09:21:56 -0500148 private Recents mRecents;
149 private Divider mDivider;
150 private WindowManager mWindowManager;
151 private CommandQueue mCommandQueue;
152 private long mLastLockToAppLongPress;
153
154 private Locale mLocale;
155 private int mLayoutDirection;
156
157 private int mSystemUiVisibility;
158 private LightBarController mLightBarController;
Charles Chen8c9a83f2018-12-18 17:44:10 +0800159 private AutoHideController mAutoHideController;
Jason Monk49fa0162017-01-11 09:21:56 -0500160
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800161 private OverviewProxyService mOverviewProxyService;
162
Charles Chen54fce2c2019-03-13 18:17:29 +0800163 @VisibleForTesting
164 public int mDisplayId;
Charles Chen10ca70b2018-11-28 00:03:38 +0800165 private boolean mIsOnDefaultDisplay;
Jason Monk49fa0162017-01-11 09:21:56 -0500166 public boolean mHomeBlockedThisTouch;
167
Charles Chen10ca70b2018-11-28 00:03:38 +0800168 private Handler mHandler = Dependency.get(Dependency.MAIN_HANDLER);
169
Matthew Ng9c3bce52018-02-01 22:00:31 +0000170 private final OverviewProxyListener mOverviewProxyListener = new OverviewProxyListener() {
171 @Override
172 public void onConnectionChanged(boolean isConnected) {
Winson Chungf9e30272018-03-26 17:25:36 -0700173 mNavigationBarView.updateStates();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000174 updateScreenPinningGestures();
Matthew Ngadd4c392019-03-01 16:02:31 -0800175
176 // Send the assistant availability upon connection
177 if (isConnected) {
178 mNavigationBarView.setAssistantAvailable(mAssistantAvailable);
179 }
Matthew Ng9c3bce52018-02-01 22:00:31 +0000180 }
181
182 @Override
Matthew Ng2ea93b72018-03-14 19:43:18 +0000183 public void onQuickStepStarted() {
Mike Digman85a9bea2018-02-23 15:08:53 -0800184 // Use navbar dragging as a signal to hide the rotate button
Matthew Ng761562d2018-09-17 11:13:21 -0700185 mNavigationBarView.getRotateSuggestionButton().setRotateSuggestionButtonState(false);
Matthew Ng90ef0632018-08-15 13:53:15 -0700186
187 // Hide the notifications panel when quick step starts
188 mStatusBar.collapsePanel(true /* animate */);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000189 }
Matthew Ng8f25fb962018-01-16 17:17:24 -0800190
191 @Override
192 public void onInteractionFlagsChanged(@InteractionType int flags) {
193 mNavigationBarView.updateStates();
Winson Chungf9e30272018-03-26 17:25:36 -0700194 updateScreenPinningGestures();
Matthew Ng8f25fb962018-01-16 17:17:24 -0800195 }
Matthew Ng96985e72018-05-08 15:46:13 -0700196
197 @Override
Matthew Ng42025e32019-02-20 15:02:58 -0800198 public void startAssistant(Bundle bundle) {
199 mAssistManager.startAssist(bundle);
200 }
201
202 @Override
Matthew Ng96985e72018-05-08 15:46:13 -0700203 public void onBackButtonAlphaChanged(float alpha, boolean animate) {
204 final ButtonDispatcher backButton = mNavigationBarView.getBackButton();
Sunny Goyala6a58122019-04-02 10:20:29 -0700205 if (QuickStepContract.isGesturalMode(getContext())) {
Matthew Ngc83b9892018-08-21 16:31:13 -0700206 // If property was changed to hide/show back button, going home will trigger
207 // launcher to to change the back button alpha to reflect property change
208 backButton.setVisibility(View.GONE);
209 } else {
210 backButton.setVisibility(alpha > 0 ? View.VISIBLE : View.INVISIBLE);
211 backButton.setAlpha(alpha, animate);
212 }
Matthew Ng96985e72018-05-08 15:46:13 -0700213 }
Matthew Ng9c3bce52018-02-01 22:00:31 +0000214 };
Mike Digman7d092772018-01-11 12:10:32 -0800215
Charles Chen10ca70b2018-11-28 00:03:38 +0800216 private final ContextButtonListener mRotationButtonListener = (button, visible) -> {
217 if (visible) {
218 // If the button will actually become visible and the navbar is about to hide,
219 // tell the statusbar to keep it around for longer
Charles Chen8c9a83f2018-12-18 17:44:10 +0800220 mAutoHideController.touchAutoHide();
Charles Chen10ca70b2018-11-28 00:03:38 +0800221 }
222 };
223
224 private final Runnable mAutoDim = () -> getBarTransitions().setAutoDim(true);
225
Matthew Ngadd4c392019-03-01 16:02:31 -0800226 private final ContentObserver mAssistContentObserver = new ContentObserver(
227 new Handler(Looper.getMainLooper())) {
228 @Override
229 public void onChange(boolean selfChange, Uri uri) {
230 boolean available = mAssistManager
231 .getAssistInfoForUser(UserHandle.USER_CURRENT) != null;
232 if (mAssistantAvailable != available) {
233 mNavigationBarView.setAssistantAvailable(available);
234 mAssistantAvailable = available;
235 }
236 }
237 };
238
Jason Monk9424af72018-12-19 14:17:26 -0500239 @Inject
240 public NavigationBarFragment(AccessibilityManagerWrapper accessibilityManagerWrapper,
241 DeviceProvisionedController deviceProvisionedController, MetricsLogger metricsLogger,
242 AssistManager assistManager, OverviewProxyService overviewProxyService) {
243 mAccessibilityManagerWrapper = accessibilityManagerWrapper;
244 mDeviceProvisionedController = deviceProvisionedController;
245 mMetricsLogger = metricsLogger;
246 mAssistManager = assistManager;
Matthew Ngadd4c392019-03-01 16:02:31 -0800247 mAssistantAvailable = mAssistManager.getAssistInfoForUser(UserHandle.USER_CURRENT) != null;
Jason Monk9424af72018-12-19 14:17:26 -0500248 mOverviewProxyService = overviewProxyService;
249 }
250
Jason Monk49fa0162017-01-11 09:21:56 -0500251 // ----- Fragment Lifecycle Callbacks -----
252
253 @Override
254 public void onCreate(@Nullable Bundle savedInstanceState) {
255 super.onCreate(savedInstanceState);
Jason Monk9c7844c2017-01-18 15:21:53 -0500256 mCommandQueue = SysUiServiceProvider.getComponent(getContext(), CommandQueue.class);
Jason Monk8e4e4cb2018-12-04 11:10:24 -0500257 mCommandQueue.observe(getLifecycle(), this);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500258 mStatusBar = SysUiServiceProvider.getComponent(getContext(), StatusBar.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500259 mRecents = SysUiServiceProvider.getComponent(getContext(), Recents.class);
260 mDivider = SysUiServiceProvider.getComponent(getContext(), Divider.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500261 mWindowManager = getContext().getSystemService(WindowManager.class);
262 mAccessibilityManager = getContext().getSystemService(AccessibilityManager.class);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700263 mContentResolver = getContext().getContentResolver();
Casey Burkhardt74922c62017-02-13 12:43:16 -0800264 mMagnificationObserver = new MagnificationContentObserver(
265 getContext().getMainThreadHandler());
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700266 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(
Casey Burkhardt74922c62017-02-13 12:43:16 -0800267 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED), false,
Casey Burkhardt5d614402017-04-06 13:46:50 -0700268 mMagnificationObserver, UserHandle.USER_ALL);
Matthew Ngadd4c392019-03-01 16:02:31 -0800269 mContentResolver.registerContentObserver(
270 Settings.Secure.getUriFor(Settings.Secure.ASSISTANT),
271 false /* notifyForDescendants */, mAssistContentObserver, UserHandle.USER_ALL);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800272
Jason Monk49fa0162017-01-11 09:21:56 -0500273 if (savedInstanceState != null) {
274 mDisabledFlags1 = savedInstanceState.getInt(EXTRA_DISABLE_STATE, 0);
Mike Digmandd2f49e2018-03-16 10:54:22 -0700275 mDisabledFlags2 = savedInstanceState.getInt(EXTRA_DISABLE2_STATE, 0);
Jason Monk49fa0162017-01-11 09:21:56 -0500276 }
Jason Monk9424af72018-12-19 14:17:26 -0500277 mAccessibilityManagerWrapper.addCallback(mAccessibilityListener);
Tetsutoki Shiozawae294e492019-01-11 18:46:28 +0900278
279 // Respect the latest disabled-flags.
280 mCommandQueue.recomputeDisableFlags(mDisplayId, false);
Jason Monk49fa0162017-01-11 09:21:56 -0500281 }
282
283 @Override
284 public void onDestroy() {
285 super.onDestroy();
Jason Monk9424af72018-12-19 14:17:26 -0500286 mAccessibilityManagerWrapper.removeCallback(mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700287 mContentResolver.unregisterContentObserver(mMagnificationObserver);
Matthew Ngadd4c392019-03-01 16:02:31 -0800288 mContentResolver.unregisterContentObserver(mAssistContentObserver);
Jason Monk49fa0162017-01-11 09:21:56 -0500289 }
290
291 @Override
292 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
293 Bundle savedInstanceState) {
294 return inflater.inflate(R.layout.navigation_bar, container, false);
295 }
296
297 @Override
298 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
299 super.onViewCreated(view, savedInstanceState);
300 mNavigationBarView = (NavigationBarView) view;
Riddle Hsu19607772018-11-01 18:11:26 +0800301 final Display display = view.getDisplay();
302 // It may not have display when running unit test.
303 if (display != null) {
Charles Chen10ca70b2018-11-28 00:03:38 +0800304 mDisplayId = display.getDisplayId();
305 mIsOnDefaultDisplay = mDisplayId == Display.DEFAULT_DISPLAY;
Riddle Hsu19607772018-11-01 18:11:26 +0800306 }
Jason Monk49fa0162017-01-11 09:21:56 -0500307
Matthew Ng0548fbc2019-01-11 12:24:13 -0800308 mNavigationBarView.setComponents(mStatusBar.getPanel(), mAssistManager);
Matthew Ngf0f84542018-10-15 11:53:22 -0700309 mNavigationBarView.setDisabledFlags(mDisabledFlags1);
Jason Monk49fa0162017-01-11 09:21:56 -0500310 mNavigationBarView.setOnVerticalChangedListener(this::onVerticalChanged);
311 mNavigationBarView.setOnTouchListener(this::onNavigationTouch);
312 if (savedInstanceState != null) {
313 mNavigationBarView.getLightTransitionsController().restoreState(savedInstanceState);
314 }
315
316 prepareNavigationBarView();
317 checkNavBarModes();
318
319 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
320 filter.addAction(Intent.ACTION_SCREEN_ON);
Phil Weavera858c5f2018-04-26 13:27:30 -0700321 filter.addAction(Intent.ACTION_USER_SWITCHED);
Jason Monk49fa0162017-01-11 09:21:56 -0500322 getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100323 notifyNavigationBarScreenOn();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000324 mOverviewProxyService.addCallback(mOverviewProxyListener);
Matthew Ng761562d2018-09-17 11:13:21 -0700325
Riddle Hsu19607772018-11-01 18:11:26 +0800326 // Currently there is no accelerometer sensor on non-default display.
327 if (mIsOnDefaultDisplay) {
328 final RotationContextButton rotationButton =
329 mNavigationBarView.getRotateSuggestionButton();
330 rotationButton.setListener(mRotationButtonListener);
331 rotationButton.addRotationCallback(mRotationWatcher);
Matthew Ng761562d2018-09-17 11:13:21 -0700332
Riddle Hsu19607772018-11-01 18:11:26 +0800333 // Reset user rotation pref to match that of the WindowManager if starting in locked
334 // mode. This will automatically happen when switching from auto-rotate to locked mode.
335 if (display != null && rotationButton.isRotationLocked()) {
336 final int winRotation = display.getRotation();
337 rotationButton.setRotationLockedAtAngle(winRotation);
338 }
339 } else {
340 mDisabledFlags2 |= StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS;
Matthew Ng761562d2018-09-17 11:13:21 -0700341 }
Riddle Hsu19607772018-11-01 18:11:26 +0800342 setDisabled2Flags(mDisabledFlags2);
Jason Monk49fa0162017-01-11 09:21:56 -0500343 }
344
345 @Override
346 public void onDestroyView() {
347 super.onDestroyView();
Winson Chungb03d44d2018-09-18 12:42:45 -0700348 if (mNavigationBarView != null) {
349 mNavigationBarView.getBarTransitions().destroy();
350 mNavigationBarView.getLightTransitionsController().destroy(getContext());
351 }
Matthew Ng9c3bce52018-02-01 22:00:31 +0000352 mOverviewProxyService.removeCallback(mOverviewProxyListener);
Jason Monk49fa0162017-01-11 09:21:56 -0500353 getContext().unregisterReceiver(mBroadcastReceiver);
354 }
355
356 @Override
357 public void onSaveInstanceState(Bundle outState) {
358 super.onSaveInstanceState(outState);
359 outState.putInt(EXTRA_DISABLE_STATE, mDisabledFlags1);
Mike Digmandd2f49e2018-03-16 10:54:22 -0700360 outState.putInt(EXTRA_DISABLE2_STATE, mDisabledFlags2);
Jason Monk49fa0162017-01-11 09:21:56 -0500361 if (mNavigationBarView != null) {
362 mNavigationBarView.getLightTransitionsController().saveState(outState);
363 }
364 }
365
366 @Override
367 public void onConfigurationChanged(Configuration newConfig) {
368 super.onConfigurationChanged(newConfig);
369 final Locale locale = getContext().getResources().getConfiguration().locale;
370 final int ld = TextUtils.getLayoutDirectionFromLocale(locale);
371 if (!locale.equals(mLocale) || ld != mLayoutDirection) {
372 if (DEBUG) {
373 Log.v(TAG, String.format(
374 "config changed locale/LD: %s (%d) -> %s (%d)", mLocale, mLayoutDirection,
375 locale, ld));
376 }
377 mLocale = locale;
378 mLayoutDirection = ld;
379 refreshLayout(ld);
380 }
381 repositionNavigationBar();
382 }
383
384 @Override
385 public void dump(String prefix, FileDescriptor fd, PrintWriter pw, String[] args) {
386 if (mNavigationBarView != null) {
387 pw.print(" mNavigationBarWindowState=");
388 pw.println(windowStateToString(mNavigationBarWindowState));
389 pw.print(" mNavigationBarMode=");
390 pw.println(BarTransitions.modeToString(mNavigationBarMode));
391 dumpBarTransitions(pw, "mNavigationBarView", mNavigationBarView.getBarTransitions());
392 }
393
394 pw.print(" mNavigationBarView=");
395 if (mNavigationBarView == null) {
396 pw.println("null");
397 } else {
398 mNavigationBarView.dump(fd, pw, args);
399 }
400 }
401
402 // ----- CommandQueue Callbacks -----
403
404 @Override
Charles Chenf3d295c2018-11-30 18:15:21 +0800405 public void setImeWindowStatus(int displayId, IBinder token, int vis, int backDisposition,
Jason Monk49fa0162017-01-11 09:21:56 -0500406 boolean showImeSwitcher) {
Charles Chenf3d295c2018-11-30 18:15:21 +0800407 if (displayId != mDisplayId) {
408 return;
409 }
Jason Monk49fa0162017-01-11 09:21:56 -0500410 boolean imeShown = (vis & InputMethodService.IME_VISIBLE) != 0;
411 int hints = mNavigationIconHints;
Yohei Yukawa386f50e2018-03-14 13:03:42 -0700412 switch (backDisposition) {
413 case InputMethodService.BACK_DISPOSITION_DEFAULT:
414 case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
415 case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
416 if (imeShown) {
417 hints |= NAVIGATION_HINT_BACK_ALT;
418 } else {
419 hints &= ~NAVIGATION_HINT_BACK_ALT;
420 }
421 break;
422 case InputMethodService.BACK_DISPOSITION_ADJUST_NOTHING:
423 hints &= ~NAVIGATION_HINT_BACK_ALT;
424 break;
Jason Monk49fa0162017-01-11 09:21:56 -0500425 }
426 if (showImeSwitcher) {
427 hints |= NAVIGATION_HINT_IME_SHOWN;
428 } else {
429 hints &= ~NAVIGATION_HINT_IME_SHOWN;
430 }
431 if (hints == mNavigationIconHints) return;
432
433 mNavigationIconHints = hints;
434
435 if (mNavigationBarView != null) {
436 mNavigationBarView.setNavigationIconHints(hints);
437 }
Charles Chen10ca70b2018-11-28 00:03:38 +0800438 checkBarModes();
Jason Monk49fa0162017-01-11 09:21:56 -0500439 }
440
441 @Override
Charles Chenf3d295c2018-11-30 18:15:21 +0800442 public void topAppWindowChanged(int displayId, boolean showMenu) {
443 if (displayId == mDisplayId && mNavigationBarView != null) {
Jason Monk49fa0162017-01-11 09:21:56 -0500444 mNavigationBarView.setMenuVisibility(showMenu);
445 }
446 }
447
448 @Override
Charles Chenf3d295c2018-11-30 18:15:21 +0800449 public void setWindowState(
450 int displayId, @WindowType int window, @WindowVisibleState int state) {
451 if (displayId == mDisplayId
452 && mNavigationBarView != null
Jason Monk49fa0162017-01-11 09:21:56 -0500453 && window == StatusBarManager.WINDOW_NAVIGATION_BAR
454 && mNavigationBarWindowState != state) {
455 mNavigationBarWindowState = state;
456 if (DEBUG_WINDOW_STATE) Log.d(TAG, "Navigation bar " + windowStateToString(state));
Mike Digman5aeca792018-03-05 11:14:39 -0800457
Matthew Ng761562d2018-09-17 11:13:21 -0700458 mNavigationBarView.getRotateSuggestionButton()
459 .onNavigationBarWindowVisibilityChange(state == WINDOW_STATE_SHOWING);
Jason Monk49fa0162017-01-11 09:21:56 -0500460 }
461 }
462
Mike Digman7d092772018-01-11 12:10:32 -0800463 @Override
Mike Digmane0777312018-01-19 12:41:51 -0800464 public void onRotationProposal(final int rotation, boolean isValid) {
Riddle Hsu19607772018-11-01 18:11:26 +0800465 final int winRotation = mNavigationBarView.getDisplay().getRotation();
Matthew Ng761562d2018-09-17 11:13:21 -0700466 final boolean rotateSuggestionsDisabled = RotationContextButton
467 .hasDisable2RotateSuggestionFlag(mDisabledFlags2);
468 if (RotationContextButton.DEBUG_ROTATION) {
Mike Digman3e33da62018-03-14 16:25:11 -0700469 Log.v(TAG, "onRotationProposal proposedRotation=" + Surface.rotationToString(rotation)
470 + ", winRotation=" + Surface.rotationToString(winRotation)
471 + ", isValid=" + isValid + ", mNavBarWindowState="
472 + StatusBarManager.windowStateToString(mNavigationBarWindowState)
Mike Digmandd2f49e2018-03-16 10:54:22 -0700473 + ", rotateSuggestionsDisabled=" + rotateSuggestionsDisabled
Mike Digman3e33da62018-03-14 16:25:11 -0700474 + ", isRotateButtonVisible=" + (mNavigationBarView == null ? "null" :
Matthew Ng761562d2018-09-17 11:13:21 -0700475 mNavigationBarView.getRotateSuggestionButton().isVisible()));
Mike Digman3e33da62018-03-14 16:25:11 -0700476 }
477
Mike Digmandd2f49e2018-03-16 10:54:22 -0700478 // Respect the disabled flag, no need for action as flag change callback will handle hiding
479 if (rotateSuggestionsDisabled) return;
480
Matthew Ngb55c02c2019-02-15 16:20:31 -0800481 View rotationButton = mNavigationBarView.getRotateSuggestionButton().getCurrentView();
482 if (rotationButton != null && rotationButton.isAttachedToWindow()) {
483 mNavigationBarView.getRotateSuggestionButton()
484 .onRotationProposal(rotation, winRotation, isValid);
485 }
Mike Digman50752642018-02-15 13:36:09 -0800486 }
487
Charles Chen10ca70b2018-11-28 00:03:38 +0800488 /**
489 * Sets System UI flags to {@link NavigationBarFragment}.
490 *
491 * @see View#setSystemUiVisibility(int)
492 */
493 public void setSystemUiVisibility(int systemUiVisibility) {
Jason Monk49fa0162017-01-11 09:21:56 -0500494 mSystemUiVisibility = systemUiVisibility;
Charles Chen10ca70b2018-11-28 00:03:38 +0800495 final int barMode = computeBarMode(0, mSystemUiVisibility);
Matthew Ng5a250202018-10-09 16:09:27 -0700496 if (barMode != -1) {
497 mNavigationBarMode = barMode;
498 }
Jason Monk49fa0162017-01-11 09:21:56 -0500499 checkNavBarModes();
Charles Chen8c9a83f2018-12-18 17:44:10 +0800500 mAutoHideController.touchAutoHide();
Andrii Kulian3ddd7de2018-10-10 00:33:19 -0700501
Charles Chen10ca70b2018-11-28 00:03:38 +0800502 mLightBarController.onNavigationVisibilityChanged(mSystemUiVisibility, 0 /* mask */,
Charles Chen8c9a83f2018-12-18 17:44:10 +0800503 true /* nbModeChanged */, mNavigationBarMode);
Jason Monk49fa0162017-01-11 09:21:56 -0500504 }
505
506 @Override
Charles Chenf3d295c2018-11-30 18:15:21 +0800507 public void setSystemUiVisibility(int displayId, int vis, int fullscreenStackVis,
508 int dockedStackVis, int mask, Rect fullscreenStackBounds, Rect dockedStackBounds) {
509 if (displayId != mDisplayId) {
510 return;
511 }
Jason Monk49fa0162017-01-11 09:21:56 -0500512 final int oldVal = mSystemUiVisibility;
513 final int newVal = (oldVal & ~mask) | (vis & mask);
514 final int diff = newVal ^ oldVal;
515 boolean nbModeChanged = false;
516 if (diff != 0) {
517 mSystemUiVisibility = newVal;
518
519 // update navigation bar mode
520 final int nbMode = getView() == null
Charles Chen10ca70b2018-11-28 00:03:38 +0800521 ? -1 : computeBarMode(oldVal, newVal);
Jason Monk49fa0162017-01-11 09:21:56 -0500522 nbModeChanged = nbMode != -1;
523 if (nbModeChanged) {
524 if (mNavigationBarMode != nbMode) {
Tracy Zhou1ac592c2018-07-25 13:47:37 -0700525 if (mNavigationBarMode == MODE_TRANSPARENT
526 || mNavigationBarMode == MODE_LIGHTS_OUT_TRANSPARENT) {
527 mNavigationBarView.hideRecentsOnboarding();
528 }
Jason Monk49fa0162017-01-11 09:21:56 -0500529 mNavigationBarMode = nbMode;
530 checkNavBarModes();
531 }
Charles Chen8c9a83f2018-12-18 17:44:10 +0800532 mAutoHideController.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500533 }
534 }
Charles Chen10ca70b2018-11-28 00:03:38 +0800535 mLightBarController.onNavigationVisibilityChanged(
536 vis, mask, nbModeChanged, mNavigationBarMode);
537 }
Jason Monk49fa0162017-01-11 09:21:56 -0500538
Charles Chen10ca70b2018-11-28 00:03:38 +0800539 private @TransitionMode int computeBarMode(int oldVis, int newVis) {
540 final int oldMode = barMode(oldVis);
541 final int newMode = barMode(newVis);
542 if (oldMode == newMode) {
543 return -1; // no mode change
544 }
545 return newMode;
546 }
547
548 private @TransitionMode int barMode(int vis) {
549 final int lightsOutTransparent =
550 View.SYSTEM_UI_FLAG_LOW_PROFILE | View.NAVIGATION_BAR_TRANSIENT;
551 if ((vis & View.NAVIGATION_BAR_TRANSIENT) != 0) {
552 return MODE_SEMI_TRANSPARENT;
553 } else if ((vis & View.NAVIGATION_BAR_TRANSLUCENT) != 0) {
554 return MODE_TRANSLUCENT;
555 } else if ((vis & lightsOutTransparent) == lightsOutTransparent) {
556 return MODE_LIGHTS_OUT_TRANSPARENT;
557 } else if ((vis & View.NAVIGATION_BAR_TRANSPARENT) != 0) {
558 return MODE_TRANSPARENT;
559 } else if ((vis & View.SYSTEM_UI_FLAG_LOW_PROFILE) != 0) {
560 return MODE_LIGHTS_OUT;
561 } else {
562 return MODE_OPAQUE;
Andrii Kulian3ddd7de2018-10-10 00:33:19 -0700563 }
Jason Monk49fa0162017-01-11 09:21:56 -0500564 }
565
566 @Override
Charles Chenf3d295c2018-11-30 18:15:21 +0800567 public void disable(int displayId, int state1, int state2, boolean animate) {
568 if (displayId != mDisplayId) {
569 return;
570 }
Mike Digmandd2f49e2018-03-16 10:54:22 -0700571 // Navigation bar flags are in both state1 and state2.
572 final int masked = state1 & (StatusBarManager.DISABLE_HOME
Jason Monk49fa0162017-01-11 09:21:56 -0500573 | StatusBarManager.DISABLE_RECENT
574 | StatusBarManager.DISABLE_BACK
575 | StatusBarManager.DISABLE_SEARCH);
576 if (masked != mDisabledFlags1) {
577 mDisabledFlags1 = masked;
578 if (mNavigationBarView != null) mNavigationBarView.setDisabledFlags(state1);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000579 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500580 }
Mike Digmandd2f49e2018-03-16 10:54:22 -0700581
Riddle Hsu19607772018-11-01 18:11:26 +0800582 // Only default display supports rotation suggestions.
583 if (mIsOnDefaultDisplay) {
584 final int masked2 = state2 & (StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS);
585 if (masked2 != mDisabledFlags2) {
586 mDisabledFlags2 = masked2;
587 setDisabled2Flags(masked2);
588 }
Mike Digmandd2f49e2018-03-16 10:54:22 -0700589 }
590 }
591
592 private void setDisabled2Flags(int state2) {
593 // Method only called on change of disable2 flags
Evan Lairdb8b717f2018-10-25 14:08:32 -0400594 if (mNavigationBarView != null) {
595 mNavigationBarView.getRotateSuggestionButton().onDisable2FlagChanged(state2);
596 }
Jason Monk49fa0162017-01-11 09:21:56 -0500597 }
598
Charles Chen10ca70b2018-11-28 00:03:38 +0800599 // ----- Internal stuff -----
Jason Monk49fa0162017-01-11 09:21:56 -0500600
601 private void refreshLayout(int layoutDirection) {
602 if (mNavigationBarView != null) {
603 mNavigationBarView.setLayoutDirection(layoutDirection);
604 }
605 }
606
607 private boolean shouldDisableNavbarGestures() {
Jason Monk297c04e2018-08-23 17:16:59 -0400608 return !mDeviceProvisionedController.isDeviceProvisioned()
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800609 || (mDisabledFlags1 & StatusBarManager.DISABLE_SEARCH) != 0;
Jason Monk49fa0162017-01-11 09:21:56 -0500610 }
611
612 private void repositionNavigationBar() {
613 if (mNavigationBarView == null || !mNavigationBarView.isAttachedToWindow()) return;
614
615 prepareNavigationBarView();
616
617 mWindowManager.updateViewLayout((View) mNavigationBarView.getParent(),
618 ((View) mNavigationBarView.getParent()).getLayoutParams());
619 }
620
Matthew Ng9c3bce52018-02-01 22:00:31 +0000621 private void updateScreenPinningGestures() {
622 if (mNavigationBarView == null) {
623 return;
624 }
625
626 // Change the cancel pin gesture to home and back if recents button is invisible
627 boolean recentsVisible = mNavigationBarView.isRecentsButtonVisible();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000628 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
629 if (recentsVisible) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000630 backButton.setOnLongClickListener(this::onLongPressBackRecents);
631 } else {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000632 backButton.setOnLongClickListener(this::onLongPressBackHome);
633 }
634 }
635
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100636 private void notifyNavigationBarScreenOn() {
Matthew Ngd0a73e72018-03-02 15:16:03 -0800637 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500638 }
639
640 private void prepareNavigationBarView() {
641 mNavigationBarView.reorient();
642
643 ButtonDispatcher recentsButton = mNavigationBarView.getRecentsButton();
644 recentsButton.setOnClickListener(this::onRecentsClick);
645 recentsButton.setOnTouchListener(this::onRecentsTouch);
646 recentsButton.setLongClickable(true);
647 recentsButton.setOnLongClickListener(this::onLongPressBackRecents);
648
649 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
650 backButton.setLongClickable(true);
Jason Monk49fa0162017-01-11 09:21:56 -0500651
652 ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();
653 homeButton.setOnTouchListener(this::onHomeTouch);
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800654 homeButton.setOnLongClickListener(this::onHomeLongClick);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800655
656 ButtonDispatcher accessibilityButton = mNavigationBarView.getAccessibilityButton();
657 accessibilityButton.setOnClickListener(this::onAccessibilityClick);
658 accessibilityButton.setOnLongClickListener(this::onAccessibilityLongClick);
Phil Weaverdb9a7742017-04-18 08:15:06 -0700659 updateAccessibilityServicesState(mAccessibilityManager);
Mike Digman7d092772018-01-11 12:10:32 -0800660
Matthew Ng9c3bce52018-02-01 22:00:31 +0000661 updateScreenPinningGestures();
Jason Monk49fa0162017-01-11 09:21:56 -0500662 }
663
664 private boolean onHomeTouch(View v, MotionEvent event) {
665 if (mHomeBlockedThisTouch && event.getActionMasked() != MotionEvent.ACTION_DOWN) {
666 return true;
667 }
668 // If an incoming call is ringing, HOME is totally disabled.
669 // (The user is already on the InCallUI at this point,
670 // and his ONLY options are to answer or reject the call.)
671 switch (event.getAction()) {
672 case MotionEvent.ACTION_DOWN:
673 mHomeBlockedThisTouch = false;
674 TelecomManager telecomManager =
675 getContext().getSystemService(TelecomManager.class);
676 if (telecomManager != null && telecomManager.isRinging()) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500677 if (mStatusBar.isKeyguardShowing()) {
Jason Monk49fa0162017-01-11 09:21:56 -0500678 Log.i(TAG, "Ignoring HOME; there's a ringing incoming call. " +
679 "No heads up");
680 mHomeBlockedThisTouch = true;
681 return true;
682 }
683 }
684 break;
685 case MotionEvent.ACTION_UP:
686 case MotionEvent.ACTION_CANCEL:
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500687 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500688 break;
689 }
690 return false;
691 }
692
693 private void onVerticalChanged(boolean isVertical) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500694 mStatusBar.setQsScrimEnabled(!isVertical);
Jason Monk49fa0162017-01-11 09:21:56 -0500695 }
696
697 private boolean onNavigationTouch(View v, MotionEvent event) {
Charles Chen8c9a83f2018-12-18 17:44:10 +0800698 mAutoHideController.checkUserAutoHide(event);
Jason Monk49fa0162017-01-11 09:21:56 -0500699 return false;
700 }
701
Jason Monk865246d2017-01-19 08:27:01 -0500702 @VisibleForTesting
703 boolean onHomeLongClick(View v) {
Matthew Ng6ff33b72018-02-27 13:47:38 -0800704 if (!mNavigationBarView.isRecentsButtonVisible()
705 && ActivityManagerWrapper.getInstance().isScreenPinningActive()) {
Matthew Ngfee0b5b2018-02-21 15:38:21 -0800706 return onLongPressBackHome(v);
707 }
Jason Monk49fa0162017-01-11 09:21:56 -0500708 if (shouldDisableNavbarGestures()) {
709 return false;
710 }
Matthew Ng2ea93b72018-03-14 19:43:18 +0000711 mNavigationBarView.onNavigationButtonLongPress(v);
Mike Digmanc94759d2018-01-23 11:01:21 -0800712 mMetricsLogger.action(MetricsEvent.ACTION_ASSIST_LONG_PRESS);
Jason Monk49fa0162017-01-11 09:21:56 -0500713 mAssistManager.startAssist(new Bundle() /* args */);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500714 mStatusBar.awakenDreams();
Matthew Ngdc79e5c2017-12-14 17:37:35 -0800715
Jason Monk49fa0162017-01-11 09:21:56 -0500716 if (mNavigationBarView != null) {
717 mNavigationBarView.abortCurrentGesture();
718 }
719 return true;
720 }
721
722 // additional optimization when we have software system buttons - start loading the recent
723 // tasks on touch down
724 private boolean onRecentsTouch(View v, MotionEvent event) {
725 int action = event.getAction() & MotionEvent.ACTION_MASK;
726 if (action == MotionEvent.ACTION_DOWN) {
727 mCommandQueue.preloadRecentApps();
728 } else if (action == MotionEvent.ACTION_CANCEL) {
729 mCommandQueue.cancelPreloadRecentApps();
730 } else if (action == MotionEvent.ACTION_UP) {
731 if (!v.isPressed()) {
732 mCommandQueue.cancelPreloadRecentApps();
733 }
734 }
735 return false;
736 }
737
738 private void onRecentsClick(View v) {
739 if (LatencyTracker.isEnabled(getContext())) {
740 LatencyTracker.getInstance(getContext()).onActionStart(
741 LatencyTracker.ACTION_TOGGLE_RECENTS);
742 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500743 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500744 mCommandQueue.toggleRecentApps();
745 }
746
Matthew Ng9c3bce52018-02-01 22:00:31 +0000747 private boolean onLongPressBackHome(View v) {
Matthew Ng2ea93b72018-03-14 19:43:18 +0000748 mNavigationBarView.onNavigationButtonLongPress(v);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000749 return onLongPressNavigationButtons(v, R.id.back, R.id.home);
750 }
751
752 private boolean onLongPressBackRecents(View v) {
Matthew Ng2ea93b72018-03-14 19:43:18 +0000753 mNavigationBarView.onNavigationButtonLongPress(v);
Matthew Ng9c3bce52018-02-01 22:00:31 +0000754 return onLongPressNavigationButtons(v, R.id.back, R.id.recent_apps);
755 }
756
Jason Monk49fa0162017-01-11 09:21:56 -0500757 /**
Matthew Ng9c3bce52018-02-01 22:00:31 +0000758 * This handles long-press of both back and recents/home. Back is the common button with
759 * combination of recents if it is visible or home if recents is invisible.
760 * They are handled together to capture them both being long-pressed
Jason Monk49fa0162017-01-11 09:21:56 -0500761 * at the same time to exit screen pinning (lock task).
762 *
Matthew Ng9c3bce52018-02-01 22:00:31 +0000763 * When accessibility mode is on, only a long-press from recents/home
Jason Monk49fa0162017-01-11 09:21:56 -0500764 * is required to exit.
765 *
766 * In all other circumstances we try to pass through long-press events
767 * for Back, so that apps can still use it. Which can be from two things.
768 * 1) Not currently in screen pinning (lock task).
Matthew Ng9c3bce52018-02-01 22:00:31 +0000769 * 2) Back is long-pressed without recents/home.
Jason Monk49fa0162017-01-11 09:21:56 -0500770 */
Matthew Ng9c3bce52018-02-01 22:00:31 +0000771 private boolean onLongPressNavigationButtons(View v, @IdRes int btnId1, @IdRes int btnId2) {
Jason Monk49fa0162017-01-11 09:21:56 -0500772 try {
773 boolean sendBackLongPress = false;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700774 IActivityTaskManager activityManager = ActivityTaskManager.getService();
Jason Monk49fa0162017-01-11 09:21:56 -0500775 boolean touchExplorationEnabled = mAccessibilityManager.isTouchExplorationEnabled();
776 boolean inLockTaskMode = activityManager.isInLockTaskMode();
777 if (inLockTaskMode && !touchExplorationEnabled) {
778 long time = System.currentTimeMillis();
Matthew Ng9c3bce52018-02-01 22:00:31 +0000779
Jason Monk49fa0162017-01-11 09:21:56 -0500780 // If we recently long-pressed the other button then they were
781 // long-pressed 'together'
782 if ((time - mLastLockToAppLongPress) < LOCK_TO_APP_GESTURE_TOLERENCE) {
Benjamin Franza83859f2017-07-03 16:34:14 +0100783 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500784 // When exiting refresh disabled flags.
Matthew Ngd0a73e72018-03-02 15:16:03 -0800785 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500786 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000787 } else if (v.getId() == btnId1) {
788 ButtonDispatcher button = btnId2 == R.id.recent_apps
789 ? mNavigationBarView.getRecentsButton()
790 : mNavigationBarView.getHomeButton();
791 if (!button.getCurrentView().isPressed()) {
792 // If we aren't pressing recents/home right now then they presses
793 // won't be together, so send the standard long-press action.
794 sendBackLongPress = true;
795 }
Jason Monk49fa0162017-01-11 09:21:56 -0500796 }
797 mLastLockToAppLongPress = time;
798 } else {
799 // If this is back still need to handle sending the long-press event.
Matthew Ng9c3bce52018-02-01 22:00:31 +0000800 if (v.getId() == btnId1) {
Jason Monk49fa0162017-01-11 09:21:56 -0500801 sendBackLongPress = true;
802 } else if (touchExplorationEnabled && inLockTaskMode) {
Matthew Ng9c3bce52018-02-01 22:00:31 +0000803 // When in accessibility mode a long press that is recents/home (not back)
Jason Monk49fa0162017-01-11 09:21:56 -0500804 // should stop lock task.
Benjamin Franza83859f2017-07-03 16:34:14 +0100805 activityManager.stopSystemLockTaskMode();
Jason Monk49fa0162017-01-11 09:21:56 -0500806 // When exiting refresh disabled flags.
Matthew Ngd0a73e72018-03-02 15:16:03 -0800807 mNavigationBarView.updateNavButtonIcons();
Jason Monk49fa0162017-01-11 09:21:56 -0500808 return true;
Matthew Ng9c3bce52018-02-01 22:00:31 +0000809 } else if (v.getId() == btnId2) {
810 return btnId2 == R.id.recent_apps
811 ? onLongPressRecents()
812 : onHomeLongClick(mNavigationBarView.getHomeButton().getCurrentView());
Jason Monk49fa0162017-01-11 09:21:56 -0500813 }
814 }
815 if (sendBackLongPress) {
816 KeyButtonView keyButtonView = (KeyButtonView) v;
817 keyButtonView.sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS);
818 keyButtonView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
819 return true;
820 }
821 } catch (RemoteException e) {
822 Log.d(TAG, "Unable to reach activity manager", e);
823 }
824 return false;
825 }
826
827 private boolean onLongPressRecents() {
Wale Ogunwale65ebd952018-04-25 15:41:44 -0700828 if (mRecents == null || !ActivityTaskManager.supportsMultiWindow(getContext())
Matthew Ng43db6d22017-06-27 15:29:39 -0700829 || !mDivider.getView().getSnapAlgorithm().isSplitScreenFeasible()
Winson Chung67f5c8b2018-09-24 12:09:19 -0700830 || ActivityManager.isLowRamDeviceStatic()
Winson Chung074c4342018-03-26 17:27:19 -0700831 // If we are connected to the overview service, then disable the recents button
832 || mOverviewProxyService.getProxy() != null) {
Jason Monk49fa0162017-01-11 09:21:56 -0500833 return false;
834 }
835
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500836 return mStatusBar.toggleSplitScreenMode(MetricsEvent.ACTION_WINDOW_DOCK_LONGPRESS,
Jason Monk49fa0162017-01-11 09:21:56 -0500837 MetricsEvent.ACTION_WINDOW_UNDOCK_LONGPRESS);
838 }
839
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800840 private void onAccessibilityClick(View v) {
Jackal Guo8b729932018-10-19 18:02:51 +0800841 final Display display = v.getDisplay();
842 mAccessibilityManager.notifyAccessibilityButtonClicked(
843 display != null ? display.getDisplayId() : Display.DEFAULT_DISPLAY);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800844 }
845
846 private boolean onAccessibilityLongClick(View v) {
Casey Burkhardt5e8b9802017-03-24 10:07:20 -0700847 Intent intent = new Intent(AccessibilityManager.ACTION_CHOOSE_ACCESSIBILITY_BUTTON);
848 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Casey Burkhardt5d614402017-04-06 13:46:50 -0700849 v.getContext().startActivityAsUser(intent, UserHandle.CURRENT);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800850 return true;
851 }
852
Phil Weaverdb9a7742017-04-18 08:15:06 -0700853 private void updateAccessibilityServicesState(AccessibilityManager accessibilityManager) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800854 int requestingServices = 0;
855 try {
Casey Burkhardt5d614402017-04-06 13:46:50 -0700856 if (Settings.Secure.getIntForUser(mContentResolver,
857 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED,
858 UserHandle.USER_CURRENT) == 1) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800859 requestingServices++;
860 }
861 } catch (Settings.SettingNotFoundException e) {
862 }
863
Mike Digman90402952018-01-22 16:05:51 -0800864 boolean feedbackEnabled = false;
Casey Burkhardt5d614402017-04-06 13:46:50 -0700865 // AccessibilityManagerService resolves services for the current user since the local
866 // AccessibilityManager is created from a Context with the INTERACT_ACROSS_USERS permission
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800867 final List<AccessibilityServiceInfo> services =
Phil Weaverdb9a7742017-04-18 08:15:06 -0700868 accessibilityManager.getEnabledAccessibilityServiceList(
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800869 AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800870 for (int i = services.size() - 1; i >= 0; --i) {
871 AccessibilityServiceInfo info = services.get(i);
872 if ((info.flags & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0) {
873 requestingServices++;
874 }
Mike Digman90402952018-01-22 16:05:51 -0800875
876 if (info.feedbackType != 0 && info.feedbackType !=
877 AccessibilityServiceInfo.FEEDBACK_GENERIC) {
878 feedbackEnabled = true;
879 }
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800880 }
881
Matthew Ng761562d2018-09-17 11:13:21 -0700882 mNavigationBarView.getRotateSuggestionButton()
883 .setAccessibilityFeedbackEnabled(feedbackEnabled);
Mike Digman90402952018-01-22 16:05:51 -0800884
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800885 final boolean showAccessibilityButton = requestingServices >= 1;
886 final boolean targetSelection = requestingServices >= 2;
887 mNavigationBarView.setAccessibilityButtonState(showAccessibilityButton, targetSelection);
888 }
889
Charles Chen10ca70b2018-11-28 00:03:38 +0800890 // ----- Methods that DisplayNavigationBarController talks to -----
891
Charles Chen8c9a83f2018-12-18 17:44:10 +0800892 /** Applies auto dimming animation on navigation bar when touched. */
Charles Chen10ca70b2018-11-28 00:03:38 +0800893 public void touchAutoDim() {
894 getBarTransitions().setAutoDim(false);
895 mHandler.removeCallbacks(mAutoDim);
896 int state = Dependency.get(StatusBarStateController.class).getState();
897 if (state != StatusBarState.KEYGUARD && state != StatusBarState.SHADE_LOCKED) {
Charles Chen8c9a83f2018-12-18 17:44:10 +0800898 mHandler.postDelayed(mAutoDim, AUTODIM_TIMEOUT_MS);
Charles Chen10ca70b2018-11-28 00:03:38 +0800899 }
900 }
Jason Monk49fa0162017-01-11 09:21:56 -0500901
Jason Monk49fa0162017-01-11 09:21:56 -0500902 public void setLightBarController(LightBarController lightBarController) {
903 mLightBarController = lightBarController;
904 mLightBarController.setNavigationBar(mNavigationBarView.getLightTransitionsController());
905 }
906
Charles Chen8c9a83f2018-12-18 17:44:10 +0800907 /** Sets {@link AutoHideController} to the navigation bar. */
908 public void setAutoHideController(AutoHideController autoHideController) {
909 mAutoHideController = autoHideController;
910 mAutoHideController.setNavigationBar(this);
911 }
912
Jason Monk49fa0162017-01-11 09:21:56 -0500913 public boolean isSemiTransparent() {
914 return mNavigationBarMode == MODE_SEMI_TRANSPARENT;
915 }
916
Charles Chen10ca70b2018-11-28 00:03:38 +0800917 private void checkBarModes() {
918 // We only have status bar on default display now.
919 if (mIsOnDefaultDisplay) {
920 mStatusBar.checkBarModes();
921 } else {
922 checkNavBarModes();
923 }
924 }
925
926 /**
927 * Checks current navigation bar mode and make transitions.
928 */
929 public void checkNavBarModes() {
930 final boolean anim = mStatusBar.isDeviceInteractive()
931 && mNavigationBarWindowState != WINDOW_STATE_HIDDEN;
932 mNavigationBarView.getBarTransitions().transitionTo(mNavigationBarMode, anim);
933 }
934
Jason Monk49fa0162017-01-11 09:21:56 -0500935 public void disableAnimationsDuringHide(long delay) {
936 mNavigationBarView.setLayoutTransitionsEnabled(false);
937 mNavigationBarView.postDelayed(() -> mNavigationBarView.setLayoutTransitionsEnabled(true),
938 delay + StackStateAnimator.ANIMATION_DURATION_GO_TO_FULL_SHADE);
939 }
940
Charles Chen10ca70b2018-11-28 00:03:38 +0800941 /**
942 * Performs transitions on navigation bar.
943 *
944 * @param barMode transition bar mode.
945 * @param animate shows animations if {@code true}.
946 */
947 public void transitionTo(@TransitionMode int barMode, boolean animate) {
948 getBarTransitions().transitionTo(barMode, animate);
Jason Monk49fa0162017-01-11 09:21:56 -0500949 }
950
Charles Chen10ca70b2018-11-28 00:03:38 +0800951 private BarTransitions getBarTransitions() {
952 return mNavigationBarView.getBarTransitions();
Jason Monk49fa0162017-01-11 09:21:56 -0500953 }
954
955 public void finishBarAnimations() {
956 mNavigationBarView.getBarTransitions().finishAnimations();
957 }
958
Jason Monk91e587e2017-04-13 13:41:23 -0400959 private final AccessibilityServicesStateChangeListener mAccessibilityListener =
960 this::updateAccessibilityServicesState;
961
Casey Burkhardt74922c62017-02-13 12:43:16 -0800962 private class MagnificationContentObserver extends ContentObserver {
963
964 public MagnificationContentObserver(Handler handler) {
965 super(handler);
966 }
967
968 @Override
969 public void onChange(boolean selfChange) {
Phil Weaverdb9a7742017-04-18 08:15:06 -0700970 NavigationBarFragment.this.updateAccessibilityServicesState(mAccessibilityManager);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800971 }
972 }
973
Matthew Ng761562d2018-09-17 11:13:21 -0700974 private final Consumer<Integer> mRotationWatcher = rotation -> {
975 if (mNavigationBarView != null
976 && mNavigationBarView.needsReorient(rotation)) {
977 repositionNavigationBar();
Mike Digman85ff7fa2018-01-23 14:59:52 -0800978 }
Jason Monk49fa0162017-01-11 09:21:56 -0500979 };
980
981 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
982 @Override
983 public void onReceive(Context context, Intent intent) {
984 String action = intent.getAction();
Siarhei Vishniakoud002a0a2017-06-05 22:44:37 +0100985 if (Intent.ACTION_SCREEN_OFF.equals(action)
986 || Intent.ACTION_SCREEN_ON.equals(action)) {
987 notifyNavigationBarScreenOn();
Matthew Ng355fe212018-12-14 17:42:38 -0800988
989 if (Intent.ACTION_SCREEN_ON.equals(action)) {
990 // Enabled and screen is on, start it again if enabled
991 if (NavBarTintController.isEnabled(getContext())) {
Winson Chung775d2872019-03-27 14:14:01 -0700992 mNavigationBarView.getTintController().start();
Matthew Ng355fe212018-12-14 17:42:38 -0800993 }
994 } else {
995 // Screen off disable it
Winson Chung775d2872019-03-27 14:14:01 -0700996 mNavigationBarView.getTintController().stop();
Matthew Ng355fe212018-12-14 17:42:38 -0800997 }
Jason Monk49fa0162017-01-11 09:21:56 -0500998 }
Phil Weavera858c5f2018-04-26 13:27:30 -0700999 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
1000 // The accessibility settings may be different for the new user
1001 updateAccessibilityServicesState(mAccessibilityManager);
Charles Chen10ca70b2018-11-28 00:03:38 +08001002 }
Jason Monk49fa0162017-01-11 09:21:56 -05001003 }
1004 };
1005
1006 public static View create(Context context, FragmentListener listener) {
1007 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
Charles Chenb6aff432018-11-16 20:25:46 +08001008 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
1009 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR,
Jason Monk49fa0162017-01-11 09:21:56 -05001010 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
1011 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1012 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
1013 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1014 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
1015 | WindowManager.LayoutParams.FLAG_SLIPPERY,
1016 PixelFormat.TRANSLUCENT);
1017 lp.token = new Binder();
Charles Chen10ca70b2018-11-28 00:03:38 +08001018 lp.setTitle("NavigationBar" + context.getDisplayId());
Phil Weaver8583ae82018-02-13 11:01:24 -08001019 lp.accessibilityTitle = context.getString(R.string.nav_bar);
Jason Monk49fa0162017-01-11 09:21:56 -05001020 lp.windowAnimations = 0;
Peiyong Lin75045382019-03-04 19:22:33 -08001021 lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COLOR_SPACE_AGNOSTIC;
Jason Monk49fa0162017-01-11 09:21:56 -05001022
1023 View navigationBarView = LayoutInflater.from(context).inflate(
1024 R.layout.navigation_bar_window, null);
1025
1026 if (DEBUG) Log.v(TAG, "addNavigationBar: about to add " + navigationBarView);
1027 if (navigationBarView == null) return null;
1028
Jason Monk9424af72018-12-19 14:17:26 -05001029 final NavigationBarFragment fragment = FragmentHostManager.get(navigationBarView)
1030 .create(NavigationBarFragment.class);
Riddle Hsu19607772018-11-01 18:11:26 +08001031 navigationBarView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
1032 @Override
1033 public void onViewAttachedToWindow(View v) {
1034 final FragmentHostManager fragmentHost = FragmentHostManager.get(v);
1035 fragmentHost.getFragmentManager().beginTransaction()
1036 .replace(R.id.navigation_bar_frame, fragment, TAG)
1037 .commit();
1038 fragmentHost.addTagListener(TAG, listener);
1039 }
1040
1041 @Override
1042 public void onViewDetachedFromWindow(View v) {
1043 FragmentHostManager.removeAndDestroy(v);
1044 }
1045 });
Jason Monk49fa0162017-01-11 09:21:56 -05001046 context.getSystemService(WindowManager.class).addView(navigationBarView, lp);
Jason Monk49fa0162017-01-11 09:21:56 -05001047 return navigationBarView;
1048 }
1049}