Extract orientation listener to separate class

This commit is contained in:
Maxr1998
2020-08-24 22:10:19 +02:00
parent e156a52225
commit 338c8ce806
2 changed files with 27 additions and 17 deletions
@@ -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)
@@ -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
}
}
}