Skip to content

Commit

Permalink
Merge pull request #5 from vmfo22/master
Browse files Browse the repository at this point in the history
Add support for Content Uri allows reading access using temporary access permissions
  • Loading branch information
vmfo22 authored Aug 21, 2018
2 parents 36bef97 + adea62f commit e9ff3e4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "File-Viewer-Plugin",
"version": "1.0.1",
"version": "1.1.0",
"description": "File Viewer Plugin for Cordova Applications",
"cordova": {
"id": "com.outsystems.documentpreview",
Expand Down
19 changes: 17 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version='1.0' encoding='utf-8'?>
<plugin
id="com.outsystems.documentpreview"
version="1.0.1"
version="1.1.0"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">

Expand Down Expand Up @@ -34,10 +34,25 @@
<param name="android-package" value="com.outsystems.documentpreview.DocumentPreview" />
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="application">
<provider
android:name="com.outsystems.documentpreview.FileProvider"
android:authorities="${applicationId}.opener.provider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</config-file>

<config-file parent="/*" target="AndroidManifest.xml"></config-file>


<framework src="com.android.support:support-v4:24.1.1+" />

<source-file src="src/android/DocumentPreview.java" target-dir="src/com/outsystems/documentpreview" />
<source-file src="src/android/FileProvider.java" target-dir="src/com/outsystems/documentpreview" />
<source-file src="src/android/xml/provider_paths.xml" target-dir="res/xml" />
</platform>

</plugin>
37 changes: 32 additions & 5 deletions src/android/DocumentPreview.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.outsystems.documentpreview;

import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.webkit.URLUtil;

Expand All @@ -13,6 +15,8 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;


public class DocumentPreview extends CordovaPlugin {

Expand Down Expand Up @@ -69,17 +73,40 @@ private void openDocument(JSONArray args, CallbackContext callbackContext) {

private void openVideoPlayer(String filePath, String fileMIMEType) throws ActivityNotFoundException {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(filePath), fileMIMEType); //"application/pdf"
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Uri videoUri;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
videoUri = Uri.parse(filePath);
} else {
//To build the File URI property, must be removed the file protocol, if it the case
File videoFile = new File(filePath.replace("file:///", ""));
videoUri = FileProvider.getUriForFile(cordova.getActivity(), cordova.getActivity().getPackageName() + ".opener.provider", videoFile);
}

intent.setDataAndType(videoUri, fileMIMEType);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NO_HISTORY);
cordova.getActivity().startActivity(intent);
}

private void openDocumentLocalApp(String filePath, String fileMIMEType) throws ActivityNotFoundException {
ContentResolver cR = cordova.getActivity().getApplicationContext().getContentResolver();

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(filePath), fileMIMEType); //"application/pdf"
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
cordova.getActivity().startActivity(intent);

Uri fileUri;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
fileUri = Uri.parse(filePath);
} else {
//To build the File URI property, must be removed the file protocol, if it the case
File documentFile = new File(filePath.replace("file:///", ""));

fileUri = FileProvider.getUriForFile(cordova.getActivity(), cordova.getActivity().getPackageName() + ".opener.provider", documentFile);
}

String mimeType = cR.getType(fileUri);
intent.setDataAndType(fileUri, mimeType);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NO_HISTORY);
cordova.getActivity().startActivity(intent);
}

private void openUrlInBrowser(String filePath) {
Expand Down
7 changes: 7 additions & 0 deletions src/android/FileProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.outsystems.documentpreview;

/*
* http://stackoverflow.com/questions/40746144/error-with-duplicated-fileprovider-in-manifest-xml-with-cordova/41550634#41550634
*/
public class FileProvider extends android.support.v4.content.FileProvider {
}
14 changes: 14 additions & 0 deletions src/android/xml/provider_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://developer.android.com/reference/android/support/v4/content/FileProvider.html#SpecifyFiles -->
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!-- cordova.file.dataDirectory -->
<files-path name="files" path="." />
<!-- cordova.file.cacheDirectory -->
<cache-path name="cache" path="." />
<!-- cordova.file.externalDataDirectory -->
<external-files-path name="external-files" path="." />
<!-- cordova.file.externalCacheDirectory -->
<external-cache-path name="external-cache" path="." />
<!-- cordova.file.externalRootDirectory -->
<external-path name="external" path="." />
</paths>

0 comments on commit e9ff3e4

Please sign in to comment.