Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PermissionsAndroid] Prevent app from crashing when getCurrentActivity is null #10351

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
@ReactModule(name = "PermissionsAndroid")
public class PermissionsModule extends ReactContextBaseJavaModule implements PermissionListener {

private static final String ERROR_INVALID_ACTIVITY = "E_INVALID_ACTIVITY";
private final SparseArray<Callback> mCallbacks;
private int mRequestCode = 0;

Expand Down Expand Up @@ -73,7 +74,11 @@ public void shouldShowRequestPermissionRationale(final String permission, final
promise.resolve(false);
return;
}
promise.resolve(getPermissionAwareActivity().shouldShowRequestPermissionRationale(permission));
try {
promise.resolve(getPermissionAwareActivity().shouldShowRequestPermissionRationale(permission));
} catch (IllegalStateException e) {
promise.reject(ERROR_INVALID_ACTIVITY, e);
}
}

/**
Expand All @@ -95,17 +100,22 @@ public void requestPermission(final String permission, final Promise promise) {
return;
}

mCallbacks.put(
try {
PermissionAwareActivity activity = getPermissionAwareActivity();

mCallbacks.put(
mRequestCode, new Callback() {
@Override
public void invoke(Object... args) {
promise.resolve(args[0].equals(PackageManager.PERMISSION_GRANTED));
}
});

PermissionAwareActivity activity = getPermissionAwareActivity();
activity.requestPermissions(new String[]{permission}, mRequestCode, this);
mRequestCode++;
activity.requestPermissions(new String[]{permission}, mRequestCode, this);
mRequestCode++;
} catch (IllegalStateException e) {
promise.reject(ERROR_INVALID_ACTIVITY, e);
}
}

/**
Expand Down