wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*  Copyright (C) 2018-2024 Andreas Shimokawa, Arjan Schrijver, Carsten
    Pfeiffer, ladbsoft, mamucho, SalavatR, Sebastian Kranz
 
    This file is part of Gadgetbridge.
 
    Gadgetbridge is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    Gadgetbridge is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.xwatch;
 
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import android.media.AudioManager;
import android.view.KeyEvent;
import android.widget.Toast;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.UUID;
 
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.xwatch.XWatchSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.xwatch.XWatchService;
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.entities.User;
import nodomain.freeyourgadget.gadgetbridge.entities.XWatchActivitySample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice.State;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.DeviceInfo;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
 
public class XWatchSupport extends AbstractBTLEDeviceSupport {
    private static final Logger LOG = LoggerFactory.getLogger(XWatchSupport.class);
    private final GBDeviceEventVersionInfo versionCmd = new GBDeviceEventVersionInfo();
    TransactionBuilder builder = null;
    private byte dayToFetch; //0 = Today; 1 = Yesterday ...
    private byte maxDayToFetch;
    long lastButtonTimestamp;
 
    public XWatchSupport() {
        super(LOG);
 
        addSupportedService(XWatchService.UUID_SERVICE);
        addSupportedService(XWatchService.UUID_WRITE);
        addSupportedService(XWatchService.UUID_NOTIFY);
    }
 
    public static byte[] crcChecksum(byte[] data) {
        byte[] return_data = new byte[(data.length + 1)];
        byte checksum = 0;
 
        for (int i = 0; i < data.length; i++) {
            return_data[i] = data[i];
            checksum += data[i];
        }
        return_data[return_data.length - 1] = checksum;
 
        return return_data;
    }
 
    @Override
    protected TransactionBuilder initializeDevice(TransactionBuilder builder) {
        builder.add(new SetDeviceStateAction(getDevice(), State.INITIALIZING, getContext()));
 
        enableNotifications(builder)
                .setDateTime(builder)
                .setInitialized(builder);
 
        return builder;
    }
 
    /**
     * Last action of initialization sequence. Sets the device to initialized.
     * It is only invoked if all other actions were successfully run, so the device
     * must be initialized, then.
     *
     * @param builder
     */
    private void setInitialized(TransactionBuilder builder) {
        builder.add(new SetDeviceStateAction(getDevice(), State.INITIALIZED, getContext()));
    }
 
    @Override
    public boolean useAutoConnect() {
        return true;
    }
 
    @Override
    public boolean connectFirstTime() {
        for (int i = 0; i < 5; i++) {
            if (connect()) {
                return true;
            }
        }
        return false;
    }
 
    private XWatchSupport setDateTime(TransactionBuilder builder) {
        byte[] data;
 
        LOG.debug("Sending current date to the XWatch");
        BluetoothGattCharacteristic deviceData = getCharacteristic(XWatchService.UUID_WRITE);
 
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String y = time.substring(2, 4);
        String M = time.substring(4, 6);
        String d = time.substring(6, 8);
        String H = time.substring(8, 10);
        String m = time.substring(10, 12);
        String s = time.substring(12, 14);
        System.out.println(y + ":" + M + ":" + d + ":" + H + ":" + m + ":" + time.substring(12, 14));
 
        data = new byte[]{(byte) 1,
                (byte) Integer.parseInt(y, 16),
                (byte) Integer.parseInt(M, 16),
                (byte) Integer.parseInt(d, 16),
                (byte) Integer.parseInt(H, 16),
                (byte) Integer.parseInt(m, 16),
                (byte) Integer.parseInt(s, 16),
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0};
 
        data = crcChecksum(data);
 
        builder.write(deviceData, data);
 
        return this;
    }
 
    private XWatchSupport enableNotifications(TransactionBuilder builder) {
        LOG.debug("Enabling action button");
        BluetoothGattCharacteristic deviceInfo = getCharacteristic(XWatchService.UUID_NOTIFY);
        builder.notify(deviceInfo, true);
        return this;
    }
 
    @Override
    public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
        //TODO: Implement
    }
 
    @Override
    public void onNotification(NotificationSpec notificationSpec) {
        try {
            TransactionBuilder builder = performInitialized("xwatch notification");
            BluetoothGattCharacteristic deviceData = getCharacteristic(XWatchService.UUID_WRITE);
            byte[] data = new byte[]{XWatchService.COMMAND_NOTIFICATION, XWatchService.COMMAND_NOTIFICATION_MESSAGE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
            builder.write(deviceData, crcChecksum(data));
            builder.queue(getQueue());
        } catch (IOException ex) {
            LOG.error("Unable to send message notification on XWatch device", ex);
        }
    }
 
    @Override
    public void onDeleteNotification(int id) {
        //TODO: Implement
    }
 
    @Override
    public void onSetTime() {
        try {
            TransactionBuilder builder = performInitialized("Set date and time");
            setDateTime(builder);
            builder.queue(getQueue());
        } catch (IOException ex) {
            LOG.error("Unable to set time and date on XWatch device", ex);
        }
    }
 
    @Override
    public void onSetCallState(CallSpec callSpec) {
        if (callSpec.command == CallSpec.CALL_INCOMING) {
            LOG.debug("Incoming call8");
            try {
                TransactionBuilder builder = performInitialized("callnotification");
                BluetoothGattCharacteristic deviceData = getCharacteristic(XWatchService.UUID_WRITE);
                byte[] data = new byte[]{XWatchService.COMMAND_NOTIFICATION, XWatchService.COMMAND_NOTIFICATION_PHONE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
                builder.write(deviceData, crcChecksum(data));
                builder.queue(getQueue());
            } catch (IOException ex) {
                LOG.error("Unable to send call notification on XWatch device", ex);
            }
        }
    }
 
    @Override
    public void onFindDevice(boolean start) {
        //TODO: Implement
    }
 
    @Override
    public void onSetConstantVibration(int intensity) {
        //TODO: Implement
    }
 
    @Override
    public void onFetchRecordedData(int dataTypes) {
        try {
            if (builder == null) {
                builder = performInitialized("fetchActivityData");
            }
            requestSummarizedData(builder);
            performConnected(builder.getTransaction());
        } catch (IOException e) {
            GB.toast(getContext(), "Error fetching activity data: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
        }
    }
 
    @Override
    public boolean onCharacteristicChanged(BluetoothGatt gatt,
                                           BluetoothGattCharacteristic characteristic) {
        super.onCharacteristicChanged(gatt, characteristic);
 
        UUID characteristicUUID = characteristic.getUuid();
        if (XWatchService.UUID_NOTIFY.equals(characteristicUUID)) {
            byte[] data = characteristic.getValue();
            if (data[0] == XWatchService.COMMAND_ACTIVITY_TOTALS) {
                handleSummarizedData(characteristic.getValue());
            } else if (data[0] == XWatchService.COMMAND_ACTIVITY_DATA) {
                handleDetailedData(characteristic.getValue());
            } else if (data[0] == XWatchService.COMMAND_ACTION_BUTTON) {
                handleButtonPressed(characteristic.getValue());
            } else if (data[0] == XWatchService.COMMAND_CONNECTED) {
                handleDeviceInfo(data, BluetoothGatt.GATT_SUCCESS);
            } else {
                LOG.info("Handled characteristic with unknown data: " + characteristicUUID);
                logMessageContent(characteristic.getValue());
            }
        } else {
            LOG.info("Unhandled characteristic changed: " + characteristicUUID);
            logMessageContent(characteristic.getValue());
        }
        return false;
    }
 
    @Override
    public boolean onCharacteristicRead(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic, int status) {
        return super.onCharacteristicChanged(gatt, characteristic);
        //TODO: Implement (if necessary)
    }
 
    @Override
    public boolean onCharacteristicWrite(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic, int status) {
        return super.onCharacteristicWrite(gatt, characteristic, status);
        //TODO: Implement (if necessary)
    }
 
    public XWatchActivitySample createActivitySample(Device device, User user, int timestampInSeconds, SampleProvider provider) {
        XWatchActivitySample sample = new XWatchActivitySample();
        sample.setDevice(device);
        sample.setUser(user);
        sample.setTimestamp(timestampInSeconds);
        sample.setProvider(provider);
 
        return sample;
    }
 
    private void handleDeviceInfo(byte[] value, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            DeviceInfo mDeviceInfo = new DeviceInfo(value);
            LOG.warn("Device info: " + mDeviceInfo);
            versionCmd.hwVersion = "1.0";
            versionCmd.fwVersion = "1.0";
            handleGBDeviceEvent(versionCmd);
        }
    }
 
    private void handleSummarizedData(byte[] value) {
        int daysIntTotal;
        int daysIntPart;
 
        if (value.length != 16) {
            LOG.warn("GOT UNEXPECTED SENSOR DATA WITH LENGTH: " + value.length);
            for (byte b : value) {
                LOG.warn("DATA: " + String.format("0x%4x", b));
            }
        } else {
            daysIntPart = (value[1] & 255) << 24;
            daysIntTotal = daysIntPart;
            daysIntPart = (value[2] & 255) << 16;
            daysIntTotal += daysIntPart;
            daysIntPart = (value[3] & 255) << 8;
            daysIntTotal += daysIntPart;
            daysIntPart = (value[4] & 255);
            daysIntTotal += daysIntPart;
 
            dayToFetch = 0;
            maxDayToFetch = (byte) Integer.bitCount(daysIntTotal);
 
            try {
                requestDetailedData(builder);
                performConnected(builder.getTransaction());
            } catch (IOException e) {
                GB.toast(getContext(), "Error fetching activity data: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
            }
        }
    }
 
    private void handleDetailedData(byte[] value) {
        int category, intensity, steps = 0;
 
        if (value.length != 16) {
            LOG.warn("GOT UNEXPECTED SENSOR DATA WITH LENGTH: " + value.length);
            for (byte b : value) {
                LOG.warn("DATA: " + String.format("0x%4x", b));
            }
        } else {
            try (DBHandler dbHandler = GBApplication.acquireDB()) {
                XWatchSampleProvider provider = new XWatchSampleProvider(getDevice(), dbHandler.getDaoSession());
                User user = DBHelper.getUser(dbHandler.getDaoSession());
                Device device = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession());
                int timestampInSeconds = 0;
 
                timestampInSeconds = getTimestampFromData(
                        value[2],
                        value[3],
                        value[4],
                        value[5]
                );
 
                category = ActivityKind.ACTIVITY.getCode();
                intensity = (value[7] & 255) + ((value[8] & 255) << 8);
                steps = (value[9] & 255) + ((value[10] & 255) << 8);
 
                XWatchActivitySample sample = createActivitySample(device, user, timestampInSeconds, provider);
                sample.setRawIntensity(intensity);
                sample.setSteps(steps);
                sample.setRawKind(category);
 
                if (LOG.isDebugEnabled()) {
                    LOG.debug("sample: " + sample);
                }
 
                provider.addGBActivitySample(sample);
 
                if (value[5] == 95) {
                    dayToFetch++;
                    if (dayToFetch <= maxDayToFetch) {
                        try {
                            builder = performInitialized("fetchActivityData");
                            requestDetailedData(builder);
                            builder.queue(getQueue());
                        } catch (IOException e) {
                            GB.toast(getContext(), "Error fetching activity data: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
                        }
                    }
                }
            } catch (Exception ex) {
                GB.toast(getContext(), ex.getMessage(), Toast.LENGTH_LONG, GB.ERROR, ex);
            }
        }
    }
 
    private void handleButtonPressed(byte[] value) {
        long currentTimestamp = System.currentTimeMillis();
 
        AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.isWiredHeadsetOn()) {
            if (currentTimestamp - lastButtonTimestamp < 1000) {
                if (audioManager.isMusicActive()) {
                    audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
                    audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
                } else {
                    audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
                    audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
                    audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
                    audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
                }
            } else {
                audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
                audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
            }
        }
 
        lastButtonTimestamp = currentTimestamp;
    }
 
    private void requestSummarizedData(TransactionBuilder builder) {
        byte[] fetch = new byte[]{(byte) XWatchService.COMMAND_ACTIVITY_TOTALS,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0};
 
        fetch = XWatchSupport.crcChecksum(fetch);
        builder.write(getCharacteristic(XWatchService.UUID_WRITE), fetch);
    }
 
    private void requestDetailedData(TransactionBuilder builder) {
        byte[] fetch = new byte[]{(byte) XWatchService.COMMAND_ACTIVITY_DATA,
                (byte) dayToFetch,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0,
                (byte) 0};
 
        fetch = XWatchSupport.crcChecksum(fetch);
        builder.write(getCharacteristic(XWatchService.UUID_WRITE), fetch);
    }
 
    private int getTimestampFromData(byte year, byte month, byte day, byte hoursminutes) {
        int timestamp = 0;
        int yearInt, monthInt, dayInt, hoursMinutesInt = 0;
        int hours, minutes = 0;
 
        yearInt = Integer.valueOf(String.format("%02x", year), 16);
        monthInt = Integer.valueOf(String.format("%02x", month), 16);
        dayInt = Integer.valueOf(String.format("%02x", day), 16);
        hoursMinutesInt = Integer.valueOf(String.format("%02x", hoursminutes), 16);
 
        minutes = hoursMinutesInt % 4;
        hours = (hoursMinutesInt - minutes) / 4;
        minutes = minutes * 15;
 
        GregorianCalendar cal = new GregorianCalendar(
                2000 + yearInt,
                monthInt - 1,
                dayInt,
                hours,
                minutes
        );
 
        timestamp = (int) (cal.getTimeInMillis() / 1000);
 
        return timestamp;
    }
 
    @Override
    public boolean getImplicitCallbackModify() {
        return true;
    }
 
    @Override
    public boolean getSendWriteRequestResponse() {
        return false;
    }
}