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
package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.ArrayList;
import java.util.List;
 
import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
 
class HuaweiSyncState {
    private static final Logger LOG = LoggerFactory.getLogger(HuaweiSyncState.class);
 
    private final HuaweiSupportProvider supportProvider;
    private final List<Integer> syncQueue = new ArrayList<>(2);
 
    private boolean activitySync = false;
    private boolean p2pSync = false;
    private boolean workoutSync = false;
    private int workoutGpsDownload = 0;
 
    public HuaweiSyncState(HuaweiSupportProvider supportProvider) {
        this.supportProvider = supportProvider;
    }
 
    public void addActivitySyncToQueue() {
        if (syncQueue.contains(RecordedDataTypes.TYPE_ACTIVITY))
            LOG.info("Activity type sync already queued, ignoring");
        else
            syncQueue.add(RecordedDataTypes.TYPE_ACTIVITY);
    }
 
    public void addWorkoutSyncToQueue() {
        if (syncQueue.contains(RecordedDataTypes.TYPE_GPS_TRACKS))
            LOG.info("Workout type sync already queued, ignoring");
        else
            syncQueue.add(RecordedDataTypes.TYPE_GPS_TRACKS);
    }
 
    public int getCurrentSyncType() {
        if (syncQueue.isEmpty())
            return -1;
        return syncQueue.get(0);
    }
 
    public void setActivitySync(boolean state) {
        LOG.debug("Set activity sync state to {}", state);
        this.activitySync = state;
        if (!state && !this.p2pSync) {
            this.syncQueue.remove((Integer) RecordedDataTypes.TYPE_ACTIVITY);
            supportProvider.fetchRecodedDataFromQueue();
        }
        updateState();
    }
 
    public void setP2pSync(boolean state) {
        LOG.debug("Set p2p sync state to {}", state);
        this.p2pSync = state;
        if (!state && !this.activitySync) {
            this.syncQueue.remove((Integer) RecordedDataTypes.TYPE_ACTIVITY);
            supportProvider.fetchRecodedDataFromQueue();
        }
        updateState();
    }
 
    public void setWorkoutSync(boolean state) {
        LOG.debug("Set workout sync state to {}", state);
        this.workoutSync = state;
        if (!state && this.workoutGpsDownload == 0) {
            this.syncQueue.remove((Integer) RecordedDataTypes.TYPE_GPS_TRACKS);
            supportProvider.fetchRecodedDataFromQueue();
        }
        updateState();
    }
 
    public void startWorkoutGpsDownload() {
        this.workoutGpsDownload += 1;
        LOG.debug("Add GPS download: {}", this.workoutGpsDownload);
    }
 
    public void stopWorkoutGpsDownload() {
        this.workoutGpsDownload -= 1;
        LOG.debug("Subtract GPS download: {}", this.workoutGpsDownload);
        if (this.workoutGpsDownload == 0 && !this.workoutSync) {
            this.syncQueue.remove((Integer) RecordedDataTypes.TYPE_GPS_TRACKS);
            supportProvider.fetchRecodedDataFromQueue();
        }
        updateState();
    }
 
    public void updateState() {
        updateState(true);
    }
 
    public void updateState(boolean needSync) {
        if (!activitySync && !p2pSync && !workoutSync && workoutGpsDownload == 0) {
            if (supportProvider.getDevice().isBusy()) {
                supportProvider.getDevice().unsetBusyTask();
                supportProvider.getDevice().sendDeviceUpdateIntent(supportProvider.getContext());
            }
            if (needSync)
                GB.signalActivityDataFinish(supportProvider.getDevice());
        }
    }
}