From 3589aa428899662b815bfaf816a49b056f2553f5 Mon Sep 17 00:00:00 2001 From: Caine72 Date: Fri, 3 Jun 2016 13:14:34 +0200 Subject: [PATCH] Adding Extras bundle to the URL as an json-string Hi. Note: As i cannot run your latest version, it seems to be some corruption in the plugin.xml, i have not verified this on the latest src. only on 4.1.5 and patched the code by hand. But there should at most only be simple compile issues. Reson for patch: I need to catch Extras data from a calling Intent. Hence this patch includes them as a JSON formatted string. Patch only covers this scenario from a warm start start ( i.e. app is already started) as it't the only time i need it caught. But it might be extended to the other cases in the same manner. --- .../nl/xservices/plugins/LaunchMyApp.java | 80 +++++++++++++++++-- 1 file changed, 72 insertions(+), 8 deletions(-) diff --git a/src/android/nl/xservices/plugins/LaunchMyApp.java b/src/android/nl/xservices/plugins/LaunchMyApp.java index 3a52d28..8e615a4 100644 --- a/src/android/nl/xservices/plugins/LaunchMyApp.java +++ b/src/android/nl/xservices/plugins/LaunchMyApp.java @@ -1,12 +1,16 @@ package nl.xservices.plugins; import android.content.Intent; +import android.util.Patterns; +import android.util.Log; +import android.os.Bundle; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaActivity; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaWebView; import org.apache.cordova.PluginResult; +import org.json.JSONObject; import org.json.JSONArray; import org.json.JSONException; @@ -14,12 +18,21 @@ import java.io.StringWriter; import java.io.Writer; import java.util.Locale; +import java.util.Iterator; +import java.util.Set; +import java.util.List; +import java.util.ArrayList; +import java.util.regex.Pattern; + +import java.lang.StringBuilder; +import java.net.URLEncoder; public class LaunchMyApp extends CordovaPlugin { private static final String ACTION_CHECKINTENT = "checkIntent"; private static final String ACTION_CLEARINTENT = "clearIntent"; private static final String ACTION_GETLASTINTENT = "getLastIntent"; + public static final String TAG = "LaunchMyApp"; private String lastIntentString = null; @@ -49,10 +62,13 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo return true; } else if (ACTION_CHECKINTENT.equalsIgnoreCase(action)) { final Intent intent = ((CordovaActivity) this.webView.getContext()).getIntent(); - final String intentString = intent.getDataString(); - if (intentString != null && intent.getScheme() != null) { + if (intent.getDataString() != null && intent.getScheme() != null) { + final String intentString = intent.getDataString(); + final String jsonString = jsonStringExtras(intent); + final List urlList = urlListCreator(intentString,jsonString); + final String sendUrl = urlStringBuilder(urlList); lastIntentString = intentString; - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, intent.getDataString())); + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, sendUrl)); } else { callbackContext.error("App was not started via the launchmyapp URL scheme. Ignoring this errorcallback is the best approach."); } @@ -73,19 +89,67 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo @Override public void onNewIntent(Intent intent) { final String intentString = intent.getDataString(); + final String jsonString = jsonStringExtras(intent); if (intentString != null && intent.getScheme() != null) { if (resetIntent){ intent.setData(null); } - try { - StringWriter writer = new StringWriter(intentString.length() * 2); - escapeJavaStyleString(writer, intentString, true, false); - webView.loadUrl("javascript:handleOpenURL('" + writer.toString() + "');"); - } catch (IOException ignore) { + final List urlList = urlListCreator(intentString,jsonString); + final String sendUrl = urlStringBuilder(urlList); + webView.loadUrl("javascript:handleOpenURL('"+sendUrl+"');"); } } } + + private List urlListCreator(String... urls){ + StringWriter writer = null; + List urlList = new ArrayList(); + try{ + for(String url: urls){ + writer = new StringWriter(url.length() * 2); + escapeJavaStyleString(writer, url, true, false); + urlList.add(writer.toString()); + } + } catch (IOException ignore) {} + return urlList; + } + + private String urlStringBuilder(List urls){ + StringBuilder sb = new StringBuilder(); + for(String url: urls){ + sb.append(url); + sb.append(";"); //spliting char + } + sb.setLength(Math.max(sb.length() - 1, 0)); //remove last split char + return sb.toString(); + } + + public static String jsonStringExtras(Intent i){ + Bundle bundle = i.getExtras(); + JSONObject json = new JSONObject(); + String jsonString = json.toString(); + try{ + if (bundle != null) { + Set keys = bundle.keySet(); + Iterator it = keys.iterator(); + while (it.hasNext()) { + String key = it.next(); + String value = bundle.get(key).toString(); + //As the URLdecode json.toString() method escaps '//'' we need to re-encode possible URLs again + if(Patterns.WEB_URL.matcher(value).matches()){ + value = URLEncoder.encode(value); + } + json.put(key,value); + } + jsonString = json.toString(); + } + }catch(JSONException jse){ + }finally{ + return jsonString; + } +} + // Taken from commons StringEscapeUtils private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote, boolean escapeForwardSlash) throws IOException {