Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Extras bundle to the URL as an json-string #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 72 additions & 8 deletions src/android/nl/xservices/plugins/LaunchMyApp.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
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;

import java.io.IOException;
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;

Expand Down Expand Up @@ -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<String> 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.");
}
Expand All @@ -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<String> urlList = urlListCreator(intentString,jsonString);
final String sendUrl = urlStringBuilder(urlList);
webView.loadUrl("javascript:handleOpenURL('"+sendUrl+"');");
}
}
}

private List<String> urlListCreator(String... urls){
StringWriter writer = null;
List<String> urlList = new ArrayList<String>();
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<String> 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<String> keys = bundle.keySet();
Iterator<String> 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 {
Expand Down