1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-26 03:46:49 +01:00

Use Gb DBaccess for Battery level storing

This commit is contained in:
vanous 2021-03-05 07:49:19 +01:00
parent 6aeb297aa5
commit 69d9695eac

View File

@ -44,7 +44,6 @@ import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;
@ -53,6 +52,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.FindPhoneActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.appmanager.AbstractAppManagerFragment;
import nodomain.freeyourgadget.gadgetbridge.database.DBAccess;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
@ -70,15 +70,12 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInf
import nodomain.freeyourgadget.gadgetbridge.entities.BatteryLevel;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.entities.User;
import nodomain.freeyourgadget.gadgetbridge.externalevents.NotificationListener;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;
import nodomain.freeyourgadget.gadgetbridge.service.receivers.GBCallControlReceiver;
import nodomain.freeyourgadget.gadgetbridge.service.receivers.GBMusicControlReceiver;
import nodomain.freeyourgadget.gadgetbridge.util.AlarmUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID;
import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_ID;
@ -420,19 +417,7 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
GB.removeBatteryNotification(context);
}
} else {
try (DBHandler db = GBApplication.acquireDB()) {
DaoSession daoSession = db.getDaoSession();
Device device = DBHelper.getDevice(gbDevice, daoSession);
int ts = (int) (System.currentTimeMillis() / 1000);
BatteryLevel batteryLevel = new BatteryLevel();
batteryLevel.setTimestamp(ts);
batteryLevel.setDevice(device);
batteryLevel.setLevel(deviceEvent.level);
db.getDaoSession().getBatteryLevelDao().insert(batteryLevel);
} catch (Exception e) {
LOG.debug("Error accessing database: ", GB.ERROR, e);
}
createStoreTask("Storing battery data", context, deviceEvent).execute();
//show the notification if the battery level is below threshold and only if not connected to charger
if (deviceEvent.level <= gbDevice.getBatteryThresholdPercent() &&
@ -454,6 +439,32 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
gbDevice.sendDeviceUpdateIntent(context);
}
private StoreDataTask createStoreTask(String task, Context context, GBDeviceEventBatteryInfo deviceEvent) {
return new StoreDataTask(task, context, deviceEvent);
}
public class StoreDataTask extends DBAccess {
GBDeviceEventBatteryInfo deviceEvent;
public StoreDataTask(String task, Context context, GBDeviceEventBatteryInfo deviceEvent) {
super(task, context);
this.deviceEvent = deviceEvent;
}
@Override
protected void doInBackground(DBHandler handler) {
DaoSession daoSession = handler.getDaoSession();
Device device = DBHelper.getDevice(gbDevice, daoSession);
int ts = (int) (System.currentTimeMillis() / 1000);
BatteryLevel batteryLevel = new BatteryLevel();
batteryLevel.setTimestamp(ts);
batteryLevel.setDevice(device);
batteryLevel.setLevel(deviceEvent.level);
handler.getDaoSession().getBatteryLevelDao().insert(batteryLevel);
}
}
public void handleGBDeviceEvent(GBDeviceEventDisplayMessage message) {
GB.log(message.message, message.severity, null);