-
Notifications
You must be signed in to change notification settings - Fork 376
/
CoreModule.java
191 lines (165 loc) · 5.64 KB
/
CoreModule.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
package chat.berty.core;
import android.app.Activity;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactMethod;
import chat.berty.ble.Manager;
import core.Core;
public class CoreModule extends ReactContextBaseJavaModule {
private Logger logger = new Logger("chat.berty.io");
private Notification notification;
private String filesDir = "";
private ReactApplicationContext reactContext;
public CoreModule(final ReactApplicationContext reactContext) {
super(reactContext);
this.filesDir = reactContext.getFilesDir().getAbsolutePath();
this.notification = new Notification(reactContext);
this.reactContext = reactContext;
Object o = new Manager.ActivityGetter() {
public Activity getCurrentActivity() {
return reactContext.getCurrentActivity();
}
};
Manager.getInstance().setmReactContext(o, reactContext);
}
public String getName() {
return "CoreModule";
}
@ReactMethod
public void listAccounts(Promise promise) {
try {
String data = Core.listAccounts(this.filesDir);
promise.resolve(data);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to list accounts: %s", err);
promise.reject(err);
}
}
@ReactMethod
public void initialize(Promise promise) {
try {
Core.initialize(this.logger, this.filesDir);
promise.resolve(null);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to init core: %s", err);
promise.reject(err);
}
}
@ReactMethod
public void start(String nickname, Promise promise) {
try {
core.MobileOptions coreOptions = new core.MobileOptions()
.withNickname(nickname)
.withDatastorePath(this.filesDir)
.withLoggerDriver(this.logger)
.withNotificationDriver(this.notification);
Core.start(coreOptions);
promise.resolve(null);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to start core: %s", err);
promise.reject(err);
}
}
@ReactMethod
public void restart(Promise promise) {
try {
Core.restart();
promise.resolve(null);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to restart core: %s", err);
promise.reject(err);
}
}
@ReactMethod
public void panic(Promise promise) throws Exception {
Core.panic();
promise.resolve(null);
}
@ReactMethod
public void throwException() throws Exception {
throw new Exception("thrown exception");
}
@ReactMethod
public void dropDatabase(Promise promise) {
try {
Core.dropDatabase(this.filesDir);
promise.resolve(null);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to drop database: %s", err);
promise.reject(err);
}
}
@ReactMethod
public void getPort(Promise promise) {
try {
Long data = Core.getPort();
promise.resolve(data.toString());
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to get port: %s", err);
promise.reject(err);
}
}
@ReactMethod
public void getNetworkConfig(Promise promise) {
promise.resolve(Core.getNetworkConfig());
}
@ReactMethod
public void updateNetworkConfig(String config, Promise promise) {
try {
Core.updateNetworkConfig(config);
promise.resolve(null);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to update network config: %s", err);
promise.reject(err);
}
}
@ReactMethod
public void isBotRunning(Promise promise) {
promise.resolve(Core.isBotRunning());
}
@ReactMethod
public void startBot(Promise promise) {
try {
Core.startBot();
promise.resolve(null);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to update start bot: %s", err);
promise.reject(err);
}
}
@ReactMethod
public void stopBot(Promise promise) {
try {
Core.stopBot();
promise.resolve(null);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to update stop bot: %s", err);
promise.reject(err);
}
}
@ReactMethod
public void getLocalGRPCInfos(Promise promise) {
promise.resolve(Core.getLocalGRPCInfos());
}
@ReactMethod
public void startLocalGRPC(Promise promise) {
try {
Core.startLocalGRPC();
promise.resolve(null);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to update start local gRPC: %s", err);
promise.reject(err);
}
}
@ReactMethod
public void stopLocalGRPC(Promise promise) {
try {
Core.stopLocalGRPC();
promise.resolve(null);
} catch (Exception err) {
this.logger.format(Level.ERROR, this.getName(), "Unable to update stop local gRPC: %s", err);
promise.reject(err);
}
}
}