Debian Codegraph @

core/java/android/inputmethodservice/NavigationBarController.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package android.inputmethodservice;
18
19 import static android.view.WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS;
20
21 import android.animation.ValueAnimator;
22 import android.annotation.FloatRange;
23 import android.annotation.NonNull;
24 import android.annotation.Nullable;
25 import android.app.StatusBarManager;
26 import android.graphics.Color;
27 import android.graphics.Insets;
28 import android.graphics.Rect;
29 import android.graphics.Region;
30 import android.inputmethodservice.navigationbar.NavigationBarFrame;
31 import android.inputmethodservice.navigationbar.NavigationBarView;
32 import android.view.Gravity;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.view.ViewParent;
37 import android.view.ViewTreeObserver;
38 import android.view.Window;
39 import android.view.WindowInsets;
40 import android.view.WindowInsetsController.Appearance;
41 import android.view.animation.Interpolator;
42 import android.view.animation.PathInterpolator;
43 import android.widget.FrameLayout;
44
45 import com.android.internal.inputmethod.InputMethodNavButtonFlags;
46
47 import java.util.Objects;
48
49 /**
50 * This class hides details behind {@link InputMethodService#canImeRenderGesturalNavButtons()} from
51 * {@link InputMethodService}.
52 *
53 * <p>All the package-private methods are no-op when
54 * {@link InputMethodService#canImeRenderGesturalNavButtons()} returns {@code false}.</p>
55 */
56 final class NavigationBarController {
57
58 private interface Callback {
59 default void updateTouchableInsets(@NonNull InputMethodService.Insets originalInsets,
60 @NonNull ViewTreeObserver.InternalInsetsInfo dest) {
61 }
62
63 default void onSoftInputWindowCreated(@NonNull SoftInputWindow softInputWindow) {
64 }
65
66 default void onViewInitialized() {
67 }
68
69 default void onWindowShown() {
70 }
71
72 default void onDestroy() {
73 }
74
75 default void onNavButtonFlagsChanged(@InputMethodNavButtonFlags int navButtonFlags) {
76 }
77
78 default String toDebugString() {
79 return "No-op implementation";
80 }
81
82 Callback NOOP = new Callback() {
83 };
84 }
85
86 private final Callback mImpl;
87
88 NavigationBarController(@NonNull InputMethodService inputMethodService) {
89 mImpl = InputMethodService.canImeRenderGesturalNavButtons()
90 ? new Impl(inputMethodService) : Callback.NOOP;
91 }
92
93 void updateTouchableInsets(@NonNull InputMethodService.Insets originalInsets,
94 @NonNull ViewTreeObserver.InternalInsetsInfo dest) {
95 mImpl.updateTouchableInsets(originalInsets, dest);
96 }
97
98 void onSoftInputWindowCreated(@NonNull SoftInputWindow softInputWindow) {
99 mImpl.onSoftInputWindowCreated(softInputWindow);
100 }
101
102 void onViewInitialized() {
103 mImpl.onViewInitialized();
104 }
105
106 void onWindowShown() {
107 mImpl.onWindowShown();
108 }
109
110 void onDestroy() {
111 mImpl.onDestroy();
112 }
113
114 void onNavButtonFlagsChanged(@InputMethodNavButtonFlags int navButtonFlags) {
115 mImpl.onNavButtonFlagsChanged(navButtonFlags);
116 }
117
118 String toDebugString() {
119 return mImpl.toDebugString();
120 }
121
122 private static final class Impl implements Callback, Window.DecorCallback {
123 private static final int DEFAULT_COLOR_ADAPT_TRANSITION_TIME = 1700;
124
125 // Copied from com.android.systemui.animation.Interpolators#LEGACY_DECELERATE
126 private static final Interpolator LEGACY_DECELERATE =
127 new PathInterpolator(0f, 0f, 0.2f, 1f);
128
129 @NonNull
130 private final InputMethodService mService;
131
132 private boolean mDestroyed = false;
133
134 private boolean mImeDrawsImeNavBar;
135
136 @Nullable
137 private NavigationBarFrame mNavigationBarFrame;
138 @Nullable
139 Insets mLastInsets;
140
141 private boolean mShouldShowImeSwitcherWhenImeIsShown;
142
143 @Appearance
144 private int mAppearance;
145
146 @FloatRange(from = 0.0f, to = 1.0f)
147 private float mDarkIntensity;
148
149 @Nullable
150 private ValueAnimator mTintAnimator;
151
152 private boolean mDrawLegacyNavigationBarBackground;
153
154 private final Rect mTempRect = new Rect();
155 private final int[] mTempPos = new int[2];
156
157 Impl(@NonNull InputMethodService inputMethodService) {
158 mService = inputMethodService;
159 }
160
161 @Nullable
162 private Insets getSystemInsets() {
163 if (mService.mWindow == null) {
164 return null;
165 }
166 final View decorView = mService.mWindow.getWindow().getDecorView();
167 if (decorView == null) {
168 return null;
169 }
170 final WindowInsets windowInsets = decorView.getRootWindowInsets();
171 if (windowInsets == null) {
172 return null;
173 }
174 final Insets stableBarInsets =
175 windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
176 return Insets.min(windowInsets.getInsets(WindowInsets.Type.systemBars()
177 | WindowInsets.Type.displayCutout()), stableBarInsets);
178 }
179
180 private void installNavigationBarFrameIfNecessary() {
181 if (!mImeDrawsImeNavBar) {
182 return;
183 }
184 if (mNavigationBarFrame != null) {
185 return;
186 }
187 final View rawDecorView = mService.mWindow.getWindow().getDecorView();
188 if (!(rawDecorView instanceof ViewGroup)) {
189 return;
190 }
191 final ViewGroup decorView = (ViewGroup) rawDecorView;
192 mNavigationBarFrame = decorView.findViewByPredicate(
193 NavigationBarFrame.class::isInstance);
194 final Insets systemInsets = getSystemInsets();
195 if (mNavigationBarFrame == null) {
196 mNavigationBarFrame = new NavigationBarFrame(mService);
197 LayoutInflater.from(mService).inflate(
198 com.android.internal.R.layout.input_method_navigation_bar,
199 mNavigationBarFrame);
200 if (systemInsets != null) {
201 decorView.addView(mNavigationBarFrame, new FrameLayout.LayoutParams(
202 ViewGroup.LayoutParams.MATCH_PARENT,
203 systemInsets.bottom, Gravity.BOTTOM));
204 mLastInsets = systemInsets;
205 } else {
206 decorView.addView(mNavigationBarFrame);
207 }
208 final NavigationBarView navigationBarView = mNavigationBarFrame.findViewByPredicate(
209 NavigationBarView.class::isInstance);
210 if (navigationBarView != null) {
211 // TODO(b/213337792): Support InputMethodService#setBackDisposition().
212 // TODO(b/213337792): Set NAVIGATION_HINT_IME_SHOWN only when necessary.
213 final int hints = StatusBarManager.NAVIGATION_HINT_BACK_ALT
214 | (mShouldShowImeSwitcherWhenImeIsShown
215 ? StatusBarManager.NAVIGATION_HINT_IME_SWITCHER_SHOWN
216 : 0);
217 navigationBarView.setNavigationIconHints(hints);
218 }
219 } else {
220 mNavigationBarFrame.setLayoutParams(new FrameLayout.LayoutParams(
221 ViewGroup.LayoutParams.MATCH_PARENT, systemInsets.bottom, Gravity.BOTTOM));
222 mLastInsets = systemInsets;
223 }
224
225 if (mDrawLegacyNavigationBarBackground) {
226 mNavigationBarFrame.setBackgroundColor(Color.BLACK);
227 } else {
228 mNavigationBarFrame.setBackground(null);
229 }
230
231 setIconTintInternal(calculateTargetDarkIntensity(mAppearance,
232 mDrawLegacyNavigationBarBackground));
233 }
234
235 private void uninstallNavigationBarFrameIfNecessary() {
236 if (mNavigationBarFrame == null) {
237 return;
238 }
239 final ViewParent parent = mNavigationBarFrame.getParent();
240 if (parent instanceof ViewGroup) {
241 ((ViewGroup) parent).removeView(mNavigationBarFrame);
242 }
243 mNavigationBarFrame = null;
244 }
245
246 @Override
247 public void updateTouchableInsets(@NonNull InputMethodService.Insets originalInsets,
248 @NonNull ViewTreeObserver.InternalInsetsInfo dest) {
249 if (!mImeDrawsImeNavBar || mNavigationBarFrame == null
250 || mService.isExtractViewShown()) {
251 return;
252 }
253
254 final Insets systemInsets = getSystemInsets();
255 if (systemInsets != null) {
256 final Window window = mService.mWindow.getWindow();
257 final View decor = window.getDecorView();
258 Region touchableRegion = null;
259 final View inputFrame = mService.mInputFrame;
260 switch (originalInsets.touchableInsets) {
261 case ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME:
262 if (inputFrame.getVisibility() == View.VISIBLE) {
263 inputFrame.getLocationInWindow(mTempPos);
264 mTempRect.set(mTempPos[0], mTempPos[1],
265 mTempPos[0] + inputFrame.getWidth(),
266 mTempPos[1] + inputFrame.getHeight());
267 touchableRegion = new Region(mTempRect);
268 }
269 break;
270 case ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT:
271 if (inputFrame.getVisibility() == View.VISIBLE) {
272 inputFrame.getLocationInWindow(mTempPos);
273 mTempRect.set(mTempPos[0], originalInsets.contentTopInsets,
274 mTempPos[0] + inputFrame.getWidth() ,
275 mTempPos[1] + inputFrame.getHeight());
276 touchableRegion = new Region(mTempRect);
277 }
278 break;
279 case ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE:
280 if (inputFrame.getVisibility() == View.VISIBLE) {
281 inputFrame.getLocationInWindow(mTempPos);
282 mTempRect.set(mTempPos[0], originalInsets.visibleTopInsets,
283 mTempPos[0] + inputFrame.getWidth(),
284 mTempPos[1] + inputFrame.getHeight());
285 touchableRegion = new Region(mTempRect);
286 }
287 break;
288 case ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_REGION:
289 touchableRegion = new Region();
290 touchableRegion.set(originalInsets.touchableRegion);
291 break;
292 }
293 // Hereafter "mTempRect" means a navigation bar rect.
294 mTempRect.set(decor.getLeft(), decor.getBottom() - systemInsets.bottom,
295 decor.getRight(), decor.getBottom());
296 if (touchableRegion == null) {
297 touchableRegion = new Region(mTempRect);
298 } else {
299 touchableRegion.union(mTempRect);
300 }
301
302 dest.touchableRegion.set(touchableRegion);
303 dest.setTouchableInsets(
304 ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_REGION);
305
306 // TODO(b/215443343): See if we can use View#OnLayoutChangeListener().
307 // TODO(b/215443343): See if we can replace DecorView#mNavigationColorViewState.view
308 boolean zOrderChanged = false;
309 if (decor instanceof ViewGroup) {
310 ViewGroup decorGroup = (ViewGroup) decor;
311 final View navbarBackgroundView = window.getNavigationBarBackgroundView();
312 zOrderChanged = navbarBackgroundView != null
313 && decorGroup.indexOfChild(navbarBackgroundView)
314 > decorGroup.indexOfChild(mNavigationBarFrame);
315 }
316 final boolean insetChanged = !Objects.equals(systemInsets, mLastInsets);
317 if (zOrderChanged || insetChanged) {
318 scheduleRelayout();
319 }
320 }
321 }
322
323 private void scheduleRelayout() {
324 // Capture the current frame object in case the object is replaced or cleared later.
325 final NavigationBarFrame frame = mNavigationBarFrame;
326 frame.post(() -> {
327 if (mDestroyed) {
328 return;
329 }
330 if (!frame.isAttachedToWindow()) {
331 return;
332 }
333 final Window window = mService.mWindow.getWindow();
334 if (window == null) {
335 return;
336 }
337 final View decor = window.peekDecorView();
338 if (decor == null) {
339 return;
340 }
341 if (!(decor instanceof ViewGroup)) {
342 return;
343 }
344 final ViewGroup decorGroup = (ViewGroup) decor;
345 final Insets currentSystemInsets = getSystemInsets();
346 if (!Objects.equals(currentSystemInsets, mLastInsets)) {
347 frame.setLayoutParams(new FrameLayout.LayoutParams(
348 ViewGroup.LayoutParams.MATCH_PARENT,
349 currentSystemInsets.bottom, Gravity.BOTTOM));
350 mLastInsets = currentSystemInsets;
351 }
352 final View navbarBackgroundView =
353 window.getNavigationBarBackgroundView();
354 if (navbarBackgroundView != null
355 && decorGroup.indexOfChild(navbarBackgroundView)
356 > decorGroup.indexOfChild(frame)) {
357 decorGroup.bringChildToFront(frame);
358 }
359 });
360 }
361
362 @Override
363 public void onSoftInputWindowCreated(@NonNull SoftInputWindow softInputWindow) {
364 final Window window = softInputWindow.getWindow();
365 mAppearance = window.getSystemBarAppearance();
366 window.setDecorCallback(this);
367 }
368
369 @Override
370 public void onViewInitialized() {
371 if (mDestroyed) {
372 return;
373 }
374 installNavigationBarFrameIfNecessary();
375 }
376
377 @Override
378 public void onDestroy() {
379 if (mDestroyed) {
380 return;
381 }
382 if (mTintAnimator != null) {
383 mTintAnimator.cancel();
384 mTintAnimator = null;
385 }
386 mDestroyed = true;
387 }
388
389 @Override
390 public void onWindowShown() {
391 if (mDestroyed || !mImeDrawsImeNavBar || mNavigationBarFrame == null) {
392 return;
393 }
394 final Insets systemInsets = getSystemInsets();
395 if (systemInsets != null) {
396 if (!Objects.equals(systemInsets, mLastInsets)) {
397 mNavigationBarFrame.setLayoutParams(new NavigationBarFrame.LayoutParams(
398 ViewGroup.LayoutParams.MATCH_PARENT,
399 systemInsets.bottom, Gravity.BOTTOM));
400 mLastInsets = systemInsets;
401 }
402 final Window window = mService.mWindow.getWindow();
403 View rawDecorView = window.getDecorView();
404 if (rawDecorView instanceof ViewGroup) {
405 final ViewGroup decor = (ViewGroup) rawDecorView;
406 final View navbarBackgroundView = window.getNavigationBarBackgroundView();
407 if (navbarBackgroundView != null
408 && decor.indexOfChild(navbarBackgroundView)
409 > decor.indexOfChild(mNavigationBarFrame)) {
410 decor.bringChildToFront(mNavigationBarFrame);
411 }
412 }
413 mNavigationBarFrame.setVisibility(View.VISIBLE);
414 }
415 }
416
417 @Override
418 public void onNavButtonFlagsChanged(@InputMethodNavButtonFlags int navButtonFlags) {
419 if (mDestroyed) {
420 return;
421 }
422
423 final boolean imeDrawsImeNavBar =
424 (navButtonFlags & InputMethodNavButtonFlags.IME_DRAWS_IME_NAV_BAR) != 0;
425 final boolean shouldShowImeSwitcherWhenImeIsShown =
426 (navButtonFlags & InputMethodNavButtonFlags.SHOW_IME_SWITCHER_WHEN_IME_IS_SHOWN)
427 != 0;
428
429 mImeDrawsImeNavBar = imeDrawsImeNavBar;
430 final boolean prevShouldShowImeSwitcherWhenImeIsShown =
431 mShouldShowImeSwitcherWhenImeIsShown;
432 mShouldShowImeSwitcherWhenImeIsShown = shouldShowImeSwitcherWhenImeIsShown;
433
434 if (imeDrawsImeNavBar) {
435 installNavigationBarFrameIfNecessary();
436 if (mNavigationBarFrame == null) {
437 return;
438 }
439 if (mShouldShowImeSwitcherWhenImeIsShown
440 == prevShouldShowImeSwitcherWhenImeIsShown) {
441 return;
442 }
443 final NavigationBarView navigationBarView = mNavigationBarFrame.findViewByPredicate(
444 NavigationBarView.class::isInstance);
445 if (navigationBarView == null) {
446 return;
447 }
448 final int hints = StatusBarManager.NAVIGATION_HINT_BACK_ALT
449 | (shouldShowImeSwitcherWhenImeIsShown
450 ? StatusBarManager.NAVIGATION_HINT_IME_SWITCHER_SHOWN : 0);
451 navigationBarView.setNavigationIconHints(hints);
452 } else {
453 uninstallNavigationBarFrameIfNecessary();
454 }
455 }
456
457 @Override
458 public void onSystemBarAppearanceChanged(@Appearance int appearance) {
459 if (mDestroyed) {
460 return;
461 }
462
463 mAppearance = appearance;
464
465 if (mNavigationBarFrame == null) {
466 return;
467 }
468
469 final float targetDarkIntensity = calculateTargetDarkIntensity(mAppearance,
470 mDrawLegacyNavigationBarBackground);
471
472 if (mTintAnimator != null) {
473 mTintAnimator.cancel();
474 }
475 mTintAnimator = ValueAnimator.ofFloat(mDarkIntensity, targetDarkIntensity);
476 mTintAnimator.addUpdateListener(
477 animation -> setIconTintInternal((Float) animation.getAnimatedValue()));
478 mTintAnimator.setDuration(DEFAULT_COLOR_ADAPT_TRANSITION_TIME);
479 mTintAnimator.setStartDelay(0);
480 mTintAnimator.setInterpolator(LEGACY_DECELERATE);
481 mTintAnimator.start();
482 }
483
484 private void setIconTintInternal(float darkIntensity) {
485 mDarkIntensity = darkIntensity;
486 if (mNavigationBarFrame == null) {
487 return;
488 }
489 final NavigationBarView navigationBarView =
490 mNavigationBarFrame.findViewByPredicate(NavigationBarView.class::isInstance);
491 if (navigationBarView == null) {
492 return;
493 }
494 navigationBarView.setDarkIntensity(darkIntensity);
495 }
496
497 @FloatRange(from = 0.0f, to = 1.0f)
498 private static float calculateTargetDarkIntensity(@Appearance int appearance,
499 boolean drawLegacyNavigationBarBackground) {
500 final boolean lightNavBar = !drawLegacyNavigationBarBackground
501 && (appearance & APPEARANCE_LIGHT_NAVIGATION_BARS) != 0;
502 return lightNavBar ? 1.0f : 0.0f;
503 }
504
505 @Override
506 public boolean onDrawLegacyNavigationBarBackgroundChanged(
507 boolean drawLegacyNavigationBarBackground) {
508 if (mDestroyed) {
509 return false;
510 }
511
512 if (drawLegacyNavigationBarBackground != mDrawLegacyNavigationBarBackground) {
513 mDrawLegacyNavigationBarBackground = drawLegacyNavigationBarBackground;
514 if (mNavigationBarFrame != null) {
515 if (mDrawLegacyNavigationBarBackground) {
516 mNavigationBarFrame.setBackgroundColor(Color.BLACK);
517 } else {
518 mNavigationBarFrame.setBackground(null);
519 }
520 scheduleRelayout();
521 }
522 onSystemBarAppearanceChanged(mAppearance);
523 }
524 return drawLegacyNavigationBarBackground;
525 }
526
527 @Override
528 public String toDebugString() {
529 return "{mImeDrawsImeNavBar=" + mImeDrawsImeNavBar
530 + " mNavigationBarFrame=" + mNavigationBarFrame
531 + " mShouldShowImeSwitcherWhenImeIsShown="
532 + mShouldShowImeSwitcherWhenImeIsShown
533 + " mAppearance=0x" + Integer.toHexString(mAppearance)
534 + " mDarkIntensity=" + mDarkIntensity
535 + " mDrawLegacyNavigationBarBackground=" + mDrawLegacyNavigationBarBackground
536 + "}";
537 }
538 }
539 }