-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathChtExternalAppHandler.java
More file actions
105 lines (83 loc) · 3.61 KB
/
ChtExternalAppHandler.java
File metadata and controls
105 lines (83 loc) · 3.61 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
package org.medicmobile.webapp.mobile;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.app.Activity.RESULT_OK;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static org.medicmobile.webapp.mobile.EmbeddedBrowserActivity.RequestCode;
import static org.medicmobile.webapp.mobile.JavascriptUtils.safeFormat;
import static org.medicmobile.webapp.mobile.MedicLog.error;
import static org.medicmobile.webapp.mobile.MedicLog.trace;
import static org.medicmobile.webapp.mobile.MedicLog.warn;
import android.app.Activity;
import android.content.Intent;
import androidx.core.content.ContextCompat;
import org.json.JSONObject;
import java.util.Optional;
public class ChtExternalAppHandler {
private final Activity context;
private Intent lastIntent;
ChtExternalAppHandler(Activity context) {
this.context = context;
}
String processResult(int resultCode, Intent intent) {
try {
Optional<JSONObject> json = new ChtExternalApp
.Response(intent, this.context)
.getData();
String data = json.map(JSONObject::toString).orElse(null);
if (resultCode != RESULT_OK) {
String message = "ChtExternalAppHandler :: Bad result code: %s. The external app either: " +
"explicitly returned this result, did not return any result or crashed during the operation. " +
"[" + data + "]";
warn(this, message, resultCode);
return safeFormat("console.error('" + message + "')", resultCode);
}
return makeJavaScript(data);
} catch (Exception exception) {
String message = "ChtExternalAppHandler :: Problem serialising the intent response.";
error(exception, message);
return safeFormat("console.error('" + message + "', %s)", exception);
}
}
void startIntent(ChtExternalApp chtExternalApp) {
Intent chtExternalAppIntent = chtExternalApp.createIntent();
if (ContextCompat.checkSelfPermission(this.context, READ_EXTERNAL_STORAGE) != PERMISSION_GRANTED) {
trace(this, "ChtExternalAppHandler :: Requesting storage permissions to process image files taken from external apps");
this.lastIntent = chtExternalAppIntent; // Saving intent to start it when permission is granted.
Intent requestStorageIntent = new Intent(this.context, RequestStoragePermissionActivity.class);
requestStorageIntent.putExtra(
RequestStoragePermissionActivity.TRIGGER_CLASS,
ChtExternalAppHandler.class.getName()
);
this.context.startActivityForResult(requestStorageIntent, RequestCode.ACCESS_STORAGE_PERMISSION.getCode());
return;
}
startActivity(chtExternalAppIntent);
}
void resumeActivity(int resultCode) {
if (resultCode != RESULT_OK || this.lastIntent == null) {
return;
}
startActivity(this.lastIntent);
this.lastIntent = null; // Cleaning as we don't need it anymore.
}
//> PRIVATE
private void startActivity(Intent intent) {
try {
trace(this, "ChtExternalAppHandler :: Starting activity %s %s", intent, intent.getExtras());
this.context.startActivityForResult(intent, RequestCode.CHT_EXTERNAL_APP_ACTIVITY.getCode());
} catch (Exception exception) {
error(exception, "ChtExternalAppHandler :: Error when starting the activity %s %s", intent, intent.getExtras());
}
}
private static String makeJavaScript(String data) {
String javaScript = "try {" +
"const api = window.CHTCore.AndroidApi;" +
"if (api && api.v1 && api.v1.resolveCHTExternalAppResponse) {" +
" api.v1.resolveCHTExternalAppResponse(%s);" +
"}" +
"} catch (error) { " +
" console.error('ChtExternalAppHandler :: Error on sending intent response to CHT-Core webapp', error);" +
"}";
return safeFormat(javaScript, data);
}
}