1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-03 17:02:13 +01:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/BatteryInfoActivity.java

187 lines
8.4 KiB
Java
Raw Normal View History

package nodomain.freeyourgadget.gadgetbridge.activities;
2021-02-11 18:47:05 +01:00
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
2021-02-11 18:47:05 +01:00
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
2021-02-14 16:46:57 +01:00
import android.widget.LinearLayout;
2021-02-11 18:47:05 +01:00
import android.widget.SeekBar;
import android.widget.TextView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2021-02-11 18:47:05 +01:00
import java.util.Calendar;
2021-02-14 16:46:57 +01:00
import java.util.Date;
2021-02-11 18:47:05 +01:00
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryConfig;
2021-02-14 16:46:57 +01:00
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
public class
BatteryInfoActivity extends AbstractGBActivity {
private static final Logger LOG = LoggerFactory.getLogger(BatteryInfoActivity.class);
2021-02-11 18:47:05 +01:00
GBDevice gbDevice;
private int timeFrom;
private int timeTo;
private int batteryIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
2021-02-11 18:47:05 +01:00
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);
batteryIndex = bundle.getInt(GBDevice.BATTERY_INDEX, 0);
} 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();
2021-02-11 18:47:05 +01:00
timeTo = (int) (System.currentTimeMillis() / 1000);
batteryInfoChartFragment.setDateAndGetData(gbDevice, batteryIndex, timeFrom, timeTo);
2021-02-11 18:47:05 +01:00
TextView battery_status_device_name_text = (TextView) findViewById(R.id.battery_status_device_name);
TextView battery_status_battery_voltage = (TextView) findViewById(R.id.battery_status_battery_voltage);
TextView battery_status_extra_name = (TextView) findViewById(R.id.battery_status_extra_name);
2021-02-14 16:46:57 +01:00
final TextView battery_status_date_from_text = (TextView) findViewById(R.id.battery_status_date_from_text);
final TextView battery_status_date_to_text = (TextView) findViewById(R.id.battery_status_date_to_text);
final SeekBar battery_status_time_span_seekbar = (SeekBar) findViewById(R.id.battery_status_time_span_seekbar);
2021-02-11 18:47:05 +01:00
final TextView battery_status_time_span_text = (TextView) findViewById(R.id.battery_status_time_span_text);
2021-02-14 16:46:57 +01:00
LinearLayout battery_status_date_to_layout = (LinearLayout) findViewById(R.id.battery_status_date_to_layout);
2021-03-04 17:59:51 +01:00
battery_status_time_span_seekbar.setMax(5);
2021-02-11 18:47:05 +01:00
battery_status_time_span_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
String text;
switch (i) {
case 0:
2021-02-14 16:46:57 +01:00
text = getString(R.string.calendar_day);
timeFrom = DateTimeUtils.shiftDays(timeTo, -1);
2021-02-11 18:47:05 +01:00
break;
case 1:
2021-02-14 16:46:57 +01:00
text = getString(R.string.calendar_week);
timeFrom = DateTimeUtils.shiftDays(timeTo, -7);
2021-02-11 18:47:05 +01:00
break;
case 2:
2021-02-14 16:46:57 +01:00
text = getString(R.string.calendar_two_weeks);
timeFrom = DateTimeUtils.shiftDays(timeTo, -14);
2021-02-11 18:47:05 +01:00
break;
case 3:
2021-02-14 16:46:57 +01:00
text = getString(R.string.calendar_month);
timeFrom = DateTimeUtils.shiftMonths(timeTo, -1);
2021-02-11 18:47:05 +01:00
break;
case 4:
2021-02-14 16:46:57 +01:00
text = getString(R.string.calendar_six_months);
timeFrom = DateTimeUtils.shiftMonths(timeTo, -6);
2021-02-11 18:47:05 +01:00
break;
case 5:
2021-02-14 16:46:57 +01:00
text = getString(R.string.calendar_year);
timeFrom = DateTimeUtils.shiftMonths(timeTo, -12);
2021-02-11 18:47:05 +01:00
break;
default:
2021-02-14 16:46:57 +01:00
text = getString(R.string.calendar_two_weeks);
timeFrom = DateTimeUtils.shiftDays(timeTo, -14);
2021-02-11 18:47:05 +01:00
}
battery_status_time_span_text.setText(text);
2021-02-14 16:46:57 +01:00
battery_status_date_from_text.setText(DateTimeUtils.formatDate(new Date(timeFrom * 1000L)));
battery_status_date_to_text.setText(DateTimeUtils.formatDate(new Date(timeTo * 1000L)));
batteryInfoChartFragment.setDateAndGetData(gbDevice, batteryIndex, timeFrom, timeTo);
2021-02-11 18:47:05 +01:00
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
2021-02-14 16:46:57 +01:00
//Button battery_status_calendar_button = findViewById(R.id.battery_status_calendar_button);
battery_status_date_to_layout.setOnClickListener(new View.OnClickListener() {
2021-02-11 18:47:05 +01:00
@Override
public void onClick(View v) {
final Calendar currentDate = Calendar.getInstance();
currentDate.setTimeInMillis(timeTo * 1000L);
Context context = getApplicationContext();
if (context instanceof GBApplication) {
new DatePickerDialog(BatteryInfoActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar date = Calendar.getInstance();
date.set(year, monthOfYear, dayOfMonth);
timeTo = (int) (date.getTimeInMillis() / 1000);
2021-02-14 16:46:57 +01:00
battery_status_date_to_text.setText(DateTimeUtils.formatDate(new Date(timeTo * 1000L)));
battery_status_time_span_seekbar.setProgress(0);
battery_status_time_span_seekbar.setProgress(1);
batteryInfoChartFragment.setDateAndGetData(gbDevice, batteryIndex, timeFrom, timeTo);
2021-02-11 18:47:05 +01:00
}
}, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();
}
}
});
battery_status_time_span_seekbar.setProgress(2);
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());
2021-02-11 18:47:05 +01:00
TextView battery_status_battery_level_text = (TextView) findViewById(R.id.battery_status_battery_level);
String level = gbDevice.getBatteryLevel(batteryIndex) > 0 ? String.format("%1s%%", gbDevice.getBatteryLevel(batteryIndex)) : "";
String voltage = gbDevice.getBatteryVoltage(batteryIndex) > 0 ? String.format("%1sV", gbDevice.getBatteryVoltage(batteryIndex)) : "";
battery_status_device_name_text.setText(gbDevice.getName());
2021-02-11 18:47:05 +01:00
battery_status_battery_level_text.setText(level);
battery_status_battery_voltage.setText(voltage);
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(gbDevice);
for (BatteryConfig batteryConfig : coordinator.getBatteryConfig()) {
if (batteryConfig.getBatteryIndex() == batteryIndex) {
battery_status_extra_name.setText(batteryConfig.getBatteryLabel());
battery_status_device_icon.setImageResource(batteryConfig.getBatteryIcon());
if (gbDevice.isInitialized()) {
battery_status_device_icon.setColorFilter(this.getResources().getColor(R.color.accent));
}
}
}
}
}