-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWishBridge.java
More file actions
188 lines (159 loc) · 5.59 KB
/
WishBridge.java
File metadata and controls
188 lines (159 loc) · 5.59 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
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
/**
* Copyright (C) 2020, ControlThings Oy Ab
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* @license Apache-2.0
*/
package addon;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.Looper;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;
import fi.ct.bridge.AppBridge;
import fi.ct.bridge.CoreBridge;
import utils.Util;
class WishBridge {
private final String TAG = "WishBridge";
private final int TRANSACT_CODE_RELEASE_CORE = 1;
Context _context;
WishBridgeJni _jni;
AddonService addonService;
CoreBridge coreBridge = null;
boolean mBound = false;
boolean connected = false;
/**
* This is set to true when startWish() has been invoked, and set to false in mConnection.onServiceStarted
*/
boolean wishServiceStarting = false;
Intent wish;
private byte[] _wsid;
WishBridge(Context context, WishBridgeJni jni, AddonService addonService) {
this._context = context;
this._jni = jni;
this.addonService = addonService;
//Note: Don't start wish service here, because we need to first get the wsid. Let WishBridgeJni start the service instead, after wsid is saved.
}
void startWish() {
if (wishServiceStarting) {
// startWish() is already starting Wish!
return;
}
wishServiceStarting = true;
wish = new Intent("fi.ct.wish.Wish");
wish.setPackage(_context.getPackageName());
//wish.setComponent(new ComponentName("fi.ct.mist", "fi.ct.wish.Wish"));
_wsid = _jni.getWsid();
if (_wsid == null) {
new AddonException("WSID is null!");
}
/* if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
_context.startForegroundService(wish);
} else { */
_context.startService(wish);
// }
mBound = _context.bindService(wish, mConnection, Context.BIND_AUTO_CREATE);
}
private class LocalBinder extends Binder {
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
Log.d(TAG, "onTransact, code: " + code);
if (code == TRANSACT_CODE_RELEASE_CORE) {
//addonService.unbindService(mConnection);
mConnection.onServiceDisconnected(null);
}
return super.onTransact(code, data, reply, flags);
}
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "onServiceConnected");
coreBridge = CoreBridge.Stub.asInterface(service);
connected = true;
wishServiceStarting = false;
try {
coreBridge.register(new LocalBinder(), _wsid, bridge);
} catch (RemoteException e) {
Log.d(TAG, "remote exeption in open:");
}
_jni.setConnected(true);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "onServiceDisconnected");
coreBridge = null;
connected = false;
_jni.setConnected(false);
}
};
/**
* This method is called by the WishApp C99 when it needs to send a frame to Core
*
* @param data
*/
public void receiveAppToCore(byte[] wsid, byte[] data) {
//Log.v(TAG, "in receiveAppToCore");
if (!connected) {
Log.v(TAG, "Error: not bound, attempting rebound");
startWish();
return;
}
try {
if (wsid != null) {
// Log.v(TAG, "wsid len " + wsid.length);
} else {
Log.v(TAG, "wsid is null");
}
if (data != null) {
//Log.v(TAG, "data len " + data.length);
} else {
Log.v(TAG, "data is null");
}
coreBridge.sendAppToCore(wsid, data);
} catch (RemoteException e) {
Log.v(TAG, "RemoteException occurred in receiveAppToCore");
startWish();
}
}
private AppBridge.Stub bridge = new AppBridge.Stub() {
@Override
public void sendCoreToApp(final byte[] data) {
//Log.d(TAG, "in sendCoreToApp");
/* Enqueue the request on the app's main thread, implying that incoming messages from core
to this app are executed in a serial fashion.
If this behaviour is changed, then you must ensure that the callbacks are invoked
*/
new android.os.Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
_jni.receive_core_to_app(data);
}
});
}
};
void unbind() {
Log.d(TAG, "cleanup appBridge");
if (mBound) {
try {
_context.unbindService(mConnection);
_context.stopService(wish);
} catch (IllegalArgumentException iae) {
Log.d(TAG, Util.prettyPrintException(iae));
}
connected = false;
mBound = false;
}
}
}