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 + + )} + +