1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 04:46:51 +01:00

Huawei: Implement battery polling

This commit is contained in:
Martin.JM 2024-07-03 15:20:03 +02:00 committed by José Rebelo
parent bf16bf06c3
commit 125e6371c3
5 changed files with 62 additions and 1 deletions

View File

@ -238,4 +238,9 @@ public abstract class HuaweiBRCoordinator extends AbstractBLClassicDeviceCoordin
public Class<? extends DeviceSupport> getDeviceSupportClass() {
return HuaweiBRSupport.class;
}
@Override
public boolean addBatteryPollingSettings() {
return true;
}
}

View File

@ -241,4 +241,9 @@ public abstract class HuaweiLECoordinator extends AbstractBLEDeviceCoordinator i
public Class<? extends DeviceSupport> getDeviceSupportClass() {
return HuaweiLESupport.class;
}
@Override
public boolean addBatteryPollingSettings() {
return true;
}
}

View File

@ -22,6 +22,7 @@ import android.content.SharedPreferences;
import android.location.Location;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;
import androidx.annotation.NonNull;
@ -173,6 +174,12 @@ public class HuaweiSupportProvider {
private Context context;
private HuaweiCoordinatorSupplier.HuaweiDeviceType huaweiType;
private final Handler handler = new Handler(Looper.getMainLooper());
private final Runnable batteryRunner = () -> {
LOG.info("Running retrieving battery through runner.");
getBatteryLevel();
};
private boolean firstConnection = false;
protected byte protocolVersion;
public String deviceMac; //get it from GB
@ -1012,6 +1019,18 @@ public class HuaweiSupportProvider {
// But it will disappear after reconnection - until it is enabled again
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)) {
stopBatteryRunnerDelayed();
break;
}
// Fall through if enabled
case DeviceSettingsPreferenceConst.PREF_BATTERY_POLLING_INTERVAL:
if (!startBatteryRunnerDelayed()) {
GB.toast(context, R.string.battery_polling_failed_start, Toast.LENGTH_SHORT, GB.ERROR);
LOG.error("Failed to start the battery polling");
}
break;
case ActivityUser.PREF_USER_WEIGHT_KG:
case ActivityUser.PREF_USER_HEIGHT_CM:
case ActivityUser.PREF_USER_GENDER:
@ -1050,7 +1069,7 @@ public class HuaweiSupportProvider {
LOG.warn("Recorded data type {} not implemented yet.", dataTypes);
}
// Get the battery level, as that isn't shared nicely for now
// Get the battery level as well
getBatteryLevel();
// Get the alarms as they cannot be retrieved on opening the alarm window
@ -1621,6 +1640,7 @@ public class HuaweiSupportProvider {
public void getBatteryLevel() {
try {
stopBatteryRunnerDelayed();
GetBatteryLevelRequest batteryLevelReq = new GetBatteryLevelRequest(this);
batteryLevelReq.doPerform();
} catch (IOException e) {
@ -2022,4 +2042,21 @@ public class HuaweiSupportProvider {
}
}
}
public boolean startBatteryRunnerDelayed() {
String interval_minutes = GBApplication.getDeviceSpecificSharedPrefs(deviceMac).getString(DeviceSettingsPreferenceConst.PREF_BATTERY_POLLING_INTERVAL, "15");
int interval = Integer.parseInt(interval_minutes) * 60 * 1000;
LOG.debug("Starting battery runner delayed by {} ({} minutes)", interval, interval_minutes);
handler.removeCallbacks(batteryRunner);
return handler.postDelayed(batteryRunner, interval);
}
public void stopBatteryRunnerDelayed() {
LOG.debug("Stopping battery runner delayed");
handler.removeCallbacks(batteryRunner);
}
public void dispose() {
stopBatteryRunnerDelayed();
}
}

View File

@ -16,15 +16,21 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
import android.widget.Toast;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.DeviceConfig;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class GetBatteryLevelRequest extends Request {
private static final Logger LOG = LoggerFactory.getLogger(GetBatteryLevelRequest.class);
@ -57,5 +63,12 @@ public class GetBatteryLevelRequest extends Request {
GBDeviceEventBatteryInfo batteryInfo = new GBDeviceEventBatteryInfo();
batteryInfo.level = (int)batteryLevel & 0xff;
this.supportProvider.evaluateGBDeviceEvent(batteryInfo);
if (GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress()).getBoolean(DeviceSettingsPreferenceConst.PREF_BATTERY_POLLING_ENABLE, false)) {
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

@ -2924,6 +2924,7 @@
<string name="pref_battery_polling_enable">Enable battery polling</string>
<string name="pref_battery_polling_interval">Battery polling interval</string>
<string name="pref_battery_polling_interval_format">every %1$s minutes</string>
<string name="battery_polling_failed_start">Failed to start the battery polling</string>
<string name="none">None</string>
<string name="garmin_agps_url_i">AGPS %1$d URL</string>