diff --git a/rxpermission/src/main/java/com/vanniktech/rxpermission/RealRxPermission.java b/rxpermission/src/main/java/com/vanniktech/rxpermission/RealRxPermission.java index e6049a8..0414842 100644 --- a/rxpermission/src/main/java/com/vanniktech/rxpermission/RealRxPermission.java +++ b/rxpermission/src/main/java/com/vanniktech/rxpermission/RealRxPermission.java @@ -37,6 +37,8 @@ public final class RealRxPermission implements RxPermission { static final String PREF_REQUESTED_PERMISSIONS = "requested-permissions"; + static final StringBuffer TRACES = new StringBuffer(); + /** * @param context any context * @return a Singleton instance of this class @@ -44,6 +46,7 @@ public final class RealRxPermission implements RxPermission { public static RealRxPermission getInstance(final Context context) { synchronized (RealRxPermission.class) { if (instance == null) { + TRACES.append("created new instance -> "); instance = new RealRxPermission((Application) context.getApplicationContext()); } } @@ -131,7 +134,10 @@ public static RealRxPermission getInstance(final Context context) { if (subject == null) { unrequestedPermissions.add(permission); subject = PublishSubject.create(); + TRACES.append("putting ").append(permission).append(" -> "); currentPermissionRequests.put(permission, subject); + } else { + TRACES.append("obtained ").append(permission).append(" -> "); } list.add(subject); @@ -190,22 +196,24 @@ void onRequestPermissionsResult(@NonNull final int[] grantResults, @NonNull fina for (int i = 0; i < size; i++) { final String permission = permissions[i]; - final PublishSubject subject = currentPermissionRequests.remove(permission); + final Permission result; - if (subject == null) { - throw new IllegalStateException("RealRxPermission.onRequestPermissionsResult invoked but didn't find the corresponding permission request for " + permission); + if (grantResults[i] == PERMISSION_GRANTED) { + result = Permission.granted(permission); + } else if (!rationale[i] && !rationaleAfter[i]) { + result = Permission.deniedNotShown(permission); + } else { + result = Permission.denied(permission); } - final boolean granted = grantResults[i] == PERMISSION_GRANTED; + TRACES.append("removing ").append(permission).append(" yielindg ").append(result.state().name()).append(" -> "); + final PublishSubject subject = currentPermissionRequests.remove(permission); - if (granted) { - subject.onNext(Permission.granted(permission)); - } else if (!rationale[i] && !rationaleAfter[i]) { - subject.onNext(Permission.deniedNotShown(permission)); - } else { - subject.onNext(Permission.denied(permission)); + if (subject == null) { + throw new IllegalStateException("RealRxPermission.onRequestPermissionsResult invoked but didn't find the corresponding request for " + permission + " debug traces: " + TRACES); } + subject.onNext(result); subject.onComplete(); } } @@ -215,6 +223,7 @@ void onRequestPermissionsResult(@NonNull final int[] grantResults, @NonNull fina } void cancelPermissionsRequests() { + TRACES.append("cancelPermissionsRequests -> "); currentPermissionRequests.clear(); } diff --git a/rxpermission/src/main/java/com/vanniktech/rxpermission/ShadowActivity.java b/rxpermission/src/main/java/com/vanniktech/rxpermission/ShadowActivity.java index b4a0f3e..54e8a28 100644 --- a/rxpermission/src/main/java/com/vanniktech/rxpermission/ShadowActivity.java +++ b/rxpermission/src/main/java/com/vanniktech/rxpermission/ShadowActivity.java @@ -6,6 +6,7 @@ import android.content.Intent; import android.os.Bundle; import io.reactivex.annotations.NonNull; +import java.util.Arrays; import static android.os.Build.VERSION_CODES.M; @@ -28,6 +29,8 @@ static void start(final Context context, final String[] permissions) { if (savedInstanceState == null) { handleIntent(getIntent()); } else { + final Intent intent = getIntent(); + RealRxPermission.TRACES.append("onCreate null, intent=").append(intent != null ? Arrays.toString(intent.getStringArrayExtra(ARG_PERMISSIONS)) : "null"); RealRxPermission.getInstance(this).cancelPermissionsRequests(); finish(); }