1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-02-18 05:17:08 +01:00

UM25: added notification on below current threshold

This commit is contained in:
Daniel Dakhno 2022-04-15 02:08:51 +02:00
parent bba5452f57
commit bf4e948c35
5 changed files with 94 additions and 2 deletions

View File

@ -178,6 +178,9 @@ public class DeviceSettingsPreferenceConst {
public static final String PREFS_ACTIVITY_IN_DEVICE_CARD_DISTANCE = "prefs_activity_in_device_card_distance";
public static final String PREFS_DEVICE_CHARTS_TABS = "charts_tabs";
public static final String PREF_UM25_SHOW_THRESHOLD_NOTIFICATION = "um25_current_threshold_notify";
public static final String PREF_UM25_SHOW_THRESHOLD = "um25_current_threshold";
public static final String PREF_SOUNDS = "sounds";
public static final String PREF_AUTH_KEY = "authkey";
public static final String PREF_USER_FITNESS_GOAL = "fitness_goal";

View File

@ -456,6 +456,9 @@ public class DeviceSpecificSettingsFragment extends PreferenceFragmentCompat imp
addPreferenceHandlerFor(PREF_QC35_NOISE_CANCELLING_LEVEL);
addPreferenceHandlerFor(PREF_USER_FITNESS_GOAL);
addPreferenceHandlerFor(PREF_UM25_SHOW_THRESHOLD_NOTIFICATION);
addPreferenceHandlerFor(PREF_UM25_SHOW_THRESHOLD);
String sleepTimeState = prefs.getString(PREF_SLEEP_TIME, PREF_DO_NOT_DISTURB_OFF);
boolean sleepTimeScheduled = sleepTimeState.equals(PREF_DO_NOT_DISTURB_SCHEDULED);

View File

@ -16,6 +16,7 @@ import java.util.Collections;
import cyanogenmod.app.CustomTile;
import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractDeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
@ -55,6 +56,13 @@ public class UM25Coordinator extends AbstractDeviceCoordinator {
return DeviceType.UNKNOWN;
}
@Override
public int[] getSupportedDeviceSpecificSettings(GBDevice device) {
return new int[]{
R.xml.devicesettings_um25
};
}
@Override
public DeviceType getDeviceType() {
return DeviceType.UM25;

View File

@ -1,12 +1,15 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.um25.Support;
import android.app.Notification;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import androidx.core.app.NotificationCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import org.slf4j.Logger;
@ -19,12 +22,16 @@ import java.util.UUID;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction;
import nodomain.freeyourgadget.gadgetbridge.service.devices.um25.Data.CaptureGroup;
import nodomain.freeyourgadget.gadgetbridge.service.devices.um25.Data.MeasurementData;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
public class UM25Support extends UM25BaseSupport {
@ -44,6 +51,12 @@ public class UM25Support extends UM25BaseSupport {
private static final Logger logger = LoggerFactory.getLogger(UM25Support.class);
SharedPreferences preferences;
private boolean notifyOnCurrentThreshold;
private int notificationCurrentThreshold;
private boolean wasOverNotificationCurrent = false;
private long lastOverThresholdTimestamp = 0;
public UM25Support() {
super(logger);
@ -63,8 +76,22 @@ public class UM25Support extends UM25BaseSupport {
}
};
void readPreferences(){
notifyOnCurrentThreshold = preferences.getBoolean(DeviceSettingsPreferenceConst.PREF_UM25_SHOW_THRESHOLD_NOTIFICATION, false);
notificationCurrentThreshold = Integer.parseInt(preferences.getString(DeviceSettingsPreferenceConst.PREF_UM25_SHOW_THRESHOLD, "100"));
}
@Override
public void onSendConfiguration(String config) {
readPreferences();
}
@Override
protected TransactionBuilder initializeDevice(TransactionBuilder builder) {
preferences = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
readPreferences();
return builder
.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()))
.notify(getCharacteristic(UUID.fromString(UUID_CHAR)), true)
@ -132,6 +159,46 @@ public class UM25Support extends UM25BaseSupport {
return true;
}
private void handleCurrentNotification(int currentMa){
logger.debug("current: " + currentMa);
if(!notifyOnCurrentThreshold){
return;
}
boolean isOverNotificationCurrent = currentMa > notificationCurrentThreshold;
long now = System.currentTimeMillis();
if(isOverNotificationCurrent){
lastOverThresholdTimestamp = now;
wasOverNotificationCurrent = true;
return;
}
long deltaSinceOverThreshold = now - lastOverThresholdTimestamp;
if(deltaSinceOverThreshold < 5000){
// must be below threshold for over certain time before triggering notification
return;
}
if(wasOverNotificationCurrent){
// handle change from over threshold to below threshold
wasOverNotificationCurrent = false;
Notification notification = new NotificationCompat.Builder(getContext(), GB.NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID)
.setSmallIcon(R.drawable.ic_notification_low_battery)
.setContentTitle("USB current")
.setContentText("USB current below threshold")
.build();
GB.notify(
GB.NOTIFICATION_ID_LOW_BATTERY,
notification,
getContext()
);
}
}
private void handlePayload(ByteBuffer payload){
String payloadString = StringUtils.bytesToHex(payload.array());
payloadString = payloadString.replaceAll("(..)", "$1 ");
@ -163,8 +230,6 @@ public class UM25Support extends UM25BaseSupport {
int chargingSeconds = payload.getInt(112);
int cableResistance = payload.getInt(122);
logger.debug("variable: " + chargedCurrent);
MeasurementData data = new MeasurementData(
voltage,
current,
@ -185,6 +250,8 @@ public class UM25Support extends UM25BaseSupport {
measurementIntent.putExtra(EXTRA_KEY_MEASUREMENT_DATA, data);
handleCurrentNotification(current / 10);
LocalBroadcastManager.getInstance(getContext())
.sendBroadcast(measurementIntent);
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:title="Notify if below current threshold"
android:key="um25_current_threshold_notify" />
<EditTextPreference
android:title="Current threshold im mA"
android:key="um25_current_threshold"
android:inputType="number"
android:defaultValue="100"/>
</PreferenceScreen>