Further improvements:

- append a string on the control center when the device is charging
- battery status string is no more, welcome battery state enum
- the notification will not be shown when the device is charging, even if the level is below threshold
This commit is contained in:
Daniele Gobbetti 2015-08-21 08:41:57 +02:00
parent a6b28a804c
commit eb39ce9367
7 changed files with 44 additions and 33 deletions

View File

@ -11,6 +11,7 @@ import android.widget.TextView;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.R;
@ -59,6 +60,11 @@ public class GBDeviceAdapter extends ArrayAdapter<GBDevice> {
short batteryLevel = device.getBatteryLevel();
if (batteryLevel != GBDevice.BATTERY_UNKNOWN) {
batteryStatusLabel.setText("BAT: " + device.getBatteryLevel() + "%");
GBDeviceEventBatteryInfo.BatteryState batteryState = device.getBatteryState();
if (GBDeviceEventBatteryInfo.BatteryState.BATTERY_CHARGING.equals(batteryState) ||
GBDeviceEventBatteryInfo.BatteryState.BATTERY_CHARGING_FULL.equals(batteryState)) {
batteryStatusLabel.append(" CHG");
}
} else {
batteryStatusLabel.setText("");
}

View File

@ -6,8 +6,6 @@ import java.util.GregorianCalendar;
public class GBDeviceEventBatteryInfo extends GBDeviceEvent {
public GregorianCalendar lastChargeTime= null;
public BatteryState state = BatteryState.UNKNOWN;
//TODO: I think the string should be deprecated in favor of the Enum above
public String status;
public short level = 50;
public int numCharges = -1;
@ -17,10 +15,11 @@ public class GBDeviceEventBatteryInfo extends GBDeviceEvent {
public enum BatteryState {
UNKNOWN,
CHARGE_FULL,
CHARGE_MEDIUM,
CHARGE_LOW,
CHARGING,
BATTERY_NORMAL,
BATTERY_LOW,
BATTERY_CHARGING,
BATTERY_CHARGING_FULL,
BATTERY_NOT_CHARGING_FULL
}
public boolean extendedInfoAvailable() {

View File

@ -9,6 +9,7 @@ import android.support.v4.content.LocalBroadcastManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo.BatteryState;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
@ -42,6 +43,7 @@ public class GBDevice implements Parcelable {
private short mBatteryThresholdPercent = BATTERY_THRESHOLD_PERCENT;
//TODO: get rid of String mBatteryStatus in favor of Enum mBatteryState
private String mBatteryStatus;
private BatteryState mBatteryState;
private short mRssi = RSSI_UNKNOWN;
private String mBusyTask;
@ -180,7 +182,7 @@ public class GBDevice implements Parcelable {
private void unsetDynamicState() {
setBatteryLevel(BATTERY_UNKNOWN);
setBatteryStatus(null);
setBatteryState(BatteryState.UNKNOWN);
setFirmwareVersion(null);
setRssi(RSSI_UNKNOWN);
if (mBusyTask != null) {
@ -284,18 +286,14 @@ public class GBDevice implements Parcelable {
}
}
/**
* Returns a string representation of the battery state.
*/
public String getBatteryStatus() {
return mBatteryStatus != null ? mBatteryStatus : GBApplication.getContext().getString(R.string._unknown_);
public BatteryState getBatteryState() {
return mBatteryState;
}
public void setBatteryStatus(String batteryStatus) {
mBatteryStatus = batteryStatus;
public void setBatteryState(BatteryState mBatteryState) {
this.mBatteryState = mBatteryState;
}
public short getBatteryThresholdPercent() {
return mBatteryThresholdPercent;
}

View File

@ -228,9 +228,13 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
Context context = getContext();
LOG.info("Got BATTERY_INFO device event");
gbDevice.setBatteryLevel(deviceEvent.level);
gbDevice.setBatteryStatus(deviceEvent.status);
gbDevice.setBatteryState(deviceEvent.state);
if (deviceEvent.level <= gbDevice.getBatteryThresholdPercent()) {
//show the notification if the battery level is below threshold and only if not connected to charger
if (deviceEvent.level <= gbDevice.getBatteryThresholdPercent() &&
(GBDeviceEventBatteryInfo.BatteryState.BATTERY_LOW.equals(deviceEvent.state) ||
GBDeviceEventBatteryInfo.BatteryState.BATTERY_NORMAL.equals(deviceEvent.state))
) {
GB.updateBatteryNotification(context.getString(R.string.notif_battery_low_percent, gbDevice.getName(), deviceEvent.level),
deviceEvent.extendedInfoAvailable() ?
context.getString(R.string.notif_battery_low_percent, gbDevice.getName(), deviceEvent.level) + "\n" +

View File

@ -3,10 +3,15 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.miband;
import java.util.Calendar;
import java.util.GregorianCalendar;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo.BatteryState;
public class BatteryInfo extends AbstractInfo {
public static final byte DEVICE_BATTERY_NORMAL = 0;
public static final byte DEVICE_BATTERY_LOW = 1;
public static final byte DEVICE_BATTERY_CHARGING = 2;
public static final byte DEVICE_BATTERY_CHARGING_FULL = 3;
public static final byte DEVICE_BATTERY_CHARGE_OFF = 4;
public BatteryInfo(byte[] data) {
super(data);
}
@ -18,21 +23,23 @@ public class BatteryInfo extends AbstractInfo {
return 50; // actually unknown
}
public String getStatus() {
public BatteryState getState() {
if (mData.length >= 10) {
int value = mData[9];
switch (value) {
case 1:
return GBApplication.getContext().getString(R.string.battery_low);
case 2:
return GBApplication.getContext().getString(R.string.battery_medium);
case 3:
return GBApplication.getContext().getString(R.string.battery_full);
case 4:
return GBApplication.getContext().getString(R.string.battery_not_charging);
case DEVICE_BATTERY_NORMAL:
return BatteryState.BATTERY_NORMAL;
case DEVICE_BATTERY_LOW:
return BatteryState.BATTERY_LOW;
case DEVICE_BATTERY_CHARGING:
return BatteryState.BATTERY_CHARGING;
case DEVICE_BATTERY_CHARGING_FULL:
return BatteryState.BATTERY_CHARGING_FULL;
case DEVICE_BATTERY_CHARGE_OFF:
return BatteryState.BATTERY_NOT_CHARGING_FULL;
}
}
return GBApplication.getContext().getString(R.string._unknown_);
return BatteryState.UNKNOWN;
}
public GregorianCalendar getLastChargeTime() {

View File

@ -737,6 +737,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
if (status == BluetoothGatt.GATT_SUCCESS) {
BatteryInfo info = new BatteryInfo(value);
batteryCmd.level = ((short) info.getLevelInPercent());
batteryCmd.state = info.getState();
batteryCmd.lastChargeTime = info.getLastChargeTime();
batteryCmd.numCharges = info.getNumCharges();
handleGBDeviceEvent(batteryCmd);

View File

@ -74,10 +74,6 @@
<string name="tap_a_device_to_connect">tap a device to connect</string>
<string name="cannot_connect_bt_address_invalid_">Cannot connect. BT address invalid?</string>
<string name="gadgetbridge_running">Gadgetbridge running</string>
<string name="battery_low">low</string>
<string name="battery_medium">medium</string>
<string name="battery_full">full</string>
<string name="battery_not_charging">not charging</string>
<string name="installing_binary_d_d">installing binary %1$d/%2$d</string>
<string name="installation_failed_">installation failed!</string>
<string name="installation_successful">installation successful</string>