From 1d48ce144f30a77c3e033dcf44a14784cc8fd426 Mon Sep 17 00:00:00 2001 From: londre7 Date: Fri, 20 Mar 2026 20:28:13 +0300 Subject: [PATCH] Use the compass in landscape --- .../core/data/injection/MainContainer.kt | 4 +- .../core/data/repository/SensorsRepo.kt | 40 ++++++++++++++++++- .../look4sat/feature/radar/RadarScreen.kt | 2 +- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/core/data/src/main/java/com/rtbishop/look4sat/core/data/injection/MainContainer.kt b/core/data/src/main/java/com/rtbishop/look4sat/core/data/injection/MainContainer.kt index 1e87ee65..1b15b246 100644 --- a/core/data/src/main/java/com/rtbishop/look4sat/core/data/injection/MainContainer.kt +++ b/core/data/src/main/java/com/rtbishop/look4sat/core/data/injection/MainContainer.kt @@ -5,6 +5,7 @@ import android.content.Context import android.hardware.Sensor import android.hardware.SensorManager import android.location.LocationManager +import android.view.WindowManager import androidx.room.Room import com.rtbishop.look4sat.core.data.database.Look4SatDb import com.rtbishop.look4sat.core.data.framework.BluetoothReporter @@ -64,7 +65,8 @@ class MainContainer(private val context: Context) : IMainContainer { override fun provideSensorsRepo(): ISensorsRepo { val manager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager val sensor = manager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) - return SensorsRepo(manager, sensor) + val window = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager + return SensorsRepo(manager,sensor,window) } private fun provideDatabaseRepo(): IDatabaseRepo { diff --git a/core/data/src/main/java/com/rtbishop/look4sat/core/data/repository/SensorsRepo.kt b/core/data/src/main/java/com/rtbishop/look4sat/core/data/repository/SensorsRepo.kt index 33033c2f..a0fbbb00 100644 --- a/core/data/src/main/java/com/rtbishop/look4sat/core/data/repository/SensorsRepo.kt +++ b/core/data/src/main/java/com/rtbishop/look4sat/core/data/repository/SensorsRepo.kt @@ -22,6 +22,8 @@ import android.hardware.Sensor import android.hardware.SensorEvent import android.hardware.SensorEventListener import android.hardware.SensorManager +import android.view.Surface +import android.view.WindowManager import com.rtbishop.look4sat.core.domain.predict.GeoPos import com.rtbishop.look4sat.core.domain.predict.RAD2DEG import com.rtbishop.look4sat.core.domain.repository.ISensorsRepo @@ -29,7 +31,11 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlin.math.round -class SensorsRepo(private val sensorManager: SensorManager, private val sensor: Sensor?) : +class SensorsRepo( + private val sensorManager: SensorManager, + private val sensor: Sensor?, + private val windowManager: WindowManager +) : SensorEventListener, ISensorsRepo { private val _orientation = MutableStateFlow(Pair(0f, 0f)) @@ -61,8 +67,40 @@ class SensorsRepo(private val sensorManager: SensorManager, private val sensor: else -> {} } + private fun getDisplayRotation(): Int { + return try { + @Suppress("DEPRECATION") + windowManager.defaultDisplay.rotation + } catch (e: Exception) { + Surface.ROTATION_0 + } + } + + private fun transformRotationMatrix(rotationMatrix: FloatArray, rotation: Int) { + val tempMatrix = FloatArray(9) + when (rotation) { + Surface.ROTATION_0 -> { + SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Y, tempMatrix) + System.arraycopy(tempMatrix, 0, rotationMatrix, 0, 9) + } + Surface.ROTATION_90 -> { + SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, tempMatrix) + SensorManager.remapCoordinateSystem(tempMatrix, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_Y, rotationMatrix) + } + Surface.ROTATION_180 -> { + SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_MINUS_Y, tempMatrix) + System.arraycopy(tempMatrix, 0, rotationMatrix, 0, 9) + } + Surface.ROTATION_270 -> { + SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, tempMatrix) + SensorManager.remapCoordinateSystem(tempMatrix, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_Y, rotationMatrix) + } + } + } + private fun updateOrientation(rotationVector: FloatArray) { SensorManager.getRotationMatrixFromVector(rotationMatrix, rotationVector) + transformRotationMatrix(rotationMatrix, getDisplayRotation()) SensorManager.getOrientation(rotationMatrix, orientationValues) val azimuth = (orientationValues[0] * RAD2DEG).toFloat() val pitch = (orientationValues[1] * RAD2DEG).toFloat() // roll [2] diff --git a/feature/radar/src/main/java/com/rtbishop/look4sat/feature/radar/RadarScreen.kt b/feature/radar/src/main/java/com/rtbishop/look4sat/feature/radar/RadarScreen.kt index 39f14d7b..640f36f0 100644 --- a/feature/radar/src/main/java/com/rtbishop/look4sat/feature/radar/RadarScreen.kt +++ b/feature/radar/src/main/java/com/rtbishop/look4sat/feature/radar/RadarScreen.kt @@ -229,7 +229,7 @@ private fun RadarScreen(uiState: RadarState, navigateUp: () -> Unit) { items = uiState.satTrack, azimElev = uiState.orientationValues, shouldShowSweep = uiState.shouldShowSweep, - shouldUseCompass = false, + shouldUseCompass = uiState.shouldUseCompass, modifier = Modifier.align(Alignment.Center) ) Column(