Skip to content

Commit e34ac0d

Browse files
fix for android crash (#35)
* fix for android crash For some reason, a recent commit removed the check for a null return from `getCurrentActivity()`. But since `getCurrentActivity()` might return null, the app will crash with a NullPointerException. This restores the null check. It also fixes inconsistent handling of null `tag` argument in both iOS and Android. This does not affect usage from JS because the JS interface will not pass null to that argument; however other native code could still call these methods and pass null for `tag`. * remove unnecessary parens --------- Co-authored-by: Marc Shilling <marcshilling@gmail.com>
1 parent 82e707e commit e34ac0d

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

android/src/main/java/com/marcshilling/idletimer/IdleTimerManager.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ public String getName() {
2929
@ReactMethod
3030
public void setIdleTimerDisabled(final boolean disabled, final String tag) {
3131
final Activity activity = this.getCurrentActivity();
32-
if (disabled) {
33-
activate(activity, tag);
34-
} else {
35-
deactivate(activity, tag);
32+
if (activity != null) {
33+
if (disabled) {
34+
activate(activity, tag);
35+
} else {
36+
deactivate(activity, tag);
37+
}
3638
}
3739
}
3840

@@ -42,15 +44,15 @@ public static void activate(@NotNull final Activity activity, final String tag)
4244
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
4345
});
4446
}
45-
tags.add((tag == null ? "" : tag));
47+
tags.add(tag == null ? "" : tag);
4648
}
4749

4850
public static void deactivate(@NotNull final Activity activity, final String tag) {
49-
if (tags.size() == 1 && tags.contains((tag))) {
51+
if (tags.size() == 1 && tags.contains(tag == null ? "" : tag)) {
5052
activity.runOnUiThread(() -> {
5153
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
5254
});
5355
}
54-
tags.remove((tag == null ? "" : tag));
56+
tags.remove(tag == null ? "" : tag);
5557
}
5658
}

ios/RNIdleTimer/IdleTimerManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ + (void)activate:(NSString*)tag {
3232
}
3333

3434
+ (void)deactivate:(NSString*)tag {
35-
if ([tags count] == 1 && [tags containsObject:tag]) {
35+
if ([tags count] == 1 && [tags containsObject:tag ?: @""]) {
3636
dispatch_async(dispatch_get_main_queue(), ^{
3737
[UIApplication sharedApplication].idleTimerDisabled = NO;
3838
});

0 commit comments

Comments
 (0)