From c07ae17962b0fbb757c42e873d9c06cdac3a52ae Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sun, 4 Feb 2024 22:10:12 +0100 Subject: [PATCH] Tweak OPML import (#6906) - Only request storage permission when ContentResolver fails - Easier to read error message --- .../activity/OpmlImportActivity.java | 37 ++++++++++++------- ui/i18n/src/main/res/values/strings.xml | 2 +- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportActivity.java index 10a41057c..3f1c17cdc 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/OpmlImportActivity.java @@ -6,7 +6,9 @@ import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; -import android.os.Environment; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.style.ForegroundColorSpan; import android.util.Log; import android.util.SparseBooleanArray; import android.view.Menu; @@ -45,6 +47,7 @@ import java.io.Reader; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Locale; /** * Activity for Opml Import. @@ -140,15 +143,6 @@ public class OpmlImportActivity extends AppCompatActivity { return; } this.uri = uri; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M - && uri.toString().contains(Environment.getExternalStorageDirectory().toString())) { - int permission = ActivityCompat.checkSelfPermission(this, - android.Manifest.permission.READ_EXTERNAL_STORAGE); - if (permission != PackageManager.PERMISSION_GRANTED) { - requestPermission(); - return; - } - } startImport(); } @@ -244,12 +238,29 @@ public class OpmlImportActivity extends AppCompatActivity { getTitleList()); viewBinding.feedlist.setAdapter(listAdapter); }, e -> { + Log.d(TAG, Log.getStackTraceString(e)); + String message = e.getMessage() == null ? "" : e.getMessage(); + if (message.toLowerCase(Locale.ROOT).contains("permission") + && Build.VERSION.SDK_INT >= 23) { + int permission = ActivityCompat.checkSelfPermission(this, + android.Manifest.permission.READ_EXTERNAL_STORAGE); + if (permission != PackageManager.PERMISSION_GRANTED) { + requestPermission(); + return; + } + } viewBinding.progressBar.setVisibility(View.GONE); MaterialAlertDialogBuilder alert = new MaterialAlertDialogBuilder(this); alert.setTitle(R.string.error_label); - alert.setMessage(getString(R.string.opml_reader_error) + e.getMessage()); - alert.setNeutralButton(android.R.string.ok, (dialog, which) -> dialog.dismiss()); - alert.create().show(); + String userReadable = getString(R.string.opml_reader_error); + String details = e.getMessage(); + String total = userReadable + "\n\n" + details; + SpannableString errorMessage = new SpannableString(total); + errorMessage.setSpan(new ForegroundColorSpan(0x88888888), + userReadable.length(), total.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + alert.setMessage(errorMessage); + alert.setPositiveButton(android.R.string.ok, (dialog, which) -> finish()); + alert.show(); }); } } diff --git a/ui/i18n/src/main/res/values/strings.xml b/ui/i18n/src/main/res/values/strings.xml index ab65b6c41..be9a9dcbc 100644 --- a/ui/i18n/src/main/res/values/strings.xml +++ b/ui/i18n/src/main/res/values/strings.xml @@ -577,7 +577,7 @@ Import AntennaPod database from another device OPML import Import podcast list (OPML) - An error has occurred while reading the OPML document: + An error has occurred while reading the file. Make sure that you have actually selected an OPML file and that the file is valid. No file selected! Select all Deselect all