diff --git a/FileOpener.java b/FileOpener.java index 658bbde..10aebee 100644 --- a/FileOpener.java +++ b/FileOpener.java @@ -8,52 +8,52 @@ 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)); + if( openFile(args.getString(0)) ) { + callbackContext.success(); + } + else { + callbackContext.error("Could not open file"); + } + 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 { + private boolean 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 +94,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 +102,26 @@ 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); + try + { + this.cordova.getActivity().startActivity(intent); + return true; + } + catch (Exception e) + { + return false; + } } } diff --git a/README.md b/README.md index 471c812..54f340e 100644 --- a/README.md +++ b/README.md @@ -29,10 +29,10 @@ The plugin creates the object ``````window.plugins.fileOpener``````. To use, ca Sample use: ---------- ```````javascript -window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/document.doc"); -window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/manual.pdf"); -window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/presentation.ppt"); -window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/image.jpg"); +window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/document.doc", failureCallbackFunction); +window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/manual.pdf", failureCallbackFunction); +window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/presentation.ppt", failureCallbackFunction); +window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/image.jpg", failureCallbackFunction); ``````` After you run the command above, Android device will either open the file with proper external application installed on your device or ask you which application to use, if you haven't set before which application to use to open the specific type of file. What is great, when you exit the external app, Android will return to your application. diff --git a/fileopener.js b/fileopener.js index ba99e4c..cdc1be9 100644 --- a/fileopener.js +++ b/fileopener.js @@ -12,8 +12,17 @@ function FileOpener() { }; -FileOpener.prototype.open = function(url) { - cordova.exec(null, null, "FileOpener", "openFile", [url]); +FileOpener.prototype.open = function(url, failureCB) { + var success = function() { + console.log("success!"); + } + var failure = function(error) { + console.log(error); + if(typeof failureCB === "function") { + failureCB(error); + } + } + cordova.exec(success, failure, "FileOpener", "openFile", [url]); }; /**