diff --git a/README.md b/README.md index 3fc364c..9f2ad7d 100644 --- a/README.md +++ b/README.md @@ -56,14 +56,16 @@ protected List getPackages() { ## Usage -1. In your React Native javascript code, bring in the native module: +### In your React Native javascript code, bring in the native module: ```javascript import IdleTimerManager from 'react-native-idle-timer'; ``` +
-2. To disable the idle timer on a specific view component: +### To disable the idle timer while a certain component is mounted: +Class component ```javascript componentWillMount() { IdleTimerManager.setIdleTimerDisabled(true); @@ -73,3 +75,40 @@ componentWillUnmount() { IdleTimerManager.setIdleTimerDisabled(false); } ``` + + +Function component + +```javascript +useEffect(() => { + IdleTimerManager.setIdleTimerDisabled(true); + + return () => IdleTimerManager.setIdleTimerDisabled(false); +}, []) +``` +
+ +### If you have multiple components that are responsible for interacting with the idle timer, you can pass a tag as the second parameter: + +```javascript +useEffect(() => { + IdleTimerManager.setIdleTimerDisabled(true, "video"); + + return () => IdleTimerManager.setIdleTimerDisabled(false, "video"); +}, []) +``` +
+ +### If you need to interact from the native Android or iOS side: + +Android +```java +IdleTimerManager.activate(activity, "video"); +IdleTimerManager.deactivate(activity, "video"); +``` + +iOS +```objectivec +[IdleTimerManager activate:@"video"]; +[IdleTimerManager deactivate:@"video"]; +``` diff --git a/android/src/main/java/com/marcshilling/idletimer/IdleTimerManager.java b/android/src/main/java/com/marcshilling/idletimer/IdleTimerManager.java index 0d58915..301c15d 100644 --- a/android/src/main/java/com/marcshilling/idletimer/IdleTimerManager.java +++ b/android/src/main/java/com/marcshilling/idletimer/IdleTimerManager.java @@ -1,20 +1,22 @@ package com.marcshilling.idletimer; -import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; -import java.util.Map; -import java.util.HashMap; + +import java.util.HashSet; import android.app.Activity; import android.view.WindowManager; +import org.jetbrains.annotations.NotNull; + public class IdleTimerManager extends ReactContextBaseJavaModule { static final String MODULE_NAME = "IdleTimerManager"; + static final HashSet tags = new HashSet(); + public IdleTimerManager(ReactApplicationContext reactContext) { super(reactContext); } @@ -25,22 +27,30 @@ public class IdleTimerManager extends ReactContextBaseJavaModule } @ReactMethod - public void setIdleTimerDisabled(final boolean disabled) { - + public void setIdleTimerDisabled(final boolean disabled, final String tag) { final Activity activity = this.getCurrentActivity(); - if (activity != null) { - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - if (disabled) { - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - } else { - activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - } - } + if (disabled) { + activate(activity, tag); + } else { + deactivate(activity, tag); + } + } + + public static void activate(@NotNull final Activity activity, final String tag) { + if (tags.isEmpty()) { + activity.runOnUiThread(() -> { + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); }); } + tags.add((tag == null ? "" : tag)); + } - + public static void deactivate(@NotNull final Activity activity, final String tag) { + if (tags.size() == 1 && tags.contains((tag))) { + activity.runOnUiThread(() -> { + activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + }); + } + tags.remove((tag == null ? "" : tag)); } } diff --git a/index.d.ts b/index.d.ts index 7c75674..a205369 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,4 @@ declare namespace RNIdleTimer { - function setIdleTimerDisabled(disabled: boolean): void; + function setIdleTimerDisabled(disabled: boolean, tag: string | undefined): void; } export = RNIdleTimer diff --git a/index.js b/index.js index d96d23c..16cb1b6 100644 --- a/index.js +++ b/index.js @@ -2,4 +2,6 @@ var { NativeModules } = require('react-native') -module.exports = NativeModules.IdleTimerManager; +module.exports.setIdleTimerDisabled = (disabled, tag = "") => { + NativeModules.IdleTimerManager.setIdleTimerDisabled(disabled, tag); +} diff --git a/ios/RNIdleTimer/IdleTimerManager.h b/ios/RNIdleTimer/IdleTimerManager.h index 54a32b9..3556b29 100644 --- a/ios/RNIdleTimer/IdleTimerManager.h +++ b/ios/RNIdleTimer/IdleTimerManager.h @@ -7,4 +7,7 @@ @interface IdleTimerManager : NSObject ++ (void)activate:(NSString*)tag; ++ (void)deactivate:(NSString*)tag; + @end diff --git a/ios/RNIdleTimer/IdleTimerManager.m b/ios/RNIdleTimer/IdleTimerManager.m index a5bbc33..e840e53 100644 --- a/ios/RNIdleTimer/IdleTimerManager.m +++ b/ios/RNIdleTimer/IdleTimerManager.m @@ -1,13 +1,44 @@ #import "IdleTimerManager.h" +const static NSMutableSet *tags; + @implementation IdleTimerManager -RCT_EXPORT_MODULE(); - -RCT_EXPORT_METHOD(setIdleTimerDisabled:(BOOL)disabled) { - dispatch_async(dispatch_get_main_queue(), ^{ - [UIApplication sharedApplication].idleTimerDisabled = disabled; ++ (void) initialize { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + tags = [NSMutableSet set]; }); } +RCT_EXPORT_MODULE(); + +RCT_EXPORT_METHOD(setIdleTimerDisabled:(BOOL)disabled tag:(NSString *)tag) { + if (disabled) { + [IdleTimerManager activate:tag]; + } else { + [IdleTimerManager deactivate:tag]; + } +} + ++ (void)activate:(NSString*)tag { + if ([tags count] == 0) { + dispatch_async(dispatch_get_main_queue(), ^{ + [UIApplication sharedApplication].idleTimerDisabled = YES; + }); + } + + [tags addObject:tag ?: @""]; +} + ++ (void)deactivate:(NSString*)tag { + if ([tags count] == 1 && [tags containsObject:tag]) { + dispatch_async(dispatch_get_main_queue(), ^{ + [UIApplication sharedApplication].idleTimerDisabled = NO; + }); + } + + [tags removeObject:tag ?: @""]; +} + @end