Fix permission loop problem (problem seems to be that application crashes (and is restarted) when requesting two permissions at once)

This commit is contained in:
Petr Bartos 2023-11-15 18:28:20 +01:00 committed by Simon
parent 0aaf76fa3b
commit 283c994bdd

View file

@ -89,7 +89,7 @@ import java.util.Vector;
private static final String APPLICATION = "JKXR"; private static final String APPLICATION = "JKXR";
private int permissionCount = 0; private boolean permissionsGranted = false;
private static final int READ_EXTERNAL_STORAGE_PERMISSION_ID = 1; private static final int READ_EXTERNAL_STORAGE_PERMISSION_ID = 1;
private static final int WRITE_EXTERNAL_STORAGE_PERMISSION_ID = 2; private static final int WRITE_EXTERNAL_STORAGE_PERMISSION_ID = 2;
@ -160,15 +160,8 @@ import java.util.Vector;
GLES3JNIActivity.this, GLES3JNIActivity.this,
new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
WRITE_EXTERNAL_STORAGE_PERMISSION_ID); WRITE_EXTERNAL_STORAGE_PERMISSION_ID);
} } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
else != PackageManager.PERMISSION_GRANTED){
{
permissionCount++;
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions( ActivityCompat.requestPermissions(
GLES3JNIActivity.this, GLES3JNIActivity.this,
new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},
@ -176,10 +169,10 @@ import java.util.Vector;
} }
else else
{ {
permissionCount++; permissionsGranted = true;
} }
if (permissionCount == 2) { if (permissionsGranted) {
// Permissions have already been granted. // Permissions have already been granted.
create(); create();
} }
@ -189,21 +182,13 @@ import java.util.Vector;
@Override @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results) { public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results) {
if (requestCode == READ_EXTERNAL_STORAGE_PERMISSION_ID) { if (requestCode == READ_EXTERNAL_STORAGE_PERMISSION_ID) {
if (results.length > 0 && results[0] == PackageManager.PERMISSION_GRANTED) { if (results.length > 0 && results[0] != PackageManager.PERMISSION_GRANTED) {
permissionCount++;
}
else
{
System.exit(0); System.exit(0);
} }
} }
if (requestCode == WRITE_EXTERNAL_STORAGE_PERMISSION_ID) { if (requestCode == WRITE_EXTERNAL_STORAGE_PERMISSION_ID) {
if (results.length > 0 && results[0] == PackageManager.PERMISSION_GRANTED) { if (results.length > 0 && results[0] != PackageManager.PERMISSION_GRANTED) {
permissionCount++;
}
else
{
System.exit(0); System.exit(0);
} }
} }