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>
This commit is contained in:
heath-clink 2024-03-08 12:10:30 -06:00 committed by GitHub
parent 82e707ee80
commit e34ac0d1f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 8 deletions

View File

@ -29,10 +29,12 @@ public class IdleTimerManager extends ReactContextBaseJavaModule
@ReactMethod
public void setIdleTimerDisabled(final boolean disabled, final String tag) {
final Activity activity = this.getCurrentActivity();
if (disabled) {
activate(activity, tag);
} else {
deactivate(activity, tag);
if (activity != null) {
if (disabled) {
activate(activity, tag);
} else {
deactivate(activity, tag);
}
}
}
@ -42,15 +44,15 @@ public class IdleTimerManager extends ReactContextBaseJavaModule
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
});
}
tags.add((tag == null ? "" : tag));
tags.add(tag == null ? "" : tag);
}
public static void deactivate(@NotNull final Activity activity, final String tag) {
if (tags.size() == 1 && tags.contains((tag))) {
if (tags.size() == 1 && tags.contains(tag == null ? "" : tag)) {
activity.runOnUiThread(() -> {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
});
}
tags.remove((tag == null ? "" : tag));
tags.remove(tag == null ? "" : tag);
}
}

View File

@ -32,7 +32,7 @@ RCT_EXPORT_METHOD(setIdleTimerDisabled:(BOOL)disabled tag:(NSString *)tag) {
}
+ (void)deactivate:(NSString*)tag {
if ([tags count] == 1 && [tags containsObject:tag]) {
if ([tags count] == 1 && [tags containsObject:tag ?: @""]) {
dispatch_async(dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].idleTimerDisabled = NO;
});