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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/*  Copyright (C) 2024 Damien Gaignon, Martin.JM, Vitalii Tomin
 
    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.devices.huawei;
 
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
 
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
 
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
 
import de.greenrobot.dao.query.QueryBuilder;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.CameraActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.appmanager.AppManagerActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettings;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsScreen;
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.App;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Notifications;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Notifications.NotificationConstraintsType;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Watchface;
import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummaryDao;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiActivitySampleDao;
import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiDictData;
import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiDictDataDao;
import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiDictDataValuesDao;
import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiWorkoutDataSampleDao;
import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiWorkoutPaceSampleDao;
import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiWorkoutSummarySample;
import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiWorkoutSummarySampleDao;
import nodomain.freeyourgadget.gadgetbridge.entities.HuaweiWorkoutSwimSegmentsSampleDao;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
 
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.*;
 
import androidx.annotation.NonNull;
 
public class HuaweiCoordinator {
    Logger LOG = LoggerFactory.getLogger(HuaweiCoordinator.class);
 
    TreeMap<Integer, byte[]> commandsPerService = new TreeMap<>();
    // Each byte of expandCapabilities represent a "service"
    // Each bit in a "service" represent a feature so 1 or 0 is used to check is support or not
    byte[] expandCapabilities = null;
    byte notificationCapabilities = -0x01;
    ByteBuffer notificationConstraints = null;
 
    private boolean supportsTruSleepNewSync = false;
    private boolean supportsGpsNewSync = false;
 
    private Watchface.WatchfaceDeviceParams watchfaceDeviceParams;
 
    private App.AppDeviceParams appDeviceParams;
 
    private HuaweiMusicUtils.MusicCapabilities musicDeviceParams = null;
 
    private HuaweiMusicUtils.MusicCapabilities musicExtendedDeviceParams = null;
 
    private final HuaweiCoordinatorSupplier parent;
 
    private boolean transactionCrypted=true;
 
    private int maxContactsCount = 0;
 
    public HuaweiCoordinator(HuaweiCoordinatorSupplier parent) {
        this.parent = parent;
 
        // Set non-numeric capabilities
        this.expandCapabilities = GB.hexStringToByteArray(getCapabilitiesSharedPreferences().getString("expandCapabilities", "00"));
        this.notificationCapabilities = (byte)getCapabilitiesSharedPreferences().getInt("notificationCapabilities", -0x01);
        this.notificationConstraints = ByteBuffer.wrap(GB.hexStringToByteArray(
                getCapabilitiesSharedPreferences().getString(
                        "notificationConstraints",
                        GB.hexdump(Notifications.defaultConstraints)
                )));
        this.maxContactsCount = getCapabilitiesSharedPreferences().getInt("maxContactsCount", 0);
 
        for (String key : getCapabilitiesSharedPreferences().getAll().keySet()) {
            int service;
            try {
                service = Integer.parseInt(key);
                byte[] commands = GB.hexStringToByteArray(getCapabilitiesSharedPreferences().getString(key, "00"));
                this.commandsPerService.put(service, commands);
            } catch (NumberFormatException e) {
                // These are the non-numeric capabilities, which have been set already
            }
        }
    }
 
    protected void deleteDevice(@NonNull GBDevice gbDevice, @NonNull Device device, @NonNull DaoSession session) throws GBException {
        long deviceId = device.getId();
        QueryBuilder<?> qb = session.getHuaweiActivitySampleDao().queryBuilder();
        qb.where(HuaweiActivitySampleDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
 
        QueryBuilder<HuaweiWorkoutSummarySample> qb2 = session.getHuaweiWorkoutSummarySampleDao().queryBuilder();
        List<HuaweiWorkoutSummarySample> workouts = qb2.where(HuaweiWorkoutSummarySampleDao.Properties.DeviceId.eq(deviceId)).build().list();
        for (HuaweiWorkoutSummarySample sample : workouts) {
            session.getHuaweiWorkoutDataSampleDao().queryBuilder().where(
                    HuaweiWorkoutDataSampleDao.Properties.WorkoutId.eq(sample.getWorkoutId())
            ).buildDelete().executeDeleteWithoutDetachingEntities();
 
            session.getHuaweiWorkoutPaceSampleDao().queryBuilder().where(
                    HuaweiWorkoutPaceSampleDao.Properties.WorkoutId.eq(sample.getWorkoutId())
            ).buildDelete().executeDeleteWithoutDetachingEntities();
 
            session.getHuaweiWorkoutSwimSegmentsSampleDao().queryBuilder().where(
                    HuaweiWorkoutSwimSegmentsSampleDao.Properties.WorkoutId.eq(sample.getWorkoutId())
            ).buildDelete().executeDeleteWithoutDetachingEntities();
        }
 
        session.getHuaweiWorkoutSummarySampleDao().queryBuilder().where(HuaweiWorkoutSummarySampleDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
 
        session.getBaseActivitySummaryDao().queryBuilder().where(BaseActivitySummaryDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
 
        QueryBuilder<HuaweiDictData> qb3 = session.getHuaweiDictDataDao().queryBuilder();
        List<HuaweiDictData> dictData = qb3.where(HuaweiDictDataDao.Properties.DeviceId.eq(deviceId)).build().list();
        for (HuaweiDictData data : dictData) {
            session.getHuaweiDictDataValuesDao().queryBuilder().where(
                    HuaweiDictDataValuesDao.Properties.DictId.eq(data.getDictId())
            ).buildDelete().executeDeleteWithoutDetachingEntities();
        }
 
        session.getHuaweiDictDataDao().queryBuilder().where(HuaweiDictDataDao.Properties.DeviceId.eq(deviceId)).buildDelete().executeDeleteWithoutDetachingEntities();
    }
 
    private SharedPreferences getCapabilitiesSharedPreferences() {
        return GBApplication.getContext().getSharedPreferences("huawei_coordinator_capatilities" + parent.getDeviceType().name(), Context.MODE_PRIVATE);
    }
 
    private SharedPreferences getDeviceSpecificSharedPreferences(GBDevice gbDevice) {
        return GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress());
    }
 
    public boolean getForceOption(GBDevice gbDevice, String option) {
        return getDeviceSpecificSharedPreferences(gbDevice).getBoolean(option, false);
    }
 
    private void saveCommandsForService(int service, byte[] commands) {
        commandsPerService.put(service, commands);
        getCapabilitiesSharedPreferences().edit().putString(String.valueOf(service), GB.hexdump(commands)).apply();
    }
 
    public void saveExpandCapabilities(byte[] capabilities) {
        expandCapabilities = capabilities;
        getCapabilitiesSharedPreferences().edit().putString("expandCapabilities", GB.hexdump(capabilities)).apply();
    }
 
    public void saveNotificationCapabilities(byte capabilities) {
        notificationCapabilities = capabilities;
        getCapabilitiesSharedPreferences().edit().putInt("notificationCapabilities", (int)capabilities).apply();
    }
 
    public void saveNotificationConstraints(ByteBuffer constraints) {
        notificationConstraints = constraints;
        getCapabilitiesSharedPreferences().edit().putString("notificationConstraints", GB.hexdump(constraints.array())).apply();
    }
 
    public void saveMaxContactsCount(int maxContactsCount) {
        this.maxContactsCount = maxContactsCount;
        getCapabilitiesSharedPreferences().edit().putInt("maxContactsCount", maxContactsCount).apply();
    }
 
    public void addCommandsForService(int service, byte[] commands) {
        if (!commandsPerService.containsKey(service)) {
            saveCommandsForService(service, commands);
            return;
        }
        byte[] saved = commandsPerService.get(service);
        if (saved == null) {
            saveCommandsForService(service, commands);
            return;
        }
        if (saved.length != commands.length) {
            saveCommandsForService(service, commands);
            return;
        }
        boolean changed = false;
        for (int i = 0; i < saved.length; i++) {
            if (saved[i] != commands[i]) {
                changed = true;
                break;
            }
        }
        if (changed)
            saveCommandsForService(service, commands);
    }
 
    public byte[] getCommandsForService(int service) {
        return commandsPerService.get(service);
    }
 
    // Print all Services ID and Commands ID
    public void printCommandsPerService() {
        StringBuilder msg = new StringBuilder();
        for(Map.Entry<Integer, byte[]> entry : commandsPerService.entrySet()) {
            msg.append("ServiceID: ").append(Integer.toHexString(entry.getKey())).append(" => Commands: ");
            for (byte b: entry.getValue()) {
                msg.append(Integer.toHexString(b)).append(" ");
            }
            msg.append("\n");
        }
        LOG.info(msg.toString());
    }
 
    private boolean supportsCommandForService(int service, int command) {
        byte[] commands = commandsPerService.get(service);
        if (commands == null)
            return false;
        for (byte b : commands)
            if (b == (byte) command)
                return true;
        return false;
    }
 
    private boolean supportsExpandCapability(int which) {
        // capability is a number containing :
        //  - the index of the "service"
        //  - the real capability number
        if (expandCapabilities == null) {
            LOG.debug("Expand capabilities is null");
            return false;
        }
        if (which >= expandCapabilities.length * 8) {
            LOG.debug("Capability is not supported");
            return false;
        }
        int capability = 1 << (which % 8);
        if ((expandCapabilities[which / 8] & capability) == capability) return true;
        return false;
    }
 
    private boolean supportsNotificationConstraint(byte which) {
        return notificationConstraints.get(which) == 0x01;
    }
 
    private int getNotificationConstraint(byte which) {
        return (int)notificationConstraints.getShort(which);
    }
 
    public DeviceSpecificSettings getDeviceSpecificSettings(final GBDevice device) {
        final DeviceSpecificSettings deviceSpecificSettings = new DeviceSpecificSettings();
 
        // Health
        if (supportsInactivityWarnings())
            deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.HEALTH, R.xml.devicesettings_inactivity_sheduled);
        if (supportsTruSleep())
            deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.HEALTH, R.xml.devicesettings_trusleep);
        if (supportsHeartRate())
            deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.HEALTH, R.xml.devicesettings_heartrate_automatic_enable);
        if (supportsSPo2())
            deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.HEALTH, R.xml.devicesettings_spo_automatic_enable);
        if(supportsTemperature()) {
            deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.HEALTH, R.xml.devicesettings_temperature_automatic_enable);
        }
 
        // Notifications
        final List<Integer> notifications = deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.NOTIFICATIONS);
        notifications.add(R.xml.devicesettings_notifications_enable);
        if (supportsNotificationOnBluetoothLoss())
            notifications.add(R.xml.devicesettings_disconnectnotification_noshed);
        if (supportsDoNotDisturb(device))
            notifications.add(R.xml.devicesettings_donotdisturb_allday_liftwirst_notwear);
 
        // Workout
        if (supportsSendingGps())
            deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.WORKOUT, R.xml.devicesettings_workout_send_gps_to_band);
 
        // Other
        deviceSpecificSettings.addRootScreen(R.xml.devicesettings_find_phone);
        deviceSpecificSettings.addRootScreen(R.xml.devicesettings_disable_find_phone_with_dnd);
        deviceSpecificSettings.addRootScreen(R.xml.devicesettings_allow_accept_reject_calls);
 
        // Camera control
        if (supportsCameraRemote())
            deviceSpecificSettings.addRootScreen(R.xml.devicesettings_camera_remote);
 
        //Contacts
        if (getContactsSlotCount(device) > 0) {
            deviceSpecificSettings.addRootScreen(R.xml.devicesettings_contacts);
        }
 
        //Music
        if (supportsMusicUploading() && getMusicInfoParams() != null && device.isConnected()) {
            deviceSpecificSettings.addRootScreen(R.xml.devicesettings_musicmanagement);
        }
 
        // Time
        if (supportsDateFormat()) {
            final List<Integer> dateTime = deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.DATE_TIME);
            dateTime.add(R.xml.devicesettings_dateformat);
            dateTime.add(R.xml.devicesettings_timeformat);
        }
 
        //Calendar
        if( supportsP2PService() && supportsCalendar()) {
            deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.CALENDAR, R.xml.devicesettings_sync_calendar);
        }
 
        // Display
        if (supportsWearLocation(device))
            deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.DISPLAY, R.xml.devicesettings_wearlocation);
        if (supportsAutoWorkMode())
            deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.DISPLAY, R.xml.devicesettings_workmode);
        if (supportsActivateOnLift())
            deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.DISPLAY, R.xml.devicesettings_liftwrist_display_noshed);
        if (supportsRotateToCycleInfo())
            deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.DISPLAY, R.xml.devicesettings_rotatewrist_cycleinfo);
        // Currently on main setting menu.
        /*if (supportsLanguageSetting())
            deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.DISPLAY, R.xml.devicesettings_language_generic);*/
        if(supportsTemperature()) {
            deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.DISPLAY, R.xml.devicesettings_temperature_scale_cf);
        }
 
        // Developer
        final List<Integer> developer = deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.DEVELOPER);
        developer.add(R.xml.devicesettings_huawei_debug);
 
        return deviceSpecificSettings;
    }
 
    public boolean supportsDateFormat() {
        return supportsCommandForService(0x01, 0x04);
    }
 
    public boolean supportsActivateOnLift() {
        return supportsCommandForService(0x01, 0x09);
    }
 
    public boolean supportsDoNotDisturb() {
        return supportsCommandForService(0x01, 0x0a);
    }
 
    public boolean supportsDoNotDisturb(GBDevice gbDevice) {
        return supportsDoNotDisturb() || getForceOption(gbDevice, PREF_FORCE_DND_SUPPORT);
    }
 
    public boolean supportsActivityType() {
        return supportsCommandForService(0x01, 0x12);
    }
 
    public boolean supportsWearLocation() {
        return supportsCommandForService(0x01, 0x1a);
    }
 
    public boolean supportsWearLocation(GBDevice gbDevice) {
        return supportsWearLocation() || getForceOption(gbDevice, PREF_FORCE_ENABLE_WEAR_LOCATION);
    }
 
    public boolean supportsRotateToCycleInfo() {
        return supportsCommandForService(0x01, 0x1b);
    }
 
    public boolean supportsQueryDndLiftWristDisturbType() {
        return supportsCommandForService(0x01, 0x1d);
    }
 
    public boolean supportsCameraRemote() {
        return supportsCommandForService(0x01, 0x29) && CameraActivity.supportsCamera();
    }
 
    public boolean supportsContacts() {
        return supportsCommandForService(0x03, 0x1);
    }
 
    public boolean supportsCalendarEvents() {
        return supportsP2PService() && supportsCalendar();
    }
 
    public boolean supportsAcceptAgreement() {
        return supportsCommandForService(0x01, 0x30);
    }
 
    public boolean supportsSettingRelated() {
        return supportsCommandForService(0x01, 0x31);
    }
 
    public boolean supportsTimeAndZoneId() {
        return supportsCommandForService(0x01, 0x32);
    }
 
    public boolean supportsConnectStatus() {
        return supportsCommandForService(0x01, 0x35);
    }
 
    public boolean supportsExpandCapability() {
        return supportsCommandForService(0x01, 0x37);
    }
 
    public boolean supportsNotificationAlert() {
        return supportsCommandForService(0x02, 0x01);
    }
 
    public boolean supportsNotification() {
        return supportsCommandForService(0x02, 0x04);
    }
 
    public boolean supportsWearMessagePush() {
        return supportsCommandForService(0x02, 0x08);
    }
 
    public boolean supportsMotionGoal() {
        return supportsCommandForService(0x07, 0x01);
    }
    
    public boolean supportsInactivityWarnings() {
        return supportsCommandForService(0x07, 0x06);
    }
 
    public boolean supportsActivityReminder() {
        return supportsCommandForService(0x07, 0x07);
    }
 
    public boolean supportsDeviceReportThreshold() {
        return supportsCommandForService(0x07, 0x0e);
    }
 
    public boolean supportsTruSleep() {
        return supportsCommandForService(0x07, 0x16);
    }
 
    public boolean supportsHeartRate() {
        return supportsCommandForService(0x07, 0x17);
    }
 
    public boolean supportsHeartRate(GBDevice gbDevice) {
        return supportsHeartRate() || getForceOption(gbDevice, PREF_FORCE_ENABLE_HEARTRATE_SUPPORT);
    }
 
    public boolean supportsHeartRateZones() {
        return supportsCommandForService(0x07, 0x13);
    }
 
    public boolean supportsExtendedHeartRateZones() {
        return supportsCommandForService(0x07, 0x21);
    }
 
    public boolean supportsFitnessRestHeartRate() {
        return supportsCommandForService(0x07, 0x23);
    }
 
    public boolean supportsSPo2() {
        return supportsCommandForService(0x07, 0x24);
    }
 
    public boolean supportsSPo2(GBDevice gbDevice) {
        return supportsSPo2() || getForceOption(gbDevice, PREF_FORCE_ENABLE_SPO2_SUPPORT);
    }
 
    public boolean supportsRunPaceConfig() {
        return supportsCommandForService(0x07, 0x28);
    }
 
    public boolean supportsFitnessThresholdValue() {
        return supportsCommandForService(0x07, 0x29);
    }
    public boolean supportsFitnessThresholdValueV2() { return supportsExpandCapability(0x9a) || supportsExpandCapability(0x9c); }
 
    // 0x1d - SupportTemperature
    // 0xba - SupportTemperatureClassification
    // 0x43 - SupportTemperatureStudy
    public boolean supportsTemperature() { return supportsExpandCapability(0x1d); }
 
    public boolean supportsBloodPressure() { return supportsExpandCapability(0x3b); }
 
    public boolean supportsEventAlarm() {
        return supportsCommandForService(0x08, 0x01);
    }
 
    public boolean supportsSmartAlarm() {
        return supportsCommandForService(0x08, 0x02) ;
    }
 
    public boolean supportsSmartAlarm(GBDevice gbDevice) {
        return supportsSmartAlarm() || getForceOption(gbDevice, PREF_FORCE_ENABLE_SMART_ALARM);
    }
 
    public boolean supportsSmartAlarm(GBDevice gbDevice, int alarmPosition) {
        return supportsSmartAlarm(gbDevice) && alarmPosition == 0;
    }
 
    public boolean forcedSmartWakeup(GBDevice device, int alarmPosition) {
        return supportsSmartAlarm(device, alarmPosition) && alarmPosition == 0;
    }
 
    /**
     * @return True if alarms can be changed on the device, false otherwise
     */
    public boolean supportsChangingAlarm() {
        return supportsCommandForService(0x08, 0x03);
    }
 
    public boolean supportsNotificationOnBluetoothLoss() {
        return supportsCommandForService(0x0b, 0x03);
    }
 
    public boolean supportsLanguageSetting() {
        return supportsCommandForService(0x0c, 0x01);
    }
 
    public boolean supportsWatchfaceParams(){ return supportsCommandForService(0x27, 0x01);}
 
    public boolean supportsAppParams(){ return supportsCommandForService(0x2a, 0x06);}
 
    public boolean supportsMusicUploading(){ return supportsCommandForService(0x25, 0x04);}
 
    public boolean supportsWeather() {
        return supportsCommandForService(0x0f, 0x01);
    }
 
    public boolean supportsWeatherUnit() {
        return supportsCommandForService(0x0f, 0x05);
    }
 
    public boolean supportsWeatherExtended() {
        return supportsCommandForService(0x0f, 0x06);
    }
 
    public boolean supportsWeatherErrorSimple() {
        return supportsCommandForService(0x0f, 0x07);
    }
 
    public boolean supportsWeatherForecasts() {
        return supportsCommandForService(0x0f, 0x08);
    }
 
    public boolean supportsWeatherMoonRiseSet() {
        return supportsCommandForService(0x0f, 0x0a);
    }
 
    public boolean supportsWeatherTides() {
        return supportsCommandForService(0x0f, 0x0b);
    }
 
    public boolean supportsWeatherErrorExtended() {
        return supportsCommandForService(0x0f, 0x0c);
    }
 
    public boolean supportsWeatherUvIndex() {
        return supportsExpandCapability(0x2f);
    }
 
    public boolean supportsWorkouts() {
        return supportsCommandForService(0x17, 0x01);
    }
 
    public boolean supportsWorkoutsTrustHeartRate() {
        return supportsCommandForService(0x17, 0x17);
    }
 
    public boolean supportsSendingGps() {
        return supportsCommandForService(0x18, 0x02);
    }
 
    public boolean supportsAccount() {
        return supportsCommandForService(0x1A, 0x01);
    }
 
    public boolean supportsAccountJudgment() {
        return supportsCommandForService(0x1A, 0x05);
    }
 
    public boolean supportsAccountSwitch() {
        return supportsCommandForService(0x1A, 0x06);
    }
 
    public boolean supportsDiffAccountPairingOptimization() {
        if (supportsExpandCapability())
            return supportsExpandCapability(0xac);
        return false;
    }
 
    public boolean supportsMusic() {
        return supportsCommandForService(0x25, 0x02);
    }
 
    public boolean supportsAutoWorkMode() {
        return supportsCommandForService(0x26, 0x02);
    }
 
    public boolean supportsMenstrual() {
        return supportsCommandForService(0x32, 0x01);
    }
 
    public boolean supportsP2PService() {
        return supportsCommandForService(0x34, 0x1);
    }
 
    public boolean supportsExternalCalendarService() {
        if (supportsExpandCapability())
            return supportsExpandCapability(184);
        return false;
    }
 
    public boolean supportsTrack() {
        if (supportsExpandCapability())
            return supportsExpandCapability(0x36);
        return false;
    }
 
    public boolean supportsCalendar() {
        if (supportsExpandCapability())
            return supportsExpandCapability(171) || supportsExpandCapability(184);
        return false;
    }
 
    public boolean supportsMultiDevice() {
        if (supportsExpandCapability())
            return supportsExpandCapability(109);
        return false;
    }
 
    public boolean supportsUnknownGender() {
        if (supportsExpandCapability())
            return supportsExpandCapability(0x57);
        return false;
    }
 
    public boolean supportsPrecisionWeight() {
        if (supportsExpandCapability())
            return supportsExpandCapability(0xb3);
        return false;
    }
 
    public boolean supportsMoreMusic() {
        if (supportsExpandCapability())
            return supportsExpandCapability(122);
        return false;
    }
 
 
    public boolean supportsPromptPushMessage () {
//              do not ask for capabilities under specific condition
//                  if (deviceType == 10 && deviceVersion == 73617766697368 && deviceSoftVersion == 372E312E31) -> leo device
//                  if V1V0Device
//                  if (serviceId = 0x01 && commandId = 0x03) && productType == 3
        return (((notificationCapabilities >> 1) & 1) == 0);
    }
 
    public boolean supportsOutgoingCall () {
        return (((notificationCapabilities >> 2) & 1) == 0);
    }
 
    public boolean supportsYellowPages() {
        return supportsNotificationConstraint(NotificationConstraintsType.yellowPagesSupport);
    }
 
    public boolean supportsContentSIgn() {
        return supportsNotificationConstraint(NotificationConstraintsType.contentSignSupport);
    }
 
    public boolean supportsIncomingNumber() {
        return supportsNotificationConstraint(NotificationConstraintsType.incomingNumberSupport);
    }
 
    public int getContentFormat() {
        return getNotificationConstraint(NotificationConstraintsType.contentFormat);
    }
 
    public int getYellowPagesFormat() {
        return getNotificationConstraint(NotificationConstraintsType.yellowPagesFormat);
    }
 
    public int getContentSignFormat() {
        return getNotificationConstraint(NotificationConstraintsType.contentSignFormat);
    }
 
    public int getIncomingFormatFormat() {
        return getNotificationConstraint(NotificationConstraintsType.incomingNumberFormat);
    }
 
    public int getContentLength() {
        return getNotificationConstraint(NotificationConstraintsType.contentLength);
    }
 
    public int getYellowPagesLength() {
        return getNotificationConstraint(NotificationConstraintsType.yellowPagesLength);
    }
 
    public int getContentSignLength() {
        return getNotificationConstraint(NotificationConstraintsType.contentSignLength);
    }
 
    public int getIncomingNumberLength() {
        return getNotificationConstraint(NotificationConstraintsType.incomingNumberLength);
    }
 
    public int getAlarmSlotCount(GBDevice gbDevice) {
        int alarmCount = 0;
        if (supportsEventAlarm())
            alarmCount += 5; // Always five event alarms
        if (supportsSmartAlarm(gbDevice))
            alarmCount += 1; // Always a single smart alarm
        return alarmCount;
    }
 
    public int getContactsSlotCount(GBDevice device) {
        return supportsContacts()?maxContactsCount:0;
    }
 
    public void setTransactionCrypted(boolean crypted) {
        this.transactionCrypted = crypted;
    }
 
    public boolean isTransactionCrypted() {
        return this.transactionCrypted;
    }
 
    public String[] getSupportedLanguageSettings(GBDevice device) {
        return new String[]{
                "auto",
                "ar_SA",
                "cs_CZ",
                "da_DK",
                "de_DE",
                "el_GR",
                "en_GB",
                "en_US",
                "es_ES",
                "fr_FR",
                "he_IL",
                "it_IT",
                "id_ID",
                "ko_KO",
                "nl_NL",
                "pl_PL",
                "pt_PT",
                "pt_BR",
                "ro_RO",
                "ru_RU",
                "sv_SE",
                "th_TH",
                "ja_JP",
                "tr_TR",
                "uk_UA",
                "zh_CN",
                "zh_TW",
        };
 
    }
 
    public short getWidth() {
        return (short)watchfaceDeviceParams.width;
    }
 
    public short getHeight() {
        return (short)watchfaceDeviceParams.height;
    }
 
    public void setWatchfaceDeviceParams(Watchface.WatchfaceDeviceParams watchfaceDeviceParams) {
        this.watchfaceDeviceParams = watchfaceDeviceParams;
    }
 
    public void setAppDeviceParams(App.AppDeviceParams appDeviceParams) {
        this.appDeviceParams = appDeviceParams;
    }
 
    public App.AppDeviceParams getAppDeviceParams() {
        return appDeviceParams;
    }
 
    public void setExtendedMusicInfoParams(HuaweiMusicUtils.MusicCapabilities musicDeviceParams) {
        LOG.info(musicDeviceParams.toString());
        this.musicExtendedDeviceParams = musicDeviceParams;
    }
    public HuaweiMusicUtils.MusicCapabilities getExtendedMusicInfoParams() {
        return musicExtendedDeviceParams;
    }
 
    public void setMusicInfoParams(HuaweiMusicUtils.MusicCapabilities musicDeviceParams) {
        LOG.info(musicDeviceParams.toString());
        this.musicDeviceParams = musicDeviceParams;
    }
    public HuaweiMusicUtils.MusicCapabilities getMusicInfoParams() {
        return musicDeviceParams;
    }
 
 
    public Class<? extends Activity> getAppManagerActivity() {
        return AppManagerActivity.class;
    }
 
    public boolean getSupportsAppListFetching() { return true; }
 
    public boolean getSupportsAppsManagement(GBDevice device) {
        return true;
    }
 
    public boolean getSupportsInstalledAppManagement(GBDevice device) {
        return this.supportsAppParams(); // NOTE: this check can be incorrect. But looks like it works
    }
 
    public boolean getSupportsCachedAppManagement(GBDevice device) {
        return false;
    }
 
    public InstallHandler getInstallHandler(Uri uri, Context context) {
        HuaweiInstallHandler handler = new HuaweiInstallHandler(uri, context);
        return handler.isValid() ? handler : null;
    }
 
    public boolean getSupportsTruSleepNewSync() {
        return supportsTruSleepNewSync;
    }
 
    public void setSupportsTruSleepNewSync(boolean supportsTruSleepNewSync) {
        this.supportsTruSleepNewSync = supportsTruSleepNewSync;
    }
 
    public boolean isSupportsGpsNewSync() {
        return supportsGpsNewSync;
    }
 
    public void setSupportsGpsNewSync(boolean supportsGpsNewSync) {
        this.supportsGpsNewSync = supportsGpsNewSync;
    }
}