Skip to content

Commit bc34473

Browse files
feat: tag every call to disable the idle timer and expose static method for non react-native usage (#31)
add support for tagging calls and native code usage --------- Co-authored-by: Marc Shilling <marcshilling@gmail.com>
1 parent 9763ef8 commit bc34473

6 files changed

Lines changed: 110 additions & 25 deletions

File tree

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ protected List<ReactPackage> getPackages() {
5656

5757
## Usage
5858

59-
1. In your React Native javascript code, bring in the native module:
59+
### In your React Native javascript code, bring in the native module:
6060

6161
```javascript
6262
import IdleTimerManager from 'react-native-idle-timer';
6363
```
64+
<br/>
6465

65-
2. To disable the idle timer on a specific view component:
66+
### To disable the idle timer while a certain component is mounted:
6667

68+
Class component
6769
```javascript
6870
componentWillMount() {
6971
IdleTimerManager.setIdleTimerDisabled(true);
@@ -73,3 +75,40 @@ componentWillUnmount() {
7375
IdleTimerManager.setIdleTimerDisabled(false);
7476
}
7577
```
78+
79+
80+
Function component
81+
82+
```javascript
83+
useEffect(() => {
84+
IdleTimerManager.setIdleTimerDisabled(true);
85+
86+
return () => IdleTimerManager.setIdleTimerDisabled(false);
87+
}, [])
88+
```
89+
<br/>
90+
91+
### If you have multiple components that are responsible for interacting with the idle timer, you can pass a tag as the second parameter:
92+
93+
```javascript
94+
useEffect(() => {
95+
IdleTimerManager.setIdleTimerDisabled(true, "video");
96+
97+
return () => IdleTimerManager.setIdleTimerDisabled(false, "video");
98+
}, [])
99+
```
100+
<br/>
101+
102+
### If you need to interact from the native Android or iOS side:
103+
104+
Android
105+
```java
106+
IdleTimerManager.activate(activity, "video");
107+
IdleTimerManager.deactivate(activity, "video");
108+
```
109+
110+
iOS
111+
```objectivec
112+
[IdleTimerManager activate:@"video"];
113+
[IdleTimerManager deactivate:@"video"];
114+
```
Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package com.marcshilling.idletimer;
22

3-
import com.facebook.react.bridge.NativeModule;
43
import com.facebook.react.bridge.ReactApplicationContext;
5-
import com.facebook.react.bridge.ReactContext;
64
import com.facebook.react.bridge.ReactContextBaseJavaModule;
75
import com.facebook.react.bridge.ReactMethod;
8-
import java.util.Map;
9-
import java.util.HashMap;
6+
7+
import java.util.HashSet;
108

119
import android.app.Activity;
1210
import android.view.WindowManager;
1311

12+
import org.jetbrains.annotations.NotNull;
13+
1414
public class IdleTimerManager extends ReactContextBaseJavaModule
1515
{
1616
static final String MODULE_NAME = "IdleTimerManager";
1717

18+
static final HashSet<String> tags = new HashSet();
19+
1820
public IdleTimerManager(ReactApplicationContext reactContext) {
1921
super(reactContext);
2022
}
@@ -25,22 +27,30 @@ public String getName() {
2527
}
2628

2729
@ReactMethod
28-
public void setIdleTimerDisabled(final boolean disabled) {
29-
30+
public void setIdleTimerDisabled(final boolean disabled, final String tag) {
3031
final Activity activity = this.getCurrentActivity();
31-
if (activity != null) {
32-
activity.runOnUiThread(new Runnable() {
33-
@Override
34-
public void run() {
35-
if (disabled) {
36-
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
37-
} else {
38-
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
39-
}
40-
}
41-
});
32+
if (disabled) {
33+
activate(activity, tag);
34+
} else {
35+
deactivate(activity, tag);
4236
}
37+
}
4338

39+
public static void activate(@NotNull final Activity activity, final String tag) {
40+
if (tags.isEmpty()) {
41+
activity.runOnUiThread(() -> {
42+
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
43+
});
44+
}
45+
tags.add((tag == null ? "" : tag));
46+
}
4447

48+
public static void deactivate(@NotNull final Activity activity, final String tag) {
49+
if (tags.size() == 1 && tags.contains((tag))) {
50+
activity.runOnUiThread(() -> {
51+
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
52+
});
53+
}
54+
tags.remove((tag == null ? "" : tag));
4555
}
4656
}

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
declare namespace RNIdleTimer {
2-
function setIdleTimerDisabled(disabled: boolean): void;
2+
function setIdleTimerDisabled(disabled: boolean, tag: string | undefined): void;
33
}
44
export = RNIdleTimer

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
var { NativeModules } = require('react-native')
44

5-
module.exports = NativeModules.IdleTimerManager;
5+
module.exports.setIdleTimerDisabled = (disabled, tag = "") => {
6+
NativeModules.IdleTimerManager.setIdleTimerDisabled(disabled, tag);
7+
}

ios/RNIdleTimer/IdleTimerManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77

88
@interface IdleTimerManager : NSObject <RCTBridgeModule>
99

10+
+ (void)activate:(NSString*)tag;
11+
+ (void)deactivate:(NSString*)tag;
12+
1013
@end

ios/RNIdleTimer/IdleTimerManager.m

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
11
#import "IdleTimerManager.h"
22

3+
const static NSMutableSet *tags;
4+
35
@implementation IdleTimerManager
46

7+
+ (void) initialize {
8+
static dispatch_once_t onceToken;
9+
dispatch_once(&onceToken, ^{
10+
tags = [NSMutableSet set];
11+
});
12+
}
13+
514
RCT_EXPORT_MODULE();
615

7-
RCT_EXPORT_METHOD(setIdleTimerDisabled:(BOOL)disabled) {
8-
dispatch_async(dispatch_get_main_queue(), ^{
9-
[UIApplication sharedApplication].idleTimerDisabled = disabled;
10-
});
16+
RCT_EXPORT_METHOD(setIdleTimerDisabled:(BOOL)disabled tag:(NSString *)tag) {
17+
if (disabled) {
18+
[IdleTimerManager activate:tag];
19+
} else {
20+
[IdleTimerManager deactivate:tag];
21+
}
22+
}
23+
24+
+ (void)activate:(NSString*)tag {
25+
if ([tags count] == 0) {
26+
dispatch_async(dispatch_get_main_queue(), ^{
27+
[UIApplication sharedApplication].idleTimerDisabled = YES;
28+
});
29+
}
30+
31+
[tags addObject:tag ?: @""];
32+
}
33+
34+
+ (void)deactivate:(NSString*)tag {
35+
if ([tags count] == 1 && [tags containsObject:tag]) {
36+
dispatch_async(dispatch_get_main_queue(), ^{
37+
[UIApplication sharedApplication].idleTimerDisabled = NO;
38+
});
39+
}
40+
41+
[tags removeObject:tag ?: @""];
1142
}
1243

1344
@end

0 commit comments

Comments
 (0)