Skip to content

Commit

Permalink
Merge pull request #4 from richmartin/master
Browse files Browse the repository at this point in the history
Convert intent extras to JSON so that they can be access from the application.
  • Loading branch information
napolitano committed Feb 29, 2016
2 parents 2f73561 + 06a4938 commit a554df7
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/android/IntentPlugin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -164,7 +166,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());
Expand All @@ -182,4 +184,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);
}
}
}

0 comments on commit a554df7

Please sign in to comment.