diff --git a/android/src/main/java/com/keepawake/KeepAwakeModule.kt b/android/src/main/java/com/keepawake/KeepAwakeModule.kt
index 722e128..d5233fa 100644
--- a/android/src/main/java/com/keepawake/KeepAwakeModule.kt
+++ b/android/src/main/java/com/keepawake/KeepAwakeModule.kt
@@ -1,32 +1,38 @@
package com.keepawake
+import android.util.Log
+import android.view.WindowManager
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.annotations.ReactModule
-import android.util.Log
@ReactModule(name = KeepAwakeModule.NAME)
class KeepAwakeModule(reactContext: ReactApplicationContext) :
NativeKeepAwakeSpec(reactContext) {
+ private val context = reactContext
+
override fun getName(): String {
return NAME
}
-
+ /**
+ * Stops the screen from dimming or locking
+ */
override fun activate() {
- // Implementation to keep the device awake
- Log.d(NAME, "activate")
+ Log.d(NAME, "Activating KeepAwake")
+ context.currentActivity?.runOnUiThread {
+ context.currentActivity?.window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
+ }
}
+ /**
+ * Allows the screen to dim or lock
+ */
override fun deactivate() {
- // Implementation to allow the device to sleep
- Log.d(NAME, "deactivate")
- }
-
- override fun isActive(): Boolean {
- // Implementation to check if the device is set to stay awake
- Log.d(NAME, "isActive")
- return false
+ Log.d(NAME, "Deactivating KeepAwake")
+ context.currentActivity?.runOnUiThread {
+ context.currentActivity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
+ }
}
companion object {
diff --git a/example/src/App.tsx b/example/src/App.tsx
index e2231e7..935c9c0 100644
--- a/example/src/App.tsx
+++ b/example/src/App.tsx
@@ -1,15 +1,39 @@
import * as KeepAwake from 'react-native-keep-awake';
-import { StyleSheet, Text, View } from 'react-native';
+import { Button, Platform, StyleSheet, Text, View } from 'react-native';
export default function App() {
- KeepAwake.activate();
- KeepAwake.deactivate();
- const result = KeepAwake.isActive();
- console.log('KeepAwake is active:', result);
return (
- Is KeepAwake active? {result ? 'yes' : 'no'}
+
+ HOW TO USE THIS DEMO
+ 1. Turn up your screen brightness
+
+ 2. Change your device display settings to dim as quickly as possible
+
+
+ 3. Have fun using the buttons below to control whether or not your
+ device dims/sleeps quickly!
+
+ {Platform.OS === 'ios' && (
+
+ Note: On iOS, you may need to disconnect your device from XCode to
+ see the effects
+
+ )}
+
+
);
}
@@ -19,6 +43,13 @@ const styles = StyleSheet.create({
backgroundColor: '#fff',
flex: 1,
alignItems: 'center',
+ justifyContent: 'flex-start',
+ gap: 20,
+ },
+ instructions: {
+ margin: 20,
+ alignItems: 'flex-start',
justifyContent: 'center',
+ marginBottom: 20,
},
});
diff --git a/ios/KeepAwake.mm b/ios/KeepAwake.mm
index f5c3780..ecc1b48 100644
--- a/ios/KeepAwake.mm
+++ b/ios/KeepAwake.mm
@@ -5,19 +5,19 @@ RCT_EXPORT_MODULE()
- (void)activate {
- // Implementation for activating the keep awake functionality
- NSLog(@"KeepAwake activated");
+ // Stops the screen from dimming or locking
+ NSLog(@"Activating KeepAwake");
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
+ });
}
- (void)deactivate {
- // Implementation for deactivating the keep awake functionality
- NSLog(@"KeepAwake deactivated");
-}
-
-- (BOOL)isActive {
- // Implementation to check if the keep awake functionality is active
- NSLog(@"Checking if KeepAwake is active");
- return NO;
+ // Allows the screen to dim or lock
+ NSLog(@"Deactivating KeepAwake");
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
+ });
}
- (std::shared_ptr)getTurboModule:
diff --git a/src/NativeKeepAwake.ts b/src/NativeKeepAwake.ts
index e94959b..9a256e1 100644
--- a/src/NativeKeepAwake.ts
+++ b/src/NativeKeepAwake.ts
@@ -4,7 +4,6 @@ import { TurboModuleRegistry } from 'react-native';
export interface Spec extends TurboModule {
activate(): void;
deactivate(): void;
- isActive(): boolean;
}
export default TurboModuleRegistry.getEnforcing('KeepAwake');
diff --git a/src/index.tsx b/src/index.tsx
index 9ab211f..495ab34 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -7,7 +7,3 @@ export function activate(): void {
export function deactivate(): void {
KeepAwake.deactivate();
}
-
-export function isActive(): boolean {
- return KeepAwake.isActive();
-}