From 3bef12e7b5a42fb76f2c6b3e9e386ce2217435ea Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Sat, 9 Nov 2019 13:55:31 +0100 Subject: [PATCH] Display snackbars when external storage permission isn't granted --- app/src/main/AndroidManifest.xml | 1 - .../settings/AccountSettingsFragment.java | 120 +++++++++++------- app/src/main/res/values-fr-rFR/strings.xml | 3 + app/src/main/res/values/strings.xml | 3 + 4 files changed, 79 insertions(+), 48 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 33ce2561..82d1a66c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,7 +6,6 @@ - viewModel.insertOPMLFoldersAndFeeds(opml)) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(new DisposableCompletableObserver() { + @Override + public void onComplete() { + dialog.dismiss(); + } + + @Override + public void onError(Throwable e) { + dialog.dismiss(); + + displayErrorMessage(); + } + }); + } + + //endregion + + //region opml export + private void exportAsOPMLFile() { try { String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(); @@ -185,7 +231,8 @@ public class AccountSettingsFragment extends PreferenceFragmentCompat { outputStream.close(); } catch (IOException e) { - e.printStackTrace(); + Log.e(TAG, e.getMessage()); + displayErrorMessage(); } }) .subscribe(new DisposableCompletableObserver() { @@ -196,78 +243,57 @@ public class AccountSettingsFragment extends PreferenceFragmentCompat { @Override public void onError(Throwable e) { - Log.d(TAG, "onError: "); + } }); } catch (Exception e) { - Log.d(TAG, e.getMessage()); + Log.e(TAG, e.getMessage()); + + displayErrorMessage(); } } private boolean isExternalStoragePermissionGranted() { - return ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && - ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; + return ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; } private void requestExternalStoragePermission() { - requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE_REQUEST); - } - - @Override - public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { - if (requestCode == OPEN_OPML_FILE_REQUEST && resultCode == RESULT_OK && data != null) { - Uri uri = data.getData(); - - MaterialDialog dialog = new MaterialDialog.Builder(getActivity()) - .title(R.string.opml_processing) - .content(R.string.operation_takes_time) - .progress(true, 100) - .cancelable(false) - .show(); - - parseOPMLFile(uri, dialog); - } - - super.onActivityResult(requestCode, resultCode, data); + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE_REQUEST); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == WRITE_EXTERNAL_STORAGE_REQUEST) { if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { - //if (shouldShowRequestPermissionRationale(permissions[0])) + if (shouldShowRequestPermissionRationale(permissions[0])) { + Utils.showSnackBarWithAction(getView(), getString(R.string.external_storage_opml_export), + getString(R.string.try_again), v -> requestExternalStoragePermission()); + } else { + Utils.showSnackBarWithAction(getView(), getString(R.string.external_storage_opml_export), + getString(R.string.permissions), v -> { + Intent intent = new Intent(); + intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); + intent.setData(Uri.fromParts("package", getContext().getPackageName(), null)); + getContext().startActivity(intent); + }); + } } else { exportAsOPMLFile(); } } - super.onRequestPermissionsResult(requestCode, permissions, grantResults); } - private void parseOPMLFile(Uri uri, MaterialDialog dialog) { - OPMLParser.parse(uri, getContext()) - .flatMapCompletable(opml -> viewModel.insertOPMLFoldersAndFeeds(opml)) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(new DisposableCompletableObserver() { - @Override - public void onComplete() { - dialog.dismiss(); - } + //endregion - @Override - public void onError(Throwable e) { - dialog.dismiss(); - - new MaterialDialog.Builder(getActivity()) - .title(R.string.processing_file_failed) - .neutralText(R.string.cancel) - .iconRes(R.drawable.ic_error) - .show(); - } - }); + private void displayErrorMessage() { + new MaterialDialog.Builder(getActivity()) + .title(R.string.processing_file_failed) + .neutralText(R.string.cancel) + .iconRes(R.drawable.ic_error) + .show(); } } diff --git a/app/src/main/res/values-fr-rFR/strings.xml b/app/src/main/res/values-fr-rFR/strings.xml index fa1feea9..073a3e82 100644 --- a/app/src/main/res/values-fr-rFR/strings.xml +++ b/app/src/main/res/values-fr-rFR/strings.xml @@ -99,5 +99,8 @@ Une erreur s\'est produite lors du traitement du fichier Importer un fichier OPML Exporter les flux et les dossiers dans un fichier OPML + L\'export des soubscriptions nécessite l\'accès au stockage + Réessayer + Permissions \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4aa16f04..071d49f5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -108,4 +108,7 @@ Import OPML file Export feeds and folders to an OPML file Subscriptions + Subscriptions export needs external storage permission + Try again + Permissions