|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.uimanager |
| 9 | + |
| 10 | +import android.graphics.PointF |
| 11 | +import android.view.MotionEvent |
| 12 | +import android.view.View |
| 13 | +import android.view.ViewConfiguration |
| 14 | +import android.view.ViewGroup |
| 15 | +import android.view.ViewTreeObserver |
| 16 | +import com.facebook.react.R |
| 17 | +import java.lang.ref.WeakReference |
| 18 | + |
| 19 | +/** |
| 20 | + * Keeps, for the window of a React root, the view a browser would hold focused where Android holds |
| 21 | + * none, since Android's touch mode leaves no view focused unless it is focusable in touch mode: the |
| 22 | + * nearest focusable React view of the last tap on a React root in the window, or of the view whose |
| 23 | + * focus a touch that was no tap took away. A view that takes Android's focus, or held it when |
| 24 | + * tapped, holds that focus itself, so the tap's focus ends. |
| 25 | + * |
| 26 | + * A tap is a touch with one pointer that never moves past the touch slop, so a scroll keeps the |
| 27 | + * focus, as it keeps a browser's. |
| 28 | + */ |
| 29 | +internal class TapFocusTracker(private val rootViewGroup: ViewGroup) : |
| 30 | + ViewTreeObserver.OnGlobalFocusChangeListener, ViewTreeObserver.OnTouchModeChangeListener { |
| 31 | + private val touchSlop = ViewConfiguration.get(rootViewGroup.context).scaledTouchSlop |
| 32 | + private var tapStart: PointF? = null |
| 33 | + |
| 34 | + fun handleMotionEvent(motionEvent: MotionEvent) { |
| 35 | + when (motionEvent.actionMasked) { |
| 36 | + MotionEvent.ACTION_DOWN -> tapStart = PointF(motionEvent.x, motionEvent.y) |
| 37 | + MotionEvent.ACTION_MOVE -> |
| 38 | + tapStart = tapStart?.takeIf { start -> isWithinTouchSlop(start, motionEvent) } |
| 39 | + MotionEvent.ACTION_UP -> { |
| 40 | + if (tapStart != null) { |
| 41 | + setFocusedView(findTappedView(motionEvent)?.takeUnless { it.isFocused }) |
| 42 | + } |
| 43 | + tapStart = null |
| 44 | + } |
| 45 | + else -> tapStart = null |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** Follows the focus and the touch mode of the root's window, until [stopObservingWindow]. */ |
| 50 | + fun observeWindow() { |
| 51 | + stopObservingWindow() |
| 52 | + rootViewGroup.viewTreeObserver.addOnGlobalFocusChangeListener(this) |
| 53 | + rootViewGroup.viewTreeObserver.addOnTouchModeChangeListener(this) |
| 54 | + } |
| 55 | + |
| 56 | + fun stopObservingWindow() { |
| 57 | + rootViewGroup.viewTreeObserver.removeOnGlobalFocusChangeListener(this) |
| 58 | + rootViewGroup.viewTreeObserver.removeOnTouchModeChangeListener(this) |
| 59 | + } |
| 60 | + |
| 61 | + override fun onGlobalFocusChanged(oldFocus: View?, newFocus: View?) { |
| 62 | + if (newFocus != null) { |
| 63 | + setFocusedView(null) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Android calls this before entering touch mode clears the focus of a view not focusable in it. |
| 69 | + */ |
| 70 | + override fun onTouchModeChanged(isInTouchMode: Boolean) { |
| 71 | + val focusedView = rootViewGroup.findFocus() |
| 72 | + if (isInTouchMode && focusedView != null && !focusedView.isFocusableInTouchMode) { |
| 73 | + setFocusedView(focusedView.findNearestFocusableReactView()) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private fun isWithinTouchSlop(start: PointF, motionEvent: MotionEvent): Boolean { |
| 78 | + val deltaX = motionEvent.x - start.x |
| 79 | + val deltaY = motionEvent.y - start.y |
| 80 | + return deltaX * deltaX + deltaY * deltaY <= touchSlop * touchSlop |
| 81 | + } |
| 82 | + |
| 83 | + private fun findTappedView(motionEvent: MotionEvent): View? = |
| 84 | + TouchTargetHelper.findTargetPathAndCoordinatesForTouch( |
| 85 | + motionEvent.x, |
| 86 | + motionEvent.y, |
| 87 | + rootViewGroup, |
| 88 | + FloatArray(2), |
| 89 | + ) |
| 90 | + .firstNotNullOfOrNull { target -> target.getView() } |
| 91 | + ?.findNearestFocusableReactView() |
| 92 | + |
| 93 | + private fun setFocusedView(view: View?) { |
| 94 | + rootViewGroup.rootView.setTag(R.id.tap_focused_view, view?.let(::TappedView)) |
| 95 | + } |
| 96 | + |
| 97 | + private class TappedView(view: View) { |
| 98 | + private val viewReference = WeakReference(view) |
| 99 | + private val viewTag = view.id |
| 100 | + |
| 101 | + /** |
| 102 | + * The tapped view while it could still hold focus and [rootViewGroup] is the closest React root |
| 103 | + * above it. A view recycled under another tag since the tap stands for another component. |
| 104 | + */ |
| 105 | + fun findIn(rootViewGroup: ViewGroup): View? { |
| 106 | + val view = viewReference.get() ?: return null |
| 107 | + if (view.id != viewTag || !view.isShown || !view.isFocusableReactView()) { |
| 108 | + return null |
| 109 | + } |
| 110 | + var parent = view.parent |
| 111 | + while (parent != null && parent !is RootView) { |
| 112 | + parent = parent.parent |
| 113 | + } |
| 114 | + return if (parent === rootViewGroup) view else null |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + companion object { |
| 119 | + /** The view the window of [rootViewGroup] holds focused for a browser, if it is inside it. */ |
| 120 | + fun findFocusedView(rootViewGroup: ViewGroup): View? = |
| 121 | + (rootViewGroup.rootView.getTag(R.id.tap_focused_view) as? TappedView)?.findIn(rootViewGroup) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/** A view without a positive id is no React view, so no JS event can name it. */ |
| 126 | +private fun View.isFocusableReactView(): Boolean = id > 0 && isFocusable && isEnabled |
| 127 | + |
| 128 | +private fun View.findNearestFocusableReactView(): View? = |
| 129 | + generateSequence(this) { it.parent as? View }.firstOrNull { it.isFocusableReactView() } |
0 commit comments