Skip to content

Commit

Permalink
Merge pull request #1122 from KotlinGeekDev/conditional-rotation
Browse files Browse the repository at this point in the history
Re-enable tap to fullscreen behaviour with stricter orientation checks.
  • Loading branch information
vitorpamplona authored Oct 7, 2024
2 parents 5d788ed + 7326d6b commit 18fca8a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -200,7 +201,7 @@ fun ZoomableContentView(
images,
onDismiss = {
dialogOpen = false
// if (!isFoldableOrLarge) DeviceUtils.changeDeviceOrientation(isLandscapeMode, activity)
if (!isFoldableOrLarge && !isOrientationLocked) DeviceUtils.changeDeviceOrientation(isLandscapeMode, activity)
},
accountViewModel,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 18fca8a

Please sign in to comment.