Skip to content

Commit 4d15f08

Browse files
authored
fix(android): interface orientation value changes even with auto rotation off (#7)
1 parent 097a8d9 commit 4d15f08

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.orientationdirector.implementation
2+
3+
import android.database.ContentObserver
4+
import android.os.Handler
5+
import android.provider.Settings
6+
import android.util.Log
7+
import com.facebook.react.bridge.ReactContext
8+
9+
class OrientationAutoRotationObserver(val context: ReactContext, handler: Handler?) : ContentObserver(handler) {
10+
private var lastAutoRotationStatus: Boolean = isAutoRotationEnabled()
11+
12+
fun getLastAutoRotationStatus(): Boolean {
13+
return lastAutoRotationStatus
14+
}
15+
16+
override fun onChange(selfChange: Boolean) {
17+
super.onChange(selfChange)
18+
val status = isAutoRotationEnabled()
19+
lastAutoRotationStatus = status
20+
}
21+
22+
fun enable() {
23+
context.contentResolver.registerContentObserver(
24+
Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION),
25+
true,
26+
this,
27+
)
28+
}
29+
30+
fun disable() {
31+
context.contentResolver.unregisterContentObserver(this)
32+
}
33+
34+
private fun isAutoRotationEnabled(): Boolean {
35+
return try {
36+
Settings.System.getInt(context.contentResolver, Settings.System.ACCELEROMETER_ROTATION) == 1;
37+
} catch (ex: Settings.SettingNotFoundException) {
38+
false
39+
}
40+
}
41+
42+
companion object {
43+
const val NAME = "AutoRotationObserver"
44+
}
45+
}

android/src/main/java/com/orientationdirector/implementation/OrientationDirectorImpl.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package com.orientationdirector.implementation
22

33
import android.content.pm.ActivityInfo
4+
import android.os.Handler
5+
import android.os.Looper
6+
import android.util.Log
47
import com.facebook.react.bridge.ReactApplicationContext
58

69
class OrientationDirectorImpl internal constructor(private val context: ReactApplicationContext) {
710
private var mUtils = OrientationDirectorUtilsImpl(context)
811
private var mEventEmitter = OrientationEventManager(context)
912
private var mSensorListener = OrientationSensorListener(context)
13+
private var mAutoRotationObserver = OrientationAutoRotationObserver(
14+
context, Handler(
15+
Looper.getMainLooper()
16+
)
17+
)
1018
private var mLifecycleListener = OrientationLifecycleListener()
1119

1220
private var initialSupportedInterfaceOrientations = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
@@ -27,19 +35,25 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
2735
mSensorListener.disable()
2836
}
2937

38+
mAutoRotationObserver.enable()
39+
3040
context.addLifecycleEventListener(mLifecycleListener)
3141
mLifecycleListener.setOnHostResumeCallback {
3242
if (mSensorListener.canDetectOrientation()) {
3343
mSensorListener.enable()
3444
}
45+
46+
mAutoRotationObserver.enable()
3547
}
3648
mLifecycleListener.setOnHostPauseCallback {
3749
if (initialized) {
3850
mSensorListener.disable()
51+
mAutoRotationObserver.disable()
3952
}
4053
}
4154
mLifecycleListener.setOnHostDestroyCallback {
4255
mSensorListener.disable()
56+
mAutoRotationObserver.disable()
4357
}
4458

4559
initialSupportedInterfaceOrientations =
@@ -106,6 +120,10 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
106120
}
107121

108122
private fun adaptInterfaceTo(deviceOrientation: Orientation) {
123+
if (!mAutoRotationObserver.getLastAutoRotationStatus()) {
124+
return
125+
}
126+
109127
if (isLocked) {
110128
return
111129
}

android/src/main/java/com/orientationdirector/implementation/OrientationDirectorUtilsImpl.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.orientationdirector.implementation
33
import android.content.Context
44
import android.content.pm.ActivityInfo
55
import android.os.Build
6+
import android.provider.Settings
67
import android.view.Surface
78
import android.view.WindowManager
89
import com.facebook.react.bridge.ReactContext

0 commit comments

Comments
 (0)