From a38ddb758880a43206fd65d5fb89184563d667d1 Mon Sep 17 00:00:00 2001 From: rich Date: Tue, 23 Feb 2016 18:08:18 +0100 Subject: [PATCH 1/2] Convert "extras" data to a JSONObject so it can be accessed from the application. --- src/android/IntentPlugin.java | 38 ++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/android/IntentPlugin.java b/src/android/IntentPlugin.java index cf8941d..e27958a 100644 --- a/src/android/IntentPlugin.java +++ b/src/android/IntentPlugin.java @@ -164,7 +164,7 @@ private JSONObject getIntentJson(Intent intent) { } intentJSON.put("type", intent.getType()); - intentJSON.put("extras", intent.getExtras()); + intentJSON.put("extras", toJsonObject(intent.getExtras())); intentJSON.put("action", intent.getAction()); intentJSON.put("categories", intent.getCategories()); intentJSON.put("flags", intent.getFlags()); @@ -182,4 +182,40 @@ private JSONObject getIntentJson(Intent intent) { } } + private static JSONObject toJsonObject(Bundle bundle) { + try { + return (JSONObject) toJsonValue(bundle); + } catch (JSONException e) { + throw new IllegalArgumentException("Cannot convert bundle to JSON: " + e.getMessage(), e); + } + } + + private static Object toJsonValue(final Object value) throws JSONException { + if (value == null) { + return null; + } else if (value instanceof Bundle) { + final Bundle bundle = (Bundle) value; + final JSONObject result = new JSONObject(); + for (final String key : bundle.keySet()) { + result.put(key, toJsonValue(bundle.get(key))); + } + return result; + } else if (value.getClass().isArray()) { + final JSONArray result = new JSONArray(); + int length = Array.getLength(value); + for (int i = 0; i < length; ++i) { + result.put(i, toJsonValue(Array.get(value, i))); + } + return result; + } else if ( + value instanceof String + || value instanceof Boolean + || value instanceof Integer + || value instanceof Long + || value instanceof Double) { + return value; + } else { + return String.valueOf(value); + } + } } From 06a493820e69b1f4f5c48dacfbb7e7775151d604 Mon Sep 17 00:00:00 2001 From: rich Date: Tue, 23 Feb 2016 18:15:19 +0100 Subject: [PATCH 2/2] Fixed missing imports --- src/android/IntentPlugin.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/android/IntentPlugin.java b/src/android/IntentPlugin.java index e27958a..1524b76 100644 --- a/src/android/IntentPlugin.java +++ b/src/android/IntentPlugin.java @@ -1,5 +1,6 @@ package com.napolitano.cordova.plugin.intent; +import java.lang.reflect.Array; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Set; @@ -11,6 +12,7 @@ import android.content.ClipData; import android.content.Intent; import android.os.Build; +import android.os.Bundle; import android.util.Log; import android.content.ContentResolver;