-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathTalkingDataModule.java
More file actions
164 lines (141 loc) · 5.23 KB
/
TalkingDataModule.java
File metadata and controls
164 lines (141 loc) · 5.23 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
package cn.reactnative.modules.talkingdata;
import android.content.Context;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReadableType;
import com.tendcloud.tenddata.TCAgent;
import com.tendcloud.tenddata.TalkingDataSMS;
import com.tendcloud.tenddata.TalkingDataSMSApplyCallback;
import com.tendcloud.tenddata.TalkingDataSMSVerifyCallback;
import java.util.HashMap;
/**
* Created by lvbingru on 1/11/16.
*/
public class TalkingDataModule extends ReactContextBaseJavaModule {
public TalkingDataModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "TalkingDataAPI";
}
private static Boolean registered = false;
public static void register(Context context, String appID, String channelID, boolean reportExceptions) {
if (!registered) {
if (appID != null) {
TCAgent.init(context, appID, channelID);
}
else {
TCAgent.init(context);
}
if (reportExceptions) {
TCAgent.setReportUncaughtExceptions(true);
}
registered = true;
}
}
public static void registerSMS(Context context, String appID, String secretId) {
TalkingDataSMS.init(context, appID, secretId);
}
@Override
public void initialize() {
super.initialize();
if (!registered) {
register(getReactApplicationContext(), null, null, true);
}
}
@Override
public void onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy();
}
@ReactMethod
public void setLocation(Double latitude, Double longitude){
}
@ReactMethod
public void trackPageBegin(String pageName){
TCAgent.onPageStart(getReactApplicationContext(), pageName);
}
@ReactMethod
public void trackPageEnd(String pageName){
TCAgent.onPageEnd(getReactApplicationContext(), pageName);
}
@ReactMethod
public void trackEvent(String eventName, String eventLabel, ReadableMap parameters){
if (eventLabel == null) {
TCAgent.onEvent(getReactApplicationContext(), eventName);
}
else {
if (parameters == null) {
TCAgent.onEvent(getReactApplicationContext(), eventName, eventLabel);
}
else {
HashMap map = new HashMap();
ReadableMapKeySetIterator iterator = parameters.keySetIterator();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
ReadableType type = parameters.getType(key);
if (type == ReadableType.String) {
map.put(key, parameters.getString(key));
}
else if (type == ReadableType.Boolean) {
map.put(key, new Boolean(parameters.getBoolean(key)));
}
else if (type == ReadableType.Number) {
map.put(key, new Double(parameters.getDouble(key)));
}
}
TCAgent.onEvent(getReactApplicationContext(), eventName, eventLabel, map);
}
}
}
@ReactMethod
public void getDeviceID(Callback callback) {
String deviceID = TCAgent.getDeviceId(getReactApplicationContext());
callback.invoke(deviceID==null?"":deviceID);
}
@ReactMethod
public void applyAuthCode(String countryCode, String mobile, String requestId, final Promise promise) {
if (requestId!=null) {
TalkingDataSMS.applyAuthCode(countryCode, mobile, new TalkingDataSMSApplyCallback() {
@Override
public void onApplySucc(String s) {
promise.resolve(s);
}
@Override
public void onApplyFailed(int i, String s) {
promise.reject(""+i,s);
}
});
}
else {
TalkingDataSMS.reapplyAuthCode(countryCode, mobile, requestId, new TalkingDataSMSApplyCallback() {
@Override
public void onApplySucc(String s) {
promise.resolve(s);
}
@Override
public void onApplyFailed(int i, String s) {
promise.reject(""+i,s);
}
});
}
}
@ReactMethod
public void verifyAuthCode(String countryCode, String mobile, String authCode, final Promise promise) {
TalkingDataSMS.verifyAuthCode(countryCode, mobile, authCode, new TalkingDataSMSVerifyCallback() {
@Override
public void onVerifySucc(String s) {
promise.resolve(s);
}
@Override
public void onVerifyFailed(int i, String s) {
promise.reject(""+i,s);
}
});
}
}