diff --git a/app/src/main/java/org/jellyfin/mobile/player/PlayerActivity.kt b/app/src/main/java/org/jellyfin/mobile/player/PlayerActivity.kt index 237dfa84..d6e389e4 100644 --- a/app/src/main/java/org/jellyfin/mobile/player/PlayerActivity.kt +++ b/app/src/main/java/org/jellyfin/mobile/player/PlayerActivity.kt @@ -49,27 +49,13 @@ class PlayerActivity : AppCompatActivity() { /** * Listener that watches the current device orientation. - * It makes sure that the orientation sensor can still be used after toggling - * the orientation through the fullscreen button (and if enabled). + * It makes sure that the orientation sensor can still be used (if enabled) + * after toggling the orientation through the fullscreen button. * * If the requestedOrientation was reset directly after setting it in the fullscreenSwitcher click handler, * the orientation would get reverted before the user had any chance to rotate the device to the desired position. */ - private val orientationListener: OrientationEventListener by lazy { - object : OrientationEventListener(this) { - override fun onOrientationChanged(orientation: Int) { - val isAtTarget = when (requestedOrientation) { - ActivityInfo.SCREEN_ORIENTATION_PORTRAIT -> orientation in Constants.ORIENTATION_PORTRAIT_RANGE - ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE -> orientation in Constants.ORIENTATION_LANDSCAPE_RANGE - else -> false - } - if (isAtTarget && isAutoRotateOn()) { - // Reset to unspecified orientation - requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED - } - } - } - } + private val orientationListener: OrientationEventListener by lazy { SmartOrientationListener(this) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/app/src/main/java/org/jellyfin/mobile/utils/SmartOrientationListener.kt b/app/src/main/java/org/jellyfin/mobile/utils/SmartOrientationListener.kt new file mode 100644 index 00000000..cdf82683 --- /dev/null +++ b/app/src/main/java/org/jellyfin/mobile/utils/SmartOrientationListener.kt @@ -0,0 +1,24 @@ +package org.jellyfin.mobile.utils + +import android.app.Activity +import android.content.pm.ActivityInfo +import android.view.OrientationEventListener + +/** + * Listener that watches the current device orientation. + * It makes sure that the orientation sensor can still be used (if enabled) + * after toggling the orientation manually. + */ +class SmartOrientationListener(private val activity: Activity) : OrientationEventListener(activity) { + override fun onOrientationChanged(orientation: Int) { + val isAtTarget = when (activity.requestedOrientation) { + ActivityInfo.SCREEN_ORIENTATION_PORTRAIT -> orientation in Constants.ORIENTATION_PORTRAIT_RANGE + ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE -> orientation in Constants.ORIENTATION_LANDSCAPE_RANGE + else -> false + } + if (isAtTarget && activity.isAutoRotateOn()) { + // Reset to unspecified orientation + activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED + } + } +}