From b962d3e886f9dda95ed56e5dddf9871d3637de0a Mon Sep 17 00:00:00 2001 From: Don Coleman Date: Tue, 21 May 2013 15:30:28 -0400 Subject: [PATCH 1/4] 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 2/4] 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 3/4] 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 4/4] 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 - +