From d788f88dfe5833781686ec004ccc7e30a9c681f4 Mon Sep 17 00:00:00 2001 From: Joseph Hale Date: Fri, 25 Jul 2025 22:49:57 +0000 Subject: [PATCH] feat: implement keep awake on Android/iOS --- ios/KeepAwake.mm | 20 ++++++++++---------- src/NativeKeepAwake.ts | 1 - src/index.tsx | 4 ---- example/src/App.tsx | 43 +++++++++++++++++++++++++++++++++++++------ android/src/main/java/com/keepawake/KeepAwakeModule.kt | 30 ++++++++++++++++++------------ 5 file(s) changed, 65 insertion(s)(+), 33 deletion(s)(-) diff --git a/ios/KeepAwake.mm b/ios/KeepAwake.mm --- a/ios/KeepAwake.mm +++ b/ios/KeepAwake.mm @@ -5,19 +5,19 @@ - (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 --- a/src/NativeKeepAwake.ts +++ b/src/NativeKeepAwake.ts @@ -4,7 +4,6 @@ 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 --- a/src/index.tsx +++ b/src/index.tsx @@ -7,7 +7,3 @@ export function deactivate(): void { KeepAwake.deactivate(); } - -export function isActive(): boolean { - return KeepAwake.isActive(); -} diff --git a/example/src/App.tsx b/example/src/App.tsx --- 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 + + )} + +