/
CoreModule.java
99 lines (79 loc) · 3.28 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
package chat.berty.core;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
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.core.notification.NotificationNative;
import core.Core;
import core.NativeBridge;
import core.MobileNotification;
import chat.berty.ble.BleManager;
public class CoreModule extends ReactContextBaseJavaModule {
private Logger logger = new Logger("chat.berty.io");
private ReactApplicationContext reactContext;
private MobileNotification notificationDriver = Core.getNotificationDriver();
private ConnectivityUpdateHandler connectivity;
private NativeBridge daemon = Core.newNativeBridge(this.logger);
public CoreModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
String storagePath = reactContext.getFilesDir().getAbsolutePath();
try {
Core.getDeviceInfo().setStoragePath(storagePath);
} catch (Exception error) {
logger.format(Level.ERROR, this.getName(), error.getMessage());
}
this.notificationDriver.setNative(new NotificationNative());
connectivity = new ConnectivityUpdateHandler(reactContext);
// TODO: Get rid of this and make a proper react-native module that extends ReactContextBaseJavaModule
// See https://facebook.github.io/react-native/docs/native-modules-android
Object activityAndContextGetter = actGetter(reactContext);
BleManager.setReactGetter(activityAndContextGetter, reactContext);
}
private Object actGetter(final ReactApplicationContext reactContext) {
return new BleManager.ActivityAndContextGetter() {
public Activity getCurrentActivity() {
return reactContext.getCurrentActivity();
}
public Context getApplicationContext() {
return reactContext.getApplicationContext();
}
};
}
/////////////////////////////////////////////////////////////////////////
public String getName() {
return "CoreModule";
}
@ReactMethod
public void Invoke(String method, String message, Promise promise) {
new Thread(new Runnable() {
@Override
public void run() {
try {
String data = daemon.invoke(method, message);
promise.resolve(data);
} catch (Exception err) {
logger.format(Level.ERROR, getName(), "Invoke daemon failed: %s", err);
promise.reject(err);
}
}
}).start();
}
@ReactMethod
public void throwException() throws Exception {
throw new Exception("thrown exception");
}
@ReactMethod
public void openSettings() {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", reactContext.getPackageName(), null);
intent.setData(uri);
reactContext.startActivity(intent);
}
}