Skip to content

Commit d877a78

Browse files
authored
fix(#98): initial device computation breaking sensor usages on android (#100)
1 parent aa5f12b commit d877a78

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class OrientationDirectorModuleImpl internal constructor(private val context: Re
3636
checkInterfaceOrientation(false)
3737
}
3838

39+
// NOTE(1.init): This is needed to compute the initial device orientation
40+
mOrientationSensorsEventListener.enable()
41+
3942
context.addLifecycleEventListener(mLifecycleListener)
4043
mLifecycleListener.setOnHostResumeCallback {
4144
if (!didComputeInitialDeviceOrientation || areOrientationSensorsEnabled) {
@@ -45,17 +48,17 @@ class OrientationDirectorModuleImpl internal constructor(private val context: Re
4548
mBroadcastReceiver.register()
4649
}
4750
mLifecycleListener.setOnHostPauseCallback {
48-
if (initialized && areOrientationSensorsEnabled) {
51+
if (!didComputeInitialDeviceOrientation || areOrientationSensorsEnabled) {
4952
mOrientationSensorsEventListener.disable()
50-
mAutoRotationObserver.disable()
5153
}
54+
mAutoRotationObserver.disable()
5255
mBroadcastReceiver.unregister()
5356
}
5457
mLifecycleListener.setOnHostDestroyCallback {
55-
if (areOrientationSensorsEnabled) {
58+
if (!didComputeInitialDeviceOrientation || areOrientationSensorsEnabled) {
5659
mOrientationSensorsEventListener.disable()
57-
mAutoRotationObserver.disable()
5860
}
61+
mAutoRotationObserver.disable()
5962
mBroadcastReceiver.unregister()
6063
}
6164

@@ -163,8 +166,14 @@ class OrientationDirectorModuleImpl internal constructor(private val context: Re
163166

164167
checkInterfaceOrientation()
165168

166-
if (!didComputeInitialDeviceOrientation) {
167-
didComputeInitialDeviceOrientation = true
169+
// NOTE(2.init): This is needed to disable sensors if they were needed just for the initial
170+
// device computation.
171+
if (didComputeInitialDeviceOrientation) {
172+
return
173+
}
174+
didComputeInitialDeviceOrientation = true
175+
176+
if (!areOrientationSensorsEnabled) {
168177
mOrientationSensorsEventListener.disable()
169178
}
170179
}

0 commit comments

Comments
 (0)