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: + + <script> + cordova.plugins.fileOpener2.open( + '/sdcard/Download/gmail.apk', + 'application/vnd.android.package-archive' + ); + </script> + +Open a PDF document with the default PDF reader and optional callback object: + + <script> + cordova.plugins.fileOpener2.open( + '/sdcard/Download/starwars.pdf', + 'application/pdf', + { + error : function(errorObj) { + alert('Error status: ' + errorObj.status + ' - Error message: ' + errorObj.message); + }, + success : function () { + alert('file opened successfully'); + } + } + ); + </script> + +__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 @@ +<?xml version="1.0" encoding="UTF-8"?><plugin xmlns="http://www.phonegap.com/ns/plugins/1.0" + xmlns:android="http://schemas.android.com/apk/res/android" + id="io.github.pwlin.cordova.plugins.fileopener2" + version="1.0.3"> + + <name>File Opener2</name> + <description>A File Opener Plugin for Cordova.</description> + <license>MIT</license> + + <engines> + <engine name="cordova" version=">=3.0.0" /> + </engines> + + <js-module src="www/plugins.FileOpener2.js" name="FileOpener2"> + <clobbers target="cordova.plugins.fileOpener2" /> + </js-module> + + <!-- android --> + <platform name="android"> + <source-file src="src/android/io/github/pwlin/cordova/plugins/fileopener2/FileOpener.java" target-dir="src/io/github/pwlin/cordova/plugins/fileopener2" /> + <config-file target="res/xml/config.xml" parent="/*"> + <feature name="FileOpener2"> + <param name="android-package" value="io.github.pwlin.cordova.plugins.fileopener2.FileOpener2" /> + </feature> + </config-file> + </platform> + + <platform name="ios"> + </platform> + +</plugin> 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;