mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-02-04 13:04:08 +01:00
make Battery Info time span jump as calendar months and not as days
This commit is contained in:
parent
21c6463e73
commit
63fc6b7ab6
@ -27,7 +27,6 @@ public class BatteryInfoActivity extends AbstractGBActivity {
|
|||||||
GBDevice gbDevice;
|
GBDevice gbDevice;
|
||||||
private int timeFrom;
|
private int timeFrom;
|
||||||
private int timeTo;
|
private int timeTo;
|
||||||
private int timeSpanDays = 14;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -55,7 +54,6 @@ public class BatteryInfoActivity extends AbstractGBActivity {
|
|||||||
.commit();
|
.commit();
|
||||||
|
|
||||||
timeTo = (int) (System.currentTimeMillis() / 1000);
|
timeTo = (int) (System.currentTimeMillis() / 1000);
|
||||||
timeFrom = timeTo - 24 * 3600 * timeSpanDays;
|
|
||||||
|
|
||||||
batteryInfoChartFragment.setDateAndGetData(gbDevice, timeFrom, timeTo);
|
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);
|
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_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 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);
|
||||||
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);
|
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);
|
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) {
|
switch (i) {
|
||||||
case 0:
|
case 0:
|
||||||
text = getString(R.string.calendar_day);
|
text = getString(R.string.calendar_day);
|
||||||
timeSpanDays = 1;
|
timeFrom = DateTimeUtils.shiftDays(timeTo, -1);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
text = getString(R.string.calendar_week);
|
text = getString(R.string.calendar_week);
|
||||||
timeSpanDays = 7;
|
timeFrom = DateTimeUtils.shiftDays(timeTo, -7);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
text = getString(R.string.calendar_two_weeks);
|
text = getString(R.string.calendar_two_weeks);
|
||||||
timeSpanDays = 14;
|
timeFrom = DateTimeUtils.shiftDays(timeTo, -14);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
text = getString(R.string.calendar_month);
|
text = getString(R.string.calendar_month);
|
||||||
timeSpanDays = 30;
|
timeFrom = DateTimeUtils.shiftMonths(timeTo, -1);
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
text = getString(R.string.calendar_six_months);
|
text = getString(R.string.calendar_six_months);
|
||||||
timeSpanDays = 182;
|
timeFrom = DateTimeUtils.shiftMonths(timeTo, -6);
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
text = getString(R.string.calendar_year);
|
text = getString(R.string.calendar_year);
|
||||||
timeSpanDays = 365;
|
timeFrom = DateTimeUtils.shiftMonths(timeTo, -12);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
text = getString(R.string.calendar_two_weeks);
|
text = getString(R.string.calendar_two_weeks);
|
||||||
timeSpanDays = 14;
|
timeFrom = DateTimeUtils.shiftDays(timeTo, -14);
|
||||||
}
|
}
|
||||||
|
|
||||||
battery_status_time_span_text.setText(text);
|
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_from_text.setText(DateTimeUtils.formatDate(new Date(timeFrom * 1000L)));
|
||||||
battery_status_date_to_text.setText(DateTimeUtils.formatDate(new Date(timeTo * 1000L)));
|
battery_status_date_to_text.setText(DateTimeUtils.formatDate(new Date(timeTo * 1000L)));
|
||||||
batteryInfoChartFragment.setDateAndGetData(gbDevice, timeFrom, timeTo);
|
batteryInfoChartFragment.setDateAndGetData(gbDevice, timeFrom, timeTo);
|
||||||
@ -141,9 +137,10 @@ public class BatteryInfoActivity extends AbstractGBActivity {
|
|||||||
Calendar date = Calendar.getInstance();
|
Calendar date = Calendar.getInstance();
|
||||||
date.set(year, monthOfYear, dayOfMonth);
|
date.set(year, monthOfYear, dayOfMonth);
|
||||||
timeTo = (int) (date.getTimeInMillis() / 1000);
|
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_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);
|
batteryInfoChartFragment.setDateAndGetData(gbDevice, timeFrom, timeTo);
|
||||||
}
|
}
|
||||||
}, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();
|
}, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();
|
||||||
|
@ -113,35 +113,35 @@ public class ActivityListingDashboard extends DialogFragment {
|
|||||||
switch (i) {
|
switch (i) {
|
||||||
case 0:
|
case 0:
|
||||||
text = getString(R.string.calendar_day);
|
text = getString(R.string.calendar_day);
|
||||||
timeFrom = shiftDateBackDays(timeTo, 1);
|
timeFrom = DateTimeUtils.shiftDays(timeTo, -1);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
text = getString(R.string.calendar_week);
|
text = getString(R.string.calendar_week);
|
||||||
timeFrom = shiftDateBackDays(timeTo, 7);
|
timeFrom = DateTimeUtils.shiftDays(timeTo, -7);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
text = getString(R.string.calendar_two_weeks);
|
text = getString(R.string.calendar_two_weeks);
|
||||||
timeFrom = shiftDateBackDays(timeTo, 14);
|
timeFrom = DateTimeUtils.shiftDays(timeTo, -14);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
text = getString(R.string.calendar_month);
|
text = getString(R.string.calendar_month);
|
||||||
timeFrom = subtractMonths(timeTo, -1);
|
timeFrom = DateTimeUtils.shiftMonths(timeTo, -1);
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
text = getString(R.string.calendar_three_months);
|
text = getString(R.string.calendar_three_months);
|
||||||
timeFrom = subtractMonths(timeTo, -3);
|
timeFrom = DateTimeUtils.shiftMonths(timeTo, -3);
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
text = getString(R.string.calendar_six_months);
|
text = getString(R.string.calendar_six_months);
|
||||||
timeFrom = subtractMonths(timeTo, -6);
|
timeFrom = DateTimeUtils.shiftMonths(timeTo, -6);
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
text = getString(R.string.calendar_year);
|
text = getString(R.string.calendar_year);
|
||||||
timeFrom = subtractMonths(timeTo, -12);
|
timeFrom = DateTimeUtils.shiftMonths(timeTo, -12);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
text = getString(R.string.calendar_two_weeks);
|
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);
|
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) {
|
private int daysBetweenTimes(int time1, int time2) {
|
||||||
return (int) TimeUnit.MILLISECONDS.toDays((time2 - time1) * 1000L);
|
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) {
|
protected RefreshTask createRefreshTask(String task, Context context) {
|
||||||
return new RefreshTask(task, context);
|
return new RefreshTask(task, context);
|
||||||
|
@ -21,6 +21,7 @@ import android.text.format.DateUtils;
|
|||||||
|
|
||||||
import com.github.pfichtner.durationformatter.DurationFormatter;
|
import com.github.pfichtner.durationformatter.DurationFormatter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.text.FieldPosition;
|
import java.text.FieldPosition;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.ParsePosition;
|
import java.text.ParsePosition;
|
||||||
@ -156,4 +157,36 @@ public class DateTimeUtils {
|
|||||||
public static boolean isYesterday(Date d) {
|
public static boolean isYesterday(Date d) {
|
||||||
return DateUtils.isToday(d.getTime() + DateUtils.DAY_IN_MILLIS);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user