From 17c9a97a4101c6c05c2a596d4604d0379a01a8a3 Mon Sep 17 00:00:00 2001 From: devgianlu Date: Fri, 6 Oct 2023 16:11:20 +0200 Subject: [PATCH] Skip asking READ/WRITE_EXTERNAL_STORAGE permission for Android >= 13 --- CommonUtils | 2 +- .../com/gianlu/aria2app/LoadingActivity.java | 11 ++++- .../aria2app/activities/BatchAddActivity.java | 11 ++++- .../adddownload/Base64Fragment.java | 11 ++++- .../editprofile/CertificateInputView.java | 11 ++++- .../editprofile/DirectDownloadFragment.java | 41 +++++++++++-------- .../gianlu/aria2app/main/MainActivity.java | 11 ++++- .../aria2app/options/OptionsDialog.java | 12 ++++-- 8 files changed, 79 insertions(+), 31 deletions(-) diff --git a/CommonUtils b/CommonUtils index 36cbb597c..90a3d8781 160000 --- a/CommonUtils +++ b/CommonUtils @@ -1 +1 @@ -Subproject commit 36cbb597cbd9b643cbba9cc9d7e0fb3bb2ca6559 +Subproject commit 90a3d87817e188b8cc9480067a16225d32e2d8fb diff --git a/app/src/main/java/com/gianlu/aria2app/LoadingActivity.java b/app/src/main/java/com/gianlu/aria2app/LoadingActivity.java index 10f4e2a76..ece1f8306 100644 --- a/app/src/main/java/com/gianlu/aria2app/LoadingActivity.java +++ b/app/src/main/java/com/gianlu/aria2app/LoadingActivity.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Looper; @@ -201,7 +202,7 @@ protected void onCreate(Bundle savedInstanceState) { } private void connectToInAppDownloader(@NonNull MultiProfile profile) { - AskPermission.ask(this, Manifest.permission.WRITE_EXTERNAL_STORAGE, new AskPermission.Listener() { + AskPermission.Listener listener = new AskPermission.Listener() { @Override public void permissionGranted(@NonNull String permission) { connecting.setVisibility(View.VISIBLE); @@ -241,7 +242,13 @@ public void askRationale(@NonNull AlertDialog.Builder builder) { builder.setTitle(R.string.writeExternalStorageRequest_title) .setMessage(R.string.writeExternalStorageRequest_message); } - }); + }; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + listener.permissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE); + return; + } + + AskPermission.ask(this, Manifest.permission.WRITE_EXTERNAL_STORAGE, listener); } @Override diff --git a/app/src/main/java/com/gianlu/aria2app/activities/BatchAddActivity.java b/app/src/main/java/com/gianlu/aria2app/activities/BatchAddActivity.java index 986a9cca7..b5820d8bf 100644 --- a/app/src/main/java/com/gianlu/aria2app/activities/BatchAddActivity.java +++ b/app/src/main/java/com/gianlu/aria2app/activities/BatchAddActivity.java @@ -4,6 +4,7 @@ import android.content.ClipData; import android.content.Intent; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.Menu; @@ -130,7 +131,7 @@ public void onException(@NonNull Exception ex) { } private void openDocument(@NonNull final String mime, @NonNull final String text, final int requestCode) { - AskPermission.ask(this, Manifest.permission.READ_EXTERNAL_STORAGE, new AskPermission.Listener() { + AskPermission.Listener listener = new AskPermission.Listener() { @Override public void permissionGranted(@NonNull String permission) { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); @@ -150,7 +151,13 @@ public void askRationale(@NonNull AlertDialog.Builder builder) { builder.setTitle(R.string.readExternalStorageRequest_title) .setMessage(R.string.readExternalStorageRequest_base64Message); } - }); + }; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + listener.permissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE); + return; + } + + AskPermission.ask(this, Manifest.permission.READ_EXTERNAL_STORAGE, listener); } @Override diff --git a/app/src/main/java/com/gianlu/aria2app/activities/adddownload/Base64Fragment.java b/app/src/main/java/com/gianlu/aria2app/activities/adddownload/Base64Fragment.java index 6392da8ca..f57248181 100644 --- a/app/src/main/java/com/gianlu/aria2app/activities/adddownload/Base64Fragment.java +++ b/app/src/main/java/com/gianlu/aria2app/activities/adddownload/Base64Fragment.java @@ -5,6 +5,7 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; @@ -109,7 +110,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c pick.setOnClickListener(v -> { if (getActivity() == null) return; - AskPermission.ask(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE, new AskPermission.Listener() { + AskPermission.Listener listener = new AskPermission.Listener() { @Override public void permissionGranted(@NonNull String permission) { showFilePicker(); @@ -125,7 +126,13 @@ public void askRationale(@NonNull AlertDialog.Builder builder) { builder.setTitle(R.string.readExternalStorageRequest_title) .setMessage(R.string.readExternalStorageRequest_base64Message); } - }); + }; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + listener.permissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE); + return; + } + + AskPermission.ask(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE, listener); }); SuperTextView help = layout.findViewById(R.id.base64Fragment_help); diff --git a/app/src/main/java/com/gianlu/aria2app/activities/editprofile/CertificateInputView.java b/app/src/main/java/com/gianlu/aria2app/activities/editprofile/CertificateInputView.java index 4384d549a..0d1139f47 100644 --- a/app/src/main/java/com/gianlu/aria2app/activities/editprofile/CertificateInputView.java +++ b/app/src/main/java/com/gianlu/aria2app/activities/editprofile/CertificateInputView.java @@ -5,6 +5,7 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.util.AttributeSet; import android.util.Log; @@ -129,7 +130,7 @@ void showPicker() { final FragmentActivity activity = activityProvider.getActivity(); if (activity == null) return; - AskPermission.ask(activity, Manifest.permission.READ_EXTERNAL_STORAGE, new AskPermission.Listener() { + AskPermission.Listener listener = new AskPermission.Listener() { @Override public void permissionGranted(@NonNull String permission) { if (activityProvider instanceof Fragment) { @@ -152,7 +153,13 @@ public void askRationale(@NonNull AlertDialog.Builder builder) { builder.setTitle(R.string.readExternalStorageRequest_title) .setMessage(R.string.readExternalStorageRequest_certMessage); } - }); + }; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + listener.permissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE); + return; + } + + AskPermission.ask(activity, Manifest.permission.READ_EXTERNAL_STORAGE, listener); } void showCertificateDetails(@NonNull X509Certificate certificate) { diff --git a/app/src/main/java/com/gianlu/aria2app/activities/editprofile/DirectDownloadFragment.java b/app/src/main/java/com/gianlu/aria2app/activities/editprofile/DirectDownloadFragment.java index 572fa225a..b08a08780 100644 --- a/app/src/main/java/com/gianlu/aria2app/activities/editprofile/DirectDownloadFragment.java +++ b/app/src/main/java/com/gianlu/aria2app/activities/editprofile/DirectDownloadFragment.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -453,24 +454,30 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup p enableDirectDownload = layout.findViewById(R.id.editProfile_enableDirectDownload); enableDirectDownload.setOnCheckedChangeListener((buttonView, isChecked) -> { container.setVisibility(isChecked ? View.VISIBLE : View.GONE); - if (isChecked && getActivity() != null) { - AskPermission.ask(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, new AskPermission.Listener() { - @Override - public void permissionGranted(@NonNull String permission) { - } - - @Override - public void permissionDenied(@NonNull String permission) { - showToast(Toaster.build().message(R.string.writePermissionDenied)); - } - - @Override - public void askRationale(@NonNull AlertDialog.Builder builder) { - builder.setTitle(R.string.writeExternalStorageRequest_title) - .setMessage(R.string.writeExternalStorageRequest_message); - } - }); + if (!isChecked || getActivity() == null) return; + + AskPermission.Listener listener = new AskPermission.Listener() { + @Override + public void permissionGranted(@NonNull String permission) { + } + + @Override + public void permissionDenied(@NonNull String permission) { + showToast(Toaster.build().message(R.string.writePermissionDenied)); + } + + @Override + public void askRationale(@NonNull AlertDialog.Builder builder) { + builder.setTitle(R.string.writeExternalStorageRequest_title) + .setMessage(R.string.writeExternalStorageRequest_message); + } + }; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + listener.permissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE); + return; } + + AskPermission.ask(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, listener); }); encryptionContainer = layout.findViewById(R.id.editProfile_dd_encryption); diff --git a/app/src/main/java/com/gianlu/aria2app/main/MainActivity.java b/app/src/main/java/com/gianlu/aria2app/main/MainActivity.java index f3178bf35..2b7b18d4c 100644 --- a/app/src/main/java/com/gianlu/aria2app/main/MainActivity.java +++ b/app/src/main/java/com/gianlu/aria2app/main/MainActivity.java @@ -7,6 +7,7 @@ import android.content.pm.PackageManager; import android.content.res.Configuration; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.Menu; @@ -419,7 +420,7 @@ protected void onPostCreate() { if ("magnet".equals(scheme) || "http".equals(scheme) || "https".equals(scheme) || "ftp".equals(scheme) || "sftp".equals(scheme)) { processUrl(shareData); } else { - AskPermission.ask(this, Manifest.permission.READ_EXTERNAL_STORAGE, new AskPermission.Listener() { + AskPermission.Listener listener = new AskPermission.Listener() { @Override public void permissionGranted(@NonNull String permission) { processFileUri(shareData); @@ -437,7 +438,13 @@ public void askRationale(@NonNull AlertDialog.Builder builder) { builder.setTitle(R.string.readExternalStorageRequest_title) .setMessage(R.string.readExternalStorageRequest_base64Message); } - }); + }; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + listener.permissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE); + return; + } + + AskPermission.ask(this, Manifest.permission.READ_EXTERNAL_STORAGE, listener); } } } diff --git a/app/src/main/java/com/gianlu/aria2app/options/OptionsDialog.java b/app/src/main/java/com/gianlu/aria2app/options/OptionsDialog.java index fa91bb651..04f138b8f 100644 --- a/app/src/main/java/com/gianlu/aria2app/options/OptionsDialog.java +++ b/app/src/main/java/com/gianlu/aria2app/options/OptionsDialog.java @@ -3,6 +3,7 @@ import android.Manifest; import android.app.Dialog; import android.app.ProgressDialog; +import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.util.Log; @@ -223,8 +224,7 @@ private void doExportOptions() { private void export() { if (getActivity() == null) return; - - AskPermission.ask(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, new AskPermission.Listener() { + AskPermission.Listener listener = new AskPermission.Listener() { @Override public void permissionGranted(@NonNull String permission) { doExportOptions(); @@ -240,7 +240,13 @@ public void askRationale(@NonNull AlertDialog.Builder builder) { builder.setTitle(R.string.writeExternalStorageRequest_title) .setMessage(R.string.exportOptionsGrantWrite); } - }); + }; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + listener.permissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE); + return; + } + + AskPermission.ask(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, listener); } @Override