Skip to content

Commit

Permalink
[CORE-4969] Improved detector for device rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
KharchenkoAlex committed Nov 4, 2024
1 parent ce01da9 commit 297daff
Showing 1 changed file with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import android.util.Log
import androidx.compose.runtime.*
import androidx.compose.ui.platform.LocalContext
import com.bitmovin.player.PlayerView
import kotlin.math.abs
import kotlin.math.absoluteValue

@Composable
fun DetectRotationAndFullscreen(playerView: PlayerView?, callback: (isFullscreen: Boolean) -> Unit) {
val context = LocalContext.current
var isLandscape by remember { mutableStateOf(false) }
var lastOrientation by remember { mutableStateOf(false) }
val threshold = 5.0f

DisposableEffect(Unit) {
val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
Expand All @@ -26,35 +29,30 @@ fun DetectRotationAndFullscreen(playerView: PlayerView?, callback: (isFullscreen
val x = event.values[0]
val y = event.values[1]

// Determine if the device is in landscape or portrait orientation
if (x.absoluteValue > y.absoluteValue) {
// Landscape mode
if (!isLandscape) {
isLandscape = true
Log.d("Orientation", "Landscape mode detected")
// playerView?.enterFullscreen()
callback.invoke(true)
}
} else {
// Portrait mode
if (isLandscape) {
isLandscape = false
Log.d("Orientation", "Portrait mode detected")
// playerView?.exitFullscreen()
callback.invoke(false)
val currentOrientation = x.absoluteValue > y.absoluteValue
if (abs(x) > threshold || abs(y) > threshold) {
if (currentOrientation != lastOrientation) {
if (currentOrientation && !isLandscape) {
isLandscape = true
Log.d("Orientation", "Landscape mode detected")
callback.invoke(true)
} else if (!currentOrientation && isLandscape) {
isLandscape = false
Log.d("Orientation", "Portrait mode detected")
callback.invoke(false)
}
}
}
lastOrientation = currentOrientation
}
}

override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}
}

// Register the sensor listener
sensorManager.registerListener(sensorEventListener, accelerometer, SensorManager.SENSOR_DELAY_NORMAL)

onDispose {
// Unregister the sensor listener to avoid memory leaks
sensorManager.unregisterListener(sensorEventListener)
}
}
Expand Down

0 comments on commit 297daff

Please sign in to comment.