Skip to content

Commit 3815b4d

Browse files
authored
feat(android): expose auto rotation status to js (#9)
1 parent 4d15f08 commit 3815b4d

8 files changed

Lines changed: 44 additions & 5 deletions

File tree

android/src/main/java/com/orientationdirector/OrientationDirectorModule.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ class OrientationDirectorModule internal constructor(context: ReactApplicationCo
4040

4141
@ReactMethod(isBlockingSynchronousMethod = true)
4242
override fun isLocked(): Boolean {
43-
return orientationDirectorImpl.isLocked
43+
return orientationDirectorImpl.getIsLocked()
44+
}
45+
46+
@ReactMethod(isBlockingSynchronousMethod = true)
47+
override fun isAutoRotationEnabled(): Boolean {
48+
return orientationDirectorImpl.getIsAutoRotationEnabled()
4449
}
4550

4651
@ReactMethod()

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.orientationdirector.implementation
33
import android.content.pm.ActivityInfo
44
import android.os.Handler
55
import android.os.Looper
6-
import android.util.Log
76
import com.facebook.react.bridge.ReactApplicationContext
87

98
class OrientationDirectorImpl internal constructor(private val context: ReactApplicationContext) {
@@ -21,8 +20,7 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
2120
private var lastInterfaceOrientation = Orientation.UNKNOWN
2221
private var lastDeviceOrientation = Orientation.UNKNOWN
2322
private var initialized = false
24-
25-
var isLocked: Boolean = false
23+
private var isLocked: Boolean = false
2624

2725
init {
2826
mSensorListener.setOnOrientationChangedCallback { orientation ->
@@ -73,6 +71,14 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
7371
return lastDeviceOrientation
7472
}
7573

74+
fun getIsLocked(): Boolean {
75+
return isLocked
76+
}
77+
78+
fun getIsAutoRotationEnabled(): Boolean {
79+
return mAutoRotationObserver.getLastAutoRotationStatus()
80+
}
81+
7682
fun lockTo(jsOrientation: Int) {
7783
val interfaceOrientation = mUtils.getOrientationFromJsOrientation(jsOrientation)
7884
val screenOrientation =

android/src/oldarch/OrientationDirectorSpec.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ abstract class OrientationDirectorSpec internal constructor(context: ReactApplic
1010
abstract fun getDeviceOrientation(promise: Promise)
1111
abstract fun lockTo(orientation: Double)
1212
abstract fun unlock()
13-
1413
abstract fun isLocked(): Boolean
14+
abstract fun isAutoRotationEnabled(): Boolean
1515
abstract fun addListener(eventName: String)
1616
abstract fun removeListeners(count: Double)
1717
}

example/src/App.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ export default function App() {
5858
}}
5959
/>
6060
<View style={styles.marginBottom} />
61+
<Button
62+
title={'Log is Auto Rotation Enabled'}
63+
onPress={() => {
64+
console.log(
65+
'isAutoRotationEnabled: ',
66+
RNOrientationDirector.isAutoRotationEnabled()
67+
);
68+
}}
69+
/>
70+
<View style={styles.marginBottom} />
6171
<Button
6272
title={'Lock To Portrait'}
6373
onPress={() => {

src/NativeOrientationDirector.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface Spec extends TurboModule {
77
lockTo(orientation: number): void;
88
unlock(): void;
99
isLocked(): boolean;
10+
isAutoRotationEnabled(): boolean;
1011

1112
addListener: (eventType: string) => void;
1213
removeListeners: (count: number) => void;

src/RNOrientationDirector.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { Platform } from 'react-native';
12
import Module, { EventEmitter } from './module';
23
import Event from './types/Event.enum';
34
import type { InterfaceOrientationToLocalizedStringProvider } from './types/InterfaceOrientationToLocalizedStringProvider.type';
45
import { Orientation } from './types/Orientation.enum';
6+
import { AutoRotation } from './types/AutoRotation.enum';
57
import type { OrientationEvent } from './types/OrientationEvent.interface';
68
import type { LockableOrientation } from './types/LockableOrientation.type';
79
import type { LockedEvent } from './types/LockedEvent.interface';
@@ -44,6 +46,15 @@ class RNOrientationDirector {
4446
return Module.isLocked();
4547
}
4648

49+
static isAutoRotationEnabled(): AutoRotation {
50+
if (Platform.OS !== 'android') {
51+
return AutoRotation.unknown;
52+
}
53+
return Module.isAutoRotationEnabled()
54+
? AutoRotation.enabled
55+
: AutoRotation.disabled;
56+
}
57+
4758
static listenForDeviceOrientationChanges(
4859
callback: (orientation: OrientationEvent) => void
4960
) {

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { Orientation } from './types/Orientation.enum';
2+
export { AutoRotation } from './types/AutoRotation.enum';
23

34
import useDeviceOrientation from './hooks/useDeviceOrientation.hook';
45
export { useDeviceOrientation };

src/types/AutoRotation.enum.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum AutoRotation {
2+
unknown = 0,
3+
enabled = 1,
4+
disabled = 2,
5+
}

0 commit comments

Comments
 (0)