From 7326d6be93ffd8c197d42347ec2311368505ac52 Mon Sep 17 00:00:00 2001 From: KotlinGeekDev Date: Sat, 5 Oct 2024 17:06:36 +0100 Subject: [PATCH] Rotation:actually rotate if the device orientation is not locked. --- .../ui/components/ZoomableContentView.kt | 9 ++++--- .../ui/components/util/DeviceUtils.kt | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ZoomableContentView.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ZoomableContentView.kt index 13ca3741d..457d072b0 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ZoomableContentView.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ZoomableContentView.kt @@ -126,6 +126,7 @@ fun ZoomableContentView( val isLandscapeMode = DeviceUtils.isLandscapeMetric(LocalContext.current) val isFoldableOrLarge = DeviceUtils.windowIsLarge(windowSize = currentWindowSize, isInLandscapeMode = isLandscapeMode) + val isOrientationLocked = DeviceUtils.screenOrientationIsLocked(LocalContext.current) val contentScale = if (isFiniteHeight) { @@ -160,9 +161,9 @@ fun ZoomableContentView( nostrUriCallback = content.uri, onDialog = { dialogOpen = true - // if (!isFoldableOrLarge) { - // DeviceUtils.changeDeviceOrientation(isLandscapeMode, activity) - // } + if (!isFoldableOrLarge && !isOrientationLocked) { + DeviceUtils.changeDeviceOrientation(isLandscapeMode, activity) + } }, accountViewModel = accountViewModel, ) @@ -200,7 +201,7 @@ fun ZoomableContentView( images, onDismiss = { dialogOpen = false - // if (!isFoldableOrLarge) DeviceUtils.changeDeviceOrientation(isLandscapeMode, activity) + if (!isFoldableOrLarge && !isOrientationLocked) DeviceUtils.changeDeviceOrientation(isLandscapeMode, activity) }, accountViewModel, ) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/util/DeviceUtils.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/util/DeviceUtils.kt index 575058939..3e3f3d11c 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/util/DeviceUtils.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/util/DeviceUtils.kt @@ -23,6 +23,8 @@ package com.vitorpamplona.amethyst.ui.components.util import android.app.Activity import android.content.Context import android.content.pm.ActivityInfo +import android.content.pm.PackageManager +import android.provider.Settings import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.window.core.layout.WindowHeightSizeClass @@ -38,6 +40,31 @@ object DeviceUtils { */ fun isLandscapeMetric(context: Context): Boolean = context.resources.displayMetrics.heightPixels < context.resources.displayMetrics.widthPixels + /** + * Checks if the device's orientation is set to locked. + * + * Credits: NewPipe devs + */ + fun screenOrientationIsLocked(context: Context): Boolean { + // 1: Screen orientation changes using accelerometer + // 0: Screen orientation is locked + // if the accelerometer sensor is missing completely, assume locked orientation + return ( + Settings.System.getInt( + context.contentResolver, + Settings.System.ACCELEROMETER_ROTATION, + 0, + ) == 0 || + !context.packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_ACCELEROMETER) + ) + } + + /** + * Changes the device's orientation. This works even if the device's orientation + * is set to locked. + * Thus, to prevent unwanted behaviour, + * it's use can be guarded by conditions such as [screenOrientationIsLocked]. + */ fun changeDeviceOrientation( isInLandscape: Boolean, currentActivity: Activity,