forked from tgardner/cordova-androidwear
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WearProviderService.java
286 lines (228 loc) · 8.92 KB
/
WearProviderService.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package net.trentgardner.cordova.androidwear;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.Wearable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
public class WearProviderService extends Service implements
MessageApi.MessageListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener
{
public static final String MESSAGE_RECEIVED_PATH = "net.trentgardner.cordova.androidwear.NewMessage";
private final List<WearMessageListener> listeners = new ArrayList<WearMessageListener>();
private final List<String> nodes = new ArrayList<String>();
private static final String TAG = "WearProviderService";
private GoogleApiClient mGoogleApiClient;
private Thread mBackgroundThread;
private Handler mBackgroundHandler;
private final WearMessageApi.Stub apiEndpoint = new WearMessageApi.Stub() {
@Override
public void sendData(String nodeId, String data) throws RemoteException {
LOGD(TAG, "WearMessageApi.sendData");
WearProviderService.this.sendData(nodeId, data);
}
@Override
public void addListener(WearMessageListener listener) throws RemoteException {
LOGD(TAG, "WearMessageApi.addListener");
synchronized (listeners) {
listeners.add(listener);
}
LOGD(TAG, String.format("Notifying listener of %d connected nodes", nodes.size()));
for (String node : nodes) {
listener.onConnect(node);
}
}
@Override
public void removeListener(WearMessageListener listener) throws RemoteException {
LOGD(TAG, "WearMessageApi.removeListener");
synchronized (listeners) {
listeners.remove(listener);
}
}
};
private final class BackgroundThread extends Thread {
@Override
public void run() {
Looper.prepare();
mBackgroundHandler = new Handler();
Looper.loop();
}
}
@Override
public void onCreate() {
LOGD(TAG, "onCreate");
super.onCreate();
mBackgroundThread = new BackgroundThread();
mBackgroundThread.start();
// Build a new GoogleApiClient
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mGoogleApiClient.connect();
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
LOGD(TAG, "onDestroy");
if (null != mGoogleApiClient && mGoogleApiClient.isConnected()) {
Wearable.MessageApi.removeListener(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
LOGD(TAG, "onBind");
return apiEndpoint;
}
private void addNode(final String nodeId) {
LOGD(TAG, "addNode");
if (nodes.contains(nodeId)) return;
nodes.add(nodeId);
mBackgroundHandler.post(new Runnable() {
@Override
public void run() {
synchronized (listeners) {
for(int i = 0; i < listeners.size(); ++i) {
try {
listeners.get(i).onConnect(nodeId);
} catch (RemoteException e) {
Log.w(TAG, "Failed to notify listener ", e);
listeners.remove(i--);
}
}
}
}
});
}
private void removeNode(final String nodeId) {
LOGD(TAG, "removeNode");
if (!nodes.contains(nodeId)) return;
nodes.remove(nodes.indexOf(nodeId));
mBackgroundHandler.post(new Runnable() {
@Override
public void run() {
synchronized (listeners) {
for(int i = 0; i < listeners.size(); ++i) {
try {
listeners.get(i).onError(nodeId, "Connection dead");
} catch (RemoteException e) {
Log.w(TAG, "Failed to notify listener ", e);
listeners.remove(i--);
}
}
}
}
});
}
private void dataReceived(final String nodeId, final String message) {
LOGD(TAG, String.format("dataReceived - nodeId: %s", nodeId));
mBackgroundHandler.post(new Runnable() {
@Override
public void run() {
synchronized (listeners) {
LOGD(TAG, String.format("Notifying %d listeners", listeners.size()));
for(int i = 0; i < listeners.size(); ++i) {
try {
listeners.get(i).onDataReceived(nodeId, message);
} catch (RemoteException e) {
Log.w(TAG, "Failed to notify listener ", e);
listeners.remove(i--);
}
}
}
}
});
}
private void sendData(final String nodeId, final String data) {
LOGD(TAG, "sendMessage");
final byte[] message = data.getBytes();
mBackgroundHandler.post(new Runnable() {
@Override
public void run() {
MessageApi.SendMessageResult result =
Wearable.MessageApi.sendMessage(mGoogleApiClient, nodeId,
MESSAGE_RECEIVED_PATH, message).await();
if (result.getStatus().isSuccess()) {
Log.v(TAG, "Message sent to : " + nodeId);
} else {
// Log an error
Log.v(TAG, "MESSAGE ERROR: failed to send Message");
}
}
});
}
@Override
public void onConnected(Bundle bundle) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
LOGD(TAG, "Connected to Google Api Service");
}
Wearable.MessageApi.addListener(mGoogleApiClient, this);
mBackgroundHandler.post(new Runnable() {
@Override
public void run() {
getNodes(); }
});
}
private void getNodes() {
Task<List<Node>> nodeListTask =
Wearable.getNodeClient(getApplicationContext()).getConnectedNodes();
try {
// Block on a task and get the result synchronously (because this is on a background
// thread).
List<Node> nodes = Tasks.await(nodeListTask);
for (Node node : nodes) {
addNode(node.getId());
}
} catch (ExecutionException exception) {
Log.e(TAG, "Task failed: " + exception);
} catch (InterruptedException exception) {
Log.e(TAG, "Interrupt occurred: " + exception);
}
}
@Override
public void onConnectionSuspended(int i) {
LOGD(TAG, "Connection to Google API client was suspended");
}
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if(messageEvent.getPath().equals(MESSAGE_RECEIVED_PATH)) {
final String message = new String(messageEvent.getData());
dataReceived(messageEvent.getSourceNodeId(), message);
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
LOGD(TAG, "Connection to Google API client was suspended");
Wearable.MessageApi.removeListener(mGoogleApiClient, this);
}
/**
* As simple wrapper around Log.d
*/
private static void LOGD(final String tag, String message) {
//if (Log.isLoggable(tag, Log.DEBUG)) {
Log.d(tag, message);
//}
}
}