Use proper content type and subject when sharing exported HTML/Favorites

This commit is contained in:
cketti 2022-03-12 20:26:01 +01:00
parent 3b47deb705
commit bb175d8045
1 changed files with 35 additions and 19 deletions

View File

@ -15,6 +15,7 @@ import androidx.activity.result.contract.ActivityResultContracts;
import androidx.activity.result.contract.ActivityResultContracts.GetContent; import androidx.activity.result.contract.ActivityResultContracts.GetContent;
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult; import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ShareCompat; import androidx.core.app.ShareCompat;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
@ -101,15 +102,13 @@ public class ImportExportPreferencesFragment extends PreferenceFragmentCompat {
private void setupStorageScreen() { private void setupStorageScreen() {
findPreference(PREF_OPML_EXPORT).setOnPreferenceClickListener( findPreference(PREF_OPML_EXPORT).setOnPreferenceClickListener(
preference -> { preference -> {
openExportPathPicker(CONTENT_TYPE_OPML, dateStampFilename(DEFAULT_OPML_OUTPUT_NAME), openExportPathPicker(Export.OPML, chooseOpmlExportPathLauncher, new OpmlWriter());
chooseOpmlExportPathLauncher, new OpmlWriter());
return true; return true;
} }
); );
findPreference(PREF_HTML_EXPORT).setOnPreferenceClickListener( findPreference(PREF_HTML_EXPORT).setOnPreferenceClickListener(
preference -> { preference -> {
openExportPathPicker(CONTENT_TYPE_HTML, dateStampFilename(DEFAULT_HTML_OUTPUT_NAME), openExportPathPicker(Export.HTML, chooseHtmlExportPathLauncher, new HtmlWriter());
chooseHtmlExportPathLauncher, new HtmlWriter());
return true; return true;
}); });
findPreference(PREF_OPML_IMPORT).setOnPreferenceClickListener( findPreference(PREF_OPML_IMPORT).setOnPreferenceClickListener(
@ -133,13 +132,12 @@ public class ImportExportPreferencesFragment extends PreferenceFragmentCompat {
}); });
findPreference(PREF_FAVORITE_EXPORT).setOnPreferenceClickListener( findPreference(PREF_FAVORITE_EXPORT).setOnPreferenceClickListener(
preference -> { preference -> {
openExportPathPicker(CONTENT_TYPE_HTML, dateStampFilename(DEFAULT_FAVORITES_OUTPUT_NAME), openExportPathPicker(Export.FAVORITES, chooseFavoritesExportPathLauncher, new FavoritesWriter());
chooseFavoritesExportPathLauncher, new FavoritesWriter());
return true; return true;
}); });
} }
private void exportWithWriter(ExportWriter exportWriter, final Uri uri) { private void exportWithWriter(ExportWriter exportWriter, Uri uri, Export exportType) {
Context context = getActivity(); Context context = getActivity();
progressDialog.show(); progressDialog.show();
if (uri == null) { if (uri == null) {
@ -149,7 +147,7 @@ public class ImportExportPreferencesFragment extends PreferenceFragmentCompat {
.subscribe(output -> { .subscribe(output -> {
Uri fileUri = FileProvider.getUriForFile(context.getApplicationContext(), Uri fileUri = FileProvider.getUriForFile(context.getApplicationContext(),
context.getString(R.string.provider_authority), output); context.getString(R.string.provider_authority), output);
showExportSuccessDialog(output.toString(), fileUri); showExportSuccessDialog(output.toString(), fileUri, exportType);
}, this::showExportErrorDialog, progressDialog::dismiss); }, this::showExportErrorDialog, progressDialog::dismiss);
} else { } else {
DocumentFileExportWorker worker = new DocumentFileExportWorker(exportWriter, context, uri); DocumentFileExportWorker worker = new DocumentFileExportWorker(exportWriter, context, uri);
@ -157,7 +155,7 @@ public class ImportExportPreferencesFragment extends PreferenceFragmentCompat {
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(output -> .subscribe(output ->
showExportSuccessDialog(output.getUri().toString(), output.getUri()), showExportSuccessDialog(output.getUri().toString(), output.getUri(), exportType),
this::showExportErrorDialog, progressDialog::dismiss); this::showExportErrorDialog, progressDialog::dismiss);
} }
} }
@ -193,15 +191,15 @@ public class ImportExportPreferencesFragment extends PreferenceFragmentCompat {
builder.show(); builder.show();
} }
private void showExportSuccessDialog(final String path, final Uri streamUri) { private void showExportSuccessDialog(String path, Uri streamUri, Export exportType) {
final AlertDialog.Builder alert = new AlertDialog.Builder(getContext()); final AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
alert.setNeutralButton(android.R.string.ok, (dialog, which) -> dialog.dismiss()); alert.setNeutralButton(android.R.string.ok, (dialog, which) -> dialog.dismiss());
alert.setTitle(R.string.export_success_title); alert.setTitle(R.string.export_success_title);
alert.setMessage(getContext().getString(R.string.export_success_sum, path)); alert.setMessage(getContext().getString(R.string.export_success_sum, path));
alert.setPositiveButton(R.string.send_label, (dialog, which) -> { alert.setPositiveButton(R.string.send_label, (dialog, which) -> {
new ShareCompat.IntentBuilder(getContext()) new ShareCompat.IntentBuilder(getContext())
.setType("text/*") .setType(exportType.contentType)
.setSubject(getString(R.string.opml_export_label)) .setSubject(getString(exportType.labelResId))
.addStream(streamUri) .addStream(streamUri)
.setChooserTitle(R.string.send_label) .setChooserTitle(R.string.send_label)
.startChooser(); .startChooser();
@ -223,7 +221,7 @@ public class ImportExportPreferencesFragment extends PreferenceFragmentCompat {
return; return;
} }
final Uri uri = result.getData().getData(); final Uri uri = result.getData().getData();
exportWithWriter(new OpmlWriter(), uri); exportWithWriter(new OpmlWriter(), uri, Export.OPML);
} }
private void chooseHtmlExportPathResult(final ActivityResult result) { private void chooseHtmlExportPathResult(final ActivityResult result) {
@ -231,7 +229,7 @@ public class ImportExportPreferencesFragment extends PreferenceFragmentCompat {
return; return;
} }
final Uri uri = result.getData().getData(); final Uri uri = result.getData().getData();
exportWithWriter(new HtmlWriter(), uri); exportWithWriter(new HtmlWriter(), uri, Export.HTML);
} }
private void chooseFavoritesExportPathResult(final ActivityResult result) { private void chooseFavoritesExportPathResult(final ActivityResult result) {
@ -239,7 +237,7 @@ public class ImportExportPreferencesFragment extends PreferenceFragmentCompat {
return; return;
} }
final Uri uri = result.getData().getData(); final Uri uri = result.getData().getData();
exportWithWriter(new FavoritesWriter(), uri); exportWithWriter(new FavoritesWriter(), uri, Export.FAVORITES);
} }
private void restoreDatabaseResult(final ActivityResult result) { private void restoreDatabaseResult(final ActivityResult result) {
@ -280,11 +278,12 @@ public class ImportExportPreferencesFragment extends PreferenceFragmentCompat {
startActivity(intent); startActivity(intent);
} }
private void openExportPathPicker(String contentType, String title, private void openExportPathPicker(Export exportType, ActivityResultLauncher<Intent> result, ExportWriter writer) {
final ActivityResultLauncher<Intent> result, ExportWriter writer) { String title = dateStampFilename(exportType.outputNameTemplate);
Intent intentPickAction = new Intent(Intent.ACTION_CREATE_DOCUMENT) Intent intentPickAction = new Intent(Intent.ACTION_CREATE_DOCUMENT)
.addCategory(Intent.CATEGORY_OPENABLE) .addCategory(Intent.CATEGORY_OPENABLE)
.setType(contentType) .setType(exportType.contentType)
.putExtra(Intent.EXTRA_TITLE, title); .putExtra(Intent.EXTRA_TITLE, title);
// Creates an implicit intent to launch a file manager which lets // Creates an implicit intent to launch a file manager which lets
@ -298,7 +297,7 @@ public class ImportExportPreferencesFragment extends PreferenceFragmentCompat {
// If we are using a SDK lower than API 21 or the implicit intent failed // If we are using a SDK lower than API 21 or the implicit intent failed
// fallback to the legacy export process // fallback to the legacy export process
exportWithWriter(writer, null); exportWithWriter(writer, null, exportType);
} }
private static class BackupDatabase extends ActivityResultContracts.CreateDocument { private static class BackupDatabase extends ActivityResultContracts.CreateDocument {
@ -310,4 +309,21 @@ public class ImportExportPreferencesFragment extends PreferenceFragmentCompat {
.setType("application/x-sqlite3"); .setType("application/x-sqlite3");
} }
} }
private enum Export {
OPML(CONTENT_TYPE_OPML, DEFAULT_OPML_OUTPUT_NAME, R.string.opml_export_label),
HTML(CONTENT_TYPE_HTML, DEFAULT_HTML_OUTPUT_NAME, R.string.html_export_label),
FAVORITES(CONTENT_TYPE_HTML, DEFAULT_FAVORITES_OUTPUT_NAME, R.string.favorites_export_label);
final String contentType;
final String outputNameTemplate;
@StringRes
final int labelResId;
Export(String contentType, String outputNameTemplate, int labelResId) {
this.contentType = contentType;
this.outputNameTemplate = outputNameTemplate;
this.labelResId = labelResId;
}
}
} }