-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPhysicalOrientationTracker.kt
More file actions
89 lines (75 loc) · 3.02 KB
/
Copy pathPhysicalOrientationTracker.kt
File metadata and controls
89 lines (75 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package org.aossie.ogh
import android.content.Context
import android.hardware.SensorManager
import android.view.OrientationEventListener
private const val ORIENTATION_TRANSITION_DEGREES = 60
/** Cardinal physical orientations used independently of Android display rotation. */
internal enum class PhysicalOrientation(val degrees: Int) {
PORTRAIT(0),
LANDSCAPE_RIGHT(90),
REVERSE_PORTRAIT(180),
LANDSCAPE_LEFT(270),
;
val isPortrait: Boolean
get() = this == PORTRAIT || this == REVERSE_PORTRAIT
companion object {
fun fromDegrees(degrees: Int): PhysicalOrientation = entries.first { it.degrees == degrees }
}
}
/**
* Quantizes sensor degrees with a 15-degree hysteresis margin around each
* 45-degree cardinal boundary, avoiding rapid flips while a device is tilted.
*/
internal fun resolvePhysicalOrientation(
sensorDegrees: Int,
current: PhysicalOrientation?,
): PhysicalOrientation? {
if (sensorDegrees == OrientationEventListener.ORIENTATION_UNKNOWN) return current
val normalized = ((sensorDegrees % 360) + 360) % 360
val candidateDegrees = (((normalized + 45) / 90) % 4) * 90
val candidate = PhysicalOrientation.fromDegrees(candidateDegrees)
if (current == null || candidate == current) return candidate
val directDistance = kotlin.math.abs(normalized - current.degrees)
val distanceFromCurrent = minOf(directDistance, 360 - directDistance)
return if (distanceFromCurrent >= ORIENTATION_TRANSITION_DEGREES) candidate else current
}
/** Converts RootEncoder's display-derived camera angle into physical cardinal form. */
internal fun displayFallbackOrientation(cameraOrientation: Int): PhysicalOrientation {
val deviceDegrees = if (cameraOrientation == 0) 270 else cameraOrientation - 90
return PhysicalOrientation.fromDegrees(deviceDegrees)
}
/** Keeps the latest physical orientation available across preview surface detach/reattach. */
internal class PhysicalOrientationTracker(
context: Context,
initial: PhysicalOrientation,
private val onChanged: (PhysicalOrientation) -> Unit,
) {
@Volatile
var current: PhysicalOrientation = initial
private set
private var started = false
private val listener = object : OrientationEventListener(
context.applicationContext,
SensorManager.SENSOR_DELAY_NORMAL,
) {
override fun onOrientationChanged(orientation: Int) {
val resolved = resolvePhysicalOrientation(orientation, current) ?: return
if (resolved == current) return
current = resolved
onChanged(resolved)
}
}
fun start() {
if (started) return
started = true
onChanged(current)
if (listener.canDetectOrientation()) listener.enable()
}
fun stop() {
if (!started) return
listener.disable()
started = false
}
}
/** The preview viewport follows its surface, not physical device or stream orientation. */
internal fun isPortraitSurface(width: Int, height: Int): Boolean = height >= width