1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-28 19:45:50 +01:00

Xiaomi: Add battery polling settings

This commit is contained in:
José Rebelo 2024-07-13 15:23:38 +01:00
parent d7490639af
commit 124b1953aa
7 changed files with 26 additions and 10 deletions

View File

@ -291,7 +291,7 @@ public class DeviceSpecificSettingsFragment extends AbstractPreferenceFragment i
pollingToggle.setLayoutResource(R.layout.preference_checkbox);
pollingToggle.setKey(PREF_BATTERY_POLLING_ENABLE);
pollingToggle.setTitle(R.string.pref_battery_polling_enable);
pollingToggle.setDefaultValue(false);
pollingToggle.setDefaultValue(true);
pollingToggle.setIconSpaceReserved(false);
batteryScreen.addPreference(pollingToggle);

View File

@ -358,6 +358,11 @@ public abstract class XiaomiCoordinator extends AbstractBLEDeviceCoordinator {
return true;
}
@Override
public boolean addBatteryPollingSettings() {
return true;
}
@Override
public boolean supportsUnicodeEmojis() {
return true;

View File

@ -1020,7 +1020,7 @@ public class HuaweiSupportProvider {
GB.toast(context, context.getString(R.string.toast_setting_requires_reconnect), Toast.LENGTH_SHORT, GB.INFO);
}
case DeviceSettingsPreferenceConst.PREF_BATTERY_POLLING_ENABLE:
if (!GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()).getBoolean(DeviceSettingsPreferenceConst.PREF_BATTERY_POLLING_ENABLE, false)) {
if (!GBApplication.getDevicePrefs(gbDevice.getAddress()).getBatteryPollingEnabled()) {
stopBatteryRunnerDelayed();
break;
}

View File

@ -64,7 +64,7 @@ public class GetBatteryLevelRequest extends Request {
batteryInfo.level = (int)batteryLevel & 0xff;
this.supportProvider.evaluateGBDeviceEvent(batteryInfo);
if (GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress()).getBoolean(DeviceSettingsPreferenceConst.PREF_BATTERY_POLLING_ENABLE, false)) {
if (GBApplication.getDevicePrefs(getDevice().getAddress()).getBatteryPollingEnabled()) {
if (!this.supportProvider.startBatteryRunnerDelayed()) {
GB.toast(getContext(), R.string.battery_polling_failed_start, Toast.LENGTH_SHORT, GB.ERROR);
LOG.error("Failed to start the battery polling");

View File

@ -23,6 +23,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.xiaomi.XiaomiCoordinator;
import nodomain.freeyourgadget.gadgetbridge.proto.xiaomi.XiaomiProto;
import nodomain.freeyourgadget.gadgetbridge.service.devices.xiaomi.XiaomiSupport;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import nodomain.freeyourgadget.gadgetbridge.util.preferences.DevicePrefs;
public abstract class AbstractXiaomiService {
private final XiaomiSupport mSupport;
@ -59,8 +60,8 @@ public abstract class AbstractXiaomiService {
return (XiaomiCoordinator) getSupport().getDevice().getDeviceCoordinator();
}
protected Prefs getDevicePrefs() {
return new Prefs(GBApplication.getDeviceSpecificSharedPrefs(getSupport().getDevice().getAddress()));
protected DevicePrefs getDevicePrefs() {
return GBApplication.getDevicePrefs(getSupport().getDevice().getAddress());
}
public void onDisconnect() {}

View File

@ -35,7 +35,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
@ -64,6 +63,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import nodomain.freeyourgadget.gadgetbridge.util.SilentMode;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
import nodomain.freeyourgadget.gadgetbridge.util.preferences.DevicePrefs;
public class XiaomiSystemService extends AbstractXiaomiService implements XiaomiDataUploadService.Callback {
private static final Logger LOG = LoggerFactory.getLogger(XiaomiSystemService.class);
@ -71,7 +71,6 @@ public class XiaomiSystemService extends AbstractXiaomiService implements Xiaomi
// We persist the settings code when receiving the display items,
// so we can enforce it when sending them
private static final String PREF_SETTINGS_DISPLAY_ITEM_CODE = "xiaomi_settings_display_item_code";
private static final int BATTERY_STATE_REQUEST_INTERVAL = (int) TimeUnit.MINUTES.toMillis(15);
public static final int COMMAND_TYPE = 2;
@ -103,7 +102,7 @@ public class XiaomiSystemService extends AbstractXiaomiService implements Xiaomi
// Not null if we're installing a firmware
private XiaomiFWHelper fwHelper = null;
private Handler handler = new Handler(Looper.getMainLooper());
private final 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);
@ -222,6 +221,10 @@ public class XiaomiSystemService extends AbstractXiaomiService implements Xiaomi
@Override
public boolean onSendConfiguration(final String config, final Prefs prefs) {
switch (config) {
case DeviceSettingsPreferenceConst.PREF_BATTERY_POLLING_ENABLE:
case DeviceSettingsPreferenceConst.PREF_BATTERY_POLLING_INTERVAL:
rearmBatteryStateRequestTimer();
break;
case DeviceSettingsPreferenceConst.PREF_WEARMODE:
setWearMode();
return true;
@ -284,7 +287,7 @@ public class XiaomiSystemService extends AbstractXiaomiService implements Xiaomi
final Calendar now = GregorianCalendar.getInstance();
final TimeZone tz = TimeZone.getDefault();
final GBPrefs gbPrefs = new GBPrefs(new Prefs(GBApplication.getDeviceSpecificSharedPrefs(getSupport().getDevice().getAddress())));
final GBPrefs gbPrefs = new GBPrefs(GBApplication.getDeviceSpecificSharedPrefs(getSupport().getDevice().getAddress()));
final String timeFormat = gbPrefs.getTimeFormat();
final boolean is24hour = DeviceSettingsPreferenceConst.PREF_TIMEFORMAT_24H.equals(timeFormat);
@ -981,7 +984,10 @@ public class XiaomiSystemService extends AbstractXiaomiService implements Xiaomi
private void rearmBatteryStateRequestTimer() {
this.handler.removeCallbacks(this.batteryStateRequestRunnable);
this.handler.postDelayed(this.batteryStateRequestRunnable, BATTERY_STATE_REQUEST_INTERVAL);
final DevicePrefs devicePrefs = getDevicePrefs();
if (devicePrefs.getBatteryPollingEnabled()) {
this.handler.postDelayed(this.batteryStateRequestRunnable, devicePrefs.getBatteryPollingIntervalMinutes() * 60 * 1000L);
}
}
@Override

View File

@ -32,6 +32,10 @@ public class DevicePrefs extends Prefs {
return getInt(PREF_BATTERY_NOTIFY_FULL_THRESHOLD + batteryConfig.getBatteryIndex(), batteryConfig.getDefaultFullThreshold());
}
public boolean getBatteryPollingEnabled() {
return getBoolean(PREF_BATTERY_POLLING_ENABLE, true);
}
public int getBatteryPollingIntervalMinutes() {
return getInt(PREF_BATTERY_POLLING_INTERVAL, 15);
}