Skip to content

Commit

Permalink
RN: Silently Fail onRequestPermissionsResult for Invalid Activities (
Browse files Browse the repository at this point in the history
…#37047)

Summary:
Pull Request resolved: #37047

We're seeing scenarios where `onRequestPermissionsResult` is being invoked but the current activity does not implement `PermissionAwareActivity`. This should not crash the app.

Changelog:
[Android][Fixed] - Fix crash when Android requests permission with activity that does not implement `PermissionAwareActivity`

Reviewed By: mdvacca

Differential Revision: D45203319

fbshipit-source-id: 4a6eb67ad67ae5c731c27301967b837b1286ccbb
  • Loading branch information
yungsters authored and facebook-github-bot committed Apr 22, 2023
1 parent 555014a commit cff4bc8
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.os.Build;
import android.os.Process;
import android.util.SparseArray;
import com.facebook.common.logging.FLog;
import com.facebook.fbreact.specs.NativePermissionsAndroidSpec;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
Expand Down Expand Up @@ -198,9 +199,17 @@ public void invoke(Object... args) {
@Override
public boolean onRequestPermissionsResult(
int requestCode, String[] permissions, int[] grantResults) {
mCallbacks.get(requestCode).invoke(grantResults, getPermissionAwareActivity());
mCallbacks.remove(requestCode);
return mCallbacks.size() == 0;
try {
mCallbacks.get(requestCode).invoke(grantResults, getPermissionAwareActivity());
mCallbacks.remove(requestCode);
return mCallbacks.size() == 0;
} catch (IllegalStateException e) {
FLog.e(
"PermissionsModule",
e,
"Unexpected invocation of `onRequestPermissionsResult` with invalid current activity");
return false;
}
}

private PermissionAwareActivity getPermissionAwareActivity() {
Expand Down

0 comments on commit cff4bc8

Please sign in to comment.