From 9267d7ad85d24efefcd53825544288caf6ce939d Mon Sep 17 00:00:00 2001 From: Maxim Belov Date: Wed, 29 Mar 2023 14:11:04 +0300 Subject: [PATCH] Add support android 11+ --- README.md | 41 ---------------- .../plugins/fileopener2/FileOpener2.java | 47 ++----------------- www/plugins.FileOpener2.js | 5 -- 3 files changed, 5 insertions(+), 88 deletions(-) diff --git a/README.md b/README.md index 46630273..a2462f40 100644 --- a/README.md +++ b/README.md @@ -56,14 +56,6 @@ Opens a file - Electron ### Quick Examples -Open an APK install dialog: - -```javascript -cordova.plugins.fileOpener2.open( - '/Downloads/gmail.apk', - 'application/vnd.android.package-archive' -); -``` Open a PDF document with the default PDF reader and optional callback object: @@ -125,28 +117,11 @@ cordova.plugins.fileOpener2.showOpenWithDialog( ``` `position` array of coordinates from top-left device screen, use for iOS dialog positioning. -## fileOpener2.uninstall(packageId, callbackContext) - -Uninstall a package with its ID. - -__Note__: You need to add `` to your `AndroidManifest.xml` ### Supported Platforms - Android 5.1+ -### Quick Example -```js -cordova.plugins.fileOpener2.uninstall('com.zynga.FarmVille2CountryEscape', { - error : function(e) { - console.log('Error status: ' + e.status + ' - Error message: ' + e.message); - }, - success : function() { - console.log('Uninstall intent activity started.'); - } -}); -``` - ## fileOpener2.appIsInstalled(packageId, callbackContext) Check if an app is already installed. @@ -169,22 +144,6 @@ cordova.plugins.fileOpener2.appIsInstalled('com.adobe.reader', { ``` --- -## 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: -```xml - - - - - -``` - -- 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. - ---- - ## SD card limitation on Android It is not always possible to open a file from the SD Card using this plugin on Android. This is because the underlying Android library used [does not support serving files from secondary external storage devices](https://stackoverflow.com/questions/40318116/fileprovider-and-secondary-external-storage). Whether or not your the SD card is treated as a secondary external device depends on your particular phone's set up. 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 399eb191..bce2555b 100644 --- a/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java +++ b/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java @@ -37,8 +37,6 @@ this software and associated documentation files (the "Software"), to deal in import android.os.Build; import android.webkit.MimeTypeMap; -import io.github.pwlin.cordova.plugins.fileopener2.FileProvider; - import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CallbackContext; import org.apache.cordova.PluginResult; @@ -67,9 +65,6 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo } this._open(fileUrl, contentType, openWithDefault, callbackContext); } - else if (action.equals("uninstall")) { - this._uninstall(args.getString(0), callbackContext); - } else if (action.equals("appIsInstalled")) { JSONObject successObj = new JSONObject(); if (this._appIsInstalled(args.getString(0))) { @@ -107,28 +102,11 @@ private void _open(String fileArg, String contentType, Boolean openWithDefault, contentType = _getMimeType(fileName); } - 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() + ".fileOpener2.provider", file); - } - intent.setDataAndType(path, contentType); - intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK); - - } else { - intent = new Intent(Intent.ACTION_VIEW); - Context context = cordova.getActivity().getApplicationContext(); - Uri path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".fileOpener2.provider", file); - intent.setDataAndType(path, contentType); - intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); - - } + Intent intent = new Intent(Intent.ACTION_VIEW); + Context context = cordova.getActivity().getApplicationContext(); + Uri path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".fileOpener2.provider", file); + intent.setDataAndType(path, contentType); + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); /* * @see @@ -168,21 +146,6 @@ private String _getMimeType(String url) { return mimeType; } - private void _uninstall(String packageId, CallbackContext callbackContext) throws JSONException { - if (this._appIsInstalled(packageId)) { - Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE); - intent.setData(Uri.parse("package:" + packageId)); - cordova.getActivity().startActivity(intent); - callbackContext.success(); - } - else { - JSONObject errorObj = new JSONObject(); - errorObj.put("status", PluginResult.Status.ERROR.ordinal()); - errorObj.put("message", "This package is not installed"); - callbackContext.error(errorObj); - } - } - private boolean _appIsInstalled(String packageId) { PackageManager pm = cordova.getActivity().getPackageManager(); boolean appInstalled = false; diff --git a/www/plugins.FileOpener2.js b/www/plugins.FileOpener2.js index 4049495f..6ebdcd03 100644 --- a/www/plugins.FileOpener2.js +++ b/www/plugins.FileOpener2.js @@ -38,11 +38,6 @@ FileOpener2.prototype.showOpenWithDialog = function (fileName, contentType, call exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'open', [fileName, contentType, false, callbackContext.position || [0, 0]]); }; -FileOpener2.prototype.uninstall = function (packageId, callbackContext) { - callbackContext = callbackContext || {}; - exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'uninstall', [packageId]); -}; - FileOpener2.prototype.appIsInstalled = function (packageId, callbackContext) { callbackContext = callbackContext || {}; exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'appIsInstalled', [packageId]);