From ba3cb010c25ac6866c675c2ab70944215b4b9c1e Mon Sep 17 00:00:00 2001 From: imgx64 Date: Wed, 18 Oct 2017 12:29:49 +0300 Subject: [PATCH 1/7] Correctly call Intent.setFlags() --- .../github/pwlin/cordova/plugins/fileopener2/FileOpener2.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 541d5e4..3dec792 100644 --- a/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java +++ b/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java @@ -109,8 +109,7 @@ public class FileOpener2 extends CordovaPlugin { 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_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NO_HISTORY); //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); List infoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); From ade1872a5c09c1315c8e0ecd0be64b442d0081c3 Mon Sep 17 00:00:00 2001 From: imgx64 Date: Sun, 22 Oct 2017 11:17:29 +0300 Subject: [PATCH 2/7] Fix and document opener_paths.xml --- src/android/res/xml/opener_paths.xml | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/android/res/xml/opener_paths.xml b/src/android/res/xml/opener_paths.xml index 9dc2098..0ffc88e 100644 --- a/src/android/res/xml/opener_paths.xml +++ b/src/android/res/xml/opener_paths.xml @@ -1,8 +1,14 @@ + + - - - - + + + + + + + + From 8dd6ca756c4a484b486cfdf7a85e5f2d3bdad70a Mon Sep 17 00:00:00 2001 From: imgx64 Date: Sun, 22 Oct 2017 11:35:47 +0300 Subject: [PATCH 3/7] Remove unnecessary context.grantUriPermission() --- .../pwlin/cordova/plugins/fileopener2/FileOpener2.java | 6 ------ 1 file changed, 6 deletions(-) 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 3dec792..b7b6fdd 100644 --- a/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java +++ b/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java @@ -111,12 +111,6 @@ public class FileOpener2 extends CordovaPlugin { intent.setDataAndType(path, contentType); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | 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); From 0922f8c16edf2ef4b582315a257993d5768fefed Mon Sep 17 00:00:00 2001 From: imgx64 Date: Sun, 22 Oct 2017 11:50:23 +0300 Subject: [PATCH 4/7] Always use FileProvider --- .../plugins/fileopener2/FileOpener2.java | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) 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 b7b6fdd..f77f055 100644 --- a/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java +++ b/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java @@ -102,20 +102,13 @@ public class FileOpener2 extends CordovaPlugin { File file = new File(fileName); if (file.exists()) { try { - Uri path = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_VIEW); - if((Build.VERSION.SDK_INT >= 23 && !contentType.equals("application/vnd.android.package-archive")) || ((Build.VERSION.SDK_INT == 24 || Build.VERSION.SDK_INT == 25) && contentType.equals("application/vnd.android.package-archive"))) { - 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.FLAG_ACTIVITY_NO_HISTORY); - //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - } - else { - intent.setDataAndType(path, contentType); - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - } + Context context = cordova.getActivity().getApplicationContext(); + Uri path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".opener.provider", file); + intent.setDataAndType(path, contentType); + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NO_HISTORY); + /* * @see * http://stackoverflow.com/questions/14321376/open-an-activity-from-a-cordovaplugin From 6e29bebc1864c826740a752376e71bb47c47e91d Mon Sep 17 00:00:00 2001 From: imgx64 Date: Sun, 22 Oct 2017 13:09:07 +0300 Subject: [PATCH 5/7] Fix opening APK files --- .../plugins/fileopener2/FileOpener2.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) 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 f77f055..d4b2fff 100644 --- a/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java +++ b/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java @@ -102,12 +102,28 @@ public class FileOpener2 extends CordovaPlugin { File file = new File(fileName); if (file.exists()) { try { - Intent intent = new Intent(Intent.ACTION_VIEW); + Intent intent; + if (contentType.equals("application/vnd.android.package-archive")) { + // https://stackoverflow.com/questions/9637629/can-we-install-an-apk-from-a-contentprovider/9672282#9672282 + intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); + Uri path; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { + path = Uri.fromFile(file); + } else { + 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); - Context context = cordova.getActivity().getApplicationContext(); - Uri path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".opener.provider", file); - intent.setDataAndType(path, contentType); - intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NO_HISTORY); + } else { + intent = new Intent(Intent.ACTION_VIEW); + Context context = cordova.getActivity().getApplicationContext(); + Uri path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".opener.provider", file); + intent.setDataAndType(path, contentType); + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NO_HISTORY); + + } /* * @see From ef84dfbcfda593305f7c126252c94283510824ff Mon Sep 17 00:00:00 2001 From: imgx64 Date: Mon, 23 Oct 2017 08:47:38 +0300 Subject: [PATCH 6/7] Add Android APK installation limitation to README --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 63b1cf0..91e689f 100644 --- a/README.md +++ b/README.md @@ -74,11 +74,24 @@ Notes - For properly opening _any_ file, you must already have a suitable reader for that particular file type installed on your device. Otherwise this will not work. - - [It is reported](https://github.com/pwlin/cordova-plugin-file-opener2/issues/2#issuecomment-41295793) that in iOS, you might need to remove `` from your `config.xml` - If you are wondering what MIME-type should you pass as the second argument to `open` function, [here is a list of all known MIME-types](http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=co) +Android APK installation limitation +--- +The following limitations apply when opening an APK file for installation: +- On Android 8+, your application must have the `ACTION_INSTALL_PACKAGE` permission. You can add it by adding this to your app's `config.xml` file: +``` + + + + + +``` + +- Before Android 7, you can only install APKs from the "external" partition. For example, you can install from `cordova.file.externalDataDirectory`, but **not** from `cordova.file.dataDirectory`. Android 7+ does not have this limitation. + Additional Android Functions --- The following functions are available in Android platform: From dfab028e034026f299585b53bcc5970a22b033e1 Mon Sep 17 00:00:00 2001 From: imgx64 Date: Mon, 23 Oct 2017 09:05:49 +0300 Subject: [PATCH 7/7] Fix Android permission instructions --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 91e689f..0f64971 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ The following limitations apply when opening an APK file for installation: - On Android 8+, your application must have the `ACTION_INSTALL_PACKAGE` permission. You can add it by adding this to your app's `config.xml` file: ``` - +