Skip to content

Commit 9267d7a

Browse files
committed
Add support android 11+
1 parent 2a8f780 commit 9267d7a

File tree

3 files changed

+5
-88
lines changed

3 files changed

+5
-88
lines changed

README.md

-41
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,6 @@ Opens a file
5656
- Electron
5757

5858
### Quick Examples
59-
Open an APK install dialog:
60-
61-
```javascript
62-
cordova.plugins.fileOpener2.open(
63-
'/Downloads/gmail.apk',
64-
'application/vnd.android.package-archive'
65-
);
66-
```
6759

6860
Open a PDF document with the default PDF reader and optional callback object:
6961

@@ -125,28 +117,11 @@ cordova.plugins.fileOpener2.showOpenWithDialog(
125117
```
126118
`position` array of coordinates from top-left device screen, use for iOS dialog positioning.
127119

128-
## fileOpener2.uninstall(packageId, callbackContext)
129-
130-
Uninstall a package with its ID.
131-
132-
__Note__: You need to add `<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />` to your `AndroidManifest.xml`
133120

134121
### Supported Platforms
135122

136123
- Android 5.1+
137124

138-
### Quick Example
139-
```js
140-
cordova.plugins.fileOpener2.uninstall('com.zynga.FarmVille2CountryEscape', {
141-
error : function(e) {
142-
console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
143-
},
144-
success : function() {
145-
console.log('Uninstall intent activity started.');
146-
}
147-
});
148-
```
149-
150125
## fileOpener2.appIsInstalled(packageId, callbackContext)
151126

152127
Check if an app is already installed.
@@ -169,22 +144,6 @@ cordova.plugins.fileOpener2.appIsInstalled('com.adobe.reader', {
169144
```
170145
---
171146

172-
## Android APK installation limitation
173-
174-
The following limitations apply when opening an APK file for installation:
175-
- 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:
176-
```xml
177-
<platform name="android">
178-
<config-file parent="/manifest" target="AndroidManifest.xml" xmlns:android="http://schemas.android.com/apk/res/android">
179-
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
180-
</config-file>
181-
</platform>
182-
```
183-
184-
- 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.
185-
186-
---
187-
188147
## SD card limitation on Android
189148

190149
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.

src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java

+5-42
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ this software and associated documentation files (the "Software"), to deal in
3737
import android.os.Build;
3838
import android.webkit.MimeTypeMap;
3939

40-
import io.github.pwlin.cordova.plugins.fileopener2.FileProvider;
41-
4240
import org.apache.cordova.CordovaPlugin;
4341
import org.apache.cordova.CallbackContext;
4442
import org.apache.cordova.PluginResult;
@@ -67,9 +65,6 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
6765
}
6866
this._open(fileUrl, contentType, openWithDefault, callbackContext);
6967
}
70-
else if (action.equals("uninstall")) {
71-
this._uninstall(args.getString(0), callbackContext);
72-
}
7368
else if (action.equals("appIsInstalled")) {
7469
JSONObject successObj = new JSONObject();
7570
if (this._appIsInstalled(args.getString(0))) {
@@ -107,28 +102,11 @@ private void _open(String fileArg, String contentType, Boolean openWithDefault,
107102
contentType = _getMimeType(fileName);
108103
}
109104

110-
Intent intent;
111-
if (contentType.equals("application/vnd.android.package-archive")) {
112-
// https://stackoverflow.com/questions/9637629/can-we-install-an-apk-from-a-contentprovider/9672282#9672282
113-
intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
114-
Uri path;
115-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
116-
path = Uri.fromFile(file);
117-
} else {
118-
Context context = cordova.getActivity().getApplicationContext();
119-
path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".fileOpener2.provider", file);
120-
}
121-
intent.setDataAndType(path, contentType);
122-
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);
123-
124-
} else {
125-
intent = new Intent(Intent.ACTION_VIEW);
126-
Context context = cordova.getActivity().getApplicationContext();
127-
Uri path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".fileOpener2.provider", file);
128-
intent.setDataAndType(path, contentType);
129-
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
130-
131-
}
105+
Intent intent = new Intent(Intent.ACTION_VIEW);
106+
Context context = cordova.getActivity().getApplicationContext();
107+
Uri path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".fileOpener2.provider", file);
108+
intent.setDataAndType(path, contentType);
109+
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
132110

133111
/*
134112
* @see
@@ -168,21 +146,6 @@ private String _getMimeType(String url) {
168146
return mimeType;
169147
}
170148

171-
private void _uninstall(String packageId, CallbackContext callbackContext) throws JSONException {
172-
if (this._appIsInstalled(packageId)) {
173-
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
174-
intent.setData(Uri.parse("package:" + packageId));
175-
cordova.getActivity().startActivity(intent);
176-
callbackContext.success();
177-
}
178-
else {
179-
JSONObject errorObj = new JSONObject();
180-
errorObj.put("status", PluginResult.Status.ERROR.ordinal());
181-
errorObj.put("message", "This package is not installed");
182-
callbackContext.error(errorObj);
183-
}
184-
}
185-
186149
private boolean _appIsInstalled(String packageId) {
187150
PackageManager pm = cordova.getActivity().getPackageManager();
188151
boolean appInstalled = false;

www/plugins.FileOpener2.js

-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ FileOpener2.prototype.showOpenWithDialog = function (fileName, contentType, call
3838
exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'open', [fileName, contentType, false, callbackContext.position || [0, 0]]);
3939
};
4040

41-
FileOpener2.prototype.uninstall = function (packageId, callbackContext) {
42-
callbackContext = callbackContext || {};
43-
exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'uninstall', [packageId]);
44-
};
45-
4641
FileOpener2.prototype.appIsInstalled = function (packageId, callbackContext) {
4742
callbackContext = callbackContext || {};
4843
exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'appIsInstalled', [packageId]);

0 commit comments

Comments
 (0)