Merge pull request #9061 from Ebola16/Fixes3

Android: Improve WRITE_EXTERNAL_STORAGE denial
This commit is contained in:
Léo Lam 2020-10-20 02:15:08 +02:00 committed by GitHub
commit 409230e088
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 38 deletions

View File

@ -213,6 +213,8 @@ public final class MainActivity extends AppCompatActivity implements MainView
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) @NonNull int[] grantResults)
{ {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PermissionsHandler.REQUEST_CODE_WRITE_PERMISSION) if (requestCode == PermissionsHandler.REQUEST_CODE_WRITE_PERMISSION)
{ {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
@ -223,13 +225,9 @@ public final class MainActivity extends AppCompatActivity implements MainView
} }
else else
{ {
Toast.makeText(this, R.string.write_permission_needed, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.write_permission_needed, Toast.LENGTH_LONG).show();
} }
} }
else
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
} }
/** /**

View File

@ -225,6 +225,8 @@ public final class TvMainActivity extends FragmentActivity implements MainView
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) @NonNull int[] grantResults)
{ {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PermissionsHandler.REQUEST_CODE_WRITE_PERMISSION) if (requestCode == PermissionsHandler.REQUEST_CODE_WRITE_PERMISSION)
{ {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
@ -234,14 +236,9 @@ public final class TvMainActivity extends FragmentActivity implements MainView
} }
else else
{ {
Toast.makeText(this, R.string.write_permission_needed, Toast.LENGTH_SHORT) Toast.makeText(this, R.string.write_permission_needed, Toast.LENGTH_LONG).show();
.show();
} }
} }
else
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
} }
private void buildRowsAdapter() private void buildRowsAdapter()

View File

@ -114,11 +114,11 @@ public class AfterDirectoryInitializationRunner
switch (state) switch (state)
{ {
case EXTERNAL_STORAGE_PERMISSION_NEEDED: case EXTERNAL_STORAGE_PERMISSION_NEEDED:
Toast.makeText(context, R.string.write_permission_needed, Toast.LENGTH_SHORT).show(); Toast.makeText(context, R.string.write_permission_needed, Toast.LENGTH_LONG).show();
return true; return true;
case CANT_FIND_EXTERNAL_STORAGE: case CANT_FIND_EXTERNAL_STORAGE:
Toast.makeText(context, R.string.external_storage_not_mounted, Toast.LENGTH_SHORT).show(); Toast.makeText(context, R.string.external_storage_not_mounted, Toast.LENGTH_LONG).show();
return true; return true;
default: default:

View File

@ -2,17 +2,12 @@ package org.dolphinemu.dolphinemu.utils;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Build; import android.os.Build;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import org.dolphinemu.dolphinemu.R;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
public class PermissionsHandler public class PermissionsHandler
@ -31,14 +26,9 @@ public class PermissionsHandler
if (hasWritePermission != PackageManager.PERMISSION_GRANTED) if (hasWritePermission != PackageManager.PERMISSION_GRANTED)
{ {
if (activity.shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE)) // We only care about displaying the "Don't ask again" check and can ignore the result.
{ // Previous toasts already explained the rationale.
showMessageOKCancel(activity, activity.getString(R.string.write_permission_needed), activity.shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE);
(dialog, which) -> activity.requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_WRITE_PERMISSION));
return false;
}
activity.requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE}, activity.requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_WRITE_PERMISSION); REQUEST_CODE_WRITE_PERMISSION);
return false; return false;
@ -57,16 +47,4 @@ public class PermissionsHandler
return true; return true;
} }
private static void showMessageOKCancel(final FragmentActivity activity, String message,
DialogInterface.OnClickListener okListener)
{
new AlertDialog.Builder(activity, R.style.DolphinDialogBase)
.setMessage(message)
.setPositiveButton(android.R.string.ok, okListener)
.setNegativeButton(android.R.string.cancel, (dialogInterface, i) ->
Toast.makeText(activity, R.string.write_permission_needed, Toast.LENGTH_SHORT)
.show())
.show();
}
} }