-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWishApp.java
More file actions
135 lines (104 loc) · 3.62 KB
/
WishApp.java
File metadata and controls
135 lines (104 loc) · 3.62 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
package wish;
import android.content.Context;
import java.util.ArrayList;
import java.util.List;
import addon.AddonException;
import addon.WishFile;
/**
* Created by jan on 11/1/16.
*/
public class WishApp {
/** startWishApp return value for success */
private static final int WISH_APP_SUCCESS = 0;
/** startWishApp return error return if started multiple times */
private static final int WISH_APP_ERROR_MULTIPLE_TIMES = -1;
/** startWishApp return error return for other errors */
private static final int WISH_APP_ERROR_UNSPECIFIED = -10;
private static List<Error> errorHandleList = new ArrayList<>();;
static {
System.loadLibrary("mist");
}
/* Private constructor must exist to enforce Singleton pattern */
private WishApp() {}
private static class MistNodeHolder {
private static final WishApp INSTANCE = new WishApp();
}
public static WishApp getInstance() {
return MistNodeHolder.INSTANCE;
}
public void startWishApp(Context context) {
String appName = context.getPackageName();
if (appName.length() > 32) {
appName = appName.substring(0, 32);
}
int ret = startWishApp(appName, new WishFile(context));
if (ret != WISH_APP_SUCCESS) {
if (ret == WISH_APP_ERROR_MULTIPLE_TIMES) {
throw new AddonException("Addon cannot be started multiple times.");
}
else {
throw new AddonException("Unspecified Addon exception");
}
}
}
native int startWishApp(String appName, WishFile wishFile);
native void stopWishApp();
/**
* Send a Wish request to the local Wish core
* @param req the request in BSON format
* @return the RPC id; The invalid RPC id 0 is returned for any errors.
*/
public native int request(byte[] req, RequestCb cb); //will call wish_app_core_with_cb_context
public native void requestCancel(int id);
/** This method simply pretty-prints a BSON object to the system console
* @param tag the console log tag
* @param bson_bytes the BSON object to be pretty printed
*/
public native void bsonConsolePrettyPrinter(String tag, byte[] bson_bytes);
void online(byte[] peerBson) {
}
void offline(byte[] peerBson) {
}
static void registerRpcErrorHandler(Error error) {
synchronized (errorHandleList) {
errorHandleList.add(error);
}
}
interface Error {
public void cb(int code, String msg);
}
public abstract static class RequestCb {
/**
* The callback invoked when "ack" is received for a RPC request
*
* @param data a document containing RPC return value as 'data' element
*/
public void ack(byte[] data) {
response(data);
end();
};
/**
* The callback invoked when "sig" is received for a RPC request
*
* @param data the contents of 'data' element of the RPC reply
*/
public void sig(byte[] data) {
response(data);
};
public abstract void response(byte[] data);
public abstract void end();
/**
* The callback invoked when "err" is received for a failed RPC request
*
* @param code the error code
* @param msg a free-text error message
*/
public void err(int code, String msg) {
synchronized (errorHandleList) {
for (Error error : errorHandleList) {
error.cb(code, msg);
}
}
};
}
}