diff --git a/README.md b/README.md index 471c812..af70f1d 100644 --- a/README.md +++ b/README.md @@ -5,22 +5,7 @@ Simple plugin which allows you to open popular files (PDF, WORD, EXCEL, JPG, GIF Adding the Plugin to your project ----------- -Using this plugin requires Android PhoneGap. It has been successfully tested on Android device with Cordova 2.2 - -To install the plugin, move ``````fileopener.js`````` to your project's www folder and include a reference to it in your html file after ``````cordova-2.2.0.js``````. - -```````html - - -``````` - -Create a directory within your project called ``````src/com/phonegap/plugins/fileopener`````` and move ``````FileOpener.java`````` into it. - -In your ``````res/xml/plugins.xml`````` file add the following line: - -``````java - -`````` + $ cordova plugin add https://github.com/don/FileOpener.git Using the plugin =========== @@ -40,6 +25,12 @@ After you run the command above, Android device will either open the file with p RELEASE NOTES ------------- +Aug 1, 2013 +Update for Cordova-3.0 + +May 21, 2013 +Update for Cordova-2.7.0 + January 2, 2013 Initial release diff --git a/fileopener.js b/fileopener.js deleted file mode 100644 index ba99e4c..0000000 --- a/fileopener.js +++ /dev/null @@ -1,28 +0,0 @@ -/* - * PhoneGap is available under *either* the terms of the modified BSD license *or* the - * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text. - * - * Copyright (c) 2005-2010, Nitobi Software Inc. - * Copyright (c) 2011, IBM Corporation - */ - -/** - * Constructor - */ -function FileOpener() { -}; - -FileOpener.prototype.open = function(url) { - cordova.exec(null, null, "FileOpener", "openFile", [url]); -}; - -/** - * Load Plugin - */ - -if(!window.plugins) { - window.plugins = {}; -} -if (!window.plugins.fileOpener) { - window.plugins.fileOpener = new FileOpener(); -} diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..ecb963e --- /dev/null +++ b/plugin.xml @@ -0,0 +1,37 @@ + + + + + + + + File Opener + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FileOpener.java b/src/android/FileOpener.java similarity index 75% rename from FileOpener.java rename to src/android/FileOpener.java index 658bbde..a006d8a 100644 --- a/FileOpener.java +++ b/src/android/FileOpener.java @@ -8,52 +8,48 @@ package com.phonegap.plugins.fileopener; -import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; +import java.net.URLConnection; +import org.apache.cordova.CallbackContext; import org.json.JSONArray; import org.json.JSONException; -import android.content.Context; import android.content.Intent; import android.net.Uri; -import org.apache.cordova.api.Plugin; -import org.apache.cordova.api.PluginResult; +import org.apache.cordova.CordovaPlugin; -public class FileOpener extends Plugin { +public class FileOpener extends CordovaPlugin { @Override - public PluginResult execute(String action, JSONArray args, String callbackId) { - PluginResult.Status status = PluginResult.Status.OK; - String result = ""; + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { try { if (action.equals("openFile")) { openFile(args.getString(0)); + callbackContext.success(); + return true; } - else { - status = PluginResult.Status.INVALID_ACTION; - } - return new PluginResult(status, result); - } catch (JSONException e) { - return new PluginResult(PluginResult.Status.JSON_EXCEPTION); } catch (IOException e) { - return new PluginResult(PluginResult.Status.IO_EXCEPTION); + e.printStackTrace(); + callbackContext.error(e.getMessage()); + } catch (RuntimeException e) { // KLUDGE for Activity Not Found + e.printStackTrace(); + callbackContext.error(e.getMessage()); } + return false; } private void openFile(String url) throws IOException { // Create URI Uri uri = Uri.parse(url); - Intent intent = null; + Intent intent; // Check what kind of file you are trying to open, by comparing the url with extensions. // When the if condition is matched, plugin sets the correct intent (mime) type, // so Android knew what application to use to open the file - + if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); @@ -94,7 +90,7 @@ private void openFile(String url) throws IOException { // Video files intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "video/*"); - } + } //if you want you can also define the intent type for any other file @@ -102,12 +98,18 @@ private void openFile(String url) throws IOException { //in this case, Android will show all applications installed on the device //so you can choose which application to use + // else { + // intent = new Intent(Intent.ACTION_VIEW); + // intent.setDataAndType(uri, "*/*"); + // } + else { + String mimeType = URLConnection.guessContentTypeFromName(url); intent = new Intent(Intent.ACTION_VIEW); - intent.setDataAndType(uri, "*/*"); + intent.setDataAndType(uri, mimeType); } - - this.cordova.getActivity().startActivity(intent); + Intent fileChooser = Intent.createChooser(intent, "Open File"); + this.cordova.getActivity().startActivity(fileChooser); // TODO handle ActivityNotFoundException } } diff --git a/src/wp/FileOpener.cs b/src/wp/FileOpener.cs new file mode 100644 index 0000000..653b7f9 --- /dev/null +++ b/src/wp/FileOpener.cs @@ -0,0 +1,50 @@ +// Copyright (c) Sergey Grebnov. Licensed under the MIT license. + +using System; +using System.Collections.Generic; +using System.IO.IsolatedStorage; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Windows.Storage; +using WPCordovaClassLib.Cordova.Commands; + +namespace WPCordovaClassLib.Cordova.Commands +{ + public class FileOpener : BaseCommand + { + async public void openFile(string options) + { + var optStings = JSON.JsonHelper.Deserialize(options); + + string filePath = optStings[0].Replace('/', '\\').Substring(2); + + try + { + + StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; + StorageFile file = await local.GetFileAsync(filePath); + + if (file == null) + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "File not found: " + filePath)); + return; + } + + var success = await Windows.System.Launcher.LaunchFileAsync(file); + + if (success) + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); + } + else + { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Unable to start the app associated with the following file: " + filePath)); + } + } + catch (Exception ex) { + this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ex.Message)); + } + } + } +} \ No newline at end of file diff --git a/www/fileopener.js b/www/fileopener.js new file mode 100644 index 0000000..db477f7 --- /dev/null +++ b/www/fileopener.js @@ -0,0 +1,17 @@ +// forked from https://github.com/markeeftb/FileOpener + +module.exports = { + open: function (url, opt_success, opt_failure) { + if (typeof opt_success == 'undefined') { + opt_success = function () { + console.log("success!"); + } + } + if (typeof opt_failure == 'undefined') { + opt_failure = function (error) { + console.log(error); + } + } + cordova.exec(opt_success, opt_failure, "FileOpener", "openFile", [url]); + } +}