From b41e64026ac99dc502dd29687e78891eedc8c10d Mon Sep 17 00:00:00 2001 From: lampaa Date: Wed, 16 May 2018 15:45:28 +0300 Subject: [PATCH] updates 6.1.0 Add full support activityForResult, sendBroadcast and RegisterReceiver. Add types of extras. --- plugin.xml | 7 +- src/android/Assets.java | 58 ++++++++ src/android/startApp.java | 279 ++++++++++++++++++++------------------ www/startApp.js | 40 +++++- www/startApp.manually.js | 67 --------- 5 files changed, 241 insertions(+), 210 deletions(-) create mode 100644 src/android/Assets.java delete mode 100644 www/startApp.manually.js diff --git a/plugin.xml b/plugin.xml index 5a2c44d..7ad671f 100644 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="6.1.0"> startApp Phonegap plugin for check or launch other application in android device. @@ -20,11 +20,6 @@ - - - - - diff --git a/src/android/Assets.java b/src/android/Assets.java new file mode 100644 index 0000000..ba63c41 --- /dev/null +++ b/src/android/Assets.java @@ -0,0 +1,58 @@ +package com.lampa.startapp; + +import android.content.Intent; +import android.util.Log; + +import org.apache.cordova.CordovaPlugin; + +import java.lang.reflect.Field; + +/** + * Created by User on 16.05.2018. + */ +public class Assets extends CordovaPlugin { + protected static final String TAG = "startApp"; + protected boolean NO_PARSE_INTENT_VALS = false; + + + /** + * functions + */ + protected String parseExtraName(String extraName) { + String parseIntentExtra = extraName; + + try { + parseIntentExtra = getIntentValueString(extraName); + } + catch(NoSuchFieldException e) { + parseIntentExtra = extraName; + } + catch(IllegalAccessException e) { + e.printStackTrace(); + return extraName; + } + + Log.e(TAG, parseIntentExtra); + + return parseIntentExtra; + } + + protected String getIntentValueString(String flag) throws NoSuchFieldException, IllegalAccessException { + + if(NO_PARSE_INTENT_VALS) { + return flag; + } + + Field field = Intent.class.getDeclaredField(flag); + field.setAccessible(true); + + return (String) field.get(null); + } + + protected int getIntentValue(String flag) throws NoSuchFieldException, IllegalAccessException { + Field field = Intent.class.getDeclaredField(flag); + field.setAccessible(true); + + return field.getInt(null); + } +} diff --git a/src/android/startApp.java b/src/android/startApp.java index 2c59416..f6e4e57 100644 --- a/src/android/startApp.java +++ b/src/android/startApp.java @@ -1,5 +1,5 @@ /** - com.lampa.startapp + com.lampa.startapp, ver. 6.1.0 https://github.com/lampaa/com.lampa.startapp Phonegap plugin for check or launch other application in android device (iOS support). @@ -8,35 +8,27 @@ package com.lampa.startapp; import org.apache.cordova.CallbackContext; -import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.PluginResult; import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONException; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; import android.content.ComponentName; +import android.content.IntentFilter; import android.content.pm.PackageManager; import android.content.pm.PackageInfo; -import android.content.pm.PackageManager.NameNotFoundException; -import java.io.File; -import java.net.URI; -import java.net.URISyntaxException; +import java.util.HashMap; import java.util.Iterator; import android.net.Uri; -import java.lang.reflect.Field; -import android.content.ActivityNotFoundException; -import android.os.Build; -import android.support.v4.content.FileProvider; -import android.util.Log; import android.os.Bundle; -public class startApp extends CordovaPlugin { - - public static final String TAG = "startApp"; - public startApp() { } - private boolean NO_PARSE_INTENT_VALS = false; - +public class startApp extends Assets { + private HashMap broadcastReceiverHashMap = new HashMap<>(); + private CallbackContext callbackContext; /** * Executes the request and returns PluginResult. * @@ -46,27 +38,86 @@ public class startApp extends CordovaPlugin { * @return Always return true. */ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { - if (action.equals("start")) { - this.start(args, callbackContext); - } - else if(action.equals("go")) { - this.start(args, callbackContext); - } - else if(action.equals("check")) { - this.check(args, callbackContext); - } - else if(action.equals("getExtras")) { - this.getExtras(callbackContext); - } - else if(action.equals("getExtra")) { - this.getExtra(args, callbackContext); - } + switch (action) { + case "start": + this.start(args, callbackContext); + break; + case "check": + this.check(args, callbackContext); + break; + case "receiver": + this.receiver(args, callbackContext); + break; + case "unReceiver": + this.receiver(args, callbackContext); + break; + case "getExtras": + this.getExtras(callbackContext); + break; + case "getExtra": + this.getExtra(args, callbackContext); + break; + } return true; } - /** + /** + * + * @param args + * @param callback + */ + private void receiver(JSONArray args, CallbackContext callback) { + BroadcastReceiver receiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + JSONObject result = new JSONObject(); + + try { + result.put("_ACTION_VALUE_FORMAT_", intent.getAction()); + + Bundle bundle = intent.getExtras(); + if (bundle != null) { + for (String key : bundle.keySet()) { + result.put(key, bundle.get(key)); + } + } + } catch (JSONException e) { + e.printStackTrace(); + } + + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result); + pluginResult.setKeepCallback(true); + + callback.sendPluginResult(pluginResult); + } + }; + + try { + JSONArray values = args.getJSONArray(0); + IntentFilter filter = new IntentFilter(); + + for(int i=0; i < values.length(); i++) { + filter.addAction(values.getString(i)); + } + + cordova.getContext().registerReceiver(receiver, filter); + broadcastReceiverHashMap.put(receiver.hashCode(), receiver); + + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, receiver.hashCode()); + pluginResult.setKeepCallback(true); + + callback.sendPluginResult(pluginResult); + } + catch (Exception ex) { + ex.printStackTrace(); + callback.error("Error register receiver: " + ex.getMessage()); + } + } + + + /** * startApp */ public void start(JSONArray args, CallbackContext callback) { @@ -76,9 +127,7 @@ public class startApp extends CordovaPlugin { JSONArray component; JSONObject extra; - JSONObject key_value; String key; - String value; int i; @@ -89,7 +138,7 @@ public class startApp extends CordovaPlugin { /** * disable parsing intent values */ - if(params.has("no_parse")) { + if(params.has("noParseAction")) { NO_PARSE_INTENT_VALS = true; } @@ -106,8 +155,9 @@ public class startApp extends CordovaPlugin { return; } } + /** - * set application + * set intent * http://developer.android.com/reference/android/content/Intent.html (java.lang.String) */ else if(params.has("intent")) { @@ -157,22 +207,7 @@ public class startApp extends CordovaPlugin { * http://developer.android.com/intl/ru/reference/android/content/Intent.html#setData%28android.net.Uri%29 */ if(params.has("uri")) { - String uri_str = params.getString("uri"); - Uri uri = null; - if (uri_str.startsWith("file://")){ - // android N surport - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - LaunchIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - try { - uri = FileProvider.getUriForFile(this.cordova.getContext(), "com.lampa.startapp.fileProvider", new File(new URI(uri_str))); - } catch (URISyntaxException e) { - e.printStackTrace(); - } - } - }else{ - uri = Uri.parse(uri_str); - } - LaunchIntent.setData(uri); + LaunchIntent.setData(Uri.parse(params.getString("uri"))); } /** @@ -208,16 +243,32 @@ public class startApp extends CordovaPlugin { while (iter.hasNext()) { key = iter.next(); - - value = extra.getString(key); - LaunchIntent.putExtra(parseExtraName(key), value); + Object value = extra.get(key); + + if(value instanceof Integer) { + LaunchIntent.putExtra(parseExtraName(key), extra.getInt(key)); + } + + if(value instanceof String) { + LaunchIntent.putExtra(parseExtraName(key), extra.getString(key)); + } + + if(value instanceof Boolean) { + LaunchIntent.putExtra(parseExtraName(key), extra.getBoolean(key)); + } } } /** * launch intent */ + + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK); + pluginResult.setKeepCallback(true); + if(params.has("intentstart") && "startActivityForResult".equals(params.getString("intentstart"))) { + cordova.setActivityResultCallback (this); + callbackContext = callback; cordova.getActivity().startActivityForResult(LaunchIntent, 1); } if(params.has("intentstart") && "sendBroadcast".equals(params.getString("intentstart"))) { @@ -227,34 +278,22 @@ public class startApp extends CordovaPlugin { cordova.getActivity().startActivity(LaunchIntent); } - callback.success(); + callback.sendPluginResult(pluginResult); } else { callback.error("Incorrect params, array is not array object!"); } } - catch (JSONException e) { - callback.error("JSONException: " + e.getMessage()); - e.printStackTrace(); - } - catch (IllegalAccessException e) { - callback.error("IllegalAccessException: " + e.getMessage()); - e.printStackTrace(); - } - catch (NoSuchFieldException e) { - callback.error("NoSuchFieldException: " + e.getMessage()); - e.printStackTrace(); - } - catch (ActivityNotFoundException e) { - callback.error("ActivityNotFoundException: " + e.getMessage()); + catch (Exception e) { e.printStackTrace(); + callback.error(e.getClass() + ": " + e.getMessage()); } } /** * checkApp */ - public void check(JSONArray args, CallbackContext callback) { + private void check(JSONArray args, CallbackContext callback) { JSONObject params; try { @@ -265,20 +304,16 @@ public class startApp extends CordovaPlugin { if(params.has("package")) { PackageManager pm = cordova.getActivity().getApplicationContext().getPackageManager(); - /** - * get package info - */ + // get package info PackageInfo PackInfo = pm.getPackageInfo(params.getString("package"), PackageManager.GET_ACTIVITIES); - /** - * create json object - */ - JSONObject info = new JSONObject(); - - info.put("versionName", PackInfo.versionName); - info.put("packageName", PackInfo.packageName); - info.put("versionCode", PackInfo.versionCode); - info.put("applicationInfo", PackInfo.applicationInfo); + // create json object + JSONObject info = new JSONObject() {{ + put("versionName", PackInfo.versionName); + put("packageName", PackInfo.packageName); + put("versionCode", PackInfo.versionCode); + put("applicationInfo", PackInfo.applicationInfo); + }}; callback.success(info); } @@ -289,12 +324,9 @@ public class startApp extends CordovaPlugin { else { callback.error("Incorrect params, array is not array object!"); } - } catch (JSONException e) { - callback.error("json: " + e.toString()); - e.printStackTrace(); } - catch (NameNotFoundException e) { - callback.error("NameNotFoundException: " + e.toString()); + catch (Exception e) { + callback.error(e.getClass() + ": " + e.getMessage()); e.printStackTrace(); } } @@ -302,7 +334,7 @@ public class startApp extends CordovaPlugin { /** * getExtras */ - public void getExtras(CallbackContext callback) { + private void getExtras(CallbackContext callback) { try { Bundle extras = cordova.getActivity().getIntent().getExtras(); JSONObject info = new JSONObject(); @@ -324,7 +356,7 @@ public class startApp extends CordovaPlugin { /** * getExtra */ - public void getExtra(JSONArray args, CallbackContext callback) { + private void getExtra(JSONArray args, CallbackContext callback) { try { String extraName = parseExtraName(args.getString(0)); Intent extraIntent = cordova.getActivity().getIntent(); @@ -333,7 +365,7 @@ public class startApp extends CordovaPlugin { String extraValue = extraIntent.getStringExtra(extraName); if (extraValue == null) { - extraValue = ((Uri) extraIntent.getParcelableExtra(extraName)).toString(); + extraValue = (extraIntent.getParcelableExtra(extraName)).toString(); } callback.success(extraValue); @@ -347,45 +379,30 @@ public class startApp extends CordovaPlugin { e.printStackTrace(); } } - - /** - * functions - */ - private String parseExtraName(String extraName) { - String parseIntentExtra = extraName; - - try { - parseIntentExtra = getIntentValueString(extraName); - } - catch(NoSuchFieldException e) { - parseIntentExtra = extraName; - } - catch(IllegalAccessException e) { - e.printStackTrace(); - return extraName; - } - - Log.e(TAG, parseIntentExtra); - - return parseIntentExtra; - } - - private String getIntentValueString(String flag) throws NoSuchFieldException, IllegalAccessException { - - if(NO_PARSE_INTENT_VALS) { - return flag; - } - - Field field = Intent.class.getDeclaredField(flag); - field.setAccessible(true); - return (String) field.get(null); - } - - private int getIntentValue(String flag) throws NoSuchFieldException, IllegalAccessException { - Field field = Intent.class.getDeclaredField(flag); - field.setAccessible(true); - - return field.getInt(null); + @Override + public void onActivityResult(int requestCode, int resultCode, Intent intent) { + if(callbackContext != null) { + JSONObject result = new JSONObject(); + + try { + result.put("_ACTION_requestCode_", requestCode); + result.put("_ACTION_resultCode_", resultCode); + + Bundle bundle = intent.getExtras(); + if (bundle != null) { + for (String key : bundle.keySet()) { + result.put(key, bundle.get(key)); + } + } + } catch (JSONException e) { + e.printStackTrace(); + } + + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result); + pluginResult.setKeepCallback(true); + + callbackContext.sendPluginResult(pluginResult); + } } } diff --git a/www/startApp.js b/www/startApp.js index c6a6070..39e9056 100644 --- a/www/startApp.js +++ b/www/startApp.js @@ -1,5 +1,6 @@ +cordova.define("com.lampa.startapp.startapp", function(require, exports, module) { /** - com.lampa.startapp + com.lampa.startapp, ver. 6.1.0 https://github.com/lampaa/com.lampa.startapp Phonegap plugin for check or launch other application in android device (iOS support). @@ -29,11 +30,25 @@ module.exports = { } return { - start: function(completeCallback, errorCallback) { + start: function(completeCallback, errorCallback, messageCallback) { completeCallback = completeCallback || function() {}; errorCallback = errorCallback || function() {}; + messageCallback = messageCallback || function() {}; - exec(completeCallback, errorCallback, "startApp", "start", output); + exec(function(result) { + if(result === "OK") { + completeCallback(result); + } + else { + var requestCode = result["_ACTION_requestCode_"]; + delete result["_ACTION_requestCode_"]; + + var resultCode = result["_ACTION_resultCode_"]; + delete result["_ACTION_resultCode_"]; + + messageCallback(result, requestCode, resultCode); + } + }, errorCallback, "startApp", "start", output); }, check: function(completeCallback, errorCallback) { completeCallback = completeCallback || function() {}; @@ -41,11 +56,22 @@ module.exports = { exec(completeCallback, errorCallback, "startApp", "check", output); }, - go: function(completeCallback, errorCallback) { + receiver: function(completeCallback, errorCallback, messageCallback) { completeCallback = completeCallback || function() {}; errorCallback = errorCallback || function() {}; - - exec(completeCallback, errorCallback, "startApp", "go", output); + messageCallback = messageCallback || function() {}; + + exec(function(result) { + if(/\d+/.test(result)) { + completeCallback(result); + } + else { + var action = result["_ACTION_VALUE_FORMAT_"]; + delete result["_ACTION_VALUE_FORMAT_"]; + + messageCallback(action, result); + } + }, errorCallback, "startApp", "receiver", output); } } }, @@ -62,3 +88,5 @@ module.exports = { this.getExtra(extraValue, completeCallback, errorCallback); } } + +}); diff --git a/www/startApp.manually.js b/www/startApp.manually.js deleted file mode 100644 index 315b33a..0000000 --- a/www/startApp.manually.js +++ /dev/null @@ -1,67 +0,0 @@ -cordova.define("com.lampa.startapp.startapp", function(require, exports, module) { -/** - com.lampa.startapp - https://github.com/lampaa/com.lampa.startapp - - Phonegap plugin for check or launch other application in android device (iOS support). - bug tracker: https://github.com/lampaa/com.lampa.startapp/issues -*/ - -var exec = require('cordova/exec'); - -module.exports = { - /** - * Set application params - * - * @param {Mixed} params params, view documentation https://github.com/lampaa/com.lampa.startapp - * @param {Mixed} extra Extra fields - * @param {Function} errorCallback The callback that is called when an error occurred when the program starts. - * - */ - - set: function(params, extra) { - var output = [params]; - - if(extra != undefined) { - output.push(extra); - } - else { - output.push(null); - } - - return { - start: function(completeCallback, errorCallback) { - completeCallback = completeCallback || function() {}; - errorCallback = errorCallback || function() {}; - - exec(completeCallback, errorCallback, "startApp", "start", output); - }, - check: function(completeCallback, errorCallback) { - completeCallback = completeCallback || function() {}; - errorCallback = errorCallback || function() {}; - - exec(completeCallback, errorCallback, "startApp", "check", output); - }, - go: function(completeCallback, errorCallback) { - completeCallback = completeCallback || function() {}; - errorCallback = errorCallback || function() {}; - - exec(completeCallback, errorCallback, "startApp", "go", output); - } - } - }, - /** - * extra values - */ - getExtras: function(completeCallback, errorCallback) { - exec(completeCallback, errorCallback, "startApp", "getExtras", []); - }, - getExtra: function(extraValue, completeCallback, errorCallback) { - exec(completeCallback, errorCallback, "startApp", "getExtra", [extraValue]); - }, - hasExtra: function(extraValue, completeCallback, errorCallback) { - this.getExtra(extraValue, completeCallback, errorCallback); - } -} - -});