1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-24 19:06:53 +01:00

Xiaomi: remove battery-related request timer on disconnect

This commit is contained in:
MrYoranimo 2024-02-29 13:39:38 +01:00 committed by José Rebelo
parent cb44982edb
commit ebf0dbc2d0
5 changed files with 45 additions and 13 deletions

View File

@ -223,6 +223,12 @@ public class XiaomiBleSupport extends XiaomiConnectionSupport {
if (characteristicActivityData != null)
characteristicActivityData.setMtu(mtu);
}
@Override
public void disconnect() {
mXiaomiSupport.onDisconnect();
super.disconnect();
}
};
public XiaomiBleSupport(final XiaomiSupport xiaomiSupport) {

View File

@ -94,6 +94,18 @@ public class XiaomiSppSupport extends XiaomiConnectionSupport {
protected UUID getSupportedService() {
return XiaomiUuids.UUID_SERVICE_SERIAL_PORT_PROFILE;
}
@Override
public void disconnect() {
mXiaomiSupport.onDisconnect();
super.disconnect();
}
@Override
public void dispose() {
mXiaomiSupport.onDisconnect();
super.dispose();
}
};
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

View File

@ -203,6 +203,13 @@ public class XiaomiSupport extends AbstractDeviceSupport {
this.cachedFirmwareVersion = version;
}
public void onDisconnect() {
// propagate disconnection to services
for (AbstractXiaomiService service : mServiceMap.values()) {
service.onDisconnect();
}
}
public void handleCommandBytes(final byte[] plainValue) {
LOG.debug("Got command: {}", GB.hexdump(plainValue));

View File

@ -62,4 +62,6 @@ public abstract class AbstractXiaomiService {
protected Prefs getDevicePrefs() {
return new Prefs(GBApplication.getDeviceSpecificSharedPrefs(getSupport().getDevice().getAddress()));
}
public void onDisconnect() {}
}

View File

@ -18,6 +18,7 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.xiaomi.services;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import com.google.protobuf.InvalidProtocolBufferException;
@ -102,14 +103,10 @@ public class XiaomiSystemService extends AbstractXiaomiService implements Xiaomi
// Not null if we're installing a firmware
private XiaomiFWHelper fwHelper = null;
private Handler handler = new Handler();
private final Runnable batteryStateRequestRunnable = new Runnable() {
@Override
public void run() {
private Handler handler = new Handler(Looper.getMainLooper());
private final Runnable batteryStateRequestRunnable = () -> {
getSupport().sendCommand("get device status", COMMAND_TYPE, CMD_DEVICE_STATE_GET);
getSupport().sendCommand("get battery state", COMMAND_TYPE, CMD_BATTERY);
handler.postDelayed(this, BATTERY_STATE_REQUEST_INTERVAL);
}
};
private WearingState currentWearingState = WearingState.UNKNOWN;
@ -135,7 +132,7 @@ public class XiaomiSystemService extends AbstractXiaomiService implements Xiaomi
getSupport().sendCommand("get widget parts", COMMAND_TYPE, CMD_WIDGET_PARTS_GET);
getSupport().sendCommand("get workout types", COMMAND_TYPE, CMD_WORKOUT_TYPES_GET);
handler.postDelayed(batteryStateRequestRunnable, BATTERY_STATE_REQUEST_INTERVAL);
rearmBatteryStateRequestTimer();
}
@Override
@ -368,8 +365,7 @@ public class XiaomiSystemService extends AbstractXiaomiService implements Xiaomi
getSupport().evaluateGBDeviceEvent(batteryInfo);
// reset battery level request timer
handler.removeCallbacks(batteryStateRequestRunnable);
handler.postDelayed(batteryStateRequestRunnable, BATTERY_STATE_REQUEST_INTERVAL);
rearmBatteryStateRequestTimer();
}
private void setPassword() {
@ -800,8 +796,7 @@ public class XiaomiSystemService extends AbstractXiaomiService implements Xiaomi
// TODO: handle activity state
// reset battery level refresh timer
handler.removeCallbacks(batteryStateRequestRunnable);
handler.postDelayed(batteryStateRequestRunnable, BATTERY_STATE_REQUEST_INTERVAL);
rearmBatteryStateRequestTimer();
}
public void handleDeviceState(XiaomiProto.DeviceState deviceState) {
@ -983,4 +978,14 @@ public class XiaomiSystemService extends AbstractXiaomiService implements Xiaomi
public void onUploadProgress(final int stringResource, final int progressPercent, final boolean ongoing) {
getSupport().getConnectionSpecificSupport().onUploadProgress(stringResource, progressPercent, ongoing);
}
private void rearmBatteryStateRequestTimer() {
this.handler.removeCallbacks(this.batteryStateRequestRunnable);
this.handler.postDelayed(this.batteryStateRequestRunnable, BATTERY_STATE_REQUEST_INTERVAL);
}
@Override
public void onDisconnect() {
this.handler.removeCallbacks(this.batteryStateRequestRunnable);
}
}