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

Commit

Permalink
Check if any system app supports a file type before opening (#3352)
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo authored May 19, 2020
1 parent 5a370bc commit 4abe405
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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. -->
<string name="download_open_file_error_title">Error</string>

<!-- This string is displayed in the body of the dialog displayed when the file type cannot be opened and
there is no system app available to handle it either. -->
<string name="download_open_file_open_unsupported_body">File type not supported.</string>

<!-- This string is displayed in the body of the dialog displayed when we try to hand over the file open to
the system but the system can't handle it either. -->
<string name="download_open_file_error_body">No application found to handle this file type.</string>
Expand Down

0 comments on commit 4abe405

Please sign in to comment.