1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-01 14:32:54 +01:00

Add batteryConfig

- clarify battery index
- icon and label settings
This commit is contained in:
vanous 2021-10-31 17:36:04 +01:00
parent 1ce0cd683e
commit 3609af3a18
13 changed files with 143 additions and 15 deletions

View File

@ -44,7 +44,7 @@ BatteryInfoActivity extends AbstractGBActivity {
Bundle bundle = intent.getExtras();
if (bundle != null) {
gbDevice = bundle.getParcelable(GBDevice.EXTRA_DEVICE);
batteryIndex = bundle.getInt("BATTERY_INDEX", 0);
batteryIndex = bundle.getInt(GBDevice.BATTERY_INDEX, 0);
} else {
throw new IllegalArgumentException("Must provide a device when invoking this activity");
}

View File

@ -155,34 +155,40 @@ public class GBDeviceAdapterv2 extends RecyclerView.Adapter<GBDeviceAdapterv2.Vi
TextView[] batteryStatusLabels = {holder.batteryStatusLabel0, holder.batteryStatusLabel1, holder.batteryStatusLabel2};
ImageView[] batteryIcons = {holder.batteryIcon0, holder.batteryIcon1, holder.batteryIcon2};
for (int battery = 0; battery < coordinator.getBatteryCount(); battery++) {
for (int batteryIndex = 0; batteryIndex < coordinator.getBatteryCount(); batteryIndex++) {
int batteryLevel = device.getBatteryLevel(battery);
float batteryVoltage = device.getBatteryVoltage(battery);
BatteryState batteryState = device.getBatteryState(battery);
int batteryLevel = device.getBatteryLevel(batteryIndex);
float batteryVoltage = device.getBatteryVoltage(batteryIndex);
BatteryState batteryState = device.getBatteryState(batteryIndex);
int batteryIcon = device.getBatteryIcon(batteryIndex);
int batteryLabel = device.getBatteryLabel(batteryIndex);
LOG.debug("battery icon: " + batteryIcon);
LOG.debug("battery index: " + batteryIndex);
if (batteryIcon != GBDevice.BATTERY_ICON_DEFAULT){
batteryIcons[batteryIndex].setImageResource(batteryIcon);
}
if (batteryLevel != GBDevice.BATTERY_UNKNOWN) {
batteryStatusBoxes[battery].setVisibility(View.VISIBLE);
batteryStatusLabels[battery].setText(device.getBatteryLevel(battery) + "%");
batteryStatusLabels[batteryIndex].setText(device.getBatteryLevel(batteryIndex) + "%");
if (BatteryState.BATTERY_CHARGING.equals(batteryState) ||
BatteryState.BATTERY_CHARGING_FULL.equals(batteryState)) {
batteryIcons[battery].setImageLevel(device.getBatteryLevel(battery) + 100);
batteryIcons[batteryIndex].setImageLevel(device.getBatteryLevel(batteryIndex) + 100);
} else {
batteryIcons[battery].setImageLevel(device.getBatteryLevel(battery));
batteryIcons[batteryIndex].setImageLevel(device.getBatteryLevel(batteryIndex));
}
} else if (BatteryState.NO_BATTERY.equals(batteryState) && batteryVoltage != GBDevice.BATTERY_UNKNOWN) {
batteryStatusBoxes[battery].setVisibility(View.VISIBLE);
batteryStatusLabels[battery].setText(String.format(Locale.getDefault(), "%.2f", batteryVoltage));
batteryIcons[battery].setImageLevel(200);
batteryStatusLabels[batteryIndex].setText(String.format(Locale.getDefault(), "%.2f", batteryVoltage));
batteryIcons[batteryIndex].setImageLevel(200);
}
final int finalBattery = battery;
batteryStatusBoxes[battery].setOnClickListener(new View.OnClickListener() {
final int finalBatteryIndex = batteryIndex;
batteryStatusBoxes[batteryIndex].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startIntent;
startIntent = new Intent(context, BatteryInfoActivity.class);
startIntent.putExtra(GBDevice.EXTRA_DEVICE, device);
startIntent.putExtra("BATTERY_INDEX", finalBattery);
startIntent.putExtra(GBDevice.BATTERY_INDEX, finalBatteryIndex);
context.startActivity(startIntent);
}
}

View File

@ -47,6 +47,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributesDao;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryConfig;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import static nodomain.freeyourgadget.gadgetbridge.GBApplication.getPrefs;
@ -251,4 +252,11 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
public int getBatteryCount() {
return 1;
} //multiple battery support, default is 1, maximum is 3, 0 will disable the battery in UI
@Override
public BatteryConfig[] getBatteryConfig() {
return new BatteryConfig[0];
}
}

View File

@ -36,6 +36,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryConfig;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
/**
@ -356,4 +357,6 @@ public interface DeviceCoordinator {
*/
int getBatteryCount();
BatteryConfig[] getBatteryConfig();
}

View File

@ -17,6 +17,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryConfig;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
public class GalaxyBudsDeviceCoordinator extends AbstractDeviceCoordinator {
@ -45,6 +46,13 @@ public class GalaxyBudsDeviceCoordinator extends AbstractDeviceCoordinator {
return 2;
}
@Override
public BatteryConfig[] getBatteryConfig() {
BatteryConfig battery1 = new BatteryConfig(0, R.drawable.ic_earbuds_battery, R.string.left);
BatteryConfig battery2 = new BatteryConfig(1, R.drawable.ic_earbuds_battery, R.string.right);
return new BatteryConfig[]{battery1, battery2};
}
@Nullable
@Override
public Class<? extends Activity> getPairingActivity() {
@ -133,6 +141,8 @@ public class GalaxyBudsDeviceCoordinator extends AbstractDeviceCoordinator {
}
@Override
public int[] getSupportedDeviceSpecificSettings(GBDevice device) {
return new int[]{

View File

@ -17,6 +17,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryConfig;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
public class Ear1Coordinator extends AbstractDeviceCoordinator {
@ -120,6 +121,19 @@ public class Ear1Coordinator extends AbstractDeviceCoordinator {
}
@Override
public int getBatteryCount() {
return 3;
}
@Override
public BatteryConfig[] getBatteryConfig() {
BatteryConfig battery1 = new BatteryConfig(0, R.drawable.ic_cases, R.string.box);
BatteryConfig battery2 = new BatteryConfig(1, R.drawable.ic_earbuds_battery, R.string.right);
BatteryConfig battery3 = new BatteryConfig(2, R.drawable.ic_earbuds_battery, R.string.left);
return new BatteryConfig[]{battery1, battery2, battery3};
}
@Override
public int[] getSupportedDeviceSpecificSettings(GBDevice device) {
return new int[] {

View File

@ -60,6 +60,8 @@ public class GBDevice implements Parcelable {
private static final Logger LOG = LoggerFactory.getLogger(GBDevice.class);
public static final short RSSI_UNKNOWN = 0;
public static final short BATTERY_UNKNOWN = -1;
public static final short BATTERY_ICON_DEFAULT = -1;
public static final short BATTERY_LABEL_DEFAULT = -1;
private static final short BATTERY_THRESHOLD_PERCENT = 10;
public static final String EXTRA_DEVICE = "device";
public static final String EXTRA_UUID = "extraUUID";
@ -68,6 +70,7 @@ public class GBDevice implements Parcelable {
private static final String DEVINFO_FW2_VER = "FW2: ";
private static final String DEVINFO_ADDR = "ADDR: ";
private static final String DEVINFO_ADDR2 = "ADDR2: ";
public static final String BATTERY_INDEX = "battery_index";
private String mName;
private String mAlias;
private final String mAddress;
@ -83,6 +86,8 @@ public class GBDevice implements Parcelable {
private float[] mBatteryVoltage = {BATTERY_UNKNOWN, BATTERY_UNKNOWN, BATTERY_UNKNOWN};
private short mBatteryThresholdPercent = BATTERY_THRESHOLD_PERCENT;
private BatteryState[] mBatteryState = {UNKNOWN, UNKNOWN, UNKNOWN};
private int[] mBatteryIcons = {BATTERY_ICON_DEFAULT, BATTERY_ICON_DEFAULT, BATTERY_ICON_DEFAULT};
private int[] mBatteryLabels = {BATTERY_LABEL_DEFAULT, BATTERY_LABEL_DEFAULT, BATTERY_LABEL_DEFAULT};
private short mRssi = RSSI_UNKNOWN;
private String mBusyTask;
@ -120,6 +125,8 @@ public class GBDevice implements Parcelable {
mBatteryVoltage = in.createFloatArray();
mBatteryThresholdPercent = (short) in.readInt();
mBatteryState = ordinalsToEnums(in.createIntArray());
mBatteryIcons = in.createIntArray();
mBatteryLabels = in.createIntArray();
mRssi = (short) in.readInt();
mBusyTask = in.readString();
mDeviceInfos = in.readArrayList(getClass().getClassLoader());
@ -146,6 +153,8 @@ public class GBDevice implements Parcelable {
dest.writeFloatArray(mBatteryVoltage);
dest.writeInt(mBatteryThresholdPercent);
dest.writeIntArray(enumsToOrdinals(mBatteryState));
dest.writeIntArray(mBatteryIcons);
dest.writeIntArray(mBatteryLabels);
dest.writeInt(mRssi);
dest.writeString(mBusyTask);
dest.writeList(mDeviceInfos);
@ -347,6 +356,7 @@ public class GBDevice implements Parcelable {
}
private void unsetDynamicState() {
setBatteryLevel(BATTERY_UNKNOWN, 0);
setBatteryLevel(BATTERY_UNKNOWN, 1);
setBatteryLevel(BATTERY_UNKNOWN, 2);
@ -569,6 +579,22 @@ public class GBDevice implements Parcelable {
this.mBatteryThresholdPercent = batteryThresholdPercent;
}
public int getBatteryIcon(int index) {
return this.mBatteryIcons[index];
}
public void setBatteryIcon(int icon, int index) {
this.mBatteryIcons[index] = icon;
}
public int getBatteryLabel(int index) {
return this.mBatteryLabels[index];
}
public void setBatteryLabel(int label, int index) {
this.mBatteryLabels[index] = label;
}
@Override
public String toString() {
return "Device " + getName() + ", " + getAddress() + ", " + getStateString(false);

View File

@ -0,0 +1,29 @@
package nodomain.freeyourgadget.gadgetbridge.model;
public class BatteryConfig {
private final int batteryIndex;
private final int icon;
private final int label;
public BatteryConfig(int batteryIndex, int icon, int label) {
this.batteryIndex = batteryIndex;
this.icon = icon;
this.label = label;
}
public int getBatteryIndex() {
return batteryIndex;
}
public int icon() {
return icon;
}
public int label() {
return label;
}
}

View File

@ -16,6 +16,7 @@ import java.util.List;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;

View File

@ -118,6 +118,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.entities.DeviceAttributes;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryConfig;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
public class DeviceHelper {
@ -340,6 +341,12 @@ public class DeviceHelper {
public GBDevice toGBDevice(Device dbDevice) {
DeviceType deviceType = DeviceType.fromKey(dbDevice.getType());
GBDevice gbDevice = new GBDevice(dbDevice.getIdentifier(), dbDevice.getName(), dbDevice.getAlias(), deviceType);
DeviceCoordinator coordinator = getCoordinator(gbDevice);
for (BatteryConfig batteryConfig : coordinator.getBatteryConfig()) {
gbDevice.setBatteryIcon(batteryConfig.icon(), batteryConfig.getBatteryIndex());
gbDevice.setBatteryLabel(batteryConfig.label(), batteryConfig.getBatteryIndex());
}
List<DeviceAttributes> deviceAttributesList = dbDevice.getDeviceAttributesList();
if (deviceAttributesList.size() > 0) {
gbDevice.setModel(dbDevice.getModel());

View File

@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M3,9H1v11c0,1.11 0.89,2 2,2h17v-2H3V9z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M18,5V3c0,-1.1 -0.9,-2 -2,-2h-4c-1.1,0 -2,0.9 -2,2v2H5v11c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2V5H18zM12,3h4v2h-4V3zM21,16H7V7h14V16z"/>
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M21,7h-1V6h-2v1h-1c-0.55,0 -1,0.45 -1,1v9c0,0.55 0.45,1 1,1h4c0.55,0 1,-0.45 1,-1V8C22,7.45 21.55,7 21,7zM20,16h-2V9h2V16zM14,9.38C14,7.51 12.49,6 10.62,6S7.25,7.51 7.25,9.38v5.25c0,1.04 -0.84,1.88 -1.88,1.88S3.5,15.66 3.5,14.62v-4.7C3.66,9.97 3.83,10 4,10c1.1,0 2,-0.9 2,-2S5.1,6 4,6S2,6.9 2,8c0,0.04 0,6.62 0,6.62C2,16.49 3.51,18 5.38,18s3.38,-1.51 3.38,-3.38V9.38c0,-1.04 0.84,-1.88 1.88,-1.88s1.88,0.84 1.88,1.88v4.7C12.34,14.03 12.17,14 12,14c-1.1,0 -2,0.9 -2,2s0.9,2 2,2s2,-0.9 2,-2C14,15.96 14,9.38 14,9.38z"/>
</vector>

View File

@ -383,6 +383,7 @@
<string name="other">Other</string>
<string name="left">Left</string>
<string name="right">Right</string>
<string name="box">Case</string>
<string name="horizontal">Horizontal</string>
<string name="vertical">Vertical</string>
<string name="miband_pairing_using_dummy_userdata">No valid user data given, using dummy user data for now.</string>