Skip to content

Commit

Permalink
Prevent app from crashing when getCurrentActivity is null
Browse files Browse the repository at this point in the history
Summary:
We're seeing a lot of crashes from `PermissionsModule` not being able to access the current activity, mentioned in facebook#10009 and here: facebook#9310 (comment)

As far as I can tell, there is no way to ensure the Activity exists since the `ReactContext` holds a `WeakReference` to the current Activity and it appears that the lifecycle calls are happening in the right order (so not the same as facebook#9310).

This will at least allow people to catch the error in JS and update the UI or try again as opposed to crashing the app.

I'm working on some bigger changes in facebook#10221 but this is a smaller change and important to get fixed I think.
Closes facebook#10351

Differential Revision: D4010242

fbshipit-source-id: 7a76973bb2b3e45817d4283917740c89a10ec0b0
  • Loading branch information
cmcewen authored and grabbou committed Oct 25, 2016
1 parent 2958676 commit 486f3b0
Showing 1 changed file with 15 additions and 5 deletions.
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

0 comments on commit 486f3b0

Please sign in to comment.