CinemaControlService.java
12.5 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
351
352
353
354
355
356
357
/*
* Copyright 2016 wugian
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xgimi.gimicinema.service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;
import com.gimi.common.cinema.model.MessageEvent;
import com.gimi.common.cinema.utils.LogUtils;
import com.gimi.common.cinema.utils.Utils;
import com.qnbar.smc.model.Light;
import com.qnbar.smc.model.Lights;
import com.qnbar.smc.service.SmartControlService;
import com.qnbar.smc.service.TelinkLightService;
import com.telink.bluetooth.light.ConnectionStatus;
import com.telink.bluetooth.light.DeviceInfo;
import com.xgimi.gimicinema.ICinemaControl;
import com.xgimi.gimicinema.application.FangTangApplication;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;
public class CinemaControlService extends Service {
private static final int LIGHT_MIN_VALUE = 5;
private ArrayList<String> movies = new ArrayList<>();
private long duration = 0;
private long currentPosition = 0;
private int currentState = 0;
private String currentPath = null;
// private static final int SAVE_BEAT = 500;
private static final int SAVE_BEAT = 5;//default 50
private int saveBeat = SAVE_BEAT;
private final ICinemaControl.Stub cinemaControl = new ICinemaControl.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public void setMovieDuration(long duration) throws RemoteException {
// Log.d("aidl", "setMovieDuration: " + duration);
CinemaControlService.this.duration = duration;
}
@Override
public long getMovieDuration() throws RemoteException {
return duration;
}
@Override
public void setCurrentMoviePosition(long duration) throws RemoteException {
// Log.d("aidl", "setCurrentMoviePosition: " + duration);
currentPosition = duration;
if (saveBeat-- == 0) {
//TODO 保存当前进度,需要时从保存位置开始播放,取消播放器播放记录功能
// 播放一段时间确保状态为关闭
// saveBeat = SAVE_BEAT;
try {
if (!fadeOuting && Lights.getInstance().getByMeshAddress(
((FangTangApplication) getApplication()).getConnectDevice().meshAddress).status
== ConnectionStatus.ON) {
fadeOut();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public long getCurrentMoviePosition() throws RemoteException {
return currentPosition;
}
@Override
public int getCurrentStatus() throws RemoteException {
return currentState;
}
@Override
public void setCurrentStatus(int state) throws RemoteException {
Log.d("aidl", "setCurrentStatus: " + state);
if (TelinkLightService.Instance() == null) {
return;
}
if (state != currentState) {
currentState = state;
Log.d("aidl", "state change," + currentPath + "," + duration + "," + currentPosition + ",state:" + state);
Log.d("aidl", "state change cinemaSMC not null");
// if (true) {
// return;
// }
// MessageEvent msgEvent = new MessageEvent();
switch (state) {
case 0://空闲
fadeIn();
showQrCodeRightNow();
// msgEvent.setEventId(0x19910);
// msgEvent.setMessage("open_switch");
// EventBus.getDefault().post(msgEvent);
break;
case 1://播放
fadeOut();
// msgEvent.setEventId(0x19910);
// msgEvent.setMessage("close_switch");
// EventBus.getDefault().post(msgEvent);
break;
case 2://暂停
// fadeIn();
// msgEvent.setEventId(0x19910);
// msgEvent.setMessage("open_switch");
// EventBus.getDefault().post(msgEvent);
break;
}
}
}
@Override
public void addPlayPath(String path) throws RemoteException {
movies.add(path);//strict contract
// MessageEvent msgEvent = new MessageEvent();
// msgEvent.setEventId(0x19910);
// msgEvent.setMessage("open_switch");
// EventBus.getDefault().post(msgEvent);
}
@Override
public void setCurrentPlayPath(String path) throws RemoteException {
// Log.d("aidl", "setCurrentPlayPath: " + path);
CinemaControlService.this.currentPath = path;
FangTangApplication application = (FangTangApplication) getApplication();
application.setCurrentPlayUrl(path);
}
@Override
public String getCurrentPath() throws RemoteException {
return CinemaControlService.this.currentPath;
}
@Override
public List<String> getPlayList() throws RemoteException {
return movies;
}
@Override
public void setPlayList(List<String> movies) throws RemoteException {
CinemaControlService.this.movies.clear();
if (movies == null || movies.size() == 0) {
return;
}
CinemaControlService.this.movies.addAll(movies);
}
public void openLight() throws RemoteException {
fadeIn();
}
public void closeLight() throws RemoteException {
fadeOut();
}
};
public CinemaControlService() {
}
Runnable doClose = new Runnable() {
@Override
public void run() {
close();
}
};
private int value;
private Handler handler = new Handler();
private Runnable closeRunnable = new Runnable() {
@Override
public void run() {
setLightValue(value);
if (value-- == maxValue) {
handler.postDelayed(this, 4 * 1000);
} else {
if (value == 0) {
//how to remove
handler.postDelayed(doClose, 3 * 1000);
} else {
handler.postDelayed(this, 80);
}
}
}
};
private Runnable openRunnable = new Runnable() {
@Override
public void run() {
if (value++ == LIGHT_MIN_VALUE) {
open();
handler.postDelayed(this, 2 * 1000);
} else {
setLightValue(value);
if (value != maxValue) {
handler.postDelayed(this, 200);
}
}
}
};
private FangTangApplication mApplication;
private Context context;
@Override
public void onCreate() {
super.onCreate();
context = this;
mApplication = (FangTangApplication) getApplication();
}
@Override
public IBinder onBind(Intent intent) {
return cinemaControl;
}
private void open() {
byte opcode = (byte) 0xD0;
int address = 0xFFFF;
byte[] params = new byte[]{0x01, 0x00, 0x00};
if (TelinkLightService.Instance() != null && TelinkLightService.Instance().sendCommandNoResponse(opcode, address, params)) {
Log.d("aidl", "open all success");
} else {
Log.d("aidl", "open all failure:" + (TelinkLightService.Instance() != null));
}
}
private void close() {
byte opcode = (byte) 0xD0;
int address = 0xFFFF;
byte[] params = new byte[]{0x00, 0x00, 0x00};
if (TelinkLightService.Instance() != null && TelinkLightService.Instance().sendCommandNoResponse(opcode, address, params)) {
Log.d("aidl", "close all success");
} else {
Log.d("aidl", "close all failure:" + (TelinkLightService.Instance() != null));
}
fadeOuting = false;
}
private boolean fadeOuting = false;
private int maxValue = 50;
public void fadeIn() {
DeviceInfo connectDevice = mApplication.getConnectDevice();
if (connectDevice == null) {
Log.d("aidl", "connect device is null");
return;
}
Light byMeshAddress = Lights.getInstance().getByMeshAddress(connectDevice.meshAddress);
if (byMeshAddress == null) {
Toast.makeText(context, "若智能灯光是出厂设置,请重新配置", Toast.LENGTH_SHORT).show();
return;
}
maxValue = Utils.getInt(context, "brightness", 50);
if (byMeshAddress.status == ConnectionStatus.ON) {
fadeOuting = false;
handler.removeCallbacks(doClose);
handler.removeCallbacks(closeRunnable);
open();
setLightValue(maxValue);
return;
}
// int status = mApplication.getConnectDevice().status;
// Log.d("status_charge", "fadeIn: " + status);
value = LIGHT_MIN_VALUE;
// if (byMeshAddress.brightness > value) {
// value = byMeshAddress.brightness;
// }
handler.post(openRunnable);
}
public void fadeOut() {
fadeOuting = true;
DeviceInfo connectDevice = mApplication.getConnectDevice();
if (connectDevice == null) {
fadeOuting = false;
Log.d("aidl", "connect device is null");
return;
}
Light byMeshAddress = Lights.getInstance().getByMeshAddress(connectDevice.meshAddress);
if (byMeshAddress == null) {
Toast.makeText(context, "若智能灯光是出厂设置,请重新配置", Toast.LENGTH_SHORT).show();
fadeOuting = false;
return;
}
if (byMeshAddress.status == ConnectionStatus.OFF) {
handler.removeCallbacks(openRunnable);
fadeOuting = false;
setLightValue(LIGHT_MIN_VALUE);
close();
return;
}
maxValue = Utils.getInt(context, "brightness", 50);
// int status = connectDevice.status;
// Log.d("status_charge", "fadeOut: " + status);
value = maxValue;
handler.post(closeRunnable);
}
private void setLightValue(int value) {
int addr = 0xFFFF;//addr of all
//change group brightness or color temperature
// addr = 0x8003;
byte opcode;
byte[] params;
opcode = (byte) 0xD2;
params = new byte[]{(byte) value};
if (TelinkLightService.Instance() != null && TelinkLightService.Instance().sendCommandNoResponse(opcode, addr, params, true)) {
Log.d("aidl", "set value success:" + value);
} else {
Log.d("aidl", "set value failure:" + (TelinkLightService.Instance() != null));
}
// opcode = (byte) 0xE2;
// params = new byte[]{0x05, (byte) value};
// TelinkLightService.Instance().sendCommandNoResponse(opcode, addr, params, true);
}
private void showQrCodeRightNow() {
if (duration - currentPosition < 1000 * 60) {
MessageEvent messageEvent = new MessageEvent();
messageEvent.setEventId(SmartControlService.ORDER_PLAY_COMPLETE);
messageEvent.setMessage("电影播放完成,记录完成订单信息");
EventBus.getDefault().post(messageEvent);
LogUtils.i("room-info", "stop media completed:" + currentPath);
} else {
LogUtils.i("room-info", "stop media but not completed:" + currentPath);
}
}
}