Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
Merge pull request #44 from Karumi/fix-race-condition
Browse files Browse the repository at this point in the history
Fix race condition
  • Loading branch information
Serchinastico committed Jan 22, 2016
2 parents 60d037e + e717e9c commit 02d02dc
Showing 1 changed file with 61 additions and 23 deletions.
84 changes: 61 additions & 23 deletions dexter/src/main/java/com/karumi/dexter/DexterInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ final class DexterInstance {
private final MultiplePermissionsReport multiplePermissionsReport;
private final AtomicBoolean isRequestingPermission;
private final AtomicBoolean rationaleAccepted;
private final Object pendingPermissionsMutex = new Object();

private Activity activity;
private MultiplePermissionsListener listener = EMPTY_LISTENER;

Expand Down Expand Up @@ -118,24 +120,18 @@ void continuePendingRequestsIfPossible(MultiplePermissionsListener listener,
*/
void onActivityReady(Activity activity) {
this.activity = activity;
Collection<String> deniedRequests = new LinkedList<>();
Collection<String> grantedRequests = new LinkedList<>();

for (String permission : pendingPermissions) {
int permissionState = androidPermissionService.checkSelfPermission(activity, permission);
switch (permissionState) {
case PackageManager.PERMISSION_DENIED:
deniedRequests.add(permission);
break;
case PackageManager.PERMISSION_GRANTED:
default:
grantedRequests.add(permission);
break;
PermissionStates permissionStates = null;
synchronized (pendingPermissionsMutex) {
if (activity != null) {
permissionStates = getPermissionStates(pendingPermissions);
}
}

handleDeniedPermissions(deniedRequests);
updatePermissionsAsGranted(grantedRequests);
if (permissionStates != null) {
handleDeniedPermissions(permissionStates.getDeniedPermissions());
updatePermissionsAsGranted(permissionStates.getGrantedPermissions());
}
}

/**
Expand Down Expand Up @@ -187,6 +183,25 @@ void requestPermissionsToSystem(Collection<String> permissions) {
permissions.toArray(new String[permissions.size()]), PERMISSIONS_REQUEST_CODE);
}

private PermissionStates getPermissionStates(Collection<String> pendingPermissions) {
PermissionStates permissionStates = new PermissionStates();

for (String permission : pendingPermissions) {
int permissionState = androidPermissionService.checkSelfPermission(activity, permission);
switch (permissionState) {
case PackageManager.PERMISSION_DENIED:
permissionStates.addDeniedPermission(permission);
break;
case PackageManager.PERMISSION_GRANTED:
default:
permissionStates.addGrantedPermission(permission);
break;
}
}

return permissionStates;
}

private void startTransparentActivityIfNeeded() {
Intent intent = intentProvider.get(context, DexterActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Expand Down Expand Up @@ -237,15 +252,17 @@ private void onPermissionsChecked(Collection<String> permissions) {
return;
}

pendingPermissions.removeAll(permissions);
if (pendingPermissions.isEmpty()) {
activity.finish();
activity = null;
isRequestingPermission.set(false);
rationaleAccepted.set(false);
MultiplePermissionsListener currentListener = listener;
listener = EMPTY_LISTENER;
currentListener.onPermissionsChecked(multiplePermissionsReport);
synchronized (pendingPermissionsMutex) {
pendingPermissions.removeAll(permissions);
if (pendingPermissions.isEmpty()) {
activity.finish();
activity = null;
isRequestingPermission.set(false);
rationaleAccepted.set(false);
MultiplePermissionsListener currentListener = listener;
listener = EMPTY_LISTENER;
currentListener.onPermissionsChecked(multiplePermissionsReport);
}
}
}

Expand Down Expand Up @@ -281,4 +298,25 @@ private void checkMultiplePermissions(MultiplePermissionsListener listener,
startTransparentActivityIfNeeded();
thread.loop();
}

private final class PermissionStates {
private final Collection<String> deniedPermissions = new LinkedList<>();
private final Collection<String> grantedPermissions = new LinkedList<>();

private void addDeniedPermission(String permission) {
deniedPermissions.add(permission);
}

private void addGrantedPermission(String permission) {
grantedPermissions.add(permission);
}

private Collection<String> getDeniedPermissions() {
return deniedPermissions;
}

private Collection<String> getGrantedPermissions() {
return grantedPermissions;
}
}
}

0 comments on commit 02d02dc

Please sign in to comment.