Skip to content

Commit

Permalink
[Fix] Android: asking permission would not work until app restart (re…
Browse files Browse the repository at this point in the history
…gression 1.32.13)
  • Loading branch information
chipweinberger committed Oct 7, 2024
1 parent 4ed3729 commit 089b8ae
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1516,22 +1516,31 @@ public boolean onActivityResult(int requestCode, int resultCode, Intent data)

@Override
public boolean onRequestPermissionsResult(int requestCode,
String[] permissions,
int[] grantResults)
{
String[] permissions,
int[] grantResults) {
OperationOnPermission operation = operationsOnPermission.get(requestCode);
operationsOnPermission.remove(requestCode); // Clean up to prevent memory leaks

if (operation != null && grantResults.length > 0) {
operation.op(grantResults[0] == PackageManager.PERMISSION_GRANTED, permissions[0]);
return true;
boolean allGranted = true;
String deniedPermission = null;
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
allGranted = false;
deniedPermission = permissions[i];
break;
}
}
operation.op(allGranted, deniedPermission);
return true; // permission result was handled
} else {
return false;
operation.op(false, null);
return false;
}
}

private void ensurePermissions(List<String> permissions, OperationOnPermission operation)
{
// only request permission we don't already have
private void ensurePermissions(List<String> permissions, OperationOnPermission operation) {
// Filter out permissions that are already granted
List<String> permissionsNeeded = new ArrayList<>();
for (String permission : permissions) {
if (permission != null && ContextCompat.checkSelfPermission(context, permission)
Expand All @@ -1540,7 +1549,7 @@ private void ensurePermissions(List<String> permissions, OperationOnPermission o
}
}

// no work to do?
// If all permissions are granted, proceed with the operation
if (permissionsNeeded.isEmpty()) {
operation.op(true, null);
return;
Expand All @@ -1549,14 +1558,15 @@ private void ensurePermissions(List<String> permissions, OperationOnPermission o
askPermission(permissionsNeeded, operation);
}

private void askPermission(List<String> permissionsNeeded, OperationOnPermission operation)
{
// finished asking for permission? call callback
private void askPermission(List<String> permissionsNeeded, OperationOnPermission operation) {
if (permissionsNeeded.isEmpty()) {
operation.op(true, null);
return;
}

// Store the operation with the current request code
operationsOnPermission.put(lastEventId, operation);

ActivityCompat.requestPermissions(
activityBinding.getActivity(),
permissionsNeeded.toArray(new String[0]),
Expand All @@ -1565,6 +1575,7 @@ private void askPermission(List<String> permissionsNeeded, OperationOnPermission
lastEventId++;
}


//////////////////////////////////////////////
// ██████ ██ ███████
// ██ ██ ██ ██
Expand Down

0 comments on commit 089b8ae

Please sign in to comment.