-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathChtExternalApp.java
More file actions
363 lines (281 loc) · 8.83 KB
/
ChtExternalApp.java
File metadata and controls
363 lines (281 loc) · 8.83 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package org.medicmobile.webapp.mobile;
import static org.medicmobile.webapp.mobile.MedicLog.error;
import static org.medicmobile.webapp.mobile.MedicLog.trace;
import static org.medicmobile.webapp.mobile.Utils.getUriFromFilePath;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Base64;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collector;
public class ChtExternalApp {
private final String action;
private final String category;
private final String type;
private final JSONObject extras;
private final Uri uri;
private final String packageName;
private final Integer flags;
private ChtExternalApp(Builder builder) {
this.action = builder.action;
this.category = builder.category;
this.type = builder.type;
this.extras = builder.extras;
this.uri = builder.uri;
this.packageName = builder.packageName;
this.flags = builder.flags;
}
public Intent createIntent() {
Intent intent = new Intent();
if (this.action != null) {
intent.setAction(this.action);
}
if (this.category != null) {
intent.addCategory(this.category);
}
if (this.extras != null) {
extras
.keys()
.forEachRemaining(key -> setIntentExtras(intent, key, extras));
}
if (this.packageName != null) {
intent.setPackage(this.packageName);
}
if (this.uri != null) {
intent.setDataAndNormalize(this.uri);
}
if (this.flags != null) {
intent.setFlags(this.flags);
}
if (this.type != null) {
intent.setType(this.type);
}
return intent;
}
//> PRIVATE
private Serializable getSerializableValue(Object value) {
// ODK does not have boolean data type
if (value instanceof String strValue && ("true".equals(strValue) || "false".equals(strValue))) {
return Boolean.parseBoolean(strValue);
}
return (Serializable) value;
}
private void setIntentExtras(Intent intent, String key, JSONObject data) {
try {
Object value = data.get(key);
if (value instanceof JSONObject) {
intent.putExtra(key, parseJsonToBundle((JSONObject) value));
return;
}
if (value instanceof JSONArray) {
intent.putExtra(key, parseJsonArrayToList((JSONArray) value));
return;
}
intent.putExtra(key, getSerializableValue(value));
} catch (Exception exception) {
error(exception, "ChtExternalApp :: Problem setting intent extras. Key=%s, Data=%s", key, data);
}
}
private Bundle parseJsonToBundle(JSONObject json) {
Bundle bundle = new Bundle();
json
.keys()
.forEachRemaining(key -> setBundleAttribute(key, json, bundle));
return bundle;
}
private Serializable parseJsonArrayToList(JSONArray jsonArray) throws JSONException {
if (jsonArray.length() > 0) {
return jsonArray.get(0) instanceof JSONObject
? parseJsonArrayToBundleList(jsonArray) : parseJsonArrayToSerializableList(jsonArray);
}
return new ArrayList<>();
}
private ArrayList<Bundle> parseJsonArrayToBundleList(JSONArray jsonArray) throws JSONException {
ArrayList<Bundle> list = new ArrayList<>(jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
list.add(parseJsonToBundle(jsonArray.getJSONObject(i)));
}
return list;
}
private ArrayList<Serializable> parseJsonArrayToSerializableList(JSONArray jsonArray) throws JSONException {
ArrayList<Serializable> list = new ArrayList<>(jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
list.add((Serializable) jsonArray.get(i));
}
return list;
}
private void setBundleAttribute(String key, JSONObject json, Bundle bundle) {
try {
Object value = json.get(key);
if (value instanceof JSONObject) {
bundle.putBundle(key, parseJsonToBundle((JSONObject) value));
return;
}
if (value instanceof JSONArray) {
bundle.putSerializable(key, parseJsonArrayToList((JSONArray) value));
return;
}
bundle.putSerializable(key, (Serializable) value);
} catch (Exception exception) {
error(exception, "ChtExternalApp :: Problem converting from JSON to Bundle. Key=%s, JSON=%s", key, json);
}
}
//> INTERNAL CLASSES
public static class Builder {
private String action;
private String category;
private String type;
private JSONObject extras;
private Uri uri;
private String packageName;
private Integer flags;
public Builder() { }
public Builder setAction(String action) {
this.action = action;
return this;
}
public Builder setCategory(String category) {
this.category = category;
return this;
}
public Builder setType(String type) {
this.type = type;
return this;
}
public Builder setExtras(JSONObject extras) {
this.extras = extras;
return this;
}
public Builder setUri(Uri uri) {
this.uri = uri;
return this;
}
public Builder setPackageName(String packageName) {
this.packageName = packageName;
return this;
}
public Builder setFlags(Integer flags) {
this.flags = flags;
return this;
}
public ChtExternalApp build() {
return new ChtExternalApp(this);
}
}
public static class Response {
private final Intent intent;
private final Activity context;
public Response(Intent intent, Activity context) {
this.intent = intent;
this.context = context;
}
public Optional<JSONObject> getData() {
if (intent == null || intent.getExtras() == null) {
return Optional.empty();
}
JSONObject json = parseBundleToJson(intent.getExtras());
if (json == null) {
return Optional.empty();
}
return Optional.of(json);
}
//> PRIVATE
private JSONObject parseBundleToJson(Bundle bundle) {
try {
JSONObject json = new JSONObject();
bundle
.keySet()
.iterator()
.forEachRemaining(key -> setValueInJson(key, json, bundle));
return json;
} catch (Exception exception) {
error(exception, "ChtExternalApp :: Problem parsing bundle to json. Bundle=%s", bundle);
}
return null;
}
private void setValueInJson(String key, JSONObject json, Bundle bundle) {
try {
Object value = bundle.get(key);
if (value instanceof Bitmap) {
json.put(key, parseBitmapImageToBase64((Bitmap) value));
return;
}
if (value instanceof Bundle) {
json.put(key, parseBundleToJson((Bundle) value));
return;
}
if (isBundleList(value)) {
JSONArray jsonArray = ((List<Bundle>) value)
.stream()
.map(this::parseBundleToJson)
.collect(Collector.of(JSONArray::new, JSONArray::put, JSONArray::put));
json.put(key, jsonArray);
return;
}
Optional<Uri> imagePath = getImageUri(value);
if (imagePath.isPresent()) {
json.put(key, getImageFromStoragePath(imagePath.get()));
return;
}
json.put(key, JSONObject.wrap(value));
} catch (Exception exception) {
error(exception, "ChtExternalApp :: Problem parsing bundle to json. Key=%s, Bundle=%s", key, bundle);
}
}
private boolean isBundleList(Object value) {
if (!(value instanceof List)) {
return false;
}
List<?> list = (List<?>) value; // Avoid casting many times to same type.
return !list.isEmpty() && list.get(0) instanceof Bundle;
}
private Optional<Uri> getImageUri(Object value) {
if (!(value instanceof String)) {
return Optional.empty();
}
String path = (String) value; // Avoid casting many times to same type.
if (!path.endsWith(".jpg") && !path.endsWith(".png")) {
return Optional.empty();
}
return getUriFromFilePath(path);
}
private String getImageFromStoragePath(Uri filePath) {
trace(this, "ChtExternalApp :: Retrieving image from storage path.");
try (
ParcelFileDescriptor parcelFileDescriptor = this.context
.getContentResolver()
.openFileDescriptor(filePath, "r");
InputStream file = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
){
Bitmap imgBitmap = BitmapFactory.decodeStream(file);
return parseBitmapImageToBase64(imgBitmap);
} catch (Exception exception) {
error(exception, "ChtExternalApp :: Failed to process image file from path: %s", filePath);
}
return null;
}
private String parseBitmapImageToBase64(Bitmap imgBitmap) throws IOException {
try (ByteArrayOutputStream outputFile = new ByteArrayOutputStream()) {
trace(this, "ChtExternalApp :: Compressing image file.");
imgBitmap.compress(Bitmap.CompressFormat.JPEG, 75, outputFile);
trace(this, "ChtExternalApp :: Encoding image file to Base64.");
byte[] imageBytes = outputFile.toByteArray();
return Base64.encodeToString(imageBytes, Base64.NO_WRAP);
}
}
}
}