Manual screen rotation in reader
parent
b103589bba
commit
3eb68e1ff9
@ -0,0 +1,53 @@
|
||||
package org.koitharu.kotatsu.utils
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.pm.ActivityInfo
|
||||
import android.content.res.Configuration
|
||||
import android.database.ContentObserver
|
||||
import android.os.Handler
|
||||
import android.provider.Settings
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.channels.sendBlocking
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
|
||||
class ScreenOrientationHelper(private val activity: Activity) {
|
||||
|
||||
val isAutoRotationEnabled: Boolean
|
||||
get() = Settings.System.getInt(
|
||||
activity.contentResolver,
|
||||
Settings.System.ACCELEROMETER_ROTATION,
|
||||
0
|
||||
) == 1
|
||||
|
||||
var isLandscape: Boolean
|
||||
get() = activity.resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
|
||||
set(value) {
|
||||
activity.requestedOrientation = if (value) {
|
||||
ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
|
||||
} else {
|
||||
ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
|
||||
}
|
||||
}
|
||||
|
||||
fun toggleOrientation() {
|
||||
isLandscape = !isLandscape
|
||||
}
|
||||
|
||||
fun observeAutoOrientation() = callbackFlow<Boolean> {
|
||||
val observer = object : ContentObserver(Handler(activity.mainLooper)) {
|
||||
override fun onChange(selfChange: Boolean) {
|
||||
sendBlocking(isAutoRotationEnabled)
|
||||
}
|
||||
}
|
||||
activity.contentResolver.registerContentObserver(
|
||||
Settings.System.CONTENT_URI, true, observer
|
||||
)
|
||||
awaitClose {
|
||||
activity.contentResolver.unregisterContentObserver(observer)
|
||||
}
|
||||
}.onStart {
|
||||
emit(isAutoRotationEnabled)
|
||||
}.distinctUntilChanged()
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
<vector
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:tint="?android:textColorPrimary"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M16.48,2.52c3.27,1.55 5.61,4.72 5.97,8.48h1.5C23.44,4.84 18.29,0 12,0l-0.66,0.03 3.81,3.81 1.33,-1.32zM10.23,1.75c-0.59,-0.59 -1.54,-0.59 -2.12,0L1.75,8.11c-0.59,0.59 -0.59,1.54 0,2.12l12.02,12.02c0.59,0.59 1.54,0.59 2.12,0l6.36,-6.36c0.59,-0.59 0.59,-1.54 0,-2.12L10.23,1.75zM14.83,21.19L2.81,9.17l6.36,-6.36 12.02,12.02 -6.36,6.36zM7.52,21.48C4.25,19.94 1.91,16.76 1.55,13L0.05,13C0.56,19.16 5.71,24 12,24l0.66,-0.03 -3.81,-3.81 -1.33,1.32z" />
|
||||
</vector>
|
||||
Loading…
Reference in New Issue