1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-24 17:45:50 +01:00

initial battery level collector and simple chart

This commit is contained in:
vanous 2021-01-31 11:10:03 +01:00
parent 89e0799217
commit 3e04778696
8 changed files with 392 additions and 2 deletions

View File

@ -43,7 +43,7 @@ public class GBDaoGenerator {
public static void main(String[] args) throws Exception {
Schema schema = new Schema(32, MAIN_PACKAGE + ".entities");
Schema schema = new Schema(33, MAIN_PACKAGE + ".entities");
Entity userAttributes = addUserAttributes(schema);
Entity user = addUserInfo(schema, userAttributes);
@ -90,7 +90,7 @@ public class GBDaoGenerator {
addNotificationFilterEntry(schema, notificationFilter);
addActivitySummary(schema, user, device);
addBatteryLevel(schema, device);
new DaoGenerator().generateAll(schema, "app/src/main/java");
}
@ -604,4 +604,14 @@ public class GBDaoGenerator {
entity.addImport("de.greenrobot.dao.AbstractDao");
return entity;
}
private static Entity addBatteryLevel(Schema schema, Entity device) {
Entity batteryLevel = addEntity(schema, "BatteryLevel");
batteryLevel.implementsSerializable();
batteryLevel.addIntProperty("timestamp").notNull().primaryKey();
Property deviceId = batteryLevel.addLongProperty("deviceId").primaryKey().notNull().getProperty();
batteryLevel.addToOne(device, deviceId);
batteryLevel.addIntProperty("level").notNull();
return batteryLevel;
}
}

View File

@ -425,6 +425,11 @@
android:label="@string/about_activity_title"
android:parentActivityName=".activities.ControlCenterv2"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".activities.BatteryInfoActivity"
android:label="@string/battery_detail_activity_title"
android:parentActivityName=".activities.ControlCenterv2" />
<activity
android:name=".activities.ActivitySummaryDetail"
android:label="@string/activity_summary_detail"

View File

@ -0,0 +1,63 @@
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
public class BatteryInfoActivity extends AbstractGBActivity {
private static final Logger LOG = LoggerFactory.getLogger(BatteryInfoActivity.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
GBDevice gbDevice;
super.onCreate(savedInstanceState);
final Context appContext = this.getApplicationContext();
if (appContext instanceof GBApplication) {
setContentView(R.layout.activity_battery_info);
}
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
gbDevice = bundle.getParcelable(GBDevice.EXTRA_DEVICE);
} else {
throw new IllegalArgumentException("Must provide a device when invoking this activity");
}
final BatteryInfoChartFragment batteryInfoChartFragment = new BatteryInfoChartFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.batteryChartFragmentHolder, batteryInfoChartFragment)
.commit();
int timeTo = (int) (System.currentTimeMillis() / 1000);
int timeFrom = timeTo - 24 * 3600 * 14; //two weeks of data for the start
batteryInfoChartFragment.setDateAndGetData(gbDevice, timeFrom, timeTo);
TextView battery_status_device_name_text = (TextView) findViewById(R.id.battery_status_device_name_text);
ImageView battery_status_device_icon = findViewById(R.id.battery_status_device_icon);
battery_status_device_icon.setImageResource(gbDevice.isInitialized() ? gbDevice.getType().getIcon() : gbDevice.getType().getDisabledIcon());
TextView battery_status_battery_level_text = (TextView) findViewById(R.id.battery_status_battery_level_text);
String level = String.valueOf(gbDevice.getBatteryLevel());
String state = String.valueOf(gbDevice.getBatteryState());
battery_status_device_name_text.setText(gbDevice.getName());
battery_status_battery_level_text.setText(String.format("%s%%", level));
}
}

View File

@ -0,0 +1,200 @@
/* Copyright (C) 2015-2020 Andreas Shimokawa, Carsten Pfeiffer, Daniele
Gobbetti, Dikay900, Pavel Elagin
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LegendEntry;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import de.greenrobot.dao.query.QueryBuilder;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.AbstractChartFragment;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsData;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsHost;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.entities.BatteryLevel;
import nodomain.freeyourgadget.gadgetbridge.entities.BatteryLevelDao;
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
public class BatteryInfoChartFragment extends AbstractChartFragment {
private static final Logger LOG = LoggerFactory.getLogger(BatteryInfoChartFragment.class);
private LineChart mChart;
private int startTime;
private int endTime;
private GBDevice gbDevice;
public void setDateAndGetData(GBDevice gbDevice, long startTime, long endTime) {
this.startTime = (int) startTime;
this.endTime = (int) endTime;
this.gbDevice = gbDevice;
try {
setupLegend(mChart);
populate_charts_data();
} catch (Exception e) {
LOG.debug("Unable to fill charts data right now:", e);
}
}
private void populate_charts_data() {
try (DBHandler handler = GBApplication.acquireDB()) {
List<? extends BatteryLevel> samples = getLevels(handler, gbDevice, startTime, endTime);
List<Entry> entries = new ArrayList<Entry>();
for (BatteryLevel sample : samples) {
entries.add(new Entry(sample.getTimestamp(), sample.getLevel()));
}
LineDataSet dataSet = new LineDataSet(entries, "Battery level");
dataSet.setDrawCircles(false);
dataSet.setLineWidth(2.2f);
dataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
dataSet.setCubicIntensity(0.1f);
dataSet.setDrawCircles(false);
dataSet.setDrawValues(false);
LineData lineData = new LineData(dataSet);
try {
if (lineData != null) {
//mChart.getXAxis().setValueFormatter(null);
//mChart.setData(null);
mChart.setData((LineData) lineData);
mChart.invalidate();
}
} catch (Exception e) {
LOG.error("Unable to get charts data:", e);
}
} catch (Exception e) {
LOG.error("Unable to get charts data:", e);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
init();
View rootView = inflater.inflate(R.layout.fragment_charts, container, false);
mChart = rootView.findViewById(R.id.activitysleepchart);
if (this.gbDevice != null || mChart != null) {
setupChart();
populate_charts_data();
}
return rootView;
}
@Override
public String getTitle() {
return "";
}
private void setupChart() {
mChart.setBackgroundColor(BACKGROUND_COLOR);
mChart.getDescription().setTextColor(DESCRIPTION_COLOR);
configureBarLineChartDefaults(mChart);
XAxis x = mChart.getXAxis();
x.setDrawLabels(true);
x.setDrawGridLines(false);
x.setEnabled(true);
x.setTextColor(CHART_TEXT_COLOR);
x.setDrawLimitLinesBehindData(true);
YAxis yAxisLeft = mChart.getAxisRight();
yAxisLeft.setAxisMaximum(100L);
yAxisLeft.setAxisMinimum(0);
yAxisLeft.setEnabled(true);
YAxis yAxisRight = mChart.getAxisRight();
yAxisRight.setDrawGridLines(false);
yAxisRight.setDrawLabels(true);
yAxisRight.setDrawTopYLabelEntry(true);
yAxisRight.setTextColor(CHART_TEXT_COLOR);
yAxisRight.setAxisMaximum(100);
yAxisRight.setAxisMinimum(0);
yAxisRight.setEnabled(false);
}
private List<? extends BatteryLevel> getLevels(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
BatteryLevelDao batteryLevelDao = db.getDaoSession().getBatteryLevelDao();
Device dbDevice = DBHelper.findDevice(device, db.getDaoSession());
QueryBuilder<BatteryLevel> qb = batteryLevelDao.queryBuilder();
qb.where(BatteryLevelDao.Properties.DeviceId.eq(dbDevice.getId())).orderAsc(BatteryLevelDao.Properties.Timestamp);
qb.where(BatteryLevelDao.Properties.Timestamp.gt(tsFrom));
qb.where(BatteryLevelDao.Properties.Timestamp.lt(tsTo));
List<BatteryLevel> allLevels = new ArrayList<>();
allLevels.addAll(qb.build().list());
return allLevels;
}
@Override
protected void setupLegend(Chart chart) {
chart.getLegend().setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
}
@Override
protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) {
return null;
}
@Override
protected void renderCharts() {
}
@Override
protected List<? extends ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
return null;
}
protected Entry createLineEntry(float value, int xValue) {
return new Entry(xValue, value);
}
@Override
protected void updateChartsnUIThread(ChartsData chartsData) {
}
}

View File

@ -57,6 +57,7 @@ import java.util.Locale;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.ActivitySummariesActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.BatteryInfoActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.ConfigureAlarms;
import nodomain.freeyourgadget.gadgetbridge.activities.VibrationActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity;
@ -159,6 +160,19 @@ public class GBDeviceAdapterv2 extends RecyclerView.Adapter<GBDeviceAdapterv2.Vi
holder.batteryStatusLabel.setText(String.format(Locale.getDefault(), "%.2f", batteryVoltage));
holder.batteryIcon.setImageLevel(200);
}
holder.batteryStatusBox.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent startIntent;
startIntent = new Intent(context, BatteryInfoActivity.class);
startIntent.putExtra(GBDevice.EXTRA_DEVICE, device);
context.startActivity(startIntent);
}
}
);
//device specific settings
holder.deviceSpecificSettingsView.setVisibility(coordinator.getSupportedDeviceSpecificSettings(device) != null ? View.VISIBLE : View.GONE);

View File

@ -44,6 +44,7 @@ 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;
@ -52,6 +53,8 @@ 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.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
@ -64,11 +67,16 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicContr
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventNotificationControl;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventScreenshot;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
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;
@ -412,6 +420,20 @@ 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);
}
//show the notification if the battery level is below threshold and only if not connected to charger
if (deviceEvent.level <= gbDevice.getBatteryThresholdPercent() &&
(BatteryState.BATTERY_LOW.equals(deviceEvent.state) ||

View File

@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_battery_info_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical">
<LinearLayout
android:id="@+id/activity_battery_info_master_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="14dp"
android:layout_marginEnd="14dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/battery_status_device_icon"
android:layout_width="80dp"
android:layout_height="80dp"
app:srcCompat="@drawable/ic_devices_other"
android:contentDescription="TODO" />
<LinearLayout
android:id="@+id/activity_battery_info_top_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/battery_status_device_name_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="24sp"
tools:text="Device name" />
<TextView
android:id="@+id/battery_status_battery_level_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAlignment="textEnd"
android:textSize="48sp"
tools:text="90%" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/activity_battery_info_bottom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/batteryChartFragmentHolder"
android:layout_width="match_parent"
android:layout_height="400dp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>

View File

@ -25,6 +25,7 @@
<string name="controlcenter_snackbar_connecting">Connecting…</string>
<string name="controlcenter_snackbar_requested_screenshot">Taking a screenshot of the device</string>
<string name="controlcenter_calibrate_device">Calibrate Device</string>
<string name="battery_detail_activity_title">Battery info</string>
<!-- Strings related to Debug Activity -->
<string name="title_activity_debug">Debug</string>
<string name="debugactivity_really_factoryreset_title">Really factory reset?</string>