From 70e610e465d774d18a592140a0bbf745f4ee09e3 Mon Sep 17 00:00:00 2001 From: "Lincoln Baxter, III" Date: Fri, 27 Jan 2017 15:01:33 -0500 Subject: [PATCH] Fixes https://github.com/pwlin/cordova-plugin-file-opener2/issues/84 --- plugin.xml | 15 +++++++- .../plugins/fileopener2/FileOpener2.java | 36 +++++++++++++++---- .../plugins/fileopener2/FileProvider.java | 7 ++++ src/android/res/xml/opener_paths.xml | 4 +++ 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 src/android/io/github/pwlin/cordova/plugins/fileopener2/FileProvider.java create mode 100644 src/android/res/xml/opener_paths.xml diff --git a/plugin.xml b/plugin.xml index 3a50e8d..d0961bc 100644 --- a/plugin.xml +++ b/plugin.xml @@ -16,6 +16,7 @@ + @@ -24,6 +25,18 @@ + + + + + + @@ -46,7 +59,7 @@ - + diff --git a/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java b/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java index 8a060ef..b330b4f 100644 --- a/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java +++ b/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java @@ -23,15 +23,20 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package io.github.pwlin.cordova.plugins.fileopener2; import java.io.File; +import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.net.Uri; -//import android.util.Log; +import android.os.Build; + +import io.github.pwlin.cordova.plugins.fileopener2.FileProvider; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CallbackContext; @@ -42,7 +47,7 @@ public class FileOpener2 extends CordovaPlugin { /** * Executes the request and returns a boolean. - * + * * @param action * The action to execute. * @param args @@ -54,7 +59,7 @@ public class FileOpener2 extends CordovaPlugin { public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if (action.equals("open")) { this._open(args.getString(0), args.getString(1), callbackContext); - } + } else if (action.equals("uninstall")) { this._uninstall(args.getString(0), callbackContext); } @@ -93,8 +98,25 @@ public class FileOpener2 extends CordovaPlugin { try { Uri path = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setDataAndType(path, contentType); - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + if(Build.VERSION.SDK_INT >= 24){ + + Context context = cordova.getActivity().getApplicationContext(); + path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".opener.provider", file); + intent.setDataAndType(path, contentType); + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); + //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + + List infoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); + for (ResolveInfo resolveInfo : infoList) { + String packageName = resolveInfo.activityInfo.packageName; + context.grantUriPermission(packageName, path, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); + } + } + else { + intent.setDataAndType(path, contentType); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + } /* * @see * http://stackoverflow.com/questions/14321376/open-an-activity-from-a-cordovaplugin @@ -115,7 +137,7 @@ public class FileOpener2 extends CordovaPlugin { callbackContext.error(errorObj); } } - + private void _uninstall(String packageId, CallbackContext callbackContext) throws JSONException { if (this._appIsInstalled(packageId)) { Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE); @@ -130,7 +152,7 @@ public class FileOpener2 extends CordovaPlugin { callbackContext.error(errorObj); } } - + private boolean _appIsInstalled(String packageId) { PackageManager pm = cordova.getActivity().getPackageManager(); boolean appInstalled = false; diff --git a/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileProvider.java b/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileProvider.java new file mode 100644 index 0000000..cc6b18d --- /dev/null +++ b/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileProvider.java @@ -0,0 +1,7 @@ +package io.github.pwlin.cordova.plugins.fileopener2; + +/* + * 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 { +} diff --git a/src/android/res/xml/opener_paths.xml b/src/android/res/xml/opener_paths.xml new file mode 100644 index 0000000..a6ce6d3 --- /dev/null +++ b/src/android/res/xml/opener_paths.xml @@ -0,0 +1,4 @@ + + + +