1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-24 09:35:50 +01:00

Huawei: Split off SyncState

This commit is contained in:
Martin.JM 2024-11-24 18:00:40 +01:00 committed by José Rebelo
parent 9970a37df3
commit 8746577c4e
2 changed files with 108 additions and 91 deletions

View File

@ -196,96 +196,7 @@ public class HuaweiSupportProvider {
// TODO: Potentially use translatable messages for the toast messages
private class SyncState {
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 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);
HuaweiSupportProvider.this.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);
HuaweiSupportProvider.this.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);
HuaweiSupportProvider.this.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);
HuaweiSupportProvider.this.fetchRecodedDataFromQueue();
}
updateState();
}
public void updateState() {
updateState(true);
}
public void updateState(boolean needSync) {
if (!activitySync && !p2pSync && !workoutSync && workoutGpsDownload == 0) {
if (getDevice().isBusy()) {
getDevice().unsetBusyTask();
getDevice().sendDeviceUpdateIntent(context);
}
if (needSync)
GB.signalActivityDataFinish(getDevice());
}
}
}
private final SyncState syncState = new SyncState();
private final HuaweiSyncState syncState = new HuaweiSyncState(this);
private final int initTimeout = 2000;
@ -1240,7 +1151,7 @@ public class HuaweiSupportProvider {
getAlarms();
}
private void fetchRecodedDataFromQueue() {
protected void fetchRecodedDataFromQueue() {
int dataType = this.syncState.getCurrentSyncType();
if (dataType == -1)
return; // Empty queue

View File

@ -0,0 +1,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());
}
}
}