New dependency to format durations (and use that where applicable)

This commit is contained in:
cpfeiffer 2015-07-20 23:11:16 +02:00
parent 4aa80c149c
commit 184e0f2dea
6 changed files with 51 additions and 15 deletions

View File

@ -27,4 +27,5 @@ dependencies {
compile 'com.github.tony19:logback-android-classic:1.1.1-3' compile 'com.github.tony19:logback-android-classic:1.1.1-3'
compile 'org.slf4j:slf4j-api:1.7.7' compile 'org.slf4j:slf4j-api:1.7.7'
compile 'com.github.PhilJay:MPAndroidChart:2.1.0' compile 'com.github.PhilJay:MPAndroidChart:2.1.0'
compile 'com.github.pfichtner:durationformatter:0.1.1'
} }

View File

@ -14,6 +14,8 @@ import android.os.Looper;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.widget.Toast; import android.widget.Toast;
import com.github.pfichtner.durationformatter.DurationFormatter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -22,6 +24,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.concurrent.TimeUnit;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventScreenshot; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventScreenshot;
import nodomain.freeyourgadget.gadgetbridge.externalevents.K9Receiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.K9Receiver;
@ -248,4 +251,14 @@ public class GB {
break; break;
} }
} }
public static String formatDurationHoursMinutes(long duration, TimeUnit unit) {
DurationFormatter df = DurationFormatter.Builder.SYMBOLS
.maximum(TimeUnit.DAYS)
.minimum(TimeUnit.MINUTES)
.suppressZeros(DurationFormatter.SuppressZeros.LEADING)
.maximumAmountOfUnitsToShow(2)
.build();
return df.format(duration, unit);
}
} }

View File

@ -24,6 +24,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBActivitySample;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.GBDevice; import nodomain.freeyourgadget.gadgetbridge.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.charts.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.charts.SleepUtils; import nodomain.freeyourgadget.gadgetbridge.charts.SleepUtils;
public abstract class AbstractChartFragment extends Fragment { public abstract class AbstractChartFragment extends Fragment {
@ -54,6 +55,18 @@ public abstract class AbstractChartFragment extends Fragment {
protected static final int CHART_TEXT_COLOR = Color.WHITE; protected static final int CHART_TEXT_COLOR = Color.WHITE;
protected static final int LEGEND_TEXT_COLOR = Color.WHITE; protected static final int LEGEND_TEXT_COLOR = Color.WHITE;
protected Integer getColorFor(int activityKind) {
switch (activityKind) {
case nodomain.freeyourgadget.gadgetbridge.charts.ActivityKind.TYPE_DEEP_SLEEP:
return akDeepSleep.color;
case nodomain.freeyourgadget.gadgetbridge.charts.ActivityKind.TYPE_LIGHT_SLEEP:
return akLightSleep.color;
case nodomain.freeyourgadget.gadgetbridge.charts.ActivityKind.TYPE_ACTIVITY:
return akActivity.color;
}
return akActivity.color;
}
protected byte getProvider(GBDevice device) { protected byte getProvider(GBDevice device) {
byte provider = -1; byte provider = -1;
switch (device.getType()) { switch (device.getType()) {

View File

@ -18,14 +18,18 @@ import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData; import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet; import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.utils.ValueFormatter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import nodomain.freeyourgadget.gadgetbridge.ControlCenter; import nodomain.freeyourgadget.gadgetbridge.ControlCenter;
import nodomain.freeyourgadget.gadgetbridge.GB;
import nodomain.freeyourgadget.gadgetbridge.GBActivitySample; import nodomain.freeyourgadget.gadgetbridge.GBActivitySample;
import nodomain.freeyourgadget.gadgetbridge.GBDevice; import nodomain.freeyourgadget.gadgetbridge.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
@ -77,18 +81,25 @@ public class SleepChartFragment extends AbstractChartFragment {
private void refreshSleepAmounts(GBDevice mGBDevice, PieChart pieChart, List<GBActivitySample> samples) { private void refreshSleepAmounts(GBDevice mGBDevice, PieChart pieChart, List<GBActivitySample> samples) {
ActivityAnalysis analysis = new ActivityAnalysis(); ActivityAnalysis analysis = new ActivityAnalysis();
ActivityAmounts amounts = analysis.calculateActivityAmounts(samples); ActivityAmounts amounts = analysis.calculateActivityAmounts(samples);
float hoursOfSleep = amounts.getTotalSeconds() / (float) (60 * 60); String totalSleep = GB.formatDurationHoursMinutes(amounts.getTotalSeconds(), TimeUnit.SECONDS);
pieChart.setCenterText((int)hoursOfSleep + "h"); // FIXME pieChart.setCenterText(totalSleep);
PieData data = new PieData(); PieData data = new PieData();
List<Entry> entries = new ArrayList<>(); List<Entry> entries = new ArrayList<>();
List<Integer> colors = new ArrayList<>(); List<Integer> colors = new ArrayList<>();
int index = 0; int index = 0;
for (ActivityAmount amount : amounts.getAmounts()) { for (ActivityAmount amount : amounts.getAmounts()) {
long value = amount.getTotalSeconds();
entries.add(new Entry(amount.getTotalSeconds(), index++)); entries.add(new Entry(amount.getTotalSeconds(), index++));
colors.add(getColorFor(amount.getActivityKind())); colors.add(getColorFor(amount.getActivityKind()));
data.addXValue(amount.getName(getActivity())); data.addXValue(amount.getName(getActivity()));
} }
PieDataSet set = new PieDataSet(entries, "Sleep comparison"); PieDataSet set = new PieDataSet(entries, "");
set.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return GB.formatDurationHoursMinutes((long)value, TimeUnit.SECONDS);
}
});
set.setColors(colors); set.setColors(colors);
data.setDataSet(set); data.setDataSet(set);
pieChart.setData(data); pieChart.setData(data);
@ -98,16 +109,6 @@ public class SleepChartFragment extends AbstractChartFragment {
pieChart.invalidate(); pieChart.invalidate();
} }
private Integer getColorFor(int activityKind) {
switch (activityKind) {
case nodomain.freeyourgadget.gadgetbridge.charts.ActivityKind.TYPE_DEEP_SLEEP:
return akDeepSleep.color;
case nodomain.freeyourgadget.gadgetbridge.charts.ActivityKind.TYPE_LIGHT_SLEEP:
return akLightSleep.color;
}
return akActivity.color;
}
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
@ -138,6 +139,9 @@ public class SleepChartFragment extends AbstractChartFragment {
private void setupSleepAmountChart() { private void setupSleepAmountChart() {
mSleepAmountChart.setBackgroundColor(BACKGROUND_COLOR); mSleepAmountChart.setBackgroundColor(BACKGROUND_COLOR);
mSleepAmountChart.setDescriptionColor(DESCRIPTION_COLOR); mSleepAmountChart.setDescriptionColor(DESCRIPTION_COLOR);
mSleepAmountChart.setDescription("");
mSleepAmountChart.setNoDataTextDescription("");
mSleepAmountChart.setNoDataText("");
} }
@Override @Override

View File

@ -18,6 +18,7 @@ import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit;
import nodomain.freeyourgadget.gadgetbridge.GB; import nodomain.freeyourgadget.gadgetbridge.GB;
import nodomain.freeyourgadget.gadgetbridge.GBActivitySample; import nodomain.freeyourgadget.gadgetbridge.GBActivitySample;
@ -652,7 +653,11 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
// after dataUntilNextHeader bytes we will get a new packet of 11 bytes that should be parsed // after dataUntilNextHeader bytes we will get a new packet of 11 bytes that should be parsed
// as we just did // as we just did
GB.toast(getContext().getString(R.string.user_feedback_miband_activity_data_transfer,(dataUntilNextHeader / 3), DateFormat.getDateTimeInstance().format(timestamp.getTime())), Toast.LENGTH_SHORT, GB.INFO); if (dataUntilNextHeader != 0) {
GB.toast(getContext().getString(R.string.user_feedback_miband_activity_data_transfer,
GB.formatDurationHoursMinutes(dataUntilNextHeader / 3, TimeUnit.MINUTES),
DateFormat.getDateTimeInstance().format(timestamp.getTime())), Toast.LENGTH_LONG, GB.INFO);
}
LOG.info("total data to read: " + totalDataToRead + " len: " + (totalDataToRead / 3) + " minute(s)"); LOG.info("total data to read: " + totalDataToRead + " len: " + (totalDataToRead / 3) + " minute(s)");
LOG.info("data to read until next header: " + dataUntilNextHeader + " len: " + (dataUntilNextHeader / 3) + " minute(s)"); LOG.info("data to read until next header: " + dataUntilNextHeader + " len: " + (dataUntilNextHeader / 3) + " minute(s)");
LOG.info("TIMESTAMP: " + DateFormat.getDateTimeInstance().format(timestamp.getTime()).toString() + " magic byte: " + dataUntilNextHeader); LOG.info("TIMESTAMP: " + DateFormat.getDateTimeInstance().format(timestamp.getTime()).toString() + " magic byte: " + dataUntilNextHeader);

View File

@ -152,5 +152,5 @@
<string name="user_feedback_miband_set_alarms_failed">There was an error setting the alarms, please try again!</string> <string name="user_feedback_miband_set_alarms_failed">There was an error setting the alarms, please try again!</string>
<string name="user_feedback_miband_set_alarms_ok">Alarms sent to device!</string> <string name="user_feedback_miband_set_alarms_ok">Alarms sent to device!</string>
<string name="chart_no_data_synchronize">No data. Synchronize device?</string> <string name="chart_no_data_synchronize">No data. Synchronize device?</string>
<string name="user_feedback_miband_activity_data_transfer">About to transfer %1$s minute(s) of data starting from %2$s</string> <string name="user_feedback_miband_activity_data_transfer">About to transfer %1$s of data starting from %2$s</string>
</resources> </resources>