-
-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: keep screen awake during inference, deactivate when idle (#210)
* feat: keep screen awake during inference and deactivate when idle * fix(test): waiting for checkbox test
- Loading branch information
1 parent
bf3e88b
commit d957b50
Showing
11 changed files
with
180 additions
and
6 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
android/app/src/main/java/com/pocketpalai/KeepAwakeModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.pocketpal | ||
|
||
import android.app.Activity | ||
import android.view.WindowManager | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule | ||
import com.facebook.react.bridge.ReactMethod | ||
|
||
class KeepAwakeModule(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { | ||
|
||
override fun getName(): String = "KeepAwakeModule" | ||
|
||
@ReactMethod | ||
fun activate() { | ||
val activity = reactContext.currentActivity | ||
activity?.runOnUiThread { | ||
activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) | ||
} | ||
} | ||
|
||
@ReactMethod | ||
fun deactivate() { | ||
val activity = reactContext.currentActivity | ||
activity?.runOnUiThread { | ||
activity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
android/app/src/main/java/com/pocketpalai/KeepAwakePackage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.pocketpal | ||
|
||
import com.facebook.react.ReactPackage | ||
import com.facebook.react.bridge.NativeModule | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.uimanager.ViewManager | ||
|
||
class KeepAwakePackage : ReactPackage { | ||
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> { | ||
return listOf(KeepAwakeModule(reactContext)) | ||
} | ||
|
||
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> { | ||
return emptyList() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#import <React/RCTBridgeModule.h> | ||
|
||
@interface KeepAwakeModule : NSObject <RCTBridgeModule> | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#import "KeepAwakeModule.h" | ||
#import <UIKit/UIKit.h> | ||
|
||
@implementation KeepAwakeModule | ||
|
||
RCT_EXPORT_MODULE(KeepAwakeModule); | ||
|
||
RCT_EXPORT_METHOD(activate) | ||
{ | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
[[UIApplication sharedApplication] setIdleTimerDisabled:YES]; | ||
}); | ||
} | ||
|
||
RCT_EXPORT_METHOD(deactivate) | ||
{ | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
[[UIApplication sharedApplication] setIdleTimerDisabled:NO]; | ||
}); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {useEffect} from 'react'; | ||
import {activateKeepAwake, deactivateKeepAwake} from '../utils/keepAwake'; | ||
|
||
/** | ||
* React hook that prevents the screen from going to sleep while the component is mounted. | ||
* | ||
* @example | ||
* ```tsx | ||
* function VideoPlayer() { | ||
* useKeepAwake(); // Screen will stay awake while VideoPlayer is mounted | ||
* return <Video source={source} />; | ||
* } | ||
* ``` | ||
*/ | ||
export function useKeepAwake(): void { | ||
useEffect(() => { | ||
try { | ||
activateKeepAwake(); | ||
return () => { | ||
try { | ||
deactivateKeepAwake(); | ||
} catch (error) { | ||
console.error('Failed to deactivate keep awake in cleanup:', error); | ||
} | ||
}; | ||
} catch (error) { | ||
console.error('Failed to activate keep awake:', error); | ||
// We don't rethrow here as it would crash the component | ||
// Instead, we log the error and let the screen timeout normally | ||
} | ||
}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {NativeModules} from 'react-native'; | ||
|
||
const {KeepAwakeModule} = NativeModules; | ||
|
||
if (!KeepAwakeModule) { | ||
console.warn( | ||
'KeepAwakeModule is not available. Make sure:\n' + | ||
'- You rebuilt the app after adding the native modules\n' + | ||
'- The native module is properly linked\n' + | ||
'- You are not using Expo managed workflow', | ||
); | ||
} | ||
|
||
/** | ||
* Activates keep awake functionality to prevent the screen from going to sleep | ||
* @throws {Error} If the native module fails to activate | ||
*/ | ||
export const activateKeepAwake = (): void => { | ||
try { | ||
KeepAwakeModule.activate(); | ||
} catch (error) { | ||
console.error('Failed to activate keep awake:', error); | ||
throw error; | ||
} | ||
}; | ||
|
||
/** | ||
* Deactivates keep awake functionality allowing the screen to go to sleep | ||
* @throws {Error} If the native module fails to deactivate | ||
*/ | ||
export const deactivateKeepAwake = (): void => { | ||
try { | ||
KeepAwakeModule.deactivate(); | ||
} catch (error) { | ||
console.error('Failed to deactivate keep awake:', error); | ||
throw error; | ||
} | ||
}; |