blob: 4bfc16b7b4d6b911cd37e1ca6205132efaecc1b4 [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
22import static com.android.systemui.statusbar.phone.BarTransitions.MODE_SEMI_TRANSPARENT;
Jason Monk2a6ea9c2017-01-26 11:14:51 -050023import static com.android.systemui.statusbar.phone.StatusBar.DEBUG_WINDOW_STATE;
24import static com.android.systemui.statusbar.phone.StatusBar.dumpBarTransitions;
Jason Monk49fa0162017-01-11 09:21:56 -050025
Casey Burkhardt048c2bc2016-12-08 16:09:20 -080026import android.accessibilityservice.AccessibilityServiceInfo;
Jason Monk49fa0162017-01-11 09:21:56 -050027import android.annotation.Nullable;
28import android.app.ActivityManager;
29import android.app.ActivityManagerNative;
30import android.app.Fragment;
31import android.app.IActivityManager;
32import android.app.StatusBarManager;
33import android.content.BroadcastReceiver;
Casey Burkhardtb9dcd662017-03-20 15:10:16 -070034import android.content.ContentResolver;
Jason Monk49fa0162017-01-11 09:21:56 -050035import android.content.Context;
36import android.content.Intent;
37import android.content.IntentFilter;
38import android.content.res.Configuration;
Casey Burkhardt74922c62017-02-13 12:43:16 -080039import android.database.ContentObserver;
Jason Monk49fa0162017-01-11 09:21:56 -050040import android.graphics.PixelFormat;
41import android.graphics.Rect;
42import android.inputmethodservice.InputMethodService;
43import android.os.Binder;
44import android.os.Bundle;
45import android.os.Handler;
46import android.os.IBinder;
47import android.os.Message;
48import android.os.PowerManager;
49import android.os.RemoteException;
50import android.os.UserHandle;
Casey Burkhardt74922c62017-02-13 12:43:16 -080051import android.provider.Settings;
Jason Monk865246d2017-01-19 08:27:01 -050052import android.support.annotation.VisibleForTesting;
Jason Monk49fa0162017-01-11 09:21:56 -050053import android.telecom.TelecomManager;
54import android.text.TextUtils;
55import android.util.Log;
56import android.view.IRotationWatcher.Stub;
57import android.view.KeyEvent;
58import android.view.LayoutInflater;
59import android.view.MotionEvent;
60import android.view.View;
61import android.view.ViewGroup;
62import android.view.WindowManager;
63import android.view.WindowManager.LayoutParams;
64import android.view.WindowManagerGlobal;
65import android.view.accessibility.AccessibilityEvent;
66import android.view.accessibility.AccessibilityManager;
Jason Monk91e587e2017-04-13 13:41:23 -040067import android.view.accessibility.AccessibilityManager.AccessibilityServicesStateChangeListener;
Jason Monk49fa0162017-01-11 09:21:56 -050068
69import com.android.internal.logging.MetricsLogger;
70import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
71import com.android.keyguard.LatencyTracker;
Jason Monk9c7844c2017-01-18 15:21:53 -050072import com.android.systemui.Dependency;
Jason Monk49fa0162017-01-11 09:21:56 -050073import com.android.systemui.R;
Jason Monk9c7844c2017-01-18 15:21:53 -050074import com.android.systemui.SysUiServiceProvider;
Jason Monk49fa0162017-01-11 09:21:56 -050075import com.android.systemui.assist.AssistManager;
76import com.android.systemui.fragments.FragmentHostManager;
77import com.android.systemui.fragments.FragmentHostManager.FragmentListener;
78import com.android.systemui.recents.Recents;
79import com.android.systemui.stackdivider.Divider;
80import com.android.systemui.statusbar.CommandQueue;
81import com.android.systemui.statusbar.CommandQueue.Callbacks;
Jason Monk91e587e2017-04-13 13:41:23 -040082import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper;
Jason Monk49fa0162017-01-11 09:21:56 -050083import com.android.systemui.statusbar.policy.KeyButtonView;
84import com.android.systemui.statusbar.stack.StackStateAnimator;
85
86import java.io.FileDescriptor;
87import java.io.PrintWriter;
Casey Burkhardt048c2bc2016-12-08 16:09:20 -080088import java.util.List;
Jason Monk49fa0162017-01-11 09:21:56 -050089import java.util.Locale;
90
91/**
92 * Fragment containing the NavigationBarFragment. Contains logic for what happens
93 * on clicks and view states of the nav bar.
94 */
95public class NavigationBarFragment extends Fragment implements Callbacks {
96
97 private static final String TAG = "NavigationBar";
98 private static final boolean DEBUG = false;
99 private static final String EXTRA_DISABLE_STATE = "disabled_state";
100
101 /** Allow some time inbetween the long press for back and recents. */
102 private static final int LOCK_TO_APP_GESTURE_TOLERENCE = 200;
103
104 protected NavigationBarView mNavigationBarView = null;
105 protected AssistManager mAssistManager;
106
107 private int mNavigationBarWindowState = WINDOW_STATE_SHOWING;
108
109 private int mNavigationIconHints = 0;
110 private int mNavigationBarMode;
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800111 private AccessibilityManager mAccessibilityManager;
Casey Burkhardt74922c62017-02-13 12:43:16 -0800112 private MagnificationContentObserver mMagnificationObserver;
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700113 private ContentResolver mContentResolver;
Jason Monk49fa0162017-01-11 09:21:56 -0500114
115 private int mDisabledFlags1;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500116 private StatusBar mStatusBar;
Jason Monk49fa0162017-01-11 09:21:56 -0500117 private Recents mRecents;
118 private Divider mDivider;
119 private WindowManager mWindowManager;
120 private CommandQueue mCommandQueue;
121 private long mLastLockToAppLongPress;
122
123 private Locale mLocale;
124 private int mLayoutDirection;
125
126 private int mSystemUiVisibility;
127 private LightBarController mLightBarController;
Jason Monk49fa0162017-01-11 09:21:56 -0500128
129 public boolean mHomeBlockedThisTouch;
130
131 // ----- Fragment Lifecycle Callbacks -----
132
133 @Override
134 public void onCreate(@Nullable Bundle savedInstanceState) {
135 super.onCreate(savedInstanceState);
Jason Monk9c7844c2017-01-18 15:21:53 -0500136 mCommandQueue = SysUiServiceProvider.getComponent(getContext(), CommandQueue.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500137 mCommandQueue.addCallbacks(this);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500138 mStatusBar = SysUiServiceProvider.getComponent(getContext(), StatusBar.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500139 mRecents = SysUiServiceProvider.getComponent(getContext(), Recents.class);
140 mDivider = SysUiServiceProvider.getComponent(getContext(), Divider.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500141 mWindowManager = getContext().getSystemService(WindowManager.class);
142 mAccessibilityManager = getContext().getSystemService(AccessibilityManager.class);
Jason Monk91e587e2017-04-13 13:41:23 -0400143 Dependency.get(AccessibilityManagerWrapper.class).addCallback(
144 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700145 mContentResolver = getContext().getContentResolver();
Casey Burkhardt74922c62017-02-13 12:43:16 -0800146 mMagnificationObserver = new MagnificationContentObserver(
147 getContext().getMainThreadHandler());
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700148 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(
Casey Burkhardt74922c62017-02-13 12:43:16 -0800149 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED), false,
Casey Burkhardt5d614402017-04-06 13:46:50 -0700150 mMagnificationObserver, UserHandle.USER_ALL);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800151
Jason Monk49fa0162017-01-11 09:21:56 -0500152 if (savedInstanceState != null) {
153 mDisabledFlags1 = savedInstanceState.getInt(EXTRA_DISABLE_STATE, 0);
154 }
Jason Monk9c7844c2017-01-18 15:21:53 -0500155 mAssistManager = Dependency.get(AssistManager.class);
Jason Monk49fa0162017-01-11 09:21:56 -0500156
157 try {
158 WindowManagerGlobal.getWindowManagerService()
Andrii Kulian35fa3c22017-03-11 09:37:28 -0800159 .watchRotation(mRotationWatcher, getContext().getDisplay().getDisplayId());
Jason Monk49fa0162017-01-11 09:21:56 -0500160 } catch (RemoteException e) {
161 throw e.rethrowFromSystemServer();
162 }
163 }
164
165 @Override
166 public void onDestroy() {
167 super.onDestroy();
168 mCommandQueue.removeCallbacks(this);
Jason Monk91e587e2017-04-13 13:41:23 -0400169 Dependency.get(AccessibilityManagerWrapper.class).removeCallback(
170 mAccessibilityListener);
Casey Burkhardtb9dcd662017-03-20 15:10:16 -0700171 mContentResolver.unregisterContentObserver(mMagnificationObserver);
Jason Monk49fa0162017-01-11 09:21:56 -0500172 try {
173 WindowManagerGlobal.getWindowManagerService()
174 .removeRotationWatcher(mRotationWatcher);
175 } catch (RemoteException e) {
176 throw e.rethrowFromSystemServer();
177 }
178 }
179
180 @Override
181 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
182 Bundle savedInstanceState) {
183 return inflater.inflate(R.layout.navigation_bar, container, false);
184 }
185
186 @Override
187 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
188 super.onViewCreated(view, savedInstanceState);
189 mNavigationBarView = (NavigationBarView) view;
190
191 mNavigationBarView.setDisabledFlags(mDisabledFlags1);
192 mNavigationBarView.setComponents(mRecents, mDivider);
193 mNavigationBarView.setOnVerticalChangedListener(this::onVerticalChanged);
194 mNavigationBarView.setOnTouchListener(this::onNavigationTouch);
195 if (savedInstanceState != null) {
196 mNavigationBarView.getLightTransitionsController().restoreState(savedInstanceState);
197 }
198
199 prepareNavigationBarView();
200 checkNavBarModes();
201
202 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
203 filter.addAction(Intent.ACTION_SCREEN_ON);
204 getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);
205 PowerManager pm = getContext().getSystemService(PowerManager.class);
206 notifyNavigationBarScreenOn(pm.isScreenOn());
207 }
208
209 @Override
210 public void onDestroyView() {
211 super.onDestroyView();
Jason Monkaa573e92017-01-27 17:00:29 -0500212 mNavigationBarView.getLightTransitionsController().destroy(getContext());
Jason Monk49fa0162017-01-11 09:21:56 -0500213 getContext().unregisterReceiver(mBroadcastReceiver);
214 }
215
216 @Override
217 public void onSaveInstanceState(Bundle outState) {
218 super.onSaveInstanceState(outState);
219 outState.putInt(EXTRA_DISABLE_STATE, mDisabledFlags1);
220 if (mNavigationBarView != null) {
221 mNavigationBarView.getLightTransitionsController().saveState(outState);
222 }
223 }
224
225 @Override
226 public void onConfigurationChanged(Configuration newConfig) {
227 super.onConfigurationChanged(newConfig);
228 final Locale locale = getContext().getResources().getConfiguration().locale;
229 final int ld = TextUtils.getLayoutDirectionFromLocale(locale);
230 if (!locale.equals(mLocale) || ld != mLayoutDirection) {
231 if (DEBUG) {
232 Log.v(TAG, String.format(
233 "config changed locale/LD: %s (%d) -> %s (%d)", mLocale, mLayoutDirection,
234 locale, ld));
235 }
236 mLocale = locale;
237 mLayoutDirection = ld;
238 refreshLayout(ld);
239 }
240 repositionNavigationBar();
241 }
242
243 @Override
244 public void dump(String prefix, FileDescriptor fd, PrintWriter pw, String[] args) {
245 if (mNavigationBarView != null) {
246 pw.print(" mNavigationBarWindowState=");
247 pw.println(windowStateToString(mNavigationBarWindowState));
248 pw.print(" mNavigationBarMode=");
249 pw.println(BarTransitions.modeToString(mNavigationBarMode));
250 dumpBarTransitions(pw, "mNavigationBarView", mNavigationBarView.getBarTransitions());
251 }
252
253 pw.print(" mNavigationBarView=");
254 if (mNavigationBarView == null) {
255 pw.println("null");
256 } else {
257 mNavigationBarView.dump(fd, pw, args);
258 }
259 }
260
261 // ----- CommandQueue Callbacks -----
262
263 @Override
264 public void setImeWindowStatus(IBinder token, int vis, int backDisposition,
265 boolean showImeSwitcher) {
266 boolean imeShown = (vis & InputMethodService.IME_VISIBLE) != 0;
267 int hints = mNavigationIconHints;
268 if ((backDisposition == InputMethodService.BACK_DISPOSITION_WILL_DISMISS) || imeShown) {
269 hints |= NAVIGATION_HINT_BACK_ALT;
270 } else {
271 hints &= ~NAVIGATION_HINT_BACK_ALT;
272 }
273 if (showImeSwitcher) {
274 hints |= NAVIGATION_HINT_IME_SHOWN;
275 } else {
276 hints &= ~NAVIGATION_HINT_IME_SHOWN;
277 }
278 if (hints == mNavigationIconHints) return;
279
280 mNavigationIconHints = hints;
281
282 if (mNavigationBarView != null) {
283 mNavigationBarView.setNavigationIconHints(hints);
284 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500285 mStatusBar.checkBarModes();
Jason Monk49fa0162017-01-11 09:21:56 -0500286 }
287
288 @Override
289 public void topAppWindowChanged(boolean showMenu) {
290 if (mNavigationBarView != null) {
291 mNavigationBarView.setMenuVisibility(showMenu);
292 }
293 }
294
295 @Override
296 public void setWindowState(int window, int state) {
297 if (mNavigationBarView != null
298 && window == StatusBarManager.WINDOW_NAVIGATION_BAR
299 && mNavigationBarWindowState != state) {
300 mNavigationBarWindowState = state;
301 if (DEBUG_WINDOW_STATE) Log.d(TAG, "Navigation bar " + windowStateToString(state));
302 }
303 }
304
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500305 // Injected from StatusBar at creation.
Jason Monk49fa0162017-01-11 09:21:56 -0500306 public void setCurrentSysuiVisibility(int systemUiVisibility) {
307 mSystemUiVisibility = systemUiVisibility;
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500308 mNavigationBarMode = mStatusBar.computeBarMode(0, mSystemUiVisibility,
Jason Monk49fa0162017-01-11 09:21:56 -0500309 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
310 View.NAVIGATION_BAR_TRANSPARENT);
311 checkNavBarModes();
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500312 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500313 mLightBarController.onNavigationVisibilityChanged(mSystemUiVisibility, 0 /* mask */,
314 true /* nbModeChanged */, mNavigationBarMode);
315 }
316
317 @Override
318 public void setSystemUiVisibility(int vis, int fullscreenStackVis, int dockedStackVis,
319 int mask, Rect fullscreenStackBounds, Rect dockedStackBounds) {
320 final int oldVal = mSystemUiVisibility;
321 final int newVal = (oldVal & ~mask) | (vis & mask);
322 final int diff = newVal ^ oldVal;
323 boolean nbModeChanged = false;
324 if (diff != 0) {
325 mSystemUiVisibility = newVal;
326
327 // update navigation bar mode
328 final int nbMode = getView() == null
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500329 ? -1 : mStatusBar.computeBarMode(oldVal, newVal,
Jason Monk49fa0162017-01-11 09:21:56 -0500330 View.NAVIGATION_BAR_TRANSIENT, View.NAVIGATION_BAR_TRANSLUCENT,
331 View.NAVIGATION_BAR_TRANSPARENT);
332 nbModeChanged = nbMode != -1;
333 if (nbModeChanged) {
334 if (mNavigationBarMode != nbMode) {
335 mNavigationBarMode = nbMode;
336 checkNavBarModes();
337 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500338 mStatusBar.touchAutoHide();
Jason Monk49fa0162017-01-11 09:21:56 -0500339 }
340 }
341
342 mLightBarController.onNavigationVisibilityChanged(vis, mask, nbModeChanged,
343 mNavigationBarMode);
344 }
345
346 @Override
347 public void disable(int state1, int state2, boolean animate) {
348 // All navigation bar flags are in state1.
349 int masked = state1 & (StatusBarManager.DISABLE_HOME
350 | StatusBarManager.DISABLE_RECENT
351 | StatusBarManager.DISABLE_BACK
352 | StatusBarManager.DISABLE_SEARCH);
353 if (masked != mDisabledFlags1) {
354 mDisabledFlags1 = masked;
355 if (mNavigationBarView != null) mNavigationBarView.setDisabledFlags(state1);
356 }
357 }
358
359 // ----- Internal stuffz -----
360
361 private void refreshLayout(int layoutDirection) {
362 if (mNavigationBarView != null) {
363 mNavigationBarView.setLayoutDirection(layoutDirection);
364 }
365 }
366
367 private boolean shouldDisableNavbarGestures() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500368 return !mStatusBar.isDeviceProvisioned()
Jason Monk49fa0162017-01-11 09:21:56 -0500369 || (mDisabledFlags1 & StatusBarManager.DISABLE_SEARCH) != 0;
370 }
371
372 private void repositionNavigationBar() {
373 if (mNavigationBarView == null || !mNavigationBarView.isAttachedToWindow()) return;
374
375 prepareNavigationBarView();
376
377 mWindowManager.updateViewLayout((View) mNavigationBarView.getParent(),
378 ((View) mNavigationBarView.getParent()).getLayoutParams());
379 }
380
381 private void notifyNavigationBarScreenOn(boolean screenOn) {
382 mNavigationBarView.notifyScreenOn(screenOn);
383 }
384
385 private void prepareNavigationBarView() {
386 mNavigationBarView.reorient();
387
388 ButtonDispatcher recentsButton = mNavigationBarView.getRecentsButton();
389 recentsButton.setOnClickListener(this::onRecentsClick);
390 recentsButton.setOnTouchListener(this::onRecentsTouch);
391 recentsButton.setLongClickable(true);
392 recentsButton.setOnLongClickListener(this::onLongPressBackRecents);
393
394 ButtonDispatcher backButton = mNavigationBarView.getBackButton();
395 backButton.setLongClickable(true);
396 backButton.setOnLongClickListener(this::onLongPressBackRecents);
397
398 ButtonDispatcher homeButton = mNavigationBarView.getHomeButton();
399 homeButton.setOnTouchListener(this::onHomeTouch);
400 homeButton.setOnLongClickListener(this::onHomeLongClick);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800401
402 ButtonDispatcher accessibilityButton = mNavigationBarView.getAccessibilityButton();
403 accessibilityButton.setOnClickListener(this::onAccessibilityClick);
404 accessibilityButton.setOnLongClickListener(this::onAccessibilityLongClick);
Phil Weaverdb9a7742017-04-18 08:15:06 -0700405 updateAccessibilityServicesState(mAccessibilityManager);
Jason Monk49fa0162017-01-11 09:21:56 -0500406 }
407
408 private boolean onHomeTouch(View v, MotionEvent event) {
409 if (mHomeBlockedThisTouch && event.getActionMasked() != MotionEvent.ACTION_DOWN) {
410 return true;
411 }
412 // If an incoming call is ringing, HOME is totally disabled.
413 // (The user is already on the InCallUI at this point,
414 // and his ONLY options are to answer or reject the call.)
415 switch (event.getAction()) {
416 case MotionEvent.ACTION_DOWN:
417 mHomeBlockedThisTouch = false;
418 TelecomManager telecomManager =
419 getContext().getSystemService(TelecomManager.class);
420 if (telecomManager != null && telecomManager.isRinging()) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500421 if (mStatusBar.isKeyguardShowing()) {
Jason Monk49fa0162017-01-11 09:21:56 -0500422 Log.i(TAG, "Ignoring HOME; there's a ringing incoming call. " +
423 "No heads up");
424 mHomeBlockedThisTouch = true;
425 return true;
426 }
427 }
428 break;
429 case MotionEvent.ACTION_UP:
430 case MotionEvent.ACTION_CANCEL:
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500431 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500432 break;
433 }
434 return false;
435 }
436
437 private void onVerticalChanged(boolean isVertical) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500438 mStatusBar.setQsScrimEnabled(!isVertical);
Jason Monk49fa0162017-01-11 09:21:56 -0500439 }
440
441 private boolean onNavigationTouch(View v, MotionEvent event) {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500442 mStatusBar.checkUserAutohide(v, event);
Jason Monk49fa0162017-01-11 09:21:56 -0500443 return false;
444 }
445
Jason Monk865246d2017-01-19 08:27:01 -0500446 @VisibleForTesting
447 boolean onHomeLongClick(View v) {
Jason Monk49fa0162017-01-11 09:21:56 -0500448 if (shouldDisableNavbarGestures()) {
449 return false;
450 }
451 MetricsLogger.action(getContext(), MetricsEvent.ACTION_ASSIST_LONG_PRESS);
452 mAssistManager.startAssist(new Bundle() /* args */);
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500453 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500454 if (mNavigationBarView != null) {
455 mNavigationBarView.abortCurrentGesture();
456 }
457 return true;
458 }
459
460 // additional optimization when we have software system buttons - start loading the recent
461 // tasks on touch down
462 private boolean onRecentsTouch(View v, MotionEvent event) {
463 int action = event.getAction() & MotionEvent.ACTION_MASK;
464 if (action == MotionEvent.ACTION_DOWN) {
465 mCommandQueue.preloadRecentApps();
466 } else if (action == MotionEvent.ACTION_CANCEL) {
467 mCommandQueue.cancelPreloadRecentApps();
468 } else if (action == MotionEvent.ACTION_UP) {
469 if (!v.isPressed()) {
470 mCommandQueue.cancelPreloadRecentApps();
471 }
472 }
473 return false;
474 }
475
476 private void onRecentsClick(View v) {
477 if (LatencyTracker.isEnabled(getContext())) {
478 LatencyTracker.getInstance(getContext()).onActionStart(
479 LatencyTracker.ACTION_TOGGLE_RECENTS);
480 }
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500481 mStatusBar.awakenDreams();
Jason Monk49fa0162017-01-11 09:21:56 -0500482 mCommandQueue.toggleRecentApps();
483 }
484
485 /**
486 * This handles long-press of both back and recents. They are
487 * handled together to capture them both being long-pressed
488 * at the same time to exit screen pinning (lock task).
489 *
490 * When accessibility mode is on, only a long-press from recents
491 * is required to exit.
492 *
493 * In all other circumstances we try to pass through long-press events
494 * for Back, so that apps can still use it. Which can be from two things.
495 * 1) Not currently in screen pinning (lock task).
496 * 2) Back is long-pressed without recents.
497 */
498 private boolean onLongPressBackRecents(View v) {
499 try {
500 boolean sendBackLongPress = false;
501 IActivityManager activityManager = ActivityManagerNative.getDefault();
502 boolean touchExplorationEnabled = mAccessibilityManager.isTouchExplorationEnabled();
503 boolean inLockTaskMode = activityManager.isInLockTaskMode();
504 if (inLockTaskMode && !touchExplorationEnabled) {
505 long time = System.currentTimeMillis();
506 // If we recently long-pressed the other button then they were
507 // long-pressed 'together'
508 if ((time - mLastLockToAppLongPress) < LOCK_TO_APP_GESTURE_TOLERENCE) {
509 activityManager.stopLockTaskMode();
510 // When exiting refresh disabled flags.
511 mNavigationBarView.setDisabledFlags(mDisabledFlags1, true);
512 return true;
513 } else if ((v.getId() == R.id.back)
514 && !mNavigationBarView.getRecentsButton().getCurrentView().isPressed()) {
515 // If we aren't pressing recents right now then they presses
516 // won't be together, so send the standard long-press action.
517 sendBackLongPress = true;
518 }
519 mLastLockToAppLongPress = time;
520 } else {
521 // If this is back still need to handle sending the long-press event.
522 if (v.getId() == R.id.back) {
523 sendBackLongPress = true;
524 } else if (touchExplorationEnabled && inLockTaskMode) {
525 // When in accessibility mode a long press that is recents (not back)
526 // should stop lock task.
527 activityManager.stopLockTaskMode();
528 // When exiting refresh disabled flags.
529 mNavigationBarView.setDisabledFlags(mDisabledFlags1, true);
530 return true;
531 } else if (v.getId() == R.id.recent_apps) {
532 return onLongPressRecents();
533 }
534 }
535 if (sendBackLongPress) {
536 KeyButtonView keyButtonView = (KeyButtonView) v;
537 keyButtonView.sendEvent(KeyEvent.ACTION_DOWN, KeyEvent.FLAG_LONG_PRESS);
538 keyButtonView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
539 return true;
540 }
541 } catch (RemoteException e) {
542 Log.d(TAG, "Unable to reach activity manager", e);
543 }
544 return false;
545 }
546
547 private boolean onLongPressRecents() {
548 if (mRecents == null || !ActivityManager.supportsMultiWindow()
Winson Chungd3395382016-12-13 11:49:09 -0800549 || !mDivider.getView().getSnapAlgorithm().isSplitScreenFeasible()) {
Jason Monk49fa0162017-01-11 09:21:56 -0500550 return false;
551 }
552
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500553 return mStatusBar.toggleSplitScreenMode(MetricsEvent.ACTION_WINDOW_DOCK_LONGPRESS,
Jason Monk49fa0162017-01-11 09:21:56 -0500554 MetricsEvent.ACTION_WINDOW_UNDOCK_LONGPRESS);
555 }
556
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800557 private void onAccessibilityClick(View v) {
558 mAccessibilityManager.notifyAccessibilityButtonClicked();
559 }
560
561 private boolean onAccessibilityLongClick(View v) {
Casey Burkhardt5e8b9802017-03-24 10:07:20 -0700562 Intent intent = new Intent(AccessibilityManager.ACTION_CHOOSE_ACCESSIBILITY_BUTTON);
563 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Casey Burkhardt5d614402017-04-06 13:46:50 -0700564 v.getContext().startActivityAsUser(intent, UserHandle.CURRENT);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800565 return true;
566 }
567
Phil Weaverdb9a7742017-04-18 08:15:06 -0700568 private void updateAccessibilityServicesState(AccessibilityManager accessibilityManager) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800569 int requestingServices = 0;
570 try {
Casey Burkhardt5d614402017-04-06 13:46:50 -0700571 if (Settings.Secure.getIntForUser(mContentResolver,
572 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED,
573 UserHandle.USER_CURRENT) == 1) {
Casey Burkhardt74922c62017-02-13 12:43:16 -0800574 requestingServices++;
575 }
576 } catch (Settings.SettingNotFoundException e) {
577 }
578
Casey Burkhardt5d614402017-04-06 13:46:50 -0700579 // AccessibilityManagerService resolves services for the current user since the local
580 // AccessibilityManager is created from a Context with the INTERACT_ACROSS_USERS permission
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800581 final List<AccessibilityServiceInfo> services =
Phil Weaverdb9a7742017-04-18 08:15:06 -0700582 accessibilityManager.getEnabledAccessibilityServiceList(
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800583 AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
Casey Burkhardt048c2bc2016-12-08 16:09:20 -0800584 for (int i = services.size() - 1; i >= 0; --i) {
585 AccessibilityServiceInfo info = services.get(i);
586 if ((info.flags & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0) {
587 requestingServices++;
588 }
589 }
590
591 final boolean showAccessibilityButton = requestingServices >= 1;
592 final boolean targetSelection = requestingServices >= 2;
593 mNavigationBarView.setAccessibilityButtonState(showAccessibilityButton, targetSelection);
594 }
595
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500596 // ----- Methods that StatusBar talks to (should be minimized) -----
Jason Monk49fa0162017-01-11 09:21:56 -0500597
Jason Monk49fa0162017-01-11 09:21:56 -0500598 public void setLightBarController(LightBarController lightBarController) {
599 mLightBarController = lightBarController;
600 mLightBarController.setNavigationBar(mNavigationBarView.getLightTransitionsController());
601 }
602
603 public boolean isSemiTransparent() {
604 return mNavigationBarMode == MODE_SEMI_TRANSPARENT;
605 }
606
607 public void onKeyguardOccludedChanged(boolean keyguardOccluded) {
608 mNavigationBarView.onKeyguardOccludedChanged(keyguardOccluded);
609 }
610
611 public void disableAnimationsDuringHide(long delay) {
612 mNavigationBarView.setLayoutTransitionsEnabled(false);
613 mNavigationBarView.postDelayed(() -> mNavigationBarView.setLayoutTransitionsEnabled(true),
614 delay + StackStateAnimator.ANIMATION_DURATION_GO_TO_FULL_SHADE);
615 }
616
Jason Monk49fa0162017-01-11 09:21:56 -0500617 public BarTransitions getBarTransitions() {
618 return mNavigationBarView.getBarTransitions();
619 }
620
621 public void checkNavBarModes() {
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500622 mStatusBar.checkBarMode(mNavigationBarMode,
Jason Monk49fa0162017-01-11 09:21:56 -0500623 mNavigationBarWindowState, mNavigationBarView.getBarTransitions());
624 }
625
626 public void finishBarAnimations() {
627 mNavigationBarView.getBarTransitions().finishAnimations();
628 }
629
Jason Monk91e587e2017-04-13 13:41:23 -0400630 private final AccessibilityServicesStateChangeListener mAccessibilityListener =
631 this::updateAccessibilityServicesState;
632
Casey Burkhardt74922c62017-02-13 12:43:16 -0800633 private class MagnificationContentObserver extends ContentObserver {
634
635 public MagnificationContentObserver(Handler handler) {
636 super(handler);
637 }
638
639 @Override
640 public void onChange(boolean selfChange) {
Phil Weaverdb9a7742017-04-18 08:15:06 -0700641 NavigationBarFragment.this.updateAccessibilityServicesState(mAccessibilityManager);
Casey Burkhardt74922c62017-02-13 12:43:16 -0800642 }
643 }
644
Jason Monk49fa0162017-01-11 09:21:56 -0500645 private final Stub mRotationWatcher = new Stub() {
646 @Override
647 public void onRotationChanged(int rotation) throws RemoteException {
648 // We need this to be scheduled as early as possible to beat the redrawing of
649 // window in response to the orientation change.
650 Handler h = getView().getHandler();
651 Message msg = Message.obtain(h, () -> {
652 if (mNavigationBarView != null
653 && mNavigationBarView.needsReorient(rotation)) {
654 repositionNavigationBar();
655 }
656 });
657 msg.setAsynchronous(true);
658 h.sendMessageAtFrontOfQueue(msg);
659 }
660 };
661
662 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
663 @Override
664 public void onReceive(Context context, Intent intent) {
665 String action = intent.getAction();
666 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
667 notifyNavigationBarScreenOn(false);
668 } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
669 notifyNavigationBarScreenOn(true);
670 }
671 }
672 };
673
674 public static View create(Context context, FragmentListener listener) {
675 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
676 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
677 WindowManager.LayoutParams.TYPE_NAVIGATION_BAR,
678 WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING
679 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
680 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
681 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
682 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
683 | WindowManager.LayoutParams.FLAG_SLIPPERY,
684 PixelFormat.TRANSLUCENT);
685 lp.token = new Binder();
686 // this will allow the navbar to run in an overlay on devices that support this
687 if (ActivityManager.isHighEndGfx()) {
688 lp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
689 }
690 lp.setTitle("NavigationBar");
691 lp.windowAnimations = 0;
692
693 View navigationBarView = LayoutInflater.from(context).inflate(
694 R.layout.navigation_bar_window, null);
695
696 if (DEBUG) Log.v(TAG, "addNavigationBar: about to add " + navigationBarView);
697 if (navigationBarView == null) return null;
698
699 context.getSystemService(WindowManager.class).addView(navigationBarView, lp);
700 FragmentHostManager fragmentHost = FragmentHostManager.get(navigationBarView);
701 NavigationBarFragment fragment = new NavigationBarFragment();
702 fragmentHost.getFragmentManager().beginTransaction()
703 .replace(R.id.navigation_bar_frame, fragment, TAG)
704 .commit();
705 fragmentHost.addTagListener(TAG, listener);
706 return navigationBarView;
707 }
708}