1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-26 18:45:49 +01:00

make Battery Info time span jump as calendar months and not as days

This commit is contained in:
vanous 2021-04-02 11:55:19 +02:00
parent 21c6463e73
commit 63fc6b7ab6
4 changed files with 53 additions and 39 deletions

View File

@ -27,7 +27,6 @@ public class BatteryInfoActivity extends AbstractGBActivity {
GBDevice gbDevice;
private int timeFrom;
private int timeTo;
private int timeSpanDays = 14;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -55,7 +54,6 @@ public class BatteryInfoActivity extends AbstractGBActivity {
.commit();
timeTo = (int) (System.currentTimeMillis() / 1000);
timeFrom = timeTo - 24 * 3600 * timeSpanDays;
batteryInfoChartFragment.setDateAndGetData(gbDevice, timeFrom, timeTo);
@ -63,8 +61,7 @@ public class BatteryInfoActivity extends AbstractGBActivity {
TextView battery_status_battery_voltage = (TextView) findViewById(R.id.battery_status_battery_voltage);
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);
SeekBar battery_status_time_span_seekbar = (SeekBar) findViewById(R.id.battery_status_time_span_seekbar);
final SeekBar battery_status_time_span_seekbar = (SeekBar) findViewById(R.id.battery_status_time_span_seekbar);
final TextView battery_status_time_span_text = (TextView) findViewById(R.id.battery_status_time_span_text);
LinearLayout battery_status_date_to_layout = (LinearLayout) findViewById(R.id.battery_status_date_to_layout);
@ -78,35 +75,34 @@ public class BatteryInfoActivity extends AbstractGBActivity {
switch (i) {
case 0:
text = getString(R.string.calendar_day);
timeSpanDays = 1;
timeFrom = DateTimeUtils.shiftDays(timeTo, -1);
break;
case 1:
text = getString(R.string.calendar_week);
timeSpanDays = 7;
timeFrom = DateTimeUtils.shiftDays(timeTo, -7);
break;
case 2:
text = getString(R.string.calendar_two_weeks);
timeSpanDays = 14;
timeFrom = DateTimeUtils.shiftDays(timeTo, -14);
break;
case 3:
text = getString(R.string.calendar_month);
timeSpanDays = 30;
timeFrom = DateTimeUtils.shiftMonths(timeTo, -1);
break;
case 4:
text = getString(R.string.calendar_six_months);
timeSpanDays = 182;
timeFrom = DateTimeUtils.shiftMonths(timeTo, -6);
break;
case 5:
text = getString(R.string.calendar_year);
timeSpanDays = 365;
timeFrom = DateTimeUtils.shiftMonths(timeTo, -12);
break;
default:
text = getString(R.string.calendar_two_weeks);
timeSpanDays = 14;
timeFrom = DateTimeUtils.shiftDays(timeTo, -14);
}
battery_status_time_span_text.setText(text);
timeFrom = timeTo - 24 * 3600 * timeSpanDays;
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, timeFrom, timeTo);
@ -141,9 +137,10 @@ public class BatteryInfoActivity extends AbstractGBActivity {
Calendar date = Calendar.getInstance();
date.set(year, monthOfYear, dayOfMonth);
timeTo = (int) (date.getTimeInMillis() / 1000);
timeFrom = timeTo - 24 * 3600 * timeSpanDays;
battery_status_date_from_text.setText(DateTimeUtils.formatDate(new Date(timeFrom * 1000L)));
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, timeFrom, timeTo);
}
}, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();

View File

@ -113,35 +113,35 @@ public class ActivityListingDashboard extends DialogFragment {
switch (i) {
case 0:
text = getString(R.string.calendar_day);
timeFrom = shiftDateBackDays(timeTo, 1);
timeFrom = DateTimeUtils.shiftDays(timeTo, -1);
break;
case 1:
text = getString(R.string.calendar_week);
timeFrom = shiftDateBackDays(timeTo, 7);
timeFrom = DateTimeUtils.shiftDays(timeTo, -7);
break;
case 2:
text = getString(R.string.calendar_two_weeks);
timeFrom = shiftDateBackDays(timeTo, 14);
timeFrom = DateTimeUtils.shiftDays(timeTo, -14);
break;
case 3:
text = getString(R.string.calendar_month);
timeFrom = subtractMonths(timeTo, -1);
timeFrom = DateTimeUtils.shiftMonths(timeTo, -1);
break;
case 4:
text = getString(R.string.calendar_three_months);
timeFrom = subtractMonths(timeTo, -3);
timeFrom = DateTimeUtils.shiftMonths(timeTo, -3);
break;
case 5:
text = getString(R.string.calendar_six_months);
timeFrom = subtractMonths(timeTo, -6);
timeFrom = DateTimeUtils.shiftMonths(timeTo, -6);
break;
case 6:
text = getString(R.string.calendar_year);
timeFrom = subtractMonths(timeTo, -12);
timeFrom = DateTimeUtils.shiftMonths(timeTo, -12);
break;
default:
text = getString(R.string.calendar_two_weeks);
timeFrom = shiftDateBackDays(timeTo, 14);
timeFrom = DateTimeUtils.shiftDays(timeTo, -14);
}
@ -191,26 +191,10 @@ public class ActivityListingDashboard extends DialogFragment {
battery_status_time_span_seekbar.setProgress(2);
}
private int subtractMonths(int timeTo, int month) {
Calendar day = Calendar.getInstance();
day.setTimeInMillis(timeTo * 1000L);
day.add(Calendar.MONTH, month);
return (int) (day.getTimeInMillis() / 1000);
}
private int daysBetweenTimes(int time1, int time2) {
return (int) TimeUnit.MILLISECONDS.toDays((time2 - time1) * 1000L);
}
private int shiftDateBackDays(int timeTo, int days) {
int time = timeTo - ((24 * 3600) - 1) * days;
Calendar day = Calendar.getInstance();
day.setTimeInMillis(time * 1000L);
day.set(Calendar.HOUR_OF_DAY, 0);
day.set(Calendar.MINUTE, 0);
day.set(Calendar.SECOND, 0);
return (int) (day.getTimeInMillis() / 1000);
}
protected RefreshTask createRefreshTask(String task, Context context) {
return new RefreshTask(task, context);

View File

@ -284,7 +284,7 @@ public class ChartsActivity extends AbstractGBFragmentActivity implements Charts
return mEndDate;
}
private void handleButtonClicked(String Action) {
private void handleButtonClicked(String Action) {
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(Action));
}

View File

@ -21,6 +21,7 @@ import android.text.format.DateUtils;
import com.github.pfichtner.durationformatter.DurationFormatter;
import java.io.IOException;
import java.text.FieldPosition;
import java.text.ParseException;
import java.text.ParsePosition;
@ -156,4 +157,36 @@ public class DateTimeUtils {
public static boolean isYesterday(Date d) {
return DateUtils.isToday(d.getTime() + DateUtils.DAY_IN_MILLIS);
}
/**
* Calculates new timestamp with a month offset (positive to add or negative to remove)
* from a given time
*
* @param time
* @param month
*/
public static int shiftMonths(int time, int month) {
Calendar day = Calendar.getInstance();
day.setTimeInMillis(time * 1000L);
day.add(Calendar.MONTH, month);
return (int) (day.getTimeInMillis() / 1000);
}
/**
* Calculates new timestamp with a day offset (positive to add or negative to remove)
* from a given time
*
* @param time
* @param days
*/
public static int shiftDays(int time, int days) {
int newTime = time + ((24 * 3600) - 1) * days;
Calendar day = Calendar.getInstance();
day.setTimeInMillis(newTime * 1000L);
day.set(Calendar.HOUR_OF_DAY, 0);
day.set(Calendar.MINUTE, 0);
day.set(Calendar.SECOND, 0);
return (int) (day.getTimeInMillis() / 1000);
}
}