From b962d3e886f9dda95ed56e5dddf9871d3637de0a Mon Sep 17 00:00:00 2001 From: Don Coleman Date: Tue, 21 May 2013 15:30:28 -0400 Subject: [PATCH 01/16] update for 2.7 --- FileOpener.java | 46 ++++++++++++++++++++++++---------------------- README.md | 7 +++++-- fileopener.js | 8 +++++++- 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/FileOpener.java b/FileOpener.java index 658bbde..5aaa2eb 100644 --- a/FileOpener.java +++ b/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.api.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.api.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); + this.cordova.getActivity().startActivity(intent); // TODO handle ActivityNotFoundException } } diff --git a/README.md b/README.md index 471c812..49336c6 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,10 @@ 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``````. +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.7.0.js``````. ```````html - + ``````` @@ -40,6 +40,9 @@ After you run the command above, Android device will either open the file with p RELEASE NOTES ------------- +May 21, 2013 +Update for Cordova-2.7.0 + January 2, 2013 Initial release diff --git a/fileopener.js b/fileopener.js index ba99e4c..6f9b55b 100644 --- a/fileopener.js +++ b/fileopener.js @@ -13,7 +13,13 @@ function FileOpener() { }; FileOpener.prototype.open = function(url) { - cordova.exec(null, null, "FileOpener", "openFile", [url]); + var success = function() { + console.log("success!"); + } + var failure = function(error) { + console.log(error); + } + cordova.exec(success, failure, "FileOpener", "openFile", [url]); }; /** From 430bb4d4a22a24aad463c4265216a3bcde7e936e Mon Sep 17 00:00:00 2001 From: Don Coleman Date: Tue, 21 May 2013 16:32:23 -0300 Subject: [PATCH 02/16] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 49336c6..b30a30e 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ To install the plugin, move ``````fileopener.js`````` to your project's www fold 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: +In your ``````res/xml/config.xml`````` file add the following line: ``````java From 8174180a601d6091d708ea1d646e21de1bc31838 Mon Sep 17 00:00:00 2001 From: Don Coleman Date: Thu, 30 May 2013 14:52:36 -0400 Subject: [PATCH 03/16] plugman --- plugin.xml | 21 +++++++++++++++++++ .../android/FileOpener.java | 0 fileopener.js => www/fileopener.js | 0 3 files changed, 21 insertions(+) create mode 100644 plugin.xml rename FileOpener.java => src/android/FileOpener.java (100%) rename fileopener.js => www/fileopener.js (100%) diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..140fde5 --- /dev/null +++ b/plugin.xml @@ -0,0 +1,21 @@ + + + + FileOpener + + + + + + + + + + + + + diff --git a/FileOpener.java b/src/android/FileOpener.java similarity index 100% rename from FileOpener.java rename to src/android/FileOpener.java diff --git a/fileopener.js b/www/fileopener.js similarity index 100% rename from fileopener.js rename to www/fileopener.js From 485e14dbd2afbf059e35a5b5f72a01b3844d1b1f Mon Sep 17 00:00:00 2001 From: Don Coleman Date: Thu, 30 May 2013 15:31:30 -0400 Subject: [PATCH 04/16] js/ --- plugin.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.xml b/plugin.xml index 140fde5..cba6f2e 100644 --- a/plugin.xml +++ b/plugin.xml @@ -7,7 +7,7 @@ FileOpener - + From 41b52e36d3efbd888eb20288e98c2cc1b2453f24 Mon Sep 17 00:00:00 2001 From: Don Coleman Date: Mon, 3 Jun 2013 22:32:56 -0400 Subject: [PATCH 05/16] cordova-2.8.0? --- plugin.xml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/plugin.xml b/plugin.xml index cba6f2e..b9f9b7a 100644 --- a/plugin.xml +++ b/plugin.xml @@ -10,12 +10,10 @@ - - - + + + + - - - From cc8a8b5cd68afee4cde68a02fb2eeded670bfed2 Mon Sep 17 00:00:00 2001 From: Rod Biresch Date: Thu, 1 Aug 2013 14:14:54 -0400 Subject: [PATCH 06/16] WIP, updates to support 'cordova plugin add ...' --- plugin.xml | 24 ++++++++++++++++++------ src/android/FileOpener.java | 4 ++-- www/fileopener.js | 33 +++++++++++++++++---------------- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/plugin.xml b/plugin.xml index b9f9b7a..e986c9e 100644 --- a/plugin.xml +++ b/plugin.xml @@ -3,17 +3,29 @@ xmlns="http://www.phonegap.com/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="com.phonegap.plugins.fileopener.FileOpener" - version="0.0.1"> - - FileOpener + version="0.0.2"> - + + + + + File Opener + + + + + + + + - + + + - + \ No newline at end of file diff --git a/src/android/FileOpener.java b/src/android/FileOpener.java index 5aaa2eb..0cfc8bb 100644 --- a/src/android/FileOpener.java +++ b/src/android/FileOpener.java @@ -11,14 +11,14 @@ import java.io.IOException; import java.net.URLConnection; -import org.apache.cordova.api.CallbackContext; +import org.apache.cordova.CallbackContext; import org.json.JSONArray; import org.json.JSONException; import android.content.Intent; import android.net.Uri; -import org.apache.cordova.api.CordovaPlugin; +import org.apache.cordova.CordovaPlugin; public class FileOpener extends CordovaPlugin { diff --git a/www/fileopener.js b/www/fileopener.js index 6f9b55b..a692578 100644 --- a/www/fileopener.js +++ b/www/fileopener.js @@ -9,26 +9,27 @@ /** * Constructor */ -function FileOpener() { -}; -FileOpener.prototype.open = function(url) { - var success = function() { - console.log("success!"); - } - var failure = function(error) { - console.log(error); - } - cordova.exec(success, failure, "FileOpener", "openFile", [url]); +function FileOpener() {}; + +FileOpener.prototype.open = function (url) { + var success = function () { + console.log("success!"); + } + var failure = function (error) { + console.log(error); + } + cordova.exec(success, failure, "FileOpener", "openFile", [url]); }; +module.exports = FileOpener; /** * Load Plugin */ -if(!window.plugins) { - window.plugins = {}; -} -if (!window.plugins.fileOpener) { - window.plugins.fileOpener = new FileOpener(); -} +// if(!window.plugins) { +// window.plugins = {}; +// } +// if (!window.plugins.fileOpener) { +// window.plugins.fileOpener = new FileOpener(); +// } \ No newline at end of file From b7575000c97863d9a9f1df22c53a52a7fcc59eac Mon Sep 17 00:00:00 2001 From: Don Coleman Date: Thu, 1 Aug 2013 15:15:16 -0400 Subject: [PATCH 07/16] update js --- plugin.xml | 8 ++------ www/fileopener.js | 48 +++++++++++++---------------------------------- 2 files changed, 15 insertions(+), 41 deletions(-) diff --git a/plugin.xml b/plugin.xml index e986c9e..ff0cfd9 100644 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ @@ -11,12 +11,8 @@ File Opener - - - - - + diff --git a/www/fileopener.js b/www/fileopener.js index a692578..b3ddf2f 100644 --- a/www/fileopener.js +++ b/www/fileopener.js @@ -1,35 +1,13 @@ -/* - * 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) { - var success = function () { - console.log("success!"); - } - var failure = function (error) { - console.log(error); - } - cordova.exec(success, failure, "FileOpener", "openFile", [url]); -}; - -module.exports = FileOpener; -/** - * Load Plugin - */ - -// if(!window.plugins) { -// window.plugins = {}; -// } -// if (!window.plugins.fileOpener) { -// window.plugins.fileOpener = new FileOpener(); -// } \ No newline at end of file +// forked from https://github.com/markeeftb/FileOpener + +module.exports = { + open: function (url) { + var success = function () { + console.log("success!"); + } + var failure = function (error) { + console.log(error); + } + cordova.exec(success, failure, "FileOpener", "openFile", [url]); + } +} From d5a839011d7a91f109f2af88d46f461d91c62c06 Mon Sep 17 00:00:00 2001 From: Don Coleman Date: Thu, 1 Aug 2013 15:18:03 -0400 Subject: [PATCH 08/16] Update README.md --- README.md | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b30a30e..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.7.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/config.xml`````` file add the following line: - -``````java - -`````` + $ cordova plugin add https://github.com/don/FileOpener.git Using the plugin =========== @@ -40,6 +25,9 @@ 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 From 2696bb4f69c3d97845ad7c19b6c51bb6eaa2ff0c Mon Sep 17 00:00:00 2001 From: Don Coleman Date: Thu, 1 Aug 2013 15:19:41 -0400 Subject: [PATCH 09/16] bump version --- plugin.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin.xml b/plugin.xml index ff0cfd9..8565b93 100644 --- a/plugin.xml +++ b/plugin.xml @@ -3,7 +3,7 @@ xmlns="http://www.phonegap.com/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="com.phonegap.plugins.fileopener" - version="0.0.2"> + version="0.0.3"> @@ -24,4 +24,4 @@ - \ No newline at end of file + From 603a029b7921b529ea35ccbda49031512814a2b9 Mon Sep 17 00:00:00 2001 From: Don Coleman Date: Thu, 1 Aug 2013 15:32:30 -0400 Subject: [PATCH 10/16] fix case for clobber target --- plugin.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.xml b/plugin.xml index 8565b93..a5e6b11 100644 --- a/plugin.xml +++ b/plugin.xml @@ -12,7 +12,7 @@ File Opener - + From b1ddba0b8b3deba2b9609b3df3cc81958ab538d4 Mon Sep 17 00:00:00 2001 From: Justin Wark Date: Thu, 9 Jan 2014 11:22:02 +1100 Subject: [PATCH 11/16] declare js-module within platform declare js-module within platform, otherwise cordova will try and install the plugin for all other installed platforms (in my case iOS). --- plugin.xml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plugin.xml b/plugin.xml index a5e6b11..397e3e4 100644 --- a/plugin.xml +++ b/plugin.xml @@ -11,11 +11,12 @@ File Opener - - - - + + + + + From 6ad1e1c540b770b6e0f90b20e856d274ab07ffdc Mon Sep 17 00:00:00 2001 From: sgrebnov Date: Thu, 10 Apr 2014 23:20:34 +0400 Subject: [PATCH 12/16] WP8 support --- plugin.xml | 9 ++++++++ src/wp/FileOpener.cs | 50 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/wp/FileOpener.cs diff --git a/plugin.xml b/plugin.xml index a5e6b11..78eedba 100644 --- a/plugin.xml +++ b/plugin.xml @@ -24,4 +24,13 @@ + + + + + + + + + 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 From 41b3e0945c32508844e530ae4bf1e2b90aff18d5 Mon Sep 17 00:00:00 2001 From: Bruno Ferreira Date: Thu, 3 Jul 2014 14:09:04 +0100 Subject: [PATCH 13/16] Added two optional parameters to the open function It would be nice to have a custom (and optional) success and failure function in the open function so for example, if I want to show an alert when there's no app to open a "pdf" file for example I could write a proper failure to show an alert or something else. --- www/fileopener.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/www/fileopener.js b/www/fileopener.js index b3ddf2f..db477f7 100644 --- a/www/fileopener.js +++ b/www/fileopener.js @@ -1,13 +1,17 @@ // forked from https://github.com/markeeftb/FileOpener module.exports = { - open: function (url) { - var success = function () { - console.log("success!"); + open: function (url, opt_success, opt_failure) { + if (typeof opt_success == 'undefined') { + opt_success = function () { + console.log("success!"); + } } - var failure = function (error) { - console.log(error); + if (typeof opt_failure == 'undefined') { + opt_failure = function (error) { + console.log(error); + } } - cordova.exec(success, failure, "FileOpener", "openFile", [url]); + cordova.exec(opt_success, opt_failure, "FileOpener", "openFile", [url]); } } From dcbbb3e9efa01a4a0e81428ed06ebaf3ba4a6500 Mon Sep 17 00:00:00 2001 From: Bruno Ferreira Date: Thu, 3 Jul 2014 14:31:33 +0100 Subject: [PATCH 14/16] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index af70f1d..b8b496f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Simple plugin which allows you to open popular files (PDF, WORD, EXCEL, JPG, GIF Adding the Plugin to your project ----------- - $ cordova plugin add https://github.com/don/FileOpener.git + $ cordova plugin add https://github.com/jamirooo/FileOpener.git Using the plugin =========== From b151464d5458b1d9fc8bd55ce3564a782a1d56d4 Mon Sep 17 00:00:00 2001 From: Bruno Ferreira Date: Thu, 3 Jul 2014 14:42:19 +0100 Subject: [PATCH 15/16] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b8b496f..af70f1d 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Simple plugin which allows you to open popular files (PDF, WORD, EXCEL, JPG, GIF Adding the Plugin to your project ----------- - $ cordova plugin add https://github.com/jamirooo/FileOpener.git + $ cordova plugin add https://github.com/don/FileOpener.git Using the plugin =========== From 8a3a578485ff6c12750cdef0083c85ddc2aedd5f Mon Sep 17 00:00:00 2001 From: Mike Cunneen Date: Mon, 14 Jul 2014 13:30:02 +0800 Subject: [PATCH 16/16] Use the Chooser Intent instead of directly performing ACTION_VIEW . --- src/android/FileOpener.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/android/FileOpener.java b/src/android/FileOpener.java index 0cfc8bb..a006d8a 100644 --- a/src/android/FileOpener.java +++ b/src/android/FileOpener.java @@ -108,8 +108,8 @@ private void openFile(String url) throws IOException { intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, mimeType); } - - this.cordova.getActivity().startActivity(intent); // TODO handle ActivityNotFoundException + Intent fileChooser = Intent.createChooser(intent, "Open File"); + this.cordova.getActivity().startActivity(fileChooser); // TODO handle ActivityNotFoundException } }