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