Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 4abe405

Browse files
authored
Check if any system app supports a file type before opening (#3352)
1 parent 5a370bc commit 4abe405

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.content.Context;
1010
import android.content.SharedPreferences;
1111
import android.content.Intent;
12+
import android.content.pm.PackageManager;
1213
import android.content.res.Configuration;
1314
import android.graphics.Canvas;
1415
import android.graphics.Matrix;
@@ -1664,30 +1665,39 @@ public void onExternalResponse(@NonNull GeckoSession geckoSession, @NonNull Geck
16641665
startDownload(job, true);
16651666

16661667
} else {
1667-
showConfirmPrompt(getResources().getString(R.string.download_open_file_unsupported_title),
1668-
getResources().getString(R.string.download_open_file_unsupported_body),
1669-
new String[]{
1670-
getResources().getString(R.string.download_open_file_unsupported_cancel),
1671-
getResources().getString(R.string.download_open_file_unsupported_open)
1672-
}, (index, isChecked) -> {
1673-
if (index == PromptDialogWidget.POSITIVE) {
1674-
Uri contentUri = FileProvider.getUriForFile(
1675-
getContext(),
1676-
getContext().getApplicationContext().getPackageName() + ".provider",
1677-
new File(webResponseInfo.uri.substring("file://".length())));
1678-
Intent newIntent = new Intent(Intent.ACTION_VIEW);
1679-
newIntent.setDataAndType(contentUri, webResponseInfo.contentType);
1680-
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
1681-
try {
1682-
getContext().startActivity(newIntent);
1683-
} catch (ActivityNotFoundException ex) {
1684-
showAlert(
1685-
getResources().getString(R.string.download_open_file_error_title),
1686-
getResources().getString(R.string.download_open_file_error_body),
1687-
null);
1668+
Uri contentUri = FileProvider.getUriForFile(
1669+
getContext(),
1670+
getContext().getApplicationContext().getPackageName() + ".provider",
1671+
new File(webResponseInfo.uri.substring("file://".length())));
1672+
Intent newIntent = new Intent(Intent.ACTION_VIEW);
1673+
newIntent.setDataAndType(contentUri, webResponseInfo.contentType);
1674+
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
1675+
1676+
PackageManager packageManager = getContext().getPackageManager();
1677+
if (newIntent.resolveActivity(packageManager) != null) {
1678+
showConfirmPrompt(getResources().getString(R.string.download_open_file_unsupported_title),
1679+
getResources().getString(R.string.download_open_file_unsupported_body),
1680+
new String[]{
1681+
getResources().getString(R.string.download_open_file_unsupported_cancel),
1682+
getResources().getString(R.string.download_open_file_unsupported_open)
1683+
}, (index, isChecked) -> {
1684+
if (index == PromptDialogWidget.POSITIVE) {
1685+
try {
1686+
getContext().startActivity(newIntent);
1687+
} catch (ActivityNotFoundException ignored) {
1688+
showAlert(
1689+
getResources().getString(R.string.download_open_file_error_title),
1690+
getResources().getString(R.string.download_open_file_error_body),
1691+
null);
1692+
}
16881693
}
1689-
}
1690-
});
1694+
});
1695+
} else {
1696+
showAlert(
1697+
getResources().getString(R.string.download_open_file_error_title),
1698+
getResources().getString(R.string.download_open_file_open_unsupported_body),
1699+
null);
1700+
}
16911701
}
16921702
}
16931703

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,10 @@ the Select` button. When clicked it closes all the previously selected tabs -->
16571657
the system but the system can't handle it either. -->
16581658
<string name="download_open_file_error_title">Error</string>
16591659

1660+
<!-- This string is displayed in the body of the dialog displayed when the file type cannot be opened and
1661+
there is no system app available to handle it either. -->
1662+
<string name="download_open_file_open_unsupported_body">File type not supported.</string>
1663+
16601664
<!-- This string is displayed in the body of the dialog displayed when we try to hand over the file open to
16611665
the system but the system can't handle it either. -->
16621666
<string name="download_open_file_error_body">No application found to handle this file type.</string>

0 commit comments

Comments
 (0)