TelinkApplication.java
11.9 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright (C) 2015 The Telink Bluetooth Light Project
*
*/
package com.telink;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import com.telink.bluetooth.TelinkLog;
import com.telink.bluetooth.event.DeviceEvent;
import com.telink.bluetooth.event.LeScanEvent;
import com.telink.bluetooth.event.MeshEvent;
import com.telink.bluetooth.event.NotificationEvent;
import com.telink.bluetooth.event.ServiceEvent;
import com.telink.bluetooth.light.DeviceInfo;
import com.telink.bluetooth.light.GetAlarmNotificationParser;
import com.telink.bluetooth.light.GetGroupNotificationParser;
import com.telink.bluetooth.light.GetSceneNotificationParser;
import com.telink.bluetooth.light.GetTimeNotificationParser;
import com.telink.bluetooth.light.LightAdapter;
import com.telink.bluetooth.light.LightService;
import com.telink.bluetooth.light.NotificationInfo;
import com.telink.bluetooth.light.NotificationParser;
import com.telink.bluetooth.light.OnlineStatusNotificationParser;
import com.telink.util.Event;
import com.telink.util.EventBus;
import com.telink.util.EventListener;
import com.telink.util.Strings;
public class TelinkApplication extends Application {
private static TelinkApplication mThis;
protected final EventBus<String> mEventBus = new EventBus<>();
protected Context mContext;
protected boolean serviceStarted;
protected boolean serviceConnected;
protected final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
TelinkApplication.this.onServiceConnected(name, service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
TelinkApplication.this.onServiceDisconnected(name);
}
};
protected DeviceInfo mCurrentConnect;
private BroadcastReceiver mLightReceiver;
public static TelinkApplication getInstance() {
if (mThis == null)
mThis = new TelinkApplication();
return mThis;
}
/**
* 当前连接的设备
*
* @return
*/
public DeviceInfo getConnectDevice() {
return mCurrentConnect;
}
@Override
public void onCreate() {
mThis = this;
this.mContext = this;
super.onCreate();
TelinkLog.d("TelinkApplication Created.");
}
public void doInit() {
this.doInit(this);
}
public void doInit(Context context) {
this.doInit(context, null);
}
/**
* 执行初始化,APP启动时调用
*
* @param context 上下文
* @param clazz 要启动的LightService
*/
public void doInit(Context context, Class<? extends LightService> clazz) {
this.mContext = context;
LocalBroadcastManager.getInstance(this.mContext).registerReceiver(makeLightReceiver(), makeLightFilter());
if (clazz != null)
this.startLightService(clazz);
this.registerNotificationParser(OnlineStatusNotificationParser.create());
this.registerNotificationParser(GetGroupNotificationParser.create());
this.registerNotificationParser(GetAlarmNotificationParser.create());
this.registerNotificationParser(GetSceneNotificationParser.create());
this.registerNotificationParser(GetTimeNotificationParser.create());
}
/**
* 销毁,当退出APP时调用此方法
*/
public void doDestroy() {
this.stopLightService();
if (this.mLightReceiver != null)
LocalBroadcastManager.getInstance(this.mContext).unregisterReceiver(this.mLightReceiver);
this.removeEventListeners();
this.mCurrentConnect = null;
this.serviceStarted = false;
this.serviceConnected = false;
}
protected BroadcastReceiver makeLightReceiver() {
if (this.mLightReceiver == null)
this.mLightReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case LightService.ACTION_NOTIFICATION:
onNotify(intent);
break;
case LightService.ACTION_STATUS_CHANGED:
onStatusChanged(intent);
break;
case LightService.ACTION_UPDATE_MESH_COMPLETED:
onUpdateMeshCompleted(intent);
break;
case LightService.ACTION_OFFLINE:
onMeshOffline(intent);
break;
case LightService.ACTION_LE_SCAN:
onLeScan(intent);
break;
case LightService.ACTION_LE_SCAN_TIMEOUT:
onLeScanTimeout(intent);
break;
case LightService.ACTION_SCAN_COMPLETED:
onLeScanCompleted(intent);
break;
case LightService.ACTION_ERROR:
onError(intent);
break;
}
}
};
return this.mLightReceiver;
}
protected IntentFilter makeLightFilter() {
final IntentFilter filter = new IntentFilter();
filter.addAction(LightService.ACTION_LE_SCAN);
filter.addAction(LightService.ACTION_SCAN_COMPLETED);
filter.addAction(LightService.ACTION_LE_SCAN_TIMEOUT);
filter.addAction(LightService.ACTION_NOTIFICATION);
filter.addAction(LightService.ACTION_STATUS_CHANGED);
filter.addAction(LightService.ACTION_UPDATE_MESH_COMPLETED);
filter.addAction(LightService.ACTION_OFFLINE);
filter.addAction(LightService.ACTION_ERROR);
return filter;
}
/********************************************************************************
* Event API
*******************************************************************************/
/**
* 添加一个事件监听器
*
* @param eventType 事件类型
* @param listener 事件监听器
*/
public void addEventListener(String eventType, EventListener<String> listener) {
this.mEventBus.addEventListener(eventType, listener);
}
/**
* 移除事件监听器
*
* @param listener
*/
public void removeEventListener(EventListener<String> listener) {
this.mEventBus.removeEventListener(listener);
}
/**
* 从事件监听器中移除指定的事件
*
* @param eventType
* @param listener
*/
public void removeEventListener(String eventType, EventListener<String> listener) {
this.mEventBus.removeEventListener(eventType, listener);
}
/**
* 移除所有的事件监听器
*/
public void removeEventListeners() {
this.mEventBus.removeEventListeners();
}
/**
* 分发事件
*
* @param event
*/
public void dispatchEvent(Event<String> event) {
this.mEventBus.dispatchEvent(event);
}
/********************************************************************************
* Service API
*******************************************************************************/
/**
* 启动LightService
*
* @param clazz
*/
public void startLightService(Class<? extends LightService> clazz) {
if (this.serviceStarted || this.serviceConnected)
return;
this.serviceStarted = true;
Intent service = new Intent(this.mContext, clazz);
this.bindService(service, this.mServiceConnection,
Context.BIND_AUTO_CREATE);
}
/**
* 停止LightService
*/
public void stopLightService() {
if (!this.serviceStarted)
return;
this.serviceStarted = false;
if (this.serviceConnected) {
this.unbindService(this.mServiceConnection);
}
}
protected void onServiceConnected(ComponentName name, IBinder service) {
TelinkLog.d("service connected --> " + name.getShortClassName());
this.serviceConnected = true;
this.dispatchEvent(ServiceEvent.newInstance(this, ServiceEvent.SERVICE_CONNECTED, service));
}
protected void onServiceDisconnected(ComponentName name) {
TelinkLog.d("service disconnected --> " + name.getShortClassName());
this.serviceConnected = false;
this.dispatchEvent(ServiceEvent.newInstance(this, ServiceEvent.SERVICE_DISCONNECTED, null));
}
/********************************************************************************
* Broadcast Receiver API
*******************************************************************************/
protected void onLeScan(Intent intent) {
DeviceInfo deviceInfo = intent.getParcelableExtra(LightService.EXTRA_DEVICE);
this.dispatchEvent(LeScanEvent.newInstance(this, LeScanEvent.LE_SCAN, deviceInfo));
}
protected void onLeScanCompleted(Intent intent) {
this.dispatchEvent(LeScanEvent.newInstance(this, LeScanEvent.LE_SCAN_COMPLETED, null));
}
protected void onLeScanTimeout(Intent intent) {
this.dispatchEvent(LeScanEvent.newInstance(this, LeScanEvent.LE_SCAN_TIMEOUT, null));
}
protected void onStatusChanged(Intent intent) {
DeviceInfo deviceInfo = intent.getParcelableExtra(LightService.EXTRA_DEVICE);
if (deviceInfo.status == LightAdapter.STATUS_LOGIN) {
mCurrentConnect = deviceInfo;
this.dispatchEvent(DeviceEvent.newInstance(this, DeviceEvent.CURRENT_CONNECT_CHANGED, deviceInfo));
}
this.dispatchEvent(DeviceEvent.newInstance(this, DeviceEvent.STATUS_CHANGED, deviceInfo));
}
protected void onUpdateMeshCompleted(Intent intent) {
this.dispatchEvent(MeshEvent.newInstance(this, MeshEvent.UPDATE_COMPLETED, -1));
}
protected void onMeshOffline(Intent intent) {
this.dispatchEvent(MeshEvent.newInstance(this, MeshEvent.OFFLINE, -1));
}
protected void onError(Intent intent) {
int errorCode = intent.getIntExtra(LightService.EXTRA_ERROR_CODE, -1);
this.dispatchEvent(MeshEvent.newInstance(this, MeshEvent.ERROR, errorCode));
}
protected void onNotify(Intent intent) {
NotificationInfo notifyInfo = intent.getParcelableExtra(LightService.EXTRA_NOTIFY);
this.onNotify(notifyInfo);
}
protected void onNotify(NotificationInfo notifyInfo) {
int opcode = notifyInfo.opcode;
String eventType = NotificationEvent.getEventType((byte) opcode);
if (Strings.isEmpty(eventType))
return;
NotificationEvent event = NotificationEvent.newInstance(this, eventType, notifyInfo);
event.setThreadMode(Event.ThreadMode.Background);
this.dispatchEvent(event);
}
/********************************************************************************
* Notification Parser API
*******************************************************************************/
/**
* 注册通知解析器
*
* @param parser
* @see NotificationParser
*/
protected void registerNotificationParser(NotificationParser parser) {
NotificationParser.register(parser);
}
}