|
| 1 | +package expo.modules.t3nativecontrols |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.hardware.Sensor |
| 5 | +import android.hardware.SensorEvent |
| 6 | +import android.hardware.SensorEventListener |
| 7 | +import android.hardware.SensorManager |
| 8 | +import android.os.Build |
| 9 | +import androidx.window.layout.FoldingFeature |
| 10 | +import androidx.window.layout.WindowInfoTracker |
| 11 | +import expo.modules.kotlin.AppContext |
| 12 | +import expo.modules.kotlin.modules.Module |
| 13 | +import expo.modules.kotlin.modules.ModuleDefinition |
| 14 | +import expo.modules.kotlin.viewevent.EventDispatcher |
| 15 | +import expo.modules.kotlin.views.ExpoView |
| 16 | +import kotlinx.coroutines.CoroutineScope |
| 17 | +import kotlinx.coroutines.MainScope |
| 18 | +import kotlinx.coroutines.cancel |
| 19 | +import kotlinx.coroutines.launch |
| 20 | + |
| 21 | +class T3FoldPostureModule : Module() { |
| 22 | + override fun definition() = ModuleDefinition { |
| 23 | + Name("T3FoldPosture") |
| 24 | + |
| 25 | + View(T3FoldPostureView::class) { |
| 26 | + Events("onHingeChange") |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Reports a vertical hinge, in this view's dp coordinates, while the device is bent like a book. |
| 33 | + * The hinge sensor is only registered while a vertical fold is on screen. |
| 34 | + */ |
| 35 | +class T3FoldPostureView( |
| 36 | + context: Context, |
| 37 | + appContext: AppContext |
| 38 | +) : ExpoView(context, appContext), SensorEventListener { |
| 39 | + private val onHingeChange by EventDispatcher() |
| 40 | + private val sensorManager = context.getSystemService(SensorManager::class.java) |
| 41 | + private val hingeSensor = |
| 42 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { |
| 43 | + sensorManager?.getDefaultSensor(Sensor.TYPE_HINGE_ANGLE) |
| 44 | + } else { |
| 45 | + null |
| 46 | + } |
| 47 | + private var scope: CoroutineScope? = null |
| 48 | + private var fold: FoldingFeature? = null |
| 49 | + private var listening = false |
| 50 | + private var bent = false |
| 51 | + private var reported: Map<String, Any>? = null |
| 52 | + |
| 53 | + override fun onAttachedToWindow() { |
| 54 | + super.onAttachedToWindow() |
| 55 | + observeFold() |
| 56 | + } |
| 57 | + |
| 58 | + // The activity can be missing when the view first attaches, so layout passes retry. |
| 59 | + private fun observeFold() { |
| 60 | + if (scope != null || !isAttachedToWindow) return |
| 61 | + val activity = appContext.currentActivity ?: return |
| 62 | + scope = MainScope().also { scope -> |
| 63 | + scope.launch { |
| 64 | + WindowInfoTracker.getOrCreate(activity).windowLayoutInfo(activity).collect { info -> |
| 65 | + fold = info.displayFeatures.filterIsInstance<FoldingFeature>().firstOrNull { |
| 66 | + it.orientation == FoldingFeature.Orientation.VERTICAL |
| 67 | + } |
| 68 | + listen(fold != null) |
| 69 | + report() |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + override fun onDetachedFromWindow() { |
| 76 | + scope?.cancel() |
| 77 | + scope = null |
| 78 | + listen(false) |
| 79 | + super.onDetachedFromWindow() |
| 80 | + } |
| 81 | + |
| 82 | + override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { |
| 83 | + super.onLayout(changed, left, top, right, bottom) |
| 84 | + observeFold() |
| 85 | + if (changed) report() |
| 86 | + } |
| 87 | + |
| 88 | + override fun onSensorChanged(event: SensorEvent) { |
| 89 | + // Pixel foldables report the hinge in 5° steps and rest at 175° when nearly flat. Snapping at |
| 90 | + // 170° and releasing at 175° keeps a resting hinge from flapping the layout. |
| 91 | + val angle = event.values[0] |
| 92 | + bent = if (bent) angle < 175f else angle <= 170f |
| 93 | + report() |
| 94 | + } |
| 95 | + |
| 96 | + override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) = Unit |
| 97 | + |
| 98 | + private fun listen(enabled: Boolean) { |
| 99 | + if (enabled == listening || hingeSensor == null) return |
| 100 | + listening = enabled |
| 101 | + if (enabled) { |
| 102 | + sensorManager.registerListener(this, hingeSensor, SensorManager.SENSOR_DELAY_UI) |
| 103 | + } else { |
| 104 | + sensorManager.unregisterListener(this) |
| 105 | + bent = false |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private fun report() { |
| 110 | + val feature = fold |
| 111 | + // Without a hinge sensor, the platform's half-opened posture is the closest signal. |
| 112 | + val aligned = feature != null && |
| 113 | + if (hingeSensor != null) bent else feature.state == FoldingFeature.State.HALF_OPENED |
| 114 | + val hinge = if (aligned && feature != null) { |
| 115 | + val origin = IntArray(2).also { getLocationInWindow(it) } |
| 116 | + val density = resources.displayMetrics.density |
| 117 | + mapOf( |
| 118 | + "x" to (feature.bounds.left - origin[0]) / density, |
| 119 | + "windowX" to feature.bounds.left / density, |
| 120 | + "width" to feature.bounds.width() / density |
| 121 | + ) |
| 122 | + } else { |
| 123 | + emptyMap() |
| 124 | + } |
| 125 | + if (hinge == reported) return |
| 126 | + reported = hinge |
| 127 | + onHingeChange(hinge) |
| 128 | + } |
| 129 | +} |
0 commit comments