DeviceSettingActivity.java
8.37 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
/*
* Copyright 2017 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.qnbar.smc;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.gimi.common.cinema.utils.Utils;
import com.qnbar.smc.model.Light;
import com.qnbar.smc.model.Lights;
import com.qnbar.smc.service.TelinkLightService;
import com.telink.bluetooth.event.DeviceEvent;
import com.telink.bluetooth.event.LeScanEvent;
import com.telink.bluetooth.light.LightAdapter;
import com.telink.util.Event;
import com.telink.util.EventListener;
import com.xgimi.gimicinema.R;
import com.xgimi.gimicinema.application.FangTangApplication;
public class DeviceSettingActivity extends Activity implements OnClickListener, EventListener<String> {
private Handler handler = new Handler();
private FangTangApplication mApplication;
private SeekBar brightnessBar;
private SeekBar temperatureBar;
private TextView temperatureTv;
private TextView brightnessTv;
private Button remove;
private View delete;
private SeekBar.OnSeekBarChangeListener barChangeListener = new SeekBar.OnSeekBarChangeListener() {
private long preTime;
private int delayTime = 100;
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
this.onValueChange(seekBar, seekBar.getProgress(), true);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
this.preTime = System.currentTimeMillis();
this.onValueChange(seekBar, seekBar.getProgress(), true);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
/* if (progress % 5 != 0)
return;
long currentTime = System.currentTimeMillis();
if ((currentTime - this.preTime) >= this.delayTime) {
this.preTime = currentTime;*/
this.onValueChange(seekBar, progress, false);
//}
}
private void onValueChange(View view, int progress, boolean immediate) {
int addr = meshAddress;
//change group brightness or color temperature
// addr = 0x8003;
byte opcode;
byte[] params;
if (view == brightnessBar) {
opcode = (byte) 0xD2;
params = new byte[]{(byte) progress};
brightnessTv.setText("亮度:" + progress);
TelinkLightService.Instance().sendCommandNoResponse(opcode, addr, params, immediate);
} else if (view == temperatureBar) {
temperatureTv.setText("色温:" + progress);
opcode = (byte) 0xE2;
params = new byte[]{0x05, (byte) progress};
TelinkLightService.Instance().sendCommandNoResponse(opcode, addr, params, immediate);
}
}
};
private int meshAddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setContentView(R.layout.activity_device_setting);
mApplication = (FangTangApplication) getApplication();
this.meshAddress = this.getIntent().getIntExtra("meshAddress", 0);
Light light = Lights.getInstance().getByMeshAddress(meshAddress);
// if (light != null) {
// TextView txtTitle = (TextView) this
// .findViewById(R.id.txt_header_title);
// txtTitle.setText(TextUtils.isEmpty(light.getLabel2()) ? "" : light.getLabel2());
// }
this.temperatureTv = (TextView) findViewById(R.id.temperatureTv);
this.brightnessTv = (TextView) findViewById(R.id.brightnessTv);
this.brightnessBar = (SeekBar) findViewById(R.id.sb_brightness);
this.temperatureBar = (SeekBar) findViewById(R.id.sb_temperature);
int brightness = Utils.getInt(this, "brightness", 50);
int temperature = Utils.getInt(this, "temperature", 50);
temperatureTv.setText("色温:" + temperature);
brightnessTv.setText("亮度:" + brightness);
brightnessBar.setProgress(brightness);
temperatureBar.setProgress(temperature);
this.brightnessBar.setOnSeekBarChangeListener(barChangeListener);
this.temperatureBar.setOnSeekBarChangeListener(barChangeListener);
this.remove = (Button) findViewById(R.id.btn_remove);
this.remove.setOnClickListener(this);
this.delete = findViewById(R.id.btn_delete);
this.delete.setOnClickListener(this);
}
@Override
public void onResume() {
super.onResume();
// 监听各种事件
mApplication.addEventListener(LeScanEvent.LE_SCAN, this);
mApplication.addEventListener(DeviceEvent.STATUS_CHANGED, this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_remove:
byte opcode = (byte) 0xE3;
TelinkLightService.Instance().sendCommand(opcode, meshAddress, null);
show("请稍候,踢除成功,若需要控制请联out_of_mesh");
break;
}
}
int brightness;
int colorTemp;
public void changeTemperature(View view) {
colorTemp = 0;
handler.post(cupRunnable);
}
Runnable cupRunnable = new Runnable() {
@Override
public void run() {
if (colorTemp == 100) {
handler.removeCallbacks(this);
return;
}
temperatureBar.setProgress(colorTemp++);
handler.postDelayed(this, 200);
}
};
private void brightnessUp() {
brightness = 0;
handler.post(bupRunnable);
}
Runnable bupRunnable = new Runnable() {
@Override
public void run() {
if (brightness == 100) {
handler.removeCallbacks(this);
return;
}
brightnessBar.setProgress(brightness++);
handler.postDelayed(this, 200);
}
};
private void brightnessDown() {
}
@Override
protected void onPause() {
super.onPause();
mApplication.removeEventListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
Utils.saveInt(this, "brightness", brightnessBar.getProgress());
Utils.saveInt(this, "temperature", temperatureBar.getProgress());
}
public void changeBrightness(View view) {
brightnessUp();
}
@Override
public void performed(Event<String> event) {
if (event instanceof DeviceEvent) {
this.onDeviceEvent((DeviceEvent) event);
}
}
private void onDeviceEvent(DeviceEvent event) {
String type = event.getType();
switch (type) {
case DeviceEvent.STATUS_CHANGED:
int status = event.getArgs().status;
if (status == LightAdapter.STATUS_CONNECTED) {
show("device setting activity connected");
} else if (status == LightAdapter.STATUS_DELETE_COMPLETED) {
show("delete success");
} else if (status == LightAdapter.STATUS_DELETE_FAILURE) {
show("delete fail");
}
break;
}
}
private void show(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}