-
Notifications
You must be signed in to change notification settings - Fork 76
/
IntercomPlugin.java
213 lines (182 loc) · 6.37 KB
/
IntercomPlugin.java
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
package com.getcapacitor.community.intercom;
import com.getcapacitor.Config;
import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import io.intercom.android.sdk.Intercom;
import io.intercom.android.sdk.UserAttributes;
import io.intercom.android.sdk.identity.Registration;
@NativePlugin()
public class IntercomPlugin extends Plugin {
public static final String CONFIG_KEY_PREFIX = "plugins.IntercomPlugin.android-";
@Override()
public void load() {
//
// get config
String apiKey = this.bridge.getConfig().getString(CONFIG_KEY_PREFIX + "apiKey", "ADD_IN_CAPACITOR_CONFIG_JSON");
String appId = this.bridge.getConfig().getString(CONFIG_KEY_PREFIX + "appId", "ADD_IN_CAPACITOR_CONFIG_JSON");
//
// init intercom sdk
Intercom.initialize(this.getActivity().getApplication(), apiKey, appId);
//
// load parent
super.load();
}
@PluginMethod()
public void registerIdentifiedUser(PluginCall call) {
String email = call.getString("email");
String userId = call.getString("userId");
Registration registration = new Registration();
if (email != null && email.length() > 0) {
registration = registration.withEmail(email);
}
if (userId != null && userId.length() > 0) {
registration = registration.withUserId(userId);
}
Intercom.client().registerIdentifiedUser(registration);
call.success();
}
@PluginMethod()
public void registerUnidentifiedUser(PluginCall call) {
Intercom.client().registerUnidentifiedUser();
call.success();
}
@PluginMethod()
public void updateUser(PluginCall call) {
UserAttributes.Builder builder = new UserAttributes.Builder();
String userId = call.getString("userId");
if (userId != null && userId.length() > 0) {
builder.withUserId(userId);
}
String email = call.getString("email");
if (email != null && email.length() > 0) {
builder.withEmail(email);
}
String name = call.getString("name");
if (name != null && name.length() > 0) {
builder.withName(name);
}
String phone = call.getString("phone");
if (phone != null && phone.length() > 0) {
builder.withPhone(phone);
}
String languageOverride = call.getString("languageOverride");
if (languageOverride != null && languageOverride.length() > 0) {
builder.withLanguageOverride(languageOverride);
}
Map<String, Object> customAttributes = mapFromJSON(call.getObject("customAttributes"));
builder.withCustomAttributes(customAttributes);
Intercom.client().updateUser(builder.build());
call.success();
}
@PluginMethod()
public void logout(PluginCall call) {
Intercom.client().logout();
call.success();
}
@PluginMethod()
public void logEvent(PluginCall call) {
String eventName = call.getString("name");
Map<String, Object> metaData = mapFromJSON(call.getObject("data"));
if (metaData == null) {
Intercom.client().logEvent(eventName);
} else {
Intercom.client().logEvent(eventName, metaData);
}
call.success();
}
@PluginMethod()
public void displayMessenger(PluginCall call) {
Intercom.client().displayMessenger();
call.success();
}
@PluginMethod()
public void displayMessageComposer(PluginCall call) {
Intercom.client().displayMessageComposer();
call.success();
}
@PluginMethod()
public void displayHelpCenter(PluginCall call) {
Intercom.client().displayHelpCenter();
call.success();
}
@PluginMethod()
public void hideMessenger(PluginCall call) {
Intercom.client().hideMessenger();
call.success();
}
@PluginMethod()
public void displayLauncher(PluginCall call) {
Intercom.client().setLauncherVisibility(Intercom.VISIBLE);
call.success();
}
@PluginMethod()
public void hideLauncher(PluginCall call) {
Intercom.client().setLauncherVisibility(Intercom.GONE);
call.success();
}
@PluginMethod()
public void displayInAppMessages(PluginCall call) {
Intercom.client().setInAppMessageVisibility(Intercom.VISIBLE);
call.success();
}
@PluginMethod()
public void hideInAppMessages(PluginCall call) {
Intercom.client().setLauncherVisibility(Intercom.GONE);
call.success();
}
@PluginMethod()
public void setUserHash(PluginCall call) {
String hmac = call.getString("hmac");
Intercom.client().setUserHash(hmac);
call.success();
}
@PluginMethod()
public void setBottomPadding(PluginCall call) {
String stringValue = call.getString("value");
int value = Integer.parseInt(stringValue);
Intercom.client().setBottomPadding(value);
call.success();
}
private static Map<String, Object> mapFromJSON(JSONObject jsonObject) {
if (jsonObject == null) {
return null;
}
Map<String, Object> map = new HashMap<>();
Iterator<String> keysIter = jsonObject.keys();
while (keysIter.hasNext()) {
String key = keysIter.next();
Object value = getObject(jsonObject.opt(key));
if (value != null) {
map.put(key, value);
}
}
return map;
}
private static Object getObject(Object value) {
if (value instanceof JSONObject) {
value = mapFromJSON((JSONObject) value);
} else if (value instanceof JSONArray) {
value = listFromJSON((JSONArray) value);
}
return value;
}
private static List<Object> listFromJSON(JSONArray jsonArray) {
List<Object> list = new ArrayList<>();
for (int i = 0, count = jsonArray.length(); i < count; i++) {
Object value = getObject(jsonArray.opt(i));
if (value != null) {
list.add(value);
}
}
return list;
}
}