-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathAndroidJavaObjectExtensions.cs
More file actions
231 lines (205 loc) · 8.77 KB
/
Copy pathAndroidJavaObjectExtensions.cs
File metadata and controls
231 lines (205 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Modified MIT License
*
* Copyright 2023 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System.Collections.Generic;
using UnityEngine;
// ReSharper disable InconsistentNaming
namespace OneSignalSDK.Android.Utilities
{
/// <summary>
/// Conversion methods for common Java types wrapped by <see cref="AndroidJavaObject"/>
/// </summary>
internal static class AndroidJavaObjectExtensions
{
/// <summary>
/// Converts from a Java class which implements a toJSONObject method which returns a JSONObject representation
/// of that object to a Serializable class in Unity
/// </summary>
public static TModel ToSerializable<TModel>(this AndroidJavaObject source)
{
if (source == default)
return default;
var json = source.Call<AndroidJavaObject>("toJSONObject");
var jsonStr = json.Call<string>("toString");
var serialized = JsonUtility.FromJson<TModel>(jsonStr);
return serialized;
}
/*
* JSONObject
*/
/// <summary>
/// Converts from a Java org.json.JSONObject to a <see cref="Dictionary{TKey,TValue}"/>
/// </summary>
public static Dictionary<string, object> JSONObjectToDictionary(
this AndroidJavaObject source
) =>
source != null
? Json.Deserialize(source.Call<string>("toString")) as Dictionary<string, object>
: null;
/// <summary>
/// Converts from a <see cref="Dictionary{TKey,TValue}"/> to a Java org.json.JSONObject
/// </summary>
public static AndroidJavaObject ToJSONObject<TKey, TValue>(
this Dictionary<TKey, TValue> source
) => new AndroidJavaObject("org.json.JSONObject", Json.Serialize(source));
/*
* Map
*/
/// <summary>
/// Converts from a Java java.util.Map to a <see cref="Dictionary{TKey,TValue}"/>
/// </summary>
public static Dictionary<string, string> MapToDictionary(this AndroidJavaObject source)
{
if (source == null)
return null;
var entries = source.Call<AndroidJavaObject>("entrySet");
var iter = entries.Call<AndroidJavaObject>("iterator");
var ret = new Dictionary<string, string>();
while (iter.Call<bool>("hasNext"))
{
var entry = iter.Call<AndroidJavaObject>("next");
var key = entry.Call<string>("getKey");
var valueJO = entry.Call<AndroidJavaObject>("getValue");
ret[key] = valueJO.Call<string>("toString");
}
return ret;
}
/// <summary>
/// Converts from a <see cref="Dictionary{TKey,TValue}"/> to a Java java.util.Map
/// </summary>
public static AndroidJavaObject ToMap(this Dictionary<string, string> source)
{
var map = new AndroidJavaObject("java.util.HashMap");
var put = AndroidJNIHelper.GetMethodID(
map.GetRawClass(),
"put",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"
);
var entryArgs = new object[2];
foreach (var kv in source)
{
using (var key = new AndroidJavaObject("java.lang.String", kv.Key))
using (var value = new AndroidJavaObject("java.lang.String", kv.Value))
{
entryArgs[0] = key;
entryArgs[1] = value;
AndroidJNI.CallObjectMethod(
map.GetRawObject(),
put,
AndroidJNIHelper.CreateJNIArgArray(entryArgs)
);
}
}
return map;
}
public static AndroidJavaObject ToMap(this Dictionary<string, object> source)
{
if (source == null)
return null;
var map = new AndroidJavaObject("java.util.HashMap");
var put = AndroidJNIHelper.GetMethodID(
map.GetRawClass(),
"put",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"
);
var entryArgs = new object[2];
foreach (var kv in source)
{
using (var key = new AndroidJavaObject("java.lang.String", kv.Key))
{
AndroidJavaObject value = null;
// Preserve original data types
switch (kv.Value)
{
case string stringValue:
value = new AndroidJavaObject("java.lang.String", stringValue);
break;
case int intValue:
value = new AndroidJavaObject("java.lang.Integer", intValue);
break;
case long longValue:
value = new AndroidJavaObject("java.lang.Long", longValue);
break;
case float floatValue:
value = new AndroidJavaObject("java.lang.Float", floatValue);
break;
case double doubleValue:
value = new AndroidJavaObject("java.lang.Double", doubleValue);
break;
case bool boolValue:
value = new AndroidJavaObject("java.lang.Boolean", boolValue);
break;
// Check IDictionary before IList: some dict-like types
// (e.g. Newtonsoft's JObject) also implement IList.
case System.Collections.IDictionary dictValue:
value = new AndroidJavaObject(
"org.json.JSONObject",
Json.Serialize(dictValue)
);
break;
case System.Collections.IList listValue:
value = new AndroidJavaObject(
"org.json.JSONArray",
Json.Serialize(listValue)
);
break;
case null:
value = null;
break;
default:
// Fallback to string representation for unsupported types
value = new AndroidJavaObject("java.lang.String", kv.Value.ToString());
break;
}
try
{
entryArgs[0] = key;
entryArgs[1] = value;
AndroidJNI.CallObjectMethod(
map.GetRawObject(),
put,
AndroidJNIHelper.CreateJNIArgArray(entryArgs)
);
}
finally
{
value?.Dispose();
}
}
}
return map;
}
public static AndroidJavaObject ToArrayList(this string[] keys)
{
AndroidJavaObject arrayList = new AndroidJavaObject("java.util.ArrayList");
foreach (string key in keys)
{
arrayList.Call<bool>("add", key);
}
return arrayList;
}
}
}