Tweak OPML import (#6906)

- Only request storage permission when ContentResolver fails
- Easier to read error message
This commit is contained in:
ByteHamster 2024-02-04 22:10:12 +01:00 committed by GitHub
parent 0f5600932d
commit c07ae17962
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 14 deletions

View File

@ -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();
});
}
}

View File

@ -577,7 +577,7 @@
<string name="database_import_summary">Import AntennaPod database from another device</string>
<string name="opml_import_label">OPML import</string>
<string name="opml_add_podcast_label">Import podcast list (OPML)</string>
<string name="opml_reader_error">An error has occurred while reading the OPML document:</string>
<string name="opml_reader_error">An error has occurred while reading the file. Make sure that you have actually selected an OPML file and that the file is valid.</string>
<string name="opml_import_error_no_file">No file selected!</string>
<string name="select_all_label">Select all</string>
<string name="deselect_all_label">Deselect all</string>