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

Commit

Permalink
Support for opening unsupported files with external apps
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Mar 31, 2020
1 parent 44d91ab commit ba34ff0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

package org.mozilla.vrbrowser.ui.widgets;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Matrix;
Expand All @@ -27,6 +29,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.annotation.UiThread;
import androidx.core.content.FileProvider;
import androidx.lifecycle.ViewModelProvider;

import org.mozilla.geckoview.AllowOrDeny;
Expand Down Expand Up @@ -62,6 +65,7 @@
import org.mozilla.vrbrowser.utils.UrlUtils;
import org.mozilla.vrbrowser.utils.ViewUtils;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
Expand Down Expand Up @@ -1419,9 +1423,11 @@ public void startDownload(@NonNull DownloadJob downloadJob, boolean showConfirmD
Runnable download = () -> {
if (showConfirmDialog) {
mWidgetManager.getFocusedWindow().showConfirmPrompt(
"Download",
getResources().getString(R.string.download_confirm_title),
downloadJob.getFilename(),
new String[]{"Cancel", "Download"},
new String[]{
getResources().getString(R.string.download_confirm_cancel),
getResources().getString(R.string.download_confirm_download)},
index -> mDownloadsManager.startDownload(downloadJob)
);

Expand Down Expand Up @@ -1509,10 +1515,37 @@ public void onFirstContentfulPaint(@NonNull GeckoSession session) {

@Override
public void onExternalResponse(@NonNull GeckoSession geckoSession, @NonNull GeckoSession.WebResponseInfo webResponseInfo) {
// We don't wat to trigger downloads of already downloaded files
// We don't want to trigger downloads of already downloaded files that we can't open
// so we let the system handle it.
if (!UrlUtils.isFileUri(webResponseInfo.uri)) {
DownloadJob job = DownloadJob.from(webResponseInfo);
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 -> {
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);
}
}
});
}
}

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,14 @@
android:process=":crash"
android:permission="android.permission.BIND_JOB_SERVICE">
</service>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/downloads_provider"/>
</provider>
</application>
</manifest>
36 changes: 36 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1545,4 +1545,40 @@ the Select` button. When clicked it closes all the previously selected tabs -->

<!-- This string is displayed in the disable button of the quick Tracking Protection dialog, accessed from the Tracking Protection icon in the URL bar. -->
<string name="tracking_dialog_button_disable">Disable</string>

<!-- This string is displayed in the title of the dialog displayed when the user tries to open an unsupported
file from the Downloads library panel. -->
<string name="download_open_file_unsupported_title">Unsupported File Type</string>

<!-- This string is displayed in the body of the dialog displayed when the user tries to open an unsupported
file from the Downloads library panel. -->
<string name="download_open_file_unsupported_body">The file cannot be opened, Do you want to open it with an external application?</string>

<!-- This string is displayed in the cancel button of the dialog displayed when the user tries to open an unsupported
file from the Downloads library panel. When clicked the dialog is closed.-->
<string name="download_open_file_unsupported_cancel">Cancel</string>

<!-- This string is displayed in the cancel button of the dialog displayed when the user tries to open an unsupported
file from the Downloads library panel. When clicked we hand over the file open to the system.-->
<string name="download_open_file_unsupported_open">Open</string>

<!-- This string is displayed in the title 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_title">Error</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>

<!-- This string is displayed in the title of the dialog displayed when a download has been requested
so the user can confirm if they want to actually download the file. -->
<string name="download_confirm_title">Would you like to download this file?</string>

<!-- This string is displayed in the "Cancel" button of the dialog displayed when a download has been requested
so the user can confirm if they want to actually download the file. If clicked the download is not started. -->
<string name="download_confirm_cancel">Cancel</string>

<!-- This string is displayed in the "Download" button of the dialog displayed when a download has been requested
so the user can confirm if they want to actually download the file. If clicked the download is queued. -->
<string name="download_confirm_download">Download</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/xml/downloads_provider.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

0 comments on commit ba34ff0

Please sign in to comment.