Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit b4088b6

Browse files
Backport JSONObject.wrap in order to support Jellybean (#3566)
1 parent 9e59a83 commit b4088b6

File tree

6 files changed

+72
-5
lines changed

6 files changed

+72
-5
lines changed

shell/platform/android/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ java_library("flutter_shell_java") {
8080
"io/flutter/plugin/common/FlutterMethodChannel.java",
8181
"io/flutter/plugin/common/JSONMessageCodec.java",
8282
"io/flutter/plugin/common/JSONMethodCodec.java",
83+
"io/flutter/plugin/common/JSONUtil.java",
8384
"io/flutter/plugin/common/MessageCodec.java",
8485
"io/flutter/plugin/common/MethodCall.java",
8586
"io/flutter/plugin/common/MethodCodec.java",

shell/platform/android/io/flutter/plugin/common/JSONMessageCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public ByteBuffer encodeMessage(Object message) {
2626
if (message == null) {
2727
return null;
2828
}
29-
return StringCodec.INSTANCE.encodeMessage(JSONObject.wrap(message).toString());
29+
return StringCodec.INSTANCE.encodeMessage(JSONUtil.wrap(message).toString());
3030
}
3131

3232
@Override

shell/platform/android/io/flutter/plugin/common/JSONMethodCodec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ByteBuffer encodeMethodCall(MethodCall methodCall) {
2121
try {
2222
final JSONObject map = new JSONObject();
2323
map.put("method", methodCall.method);
24-
map.put("args", JSONObject.wrap(methodCall.arguments));
24+
map.put("args", JSONUtil.wrap(methodCall.arguments));
2525
return JSONMessageCodec.INSTANCE.encodeMessage(map);
2626
} catch (JSONException e) {
2727
throw new IllegalArgumentException("Invalid JSON", e);
@@ -49,7 +49,7 @@ public MethodCall decodeMethodCall(ByteBuffer message) {
4949
@Override
5050
public ByteBuffer encodeSuccessEnvelope(Object result) {
5151
return JSONMessageCodec.INSTANCE
52-
.encodeMessage(new JSONArray().put(JSONObject.wrap(result)));
52+
.encodeMessage(new JSONArray().put(JSONUtil.wrap(result)));
5353
}
5454

5555
@Override
@@ -58,7 +58,7 @@ public ByteBuffer encodeErrorEnvelope(String errorCode, String errorMessage,
5858
return JSONMessageCodec.INSTANCE.encodeMessage(new JSONArray()
5959
.put(errorCode)
6060
.put(errorMessage)
61-
.put(JSONObject.wrap(errorDetails)));
61+
.put(JSONUtil.wrap(errorDetails)));
6262
}
6363

6464
@Override
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.flutter.plugin.common;
2+
3+
import java.lang.reflect.Array;
4+
import java.util.Collection;
5+
import java.util.Map;
6+
import org.json.JSONArray;
7+
import org.json.JSONObject;
8+
9+
public class JSONUtil {
10+
private JSONUtil() {
11+
}
12+
13+
/**
14+
* Backport of {@link JSONObject#wrap(Object)} for use on pre-KitKat
15+
* systems.
16+
*/
17+
public static Object wrap(Object o) {
18+
if (o == null) {
19+
return JSONObject.NULL;
20+
}
21+
if (o instanceof JSONArray || o instanceof JSONObject) {
22+
return o;
23+
}
24+
if (o.equals(JSONObject.NULL)) {
25+
return o;
26+
}
27+
try {
28+
if (o instanceof Collection) {
29+
JSONArray result = new JSONArray();
30+
for (Object e : (Collection) o)
31+
result.put(wrap(e));
32+
return result;
33+
} else if (o.getClass().isArray()) {
34+
JSONArray result = new JSONArray();
35+
int length = Array.getLength(o);
36+
for (int i = 0; i < length; i++)
37+
result.put(wrap(Array.get(o, i)));
38+
return result;
39+
}
40+
if (o instanceof Map) {
41+
JSONObject result = new JSONObject();
42+
for (Map.Entry<?, ?> entry: ((Map<?, ?>) o).entrySet())
43+
result.put((String) entry.getKey(), wrap(entry.getValue()));
44+
return result;
45+
}
46+
if (o instanceof Boolean ||
47+
o instanceof Byte ||
48+
o instanceof Character ||
49+
o instanceof Double ||
50+
o instanceof Float ||
51+
o instanceof Integer ||
52+
o instanceof Long ||
53+
o instanceof Short ||
54+
o instanceof String) {
55+
return o;
56+
}
57+
if (o.getClass().getPackage().getName().startsWith("java.")) {
58+
return o.toString();
59+
}
60+
} catch (Exception ignored) {
61+
}
62+
return null;
63+
}
64+
}

shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.flutter.plugin.common.FlutterMethodChannel.MethodCallHandler;
1616
import io.flutter.plugin.common.FlutterMethodChannel.Response;
1717
import io.flutter.plugin.common.JSONMethodCodec;
18+
import io.flutter.plugin.common.JSONUtil;
1819
import io.flutter.plugin.common.MethodCall;
1920
import io.flutter.view.FlutterView;
2021

@@ -137,7 +138,7 @@ private void setTextInputEditingState(FlutterView view, JSONObject state) {
137138
}
138139

139140
void setLatestEditingState(Map<String, Object> state) {
140-
mLatestState = (JSONObject) JSONObject.wrap(state);
141+
mLatestState = (JSONObject) JSONUtil.wrap(state);
141142
}
142143

143144
private void clearTextInputClient() {

travis/licenses_golden/licenses_flutter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,7 @@ FILE: ../../../flutter/lib/ui/window/window.h
10731073
FILE: ../../../flutter/runtime/platform_impl.h
10741074
FILE: ../../../flutter/shell/common/skia_event_tracer_impl.cc
10751075
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/common/JSONMethodCodec.java
1076+
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/common/JSONUtil.java
10761077
FILE: ../../../flutter/shell/platform/darwin/desktop/Info.plist
10771078
FILE: ../../../flutter/shell/platform/darwin/desktop/flutter_mac.xib
10781079
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Flutter.podspec

0 commit comments

Comments
 (0)