You've already forked mattlution
'初始化提交'
This commit is contained in:
44
plugins/cordova-plugin-file-opener2/.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
44
plugins/cordova-plugin-file-opener2/.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
### Expected Behaviour
|
||||
|
||||
### Actual Behaviour
|
||||
|
||||
### Reproduce Scenario (including but not limited to)
|
||||
|
||||
#### Steps to Reproduce
|
||||
|
||||
#### Platform and Version (eg. Android 5.0 or iOS 9.2.1)
|
||||
|
||||
#### (Android) What device vendor (e.g. Samsung, HTC, Sony...)
|
||||
|
||||
#### Cordova CLI info
|
||||
|
||||
cordova info
|
||||
|
||||
Here is the output:
|
||||
|
||||
|
||||
#### Plugin version
|
||||
|
||||
cordova plugin version | grep cordova-plugin-file-opener2
|
||||
|
||||
Here is the output:
|
||||
|
||||
|
||||
#### Sample Code that illustrates the problem
|
||||
|
||||
#### Logs taken while reproducing problem
|
||||
Run
|
||||
|
||||
`adb logcat PluginManager:V CordovaPlugin:V CordovaLog:V chromium:V *:S`
|
||||
|
||||
while testing the bug in your app to get some output from your error. This will help you to understand more about the nature of your problem.
|
||||
|
||||
20
plugins/cordova-plugin-file-opener2/LICENSE
Normal file
20
plugins/cordova-plugin-file-opener2/LICENSE
Normal file
@@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 pwlin - pwlin05@gmail.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
191
plugins/cordova-plugin-file-opener2/README.md
Normal file
191
plugins/cordova-plugin-file-opener2/README.md
Normal file
@@ -0,0 +1,191 @@
|
||||
# A File Opener Plugin for Cordova
|
||||
|
||||
[](https://www.npmjs.com/package/cordova-plugin-file-opener2) [](https://npm-stat.com/charts.html?package=cordova-plugin-file-opener2)
|
||||
|
||||
This plugin will open a file on your device file system with its default application.
|
||||
|
||||
```js
|
||||
cordova.plugins.fileOpener2.open(
|
||||
filePath,
|
||||
fileMIMEType,
|
||||
{
|
||||
error : function(){ },
|
||||
success : function(){ }
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
```shell
|
||||
$ cordova plugin add cordova-plugin-file-opener2
|
||||
```
|
||||
|
||||
## Requirements
|
||||
|
||||
The following platforms and versions are supported by the latest release:
|
||||
|
||||
- Android 5.1+ / iOS 9+ / Windows / Electron
|
||||
- Cordova CLI 7.0 or higher
|
||||
|
||||
Cordova CLI 6.0 is supported by 2.0.19, but there are a number of issues, particularly with Android builds (see [232](https://github.com/pwlin/cordova-plugin-file-opener2/issues/232) [203](https://github.com/pwlin/cordova-plugin-file-opener2/issues/203) [207](https://github.com/pwlin/cordova-plugin-file-opener2/issues/207)). Using the [cordova-android-support-gradle-release](https://github.com/dpa99c/cordova-android-support-gradle-release) plugin may help.
|
||||
|
||||
## fileOpener2.open(filePath, mimeType, options)
|
||||
|
||||
Opens a file
|
||||
|
||||
### Supported Platforms
|
||||
|
||||
- Android 5.1+
|
||||
- iOS 9+
|
||||
- Windows
|
||||
- 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:
|
||||
|
||||
```js
|
||||
cordova.plugins.fileOpener2.open(
|
||||
'/Download/starwars.pdf', // You can also use a Cordova-style file uri: cdvfile://localhost/persistent/Downloads/starwars.pdf
|
||||
'application/pdf',
|
||||
{
|
||||
error : function(e) {
|
||||
console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
|
||||
},
|
||||
success : function () {
|
||||
console.log('file opened successfully');
|
||||
}
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
__Note on Electron:__ Do not forget to enable Node.js in your app by adding `"nodeIntegration": true` to `platforms/electron/platform_www/cdv-electron-settings.json` file, See [Cordova-Electron documentation](https://cordova.apache.org/docs/en/latest/guide/platforms/electron/index.html#customizing-the-application's-window-options).
|
||||
|
||||
### Market place installation
|
||||
Install From Market: to install an APK from a market place, such as Google Play or the App Store, you can use an `<a>` tag in combination with the `market://` protocol:
|
||||
|
||||
```html
|
||||
<a href="market://details?id=xxxx" target="_system">Install from Google Play</a>
|
||||
<a href="itms-apps://itunes.apple.com/app/my-app/idxxxxxxxx?mt=8" target="_system">Install from App Store</a>
|
||||
```
|
||||
or in code:
|
||||
|
||||
```js
|
||||
window.open("[market:// or itms-apps:// link]","_system");
|
||||
```
|
||||
|
||||
## fileOpener2.showOpenWithDialog(filePath, mimeType, options)
|
||||
|
||||
Opens with system modal to open file with an already installed app.
|
||||
|
||||
### Supported Platforms
|
||||
|
||||
- Android 5.1+
|
||||
- iOS 9+
|
||||
|
||||
### Quick Example
|
||||
|
||||
```js
|
||||
cordova.plugins.fileOpener2.showOpenWithDialog(
|
||||
'/Downloads/starwars.pdf', // You can also use a Cordova-style file uri: cdvfile://localhost/persistent/Downloads/starwars.pdf
|
||||
'application/pdf',
|
||||
{
|
||||
error : function(e) {
|
||||
console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
|
||||
},
|
||||
success : function () {
|
||||
console.log('file opened successfully');
|
||||
},
|
||||
position : [0, 0]
|
||||
}
|
||||
);
|
||||
```
|
||||
`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.
|
||||
|
||||
### Supported Platforms
|
||||
|
||||
- Android 5.1+
|
||||
|
||||
### Quick Example
|
||||
```javascript
|
||||
cordova.plugins.fileOpener2.appIsInstalled('com.adobe.reader', {
|
||||
success : function(res) {
|
||||
if (res.status === 0) {
|
||||
console.log('Adobe Reader is not installed.');
|
||||
} else {
|
||||
console.log('Adobe Reader is installed.')
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
---
|
||||
|
||||
## 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.
|
||||
|
||||
---
|
||||
|
||||
## 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 `<preference name="iosPersistentFileLocation" value="Library" />` 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)
|
||||
|
||||
|
||||
---
|
||||
|
||||
|
||||
46
plugins/cordova-plugin-file-opener2/package.json
Normal file
46
plugins/cordova-plugin-file-opener2/package.json
Normal file
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"name": "cordova-plugin-file-opener2",
|
||||
"version": "4.0.0",
|
||||
"description": "A File Opener Plugin for Cordova. (The Original Version)",
|
||||
"cordova": {
|
||||
"id": "cordova-plugin-file-opener2",
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios",
|
||||
"windows",
|
||||
"electron"
|
||||
]
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/pwlin/cordova-plugin-file-opener2.git"
|
||||
},
|
||||
"keywords": [
|
||||
"ecosystem:cordova",
|
||||
"cordova-android",
|
||||
"cordova-ios",
|
||||
"cordova-windows",
|
||||
"cordova-electron"
|
||||
],
|
||||
"engines": {
|
||||
"cordovaDependencies": {
|
||||
"2.0.0": {
|
||||
"cordova": ">=6.0.0"
|
||||
},
|
||||
"3.0.0": {
|
||||
"cordova": ">=7.0.0"
|
||||
},
|
||||
"4.0.0": {
|
||||
"cordova-android": ">=10.0.0"
|
||||
}
|
||||
}
|
||||
},
|
||||
"author": {
|
||||
"name": "pwlin05@gmail.com"
|
||||
},
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/pwlin/cordova-plugin-file-opener2/issues"
|
||||
},
|
||||
"homepage": "https://github.com/pwlin/cordova-plugin-file-opener2#readme"
|
||||
}
|
||||
90
plugins/cordova-plugin-file-opener2/plugin.xml
Normal file
90
plugins/cordova-plugin-file-opener2/plugin.xml
Normal file
@@ -0,0 +1,90 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-file-opener2" version="4.0.0">
|
||||
|
||||
<name>File Opener2</name>
|
||||
<description>A File Opener Plugin for Cordova. (The Original Version)</description>
|
||||
<license>MIT</license>
|
||||
|
||||
<js-module src="www/plugins.FileOpener2.js" name="FileOpener2">
|
||||
<clobbers target="cordova.plugins.fileOpener2" />
|
||||
</js-module>
|
||||
|
||||
<!-- Android -->
|
||||
<platform name="android">
|
||||
<source-file src="src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java" target-dir="src/io/github/pwlin/cordova/plugins/fileopener2" />
|
||||
<source-file src="src/android/io/github/pwlin/cordova/plugins/fileopener2/FileProvider.java" target-dir="src/io/github/pwlin/cordova/plugins/fileopener2" />
|
||||
<config-file target="res/xml/config.xml" parent="/*">
|
||||
<feature name="FileOpener2">
|
||||
<param name="android-package" value="io.github.pwlin.cordova.plugins.fileopener2.FileOpener2" />
|
||||
</feature>
|
||||
</config-file>
|
||||
<config-file target="AndroidManifest.xml" parent="/*">
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
</config-file>
|
||||
<config-file target="AndroidManifest.xml" parent="application">
|
||||
<provider android:name="io.github.pwlin.cordova.plugins.fileopener2.FileProvider" android:authorities="${applicationId}.fileOpener2.provider" android:exported="false" android:grantUriPermissions="true">
|
||||
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/opener_paths" />
|
||||
</provider>
|
||||
</config-file>
|
||||
<source-file src="src/android/res/xml/opener_paths.xml" target-dir="res/xml" />
|
||||
</platform>
|
||||
|
||||
<!-- iOS -->
|
||||
<platform name="ios">
|
||||
<config-file target="config.xml" parent="/*">
|
||||
<feature name="FileOpener2">
|
||||
<param name="ios-package" value="FileOpener2" />
|
||||
</feature>
|
||||
</config-file>
|
||||
<source-file src="src/ios/FileOpener2.m" />
|
||||
<header-file src="src/ios/FileOpener2.h" />
|
||||
</platform>
|
||||
|
||||
<!-- WP8 -->
|
||||
<platform name="wp8">
|
||||
<config-file target="config.xml" parent="/*">
|
||||
<feature name="FileOpener2">
|
||||
<param name="wp-package" value="FileOpener2" />
|
||||
</feature>
|
||||
</config-file>
|
||||
<source-file src="src/wp8/FileOpener2.cs" />
|
||||
</platform>
|
||||
|
||||
<!-- windows -->
|
||||
<platform name="windows">
|
||||
<js-module src="src/windows/fileOpener2Proxy.js" name="fileOpener2Proxy">
|
||||
<merges target="" />
|
||||
</js-module>
|
||||
</platform>
|
||||
|
||||
<!-- browser -->
|
||||
<platform name="browser">
|
||||
<config-file parent="/*" target="config.xml">
|
||||
<feature name="FileOpener2">
|
||||
<param name="browser-package" value="FileOpener2"/>
|
||||
</feature>
|
||||
</config-file>
|
||||
|
||||
<!-- Required for browserify: we always link module below as there is conditional reference
|
||||
to this module from requestFileSystem and resolveLocalFileSystemURI modules. -->
|
||||
<js-module src="www/browser/isChrome.js" name="isChrome">
|
||||
<runs />
|
||||
</js-module>
|
||||
|
||||
<js-module src="src/browser/FileSaver.min.js" name="FileSaver">
|
||||
<clobbers target="FileSaver"/>
|
||||
</js-module>
|
||||
|
||||
<js-module src="src/browser/FileOpener2.js" name="FileOpener2Proxy">
|
||||
<runs/>
|
||||
</js-module>
|
||||
</platform>
|
||||
|
||||
<!-- electron -->
|
||||
<platform name="electron">
|
||||
<js-module src="src/electron/FileOpener2.js" name="fileOpener2">
|
||||
<merges target="" />
|
||||
</js-module>
|
||||
</platform>
|
||||
|
||||
</plugin>
|
||||
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 pwlin - pwlin05@gmail.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
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.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;
|
||||
import org.apache.cordova.CordovaResourceApi;
|
||||
|
||||
public class FileOpener2 extends CordovaPlugin {
|
||||
|
||||
/**
|
||||
* Executes the request and returns a boolean.
|
||||
*
|
||||
* @param action
|
||||
* The action to execute.
|
||||
* @param args
|
||||
* JSONArry of arguments for the plugin.
|
||||
* @param callbackContext
|
||||
* The callback context used when calling back into JavaScript.
|
||||
* @return boolean.
|
||||
*/
|
||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
if (action.equals("open")) {
|
||||
String fileUrl = args.getString(0);
|
||||
String contentType = args.getString(1);
|
||||
Boolean openWithDefault = true;
|
||||
if(args.length() > 2){
|
||||
openWithDefault = args.getBoolean(2);
|
||||
}
|
||||
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))) {
|
||||
successObj.put("status", PluginResult.Status.OK.ordinal());
|
||||
successObj.put("message", "Installed");
|
||||
}
|
||||
else {
|
||||
successObj.put("status", PluginResult.Status.NO_RESULT.ordinal());
|
||||
successObj.put("message", "Not installed");
|
||||
}
|
||||
callbackContext.success(successObj);
|
||||
}
|
||||
else {
|
||||
JSONObject errorObj = new JSONObject();
|
||||
errorObj.put("status", PluginResult.Status.INVALID_ACTION.ordinal());
|
||||
errorObj.put("message", "Invalid action");
|
||||
callbackContext.error(errorObj);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void _open(String fileArg, String contentType, Boolean openWithDefault, CallbackContext callbackContext) throws JSONException {
|
||||
String fileName = "";
|
||||
try {
|
||||
CordovaResourceApi resourceApi = webView.getResourceApi();
|
||||
Uri fileUri = resourceApi.remapUri(Uri.parse(fileArg));
|
||||
fileName = fileUri.getPath();
|
||||
} catch (Exception e) {
|
||||
fileName = fileArg;
|
||||
}
|
||||
File file = new File(fileName);
|
||||
if (file.exists()) {
|
||||
try {
|
||||
if (contentType == null || contentType.trim().equals("")) {
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* @see
|
||||
* http://stackoverflow.com/questions/14321376/open-an-activity-from-a-cordovaplugin
|
||||
*/
|
||||
if(openWithDefault){
|
||||
cordova.getActivity().startActivity(intent);
|
||||
}
|
||||
else{
|
||||
cordova.getActivity().startActivity(Intent.createChooser(intent, "Open File in..."));
|
||||
}
|
||||
|
||||
callbackContext.success();
|
||||
} catch (android.content.ActivityNotFoundException e) {
|
||||
JSONObject errorObj = new JSONObject();
|
||||
errorObj.put("status", PluginResult.Status.ERROR.ordinal());
|
||||
errorObj.put("message", "Activity not found: " + e.getMessage());
|
||||
callbackContext.error(errorObj);
|
||||
}
|
||||
} else {
|
||||
JSONObject errorObj = new JSONObject();
|
||||
errorObj.put("status", PluginResult.Status.ERROR.ordinal());
|
||||
errorObj.put("message", "File not found");
|
||||
callbackContext.error(errorObj);
|
||||
}
|
||||
}
|
||||
|
||||
private String _getMimeType(String url) {
|
||||
String mimeType = "*/*";
|
||||
int extensionIndex = url.lastIndexOf('.');
|
||||
if (extensionIndex > 0) {
|
||||
String extMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(url.substring(extensionIndex+1));
|
||||
if (extMimeType != null) {
|
||||
mimeType = extMimeType;
|
||||
}
|
||||
}
|
||||
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;
|
||||
try {
|
||||
pm.getPackageInfo(packageId, PackageManager.GET_ACTIVITIES);
|
||||
appInstalled = true;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
appInstalled = false;
|
||||
}
|
||||
return appInstalled;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 pwlin - pwlin05@gmail.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
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 androidx.core.content.FileProvider {
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- https://developer.android.com/reference/android/support/v4/content/FileProvider.html#SpecifyFiles -->
|
||||
<paths xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<!-- cordova.file.dataDirectory -->
|
||||
<files-path name="files" path="." />
|
||||
<!-- cordova.file.cacheDirectory -->
|
||||
<cache-path name="cache" path="." />
|
||||
<!-- cordova.file.externalDataDirectory -->
|
||||
<external-files-path name="external-files" path="." />
|
||||
<!-- cordova.file.externalCacheDirectory -->
|
||||
<external-cache-path name="external-cache" path="." />
|
||||
<!-- cordova.file.externalRootDirectory -->
|
||||
<external-path name="external" path="." />
|
||||
</paths>
|
||||
125
plugins/cordova-plugin-file-opener2/src/browser/FileOpener2.js
vendored
Normal file
125
plugins/cordova-plugin-file-opener2/src/browser/FileOpener2.js
vendored
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2019 fefc - fefc.dev@gmail.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
const cacheDirectory = (require('./isChrome')()) ? 'filesystem:' + window.location.origin + '/temporary/' : 'file:///temporary/';
|
||||
const dataDirectory = (require('./isChrome')()) ? 'filesystem:' + window.location.origin + '/persistent/' : 'file:///persistent/';
|
||||
|
||||
function open(successCallback, errorCallback, data) {
|
||||
var fullFilePath = data[0];
|
||||
//var contentType = data[1]; //Not needed in browser
|
||||
//var openDialog = data[2]; //Not needed in browser
|
||||
|
||||
var dirPath = fullFilePath.substring(0, fullFilePath.lastIndexOf('/') + 1);
|
||||
var fileName = fullFilePath.substring(fullFilePath.lastIndexOf('/') + 1, fullFilePath.length);
|
||||
var fileSystemLocalPath = getLocalPathAndFileSystem(dirPath);
|
||||
|
||||
if (!fileSystemLocalPath.error) {
|
||||
window.requestFileSystem(fileSystemLocalPath.fileSystem, 0, (fs) => {
|
||||
readFile(fs.root, fileSystemLocalPath.localPath + fileName).then((blob) => {
|
||||
FileSaver.saveAs(blob, fileName);
|
||||
successCallback();
|
||||
}).catch((error) => {
|
||||
errorCallback(error);
|
||||
});
|
||||
}, (error) => {
|
||||
errorCallback(error);
|
||||
});
|
||||
} else {
|
||||
errorCallback('INVALID_PATH');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Gets the localPath according to the fileSystem (TEMPORARY or PERSISTENT).
|
||||
*
|
||||
* @param {String} Path to the file or directory to check
|
||||
* @returns {Object} value with informations to requestFileSystem later
|
||||
* @returns {string} value.localPath The localPath in relation with fileSystem.
|
||||
* @returns {number} value.fileSystem the fileSystem (TEMPORARY or PERSISTENT).
|
||||
* @returns {error} value.error if the path is not valid.
|
||||
* @returns {message} value.message error message.
|
||||
*/
|
||||
function getLocalPathAndFileSystem(pathToCheck) {
|
||||
let ret = {
|
||||
localPath: '',
|
||||
fileSystem: window.TEMPORARY
|
||||
};
|
||||
|
||||
if (pathToCheck.startsWith(cacheDirectory)) {
|
||||
ret.localPath = pathToCheck.replace(cacheDirectory, '');
|
||||
ret.fileSystem = window.TEMPORARY;
|
||||
|
||||
} else if (pathToCheck.startsWith(dataDirectory)) {
|
||||
ret.localPath = pathToCheck.replace(dataDirectory, '');
|
||||
ret.fileSystem = window.PERSISTENT;
|
||||
|
||||
} else {
|
||||
return {error: true, message: 'INVALID_PATH'};
|
||||
}
|
||||
|
||||
if (!ret.localPath.endsWith('/')) ret.localPath += '/';
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Reads a file in the fileSystem as an DataURL.
|
||||
*
|
||||
* @param {String} Root is the root folder of the fileSystem.
|
||||
* @param {String} Path is the file to be red.
|
||||
* @returns {Promise} which resolves with an Object containing DataURL, rejects if something went wrong.
|
||||
*/
|
||||
function readFile(root, filePath) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (filePath.startsWith('/')) filePath = filePath.substring(1);
|
||||
|
||||
root.getFile(filePath, {}, (fileEntry) => {
|
||||
fileEntry.file((file) => {
|
||||
let reader = new FileReader();
|
||||
|
||||
reader.onload = function() {
|
||||
resolve(reader.result);
|
||||
};
|
||||
|
||||
reader.onerror = function() {
|
||||
reject(reader.error);
|
||||
}
|
||||
|
||||
reader.readAsDataURL(file);
|
||||
|
||||
}, (error) => {
|
||||
reject(error);
|
||||
});
|
||||
}, (error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
open: open
|
||||
};
|
||||
|
||||
require( "cordova/exec/proxy" ).add( "FileOpener2", module.exports );
|
||||
3
plugins/cordova-plugin-file-opener2/src/browser/FileSaver.min.js
vendored
Normal file
3
plugins/cordova-plugin-file-opener2/src/browser/FileSaver.min.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
(function(a,b){if("function"==typeof define&&define.amd)define([],b);else if("undefined"!=typeof exports)b();else{b(),a.FileSaver={exports:{}}.exports}})(this,function(){"use strict";function b(a,b){return"undefined"==typeof b?b={autoBom:!1}:"object"!=typeof b&&(console.warn("Deprecated: Expected third argument to be a object"),b={autoBom:!b}),b.autoBom&&/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(a.type)?new Blob(["\uFEFF",a],{type:a.type}):a}function c(b,c,d){var e=new XMLHttpRequest;e.open("GET",b),e.responseType="blob",e.onload=function(){a(e.response,c,d)},e.onerror=function(){console.error("could not download file")},e.send()}function d(a){var b=new XMLHttpRequest;b.open("HEAD",a,!1);try{b.send()}catch(a){}return 200<=b.status&&299>=b.status}function e(a){try{a.dispatchEvent(new MouseEvent("click"))}catch(c){var b=document.createEvent("MouseEvents");b.initMouseEvent("click",!0,!0,window,0,0,0,80,20,!1,!1,!1,!1,0,null),a.dispatchEvent(b)}}var f="object"==typeof window&&window.window===window?window:"object"==typeof self&&self.self===self?self:"object"==typeof global&&global.global===global?global:void 0,a=f.saveAs||("object"!=typeof window||window!==f?function(){}:"download"in HTMLAnchorElement.prototype?function(b,g,h){var i=f.URL||f.webkitURL,j=document.createElement("a");g=g||b.name||"download",j.download=g,j.rel="noopener","string"==typeof b?(j.href=b,j.origin===location.origin?e(j):d(j.href)?c(b,g,h):e(j,j.target="_blank")):(j.href=i.createObjectURL(b),setTimeout(function(){i.revokeObjectURL(j.href)},4E4),setTimeout(function(){e(j)},0))}:"msSaveOrOpenBlob"in navigator?function(f,g,h){if(g=g||f.name||"download","string"!=typeof f)navigator.msSaveOrOpenBlob(b(f,h),g);else if(d(f))c(f,g,h);else{var i=document.createElement("a");i.href=f,i.target="_blank",setTimeout(function(){e(i)})}}:function(a,b,d,e){if(e=e||open("","_blank"),e&&(e.document.title=e.document.body.innerText="downloading..."),"string"==typeof a)return c(a,b,d);var g="application/octet-stream"===a.type,h=/constructor/i.test(f.HTMLElement)||f.safari,i=/CriOS\/[\d]+/.test(navigator.userAgent);if((i||g&&h)&&"undefined"!=typeof FileReader){var j=new FileReader;j.onloadend=function(){var a=j.result;a=i?a:a.replace(/^data:[^;]*;/,"data:attachment/file;"),e?e.location.href=a:location=a,e=null},j.readAsDataURL(a)}else{var k=f.URL||f.webkitURL,l=k.createObjectURL(a);e?e.location=l:location.href=l,e=null,setTimeout(function(){k.revokeObjectURL(l)},4E4)}});f.saveAs=a.saveAs=a,"undefined"!=typeof module&&(module.exports=a)});
|
||||
|
||||
//# sourceMappingURL=FileSaver.min.js.map
|
||||
35
plugins/cordova-plugin-file-opener2/src/electron/FileOpener2.js
vendored
Normal file
35
plugins/cordova-plugin-file-opener2/src/electron/FileOpener2.js
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2020 pwlin - pwlin05@gmail.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
// https://www.electronjs.org/docs/api/shell
|
||||
const { shell } = global.require('electron');
|
||||
module.exports = {
|
||||
open: function (onSuccess, onError, fileName) {
|
||||
var opn = shell.openItem(fileName[0]);
|
||||
if (opn === true) {
|
||||
onSuccess(true);
|
||||
} else {
|
||||
onError({'status': 0, 'message': 'Failed opening file.'});
|
||||
}
|
||||
}
|
||||
};
|
||||
require('cordova/exec/proxy').add('FileOpener2', module.exports);
|
||||
35
plugins/cordova-plugin-file-opener2/src/ios/FileOpener2.h
Normal file
35
plugins/cordova-plugin-file-opener2/src/ios/FileOpener2.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 pwlin - pwlin05@gmail.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#import <Cordova/CDV.h>
|
||||
|
||||
@interface FileOpener2 : CDVPlugin <UIDocumentInteractionControllerDelegate> {
|
||||
NSString *localFile;
|
||||
}
|
||||
|
||||
@property(nonatomic, strong) UIDocumentInteractionController *controller;
|
||||
@property(nonatomic, strong) CDVViewController *cdvViewController;
|
||||
|
||||
- (void) open: (CDVInvokedUrlCommand*)command;
|
||||
|
||||
@end
|
||||
151
plugins/cordova-plugin-file-opener2/src/ios/FileOpener2.m
Normal file
151
plugins/cordova-plugin-file-opener2/src/ios/FileOpener2.m
Normal file
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 pwlin - pwlin05@gmail.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#import "FileOpener2.h"
|
||||
#import <Cordova/CDV.h>
|
||||
|
||||
#import <QuartzCore/QuartzCore.h>
|
||||
#import <MobileCoreServices/MobileCoreServices.h>
|
||||
|
||||
@implementation FileOpener2
|
||||
@synthesize controller = docController;
|
||||
|
||||
CDVPluginResult* pluginResult = nil;
|
||||
NSString* callbackId = nil;
|
||||
|
||||
- (void) open: (CDVInvokedUrlCommand*)command {
|
||||
callbackId = command.callbackId;
|
||||
NSString *path = [command.arguments objectAtIndex:0];
|
||||
NSString *contentType = [command.arguments objectAtIndex:1];
|
||||
BOOL showPreview = YES;
|
||||
|
||||
if ([command.arguments count] >= 3) {
|
||||
showPreview = [[command.arguments objectAtIndex:2] boolValue];
|
||||
}
|
||||
|
||||
CDVViewController* cont = (CDVViewController*)[super viewController];
|
||||
self.cdvViewController = cont;
|
||||
NSString *uti = nil;
|
||||
|
||||
if ([contentType length] == 0) {
|
||||
NSArray *dotParts = [path componentsSeparatedByString:@"."];
|
||||
NSString *fileExt = [dotParts lastObject];
|
||||
|
||||
uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExt, NULL);
|
||||
} else {
|
||||
uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (__bridge CFStringRef)contentType, NULL);
|
||||
}
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
NSURL *fileURL = NULL;
|
||||
NSString *decodedPath = [path stringByRemovingPercentEncoding];
|
||||
|
||||
if ([path isEqualToString:decodedPath]) {
|
||||
NSLog(@"Path parameter not encoded. Building file URL encoding it...");
|
||||
fileURL = [NSURL fileURLWithPath:[path stringByReplacingOccurrencesOfString:@"file://" withString:@""]];;
|
||||
} else {
|
||||
NSLog(@"Path parameter already encoded. Building file URL without encoding it...");
|
||||
fileURL = [NSURL URLWithString:path];
|
||||
}
|
||||
|
||||
localFile = fileURL.path;
|
||||
|
||||
NSLog(@"looking for file at %@", fileURL);
|
||||
NSFileManager *fm = [NSFileManager defaultManager];
|
||||
|
||||
if (![fm fileExistsAtPath:localFile]) {
|
||||
NSDictionary *jsonObj = @{@"status" : @"9",
|
||||
@"message" : @"File does not exist"};
|
||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:jsonObj];
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
return;
|
||||
}
|
||||
|
||||
docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
|
||||
docController.delegate = self;
|
||||
docController.UTI = uti;
|
||||
|
||||
//Opens the file preview
|
||||
CGRect rect;
|
||||
|
||||
if ([command.arguments count] >= 4) {
|
||||
NSArray *positionValues = [command.arguments objectAtIndex:3];
|
||||
|
||||
if (![positionValues isEqual:[NSNull null]] && [positionValues count] >= 2) {
|
||||
rect = CGRectMake(0, 0, [[positionValues objectAtIndex:0] floatValue], [[positionValues objectAtIndex:1] floatValue]);
|
||||
} else {
|
||||
rect = CGRectMake(0, 0, 0, 0);
|
||||
}
|
||||
} else {
|
||||
rect = CGRectMake(0, 0, cont.view.bounds.size.width, cont.view.bounds.size.height);
|
||||
}
|
||||
|
||||
BOOL wasOpened = NO;
|
||||
|
||||
if (showPreview) {
|
||||
wasOpened = [docController presentPreviewAnimated: NO];
|
||||
} else {
|
||||
CDVViewController* cont = self.cdvViewController;
|
||||
wasOpened = [docController presentOpenInMenuFromRect:rect inView:cont.view animated:YES];
|
||||
}
|
||||
|
||||
if (wasOpened) {
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @""];
|
||||
} else {
|
||||
NSDictionary *jsonObj = [ [NSDictionary alloc]
|
||||
initWithObjectsAndKeys :
|
||||
@"9", @"status",
|
||||
@"Could not handle UTI", @"message",
|
||||
nil
|
||||
];
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:jsonObj];
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation FileOpener2 (UIDocumentInteractionControllerDelegate)
|
||||
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
|
||||
}
|
||||
|
||||
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller {
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
|
||||
}
|
||||
|
||||
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
|
||||
UIViewController *presentingViewController = self.viewController;
|
||||
|
||||
if (presentingViewController.view.window != [UIApplication sharedApplication].keyWindow) {
|
||||
presentingViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
|
||||
}
|
||||
|
||||
while (presentingViewController.presentedViewController != nil && ![presentingViewController.presentedViewController isBeingDismissed]) {
|
||||
presentingViewController = presentingViewController.presentedViewController;
|
||||
}
|
||||
|
||||
return presentingViewController;
|
||||
}
|
||||
|
||||
@end
|
||||
97
plugins/cordova-plugin-file-opener2/src/windows/fileOpener2Proxy.js
vendored
Normal file
97
plugins/cordova-plugin-file-opener2/src/windows/fileOpener2Proxy.js
vendored
Normal file
@@ -0,0 +1,97 @@
|
||||
|
||||
var cordova = require('cordova'),
|
||||
fileOpener2 = require('./FileOpener2');
|
||||
|
||||
var schemes = [
|
||||
{ protocol: 'ms-app', getFile: getFileFromApplicationUri },
|
||||
{ protocol: 'cdvfile', getFile: getFileFromFileUri } //protocol cdvfile
|
||||
]
|
||||
|
||||
function nthIndex(str, pat, n) {
|
||||
var L = str.length, i = -1;
|
||||
while (n-- && i++ < L) {
|
||||
i = str.indexOf(pat, i);
|
||||
if (i < 0) break;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
function getFileFromApplicationUri(uri) {
|
||||
/* bad path from a file entry due to the last '//'
|
||||
example: ms-appdata:///local//path/to/file
|
||||
*/
|
||||
var index = nthIndex(uri, "//", 3);
|
||||
var newUri = uri.substr(0, index) + uri.substr(index + 1);
|
||||
|
||||
var applicationUri = new Windows.Foundation.Uri(newUri);
|
||||
|
||||
return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(applicationUri);
|
||||
}
|
||||
|
||||
function getFileFromFileUri(uri) {
|
||||
/* uri example:
|
||||
cdvfile://localhost/persistent|temporary|another-fs-root/path/to/file
|
||||
*/
|
||||
var indexFrom = nthIndex(uri, "/", 3) + 1;
|
||||
var indexTo = nthIndex(uri, "/", 4);
|
||||
var whichFolder = uri.substring(indexFrom, indexTo);
|
||||
var filePath = uri.substr(indexTo + 1);
|
||||
var path = "\\" + filePath;
|
||||
|
||||
if (whichFolder == "persistent") {
|
||||
path = Windows.Storage.ApplicationData.current.localFolder.path + path;
|
||||
}
|
||||
else { //temporary, note: no roaming management
|
||||
path = Windows.Storage.ApplicationData.current.temporaryFolder.path + path;
|
||||
}
|
||||
|
||||
return getFileFromNativePath(path);
|
||||
}
|
||||
|
||||
function getFileFromNativePath(path) {
|
||||
var nativePath = path.split("/").join("\\");
|
||||
|
||||
return Windows.Storage.StorageFile.getFileFromPathAsync(nativePath);
|
||||
}
|
||||
|
||||
function getFileLoaderForScheme(path) {
|
||||
var fileLoader = getFileFromNativePath;
|
||||
|
||||
schemes.some(function (scheme) {
|
||||
return path.indexOf(scheme.protocol) === 0 ? ((fileLoader = scheme.getFile), true) : false;
|
||||
});
|
||||
|
||||
return fileLoader;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
open: function (successCallback, errorCallback, args) {
|
||||
|
||||
var path = args[0];
|
||||
|
||||
var getFile = getFileLoaderForScheme(path);
|
||||
|
||||
getFile(path).then(function (file) {
|
||||
var options = new Windows.System.LauncherOptions();
|
||||
|
||||
try{
|
||||
Windows.System.Launcher.launchFileAsync(file, options).then(function (success) {
|
||||
successCallback();
|
||||
}, function (error) {
|
||||
errorCallback(error);
|
||||
});
|
||||
}catch(error){
|
||||
errorCallback(error);
|
||||
}
|
||||
|
||||
}, function (error) {
|
||||
console.log("Error while opening the file: "+error);
|
||||
errorCallback(error);
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
require("cordova/exec/proxy").add("FileOpener2", module.exports);
|
||||
|
||||
26
plugins/cordova-plugin-file-opener2/www/browser/isChrome.js
vendored
Normal file
26
plugins/cordova-plugin-file-opener2/www/browser/isChrome.js
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
module.exports = function () {
|
||||
// window.webkitRequestFileSystem and window.webkitResolveLocalFileSystemURL are available only in Chrome and
|
||||
// possibly a good flag to indicate that we're running in Chrome
|
||||
return window.webkitRequestFileSystem && window.webkitResolveLocalFileSystemURL;
|
||||
};
|
||||
@@ -0,0 +1,51 @@
|
||||
/*jslint browser: true, devel: true, node: true, sloppy: true, plusplus: true*/
|
||||
/*global require, cordova */
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 pwlin - pwlin05@gmail.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
var exec = require('cordova/exec');
|
||||
|
||||
function FileOpener2() {}
|
||||
|
||||
FileOpener2.prototype.open = function (fileName, contentType, callbackContext) {
|
||||
contentType = contentType || '';
|
||||
callbackContext = callbackContext || {};
|
||||
exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'open', [fileName, contentType]);
|
||||
};
|
||||
|
||||
FileOpener2.prototype.showOpenWithDialog = function (fileName, contentType, callbackContext) {
|
||||
contentType = contentType || '';
|
||||
callbackContext = callbackContext || {};
|
||||
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]);
|
||||
};
|
||||
|
||||
module.exports = new FileOpener2();
|
||||
Reference in New Issue
Block a user