Skip to content

Commit

Permalink
Add support android 11+
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximBelov committed Mar 29, 2023
1 parent 2a8f780 commit 9267d7a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 88 deletions.
41 changes: 0 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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 `<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />` 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.
Expand All @@ -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
<platform name="android">
<config-file parent="/manifest" target="AndroidManifest.xml" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
</config-file>
</platform>
```

- 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions www/plugins.FileOpener2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 9267d7a

Please sign in to comment.