From db3bea296122874301d676060359242bba9957d7 Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Mon, 18 May 2020 14:59:59 +0200 Subject: [PATCH] Check if any system app supports a file type before opening --- .../vrbrowser/ui/widgets/WindowWidget.java | 56 +++++++++++-------- app/src/main/res/values/strings.xml | 4 ++ 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java index 4b933ec53..4011453ca 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java @@ -9,6 +9,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.Intent; +import android.content.pm.PackageManager; import android.content.res.Configuration; import android.graphics.Canvas; import android.graphics.Matrix; @@ -1664,30 +1665,39 @@ public void onExternalResponse(@NonNull GeckoSession geckoSession, @NonNull Geck startDownload(job, true); } else { - showConfirmPrompt(getResources().getString(R.string.download_open_file_unsupported_title), - getResources().getString(R.string.download_open_file_unsupported_body), - new String[]{ - getResources().getString(R.string.download_open_file_unsupported_cancel), - getResources().getString(R.string.download_open_file_unsupported_open) - }, (index, isChecked) -> { - if (index == PromptDialogWidget.POSITIVE) { - Uri contentUri = FileProvider.getUriForFile( - getContext(), - getContext().getApplicationContext().getPackageName() + ".provider", - new File(webResponseInfo.uri.substring("file://".length()))); - Intent newIntent = new Intent(Intent.ACTION_VIEW); - newIntent.setDataAndType(contentUri, webResponseInfo.contentType); - newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION); - try { - getContext().startActivity(newIntent); - } catch (ActivityNotFoundException ex) { - showAlert( - getResources().getString(R.string.download_open_file_error_title), - getResources().getString(R.string.download_open_file_error_body), - null); + Uri contentUri = FileProvider.getUriForFile( + getContext(), + getContext().getApplicationContext().getPackageName() + ".provider", + new File(webResponseInfo.uri.substring("file://".length()))); + Intent newIntent = new Intent(Intent.ACTION_VIEW); + newIntent.setDataAndType(contentUri, webResponseInfo.contentType); + newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION); + + PackageManager packageManager = getContext().getPackageManager(); + if (newIntent.resolveActivity(packageManager) != null) { + showConfirmPrompt(getResources().getString(R.string.download_open_file_unsupported_title), + getResources().getString(R.string.download_open_file_unsupported_body), + new String[]{ + getResources().getString(R.string.download_open_file_unsupported_cancel), + getResources().getString(R.string.download_open_file_unsupported_open) + }, (index, isChecked) -> { + if (index == PromptDialogWidget.POSITIVE) { + try { + getContext().startActivity(newIntent); + } catch (ActivityNotFoundException ignored) { + showAlert( + getResources().getString(R.string.download_open_file_error_title), + getResources().getString(R.string.download_open_file_error_body), + null); + } } - } - }); + }); + } else { + showAlert( + getResources().getString(R.string.download_open_file_error_title), + getResources().getString(R.string.download_open_file_open_unsupported_body), + null); + } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c22ba2994..d2daf1f78 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1657,6 +1657,10 @@ the Select` button. When clicked it closes all the previously selected tabs --> the system but the system can't handle it either. --> Error + + File type not supported. + No application found to handle this file type.