From bfb64374051453f19f9533baf514503cfe395e05 Mon Sep 17 00:00:00 2001 From: pwlin Date: Mon, 10 Feb 2014 12:42:31 +0100 Subject: [PATCH] Initial commit --- .gitignore | 31 +---- LICENSE | 2 +- README.md | 47 +++++++- plugin.xml | 31 +++++ .../plugins/fileopener2/FileOpener2.java | 114 ++++++++++++++++++ www/plugins.FileOpener2.js | 11 ++ 6 files changed, 204 insertions(+), 32 deletions(-) create mode 100644 plugin.xml create mode 100644 src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java create mode 100644 www/plugins.FileOpener2.js diff --git a/.gitignore b/.gitignore index 648a292..0f182a0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,29 +1,6 @@ -# built application files -*.apk -*.ap_ - -# files for the dex VM -*.dex - -# Java class files *.class -# generated files -bin/ -gen/ - -# Local configuration file (sdk path, etc) -local.properties - -# Eclipse project files -.classpath -.project - -# Proguard folder generated by Eclipse -proguard/ - -# Intellij project files -*.iml -*.ipr -*.iws -.idea/ +# Package Files # +*.jar +*.war +*.ear diff --git a/LICENSE b/LICENSE index 32cdf44..05560d9 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014 pwlin +Copyright (c) 2013 pwlin 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 diff --git a/README.md b/README.md index 8f0208c..3354e96 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,43 @@ -cordova-plugin-file-opener2 -=========================== - -A File Opener Plugin for Cordova +A File Opener Plugin for Cordova +========================== +This plugin will open a file on your device file system with its default application. + +Requirements +------------- +- Android +- Cordova 3.0 or higher + +Installation +------------- + cordova plugin add [url-of-the-git-repo] + +Usage +------ +Open an APK install dialog: + + + +Open a PDF document with the default PDF reader and optional callback object: + + + +__Please Note that for properly opening a PDF file, you must already have a PDF reader (Acrobat Reader, Foxit Mobile PDF, etc. ) installed on your mobile device. Otherwise this will not work.__ + diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..8a55b5d --- /dev/null +++ b/plugin.xml @@ -0,0 +1,31 @@ + + + File Opener2 + A File Opener Plugin for Cordova. + MIT + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java b/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java new file mode 100644 index 0000000..a682209 --- /dev/null +++ b/src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java @@ -0,0 +1,114 @@ +package io.github.pwlin.cordova.plugins.fileopener2; + +import java.io.File; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import android.content.Intent; +import android.net.Uri; +//import android.util.Log; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CallbackContext; +import org.apache.cordova.PluginResult; + +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")) { + + try { + + return this._open(args.getString(0), args.getString(1), callbackContext); + + } catch (JSONException e) { + + JSONObject errorObj = new JSONObject(); + errorObj.put("status", PluginResult.Status.JSON_EXCEPTION.ordinal()); + errorObj.put("message", e.getMessage()); + callbackContext.error(errorObj); + return false; + } + + } else { + + JSONObject errorObj = new JSONObject(); + errorObj.put("status", PluginResult.Status.INVALID_ACTION.ordinal()); + errorObj.put("message", "Invalid action"); + callbackContext.error(errorObj); + return false; + + } + + } + + /** + * Identifies if action to be executed returns a value and should be run + * synchronously. + * + * @param action + * The action to execute + * @return T=returns value + */ + public boolean isSynch(String action) { + return false; + } + + /** + * Called by AccelBroker when listener is to be shut down. Stop listener. + */ + public void onDestroy() { + } + + private boolean _open(String fileName, String contentType, CallbackContext callbackContext) throws JSONException { + + File file = new File(fileName); + + if (file.exists()) { + + try { + + Uri path = Uri.fromFile(file); + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setDataAndType(path, contentType); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + /* + * @see http://stackoverflow.com/questions/14321376/open-an-activity-from-a-cordovaplugin + */ + cordova.getActivity().startActivity(intent); + callbackContext.success(); + return true; + + } 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); + return false; + } + + } else { + + JSONObject errorObj = new JSONObject(); + errorObj.put("status", PluginResult.Status.ERROR.ordinal()); + errorObj.put("message", "File not found"); + callbackContext.error(errorObj); + return false; + + } + } + +} diff --git a/www/plugins.FileOpener2.js b/www/plugins.FileOpener2.js new file mode 100644 index 0000000..08c63d4 --- /dev/null +++ b/www/plugins.FileOpener2.js @@ -0,0 +1,11 @@ +var exec = require("cordova/exec"); + +function FileOpener2() {}; + +FileOpener2.prototype.open = function(fileName, contentType, callbackContext) { + callbackContext = callbackContext || {}; + exec(callbackContext.success || null, callbackContext.error || null, "FileOpener2", "open", [fileName, contentType]); +}; + +var fileOpener2 = new FileOpener2(); +module.exports = fileOpener2;