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

Use localized names and dates. Pass only needed data between activities.

Better display view. Cleaning up.
Add km/h and calculate speed to it.
Special casing calc not per key but per unit
solved min/maxPace, localized several missing labels.
totalStride is Total strides in m. Fix missing start_label
This commit is contained in:
vanous 2020-08-09 13:11:10 +02:00
parent da4c58305e
commit bce85beb21
7 changed files with 207 additions and 94 deletions

View File

@ -421,7 +421,7 @@
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".activities.ActivitySummaryDetail"
android:label="Activity Summary Detail"
android:label="@string/activity_summary_detail"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" />

View File

@ -134,7 +134,7 @@ public class ActivitySummariesActivity extends AbstractListActivity<BaseActivity
if (item != null) {
ActivitySummary summary = (ActivitySummary) item;
try {
showDetail(summary);
showActivityDetail(summary);
} catch (Exception e) {
GB.toast(getApplicationContext(), "Unable to display Activity Detail, maybe the activity is not available yet: " + e.getMessage(), Toast.LENGTH_LONG, GB.ERROR, e);
}
@ -266,9 +266,14 @@ public class ActivitySummariesActivity extends AbstractListActivity<BaseActivity
refresh();
}
private void showDetail(ActivitySummary summary){
private void showActivityDetail(ActivitySummary summary){
Intent ActivitySummaryDetailIntent = new Intent(this, ActivitySummaryDetail.class);
ActivitySummaryDetailIntent.putExtra("summary", summary);
ActivitySummaryDetailIntent.putExtra("name", summary.getName());
ActivitySummaryDetailIntent.putExtra("ActivityKind", summary.getActivityKind());
ActivitySummaryDetailIntent.putExtra("StartTime", summary.getStartTime());
ActivitySummaryDetailIntent.putExtra("EndTime", summary.getEndTime());
ActivitySummaryDetailIntent.putExtra("GpxTrack", summary.getGpxTrack());
ActivitySummaryDetailIntent.putExtra("SummaryData", summary.getSummaryData());
ActivitySummaryDetailIntent.putExtra(GBDevice.EXTRA_DEVICE, mGBDevice);
startActivity(ActivitySummaryDetailIntent);
}

View File

@ -25,6 +25,7 @@ import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
@ -32,6 +33,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
@ -42,8 +44,6 @@ import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySummary;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityTrack;
import nodomain.freeyourgadget.gadgetbridge.model.DailyTotals;
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
@ -57,14 +57,10 @@ public class ActivitySummaryDetail extends AbstractGBActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_summary_details);
Intent intent = getIntent();
mGBDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
ActivitySummary summary = (ActivitySummary) intent.getSerializableExtra("summary");
final String gpxTrack = summary.getGpxTrack();
final String gpxTrack = intent.getStringExtra("GpxTrack");
Button show_track_btn = (Button) findViewById(R.id.showTrack);
show_track_btn.setVisibility(View.GONE);
@ -81,66 +77,135 @@ public class ActivitySummaryDetail extends AbstractGBActivity {
});
}
LOG.debug("petr summary: " + summary + "," + summary.getSummaryData());
String activitykind = ActivityKind.asString(summary.getActivityKind(), getApplicationContext());
String starttime = DateTimeUtils.formatDateTime(summary.getStartTime());
String endtime = DateTimeUtils.formatDateTime(summary.getEndTime());
long startTs = summary.getStartTime().getTime() / 1000;
long endTs = summary.getEndTime().getTime() / 1000;
long durationms = (summary.getEndTime().getTime() - summary.getStartTime().getTime());
String durationhms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(durationms),
TimeUnit.MILLISECONDS.toMinutes(durationms) % TimeUnit.HOURS.toMinutes(1),
TimeUnit.MILLISECONDS.toSeconds(durationms) % TimeUnit.MINUTES.toSeconds(1));
int steps = getSteps((int) startTs, (int) endTs);
String activitykindname = ActivityKind.asString(intent.getIntExtra("ActivityKind",0), getApplicationContext());
Date starttime = (Date) intent.getSerializableExtra("StartTime");
Date endtime = (Date) intent.getSerializableExtra("EndTime");
String starttimeS = DateTimeUtils.formatDateTime(starttime);
String endtimeS = DateTimeUtils.formatDateTime(endtime);
long startTs = starttime.getTime() / 1000;
long endTs = endtime.getTime() / 1000;
String durationhms = DateTimeUtils.formatDurationHoursMinutes((endtime.getTime() - starttime.getTime()), TimeUnit.MILLISECONDS);
//int steps = getSteps((int) startTs, (int) endTs);
//unused now, as we use the more extensive summaryData
ImageView activity_icon = (ImageView) findViewById(R.id.item_image);
activity_icon.setImageResource(ActivityKind.getIconId(summary.getActivityKind()));
activity_icon.setImageResource(ActivityKind.getIconId(intent.getIntExtra("ActivityKind",0)));
TextView activity_kind = (TextView) findViewById(R.id.activitykind);
activity_kind.setText(activitykind);
activity_kind.setText(activitykindname);
TextView start_time = (TextView) findViewById(R.id.starttime);
start_time.setText(starttime);
start_time.setText(starttimeS);
TextView end_time = (TextView) findViewById(R.id.endtime);
end_time.setText(endtime);
end_time.setText(endtimeS);
TextView activity_duration = (TextView) findViewById(R.id.duration);
activity_duration.setText(durationhms);
JSONObject summaryData = null;
String sumData = summary.getSummaryData();
String sumData = intent.getStringExtra("SummaryData");
if (sumData != null) {
try {
summaryData = new JSONObject(sumData);
} catch (JSONException e) {
e.printStackTrace();
LOG.error("SportsActivity", e);
}
}
StringBuilder summaryDatastr = new StringBuilder();
if (summaryData == null) return;
Iterator<String> keys = summaryData.keys();
LOG.debug("petr summary JSON:" + summaryData + keys);
JSONObject listOfSummaries = makeSummaryList(summaryData);
TextView details = (TextView) findViewById(R.id.details);
details.setText(makeSummaryContent(listOfSummaries));
}
private String makeSummaryContent (JSONObject data){
//convert dictionary to pretty print string, use localized names
StringBuilder content = new StringBuilder();
Iterator<String> keys = data.keys();
DecimalFormat df = new DecimalFormat("#.##");
while (keys.hasNext()) {
String key = keys.next();
try {
LOG.error("SportsActivity:" + key + ": " + data.get(key) + "\n");
JSONArray innerList = (JSONArray) data.get(key);
content.append(String.format("\n%s\n", getStringResourceByName(key).toUpperCase()));
for (int i = 0; i < innerList.length(); i++) {
JSONObject innerData = innerList.getJSONObject(i);
double value = innerData.getDouble("value");
String unit = innerData.getString("unit");
String name = innerData.getString("name");
//special casing here:
switch(unit){
case "meters_second":
value = value *3.6;
unit = "km_h";
break;
case "seconds_m":
value = 3.6/value;
unit = "minutes_km";
break;
case "seconds_km":
value = value /60;
unit = "minutes_km";
break;
}
content.append(String.format("%s: %s %s\n", getStringResourceByName(name), df.format(value), getStringResourceByName(unit)));
}
} catch (JSONException e) {
LOG.error("SportsActivity", e);
}
}
return content.toString();
}
private JSONObject makeSummaryList(JSONObject summaryData){
//make dictionary with data for each group
JSONObject list = new JSONObject();
Iterator<String> keys = summaryData.keys();
LOG.error("SportsActivity JSON:" + summaryData + keys);
while (keys.hasNext()) {
String key = keys.next();
LOG.debug("petr key:" + key);
try {
LOG.debug("petr" + key + ": " + summaryData.get(key) + "\n");
DecimalFormat df = new DecimalFormat("#.##");
LOG.error("SportsActivity:" + key + ": " + summaryData.get(key) + "\n");
JSONObject innerData = (JSONObject) summaryData.get(key);
Object value = innerData.get("value");
String unit = innerData.getString("unit");
summaryDatastr.append(String.format("%s: %s %s\n", key, df.format(value), unit));
String group = innerData.getString("group");
if (!list.has(group)) {
list.put(group,new JSONArray());
}
JSONArray tmpl = (JSONArray) list.get(group);
JSONObject innernew = new JSONObject();
innernew.put("name", key);
innernew.put("value", value);
innernew.put("unit", unit);
tmpl.put(innernew);
list.put(group, tmpl);
} catch (JSONException e) {
e.printStackTrace();
LOG.error("SportsActivity", e);
}
}
TextView details = (TextView) findViewById(R.id.details);
details.setText(summaryDatastr.toString());
return list;
}
private String getStringResourceByName(String aString) {
String packageName = getPackageName();
int resId = getResources().getIdentifier(aString, "string", packageName);
if (resId==0){
LOG.warn("SportsActivity " + "Missing string in strings:" + aString);
return aString;
}else{
return getString(resId);
}
}
private int getSteps(int tsStart, int tsEnd) {
try (DBHandler handler = GBApplication.acquireDB()) {

View File

@ -72,12 +72,9 @@ public class ActivitySummariesAdapter extends AbstractItemAdapter<BaseActivitySu
Date startTime = item.getStartTime();
Long duration = (item.getEndTime().getTime() - item.getStartTime().getTime());
String durationhms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(duration),
TimeUnit.MILLISECONDS.toMinutes(duration) % TimeUnit.HOURS.toMinutes(1),
TimeUnit.MILLISECONDS.toSeconds(duration) % TimeUnit.MINUTES.toSeconds(1));
if (startTime != null) {
return DateTimeUtils.formatDateTime(startTime) + " (" + durationhms + ")";
return DateTimeUtils.formatDateTime(startTime) + " (" + DateTimeUtils.formatDurationHoursMinutes(duration, TimeUnit.MILLISECONDS) + ")";
}

View File

@ -246,13 +246,13 @@ public class FetchSportsSummaryOperation extends AbstractFetchOperation {
"\nlaps=" + laps +
""
);
addSummaryData("averageStrokeDistance", averageStrokeDistance, "m");
addSummaryData("averageStrokesPerSecond", averageStrokesPerSecond, "stroke/s");
addSummaryData("averageLapPace", averageLapPace, "s");
addSummaryData("strokes", strokes, "");
addSummaryData("swolfIndex", swolfIndex, "");
addSummaryData("swimStyle", swimStyle, "");
addSummaryData("laps", laps, "");
addSummaryData("averageStrokeDistance", averageStrokeDistance, "meter", "Strokes");
addSummaryData("averageStrokesPerSecond", averageStrokesPerSecond, "strokes_second","Strokes");
addSummaryData("averageLapPace", averageLapPace, "second", "Laps");
addSummaryData("strokes", strokes, "strokes", "Strokes");
addSummaryData("swolfIndex", swolfIndex, "swolf_index", "Swimming");
addSummaryData("swimStyle", swimStyle, "swim_style", "Swimming");
addSummaryData("laps", laps, "laps", "Laps");
} else {
// 28 bytes
@ -269,6 +269,9 @@ public class FetchSportsSummaryOperation extends AbstractFetchOperation {
"\nflatSeconds=" + flatSeconds +
""
);
addSummaryData("ascentSeconds", ascentSeconds, "seconds", "Elevation");
addSummaryData("descentSeconds", descentSeconds, "seconds", "Elevation");
addSummaryData("flatSeconds", flatSeconds, "seconds", "Elevation");
}
short averageHR = buffer.getShort();
@ -323,60 +326,39 @@ public class FetchSportsSummaryOperation extends AbstractFetchOperation {
// summary.setAveragePace(BLETypeConversions.toUnsigned(averagePace);
// summary.setAverageStride(BLETypeConversions.toUnsigned(averageStride);
addSummaryData("distanceMeters", distanceMeters, "m");
addSummaryData("ascentMeters", ascentMeters,"m");
addSummaryData("descentMeters", descentMeters,"m");
addSummaryData("maxAltitude", maxAltitude,"m");
addSummaryData("minAltitude", minAltitude,"m");
addSummaryData("steps", steps,"");
addSummaryData("activeSeconds", activeSeconds, "s");
addSummaryData("caloriesBurnt", caloriesBurnt, "kcal");
addSummaryData("maxSpeed", maxSpeed, "m/s");
addSummaryData("minPace", minPace, "");
addSummaryData("maxPace", maxPace, "");
addSummaryData("totalStride", totalStride, "");
addSummaryData("averageHR", averageHR, "bpm");
addSummaryData("averageKMPaceSeconds", averageKMPaceSeconds, "");
addSummaryData("averageStride", averageStride, "cm");
addSummaryData("distanceMeters", distanceMeters, "meters", "Activity");
addSummaryData("ascentMeters", ascentMeters,"meters", "Elevation");
addSummaryData("descentMeters", descentMeters,"meters", "Elevation");
addSummaryData("maxAltitude", maxAltitude,"meters", "Elevation");
addSummaryData("minAltitude", minAltitude,"meters", "Elevation");
addSummaryData("steps", steps,"steps_unit", "Activity");
addSummaryData("activeSeconds", activeSeconds, "seconds", "Activity");
addSummaryData("caloriesBurnt", caloriesBurnt, "calories_unit", "Activity");
addSummaryData("maxSpeed", maxSpeed, "meters_second", "Speed");
addSummaryData("minPace", minPace, "seconds_m", "Speed");
addSummaryData("maxPace", maxPace, "seconds_m", "Speed");
addSummaryData("totalStride", totalStride, "meters", "Activity");
addSummaryData("averageHR", averageHR, "bpm", "Activity");
addSummaryData("averageKMPaceSeconds", averageKMPaceSeconds, "seconds_km", "Speed");
addSummaryData("averageStride", averageStride, "cm", "Speed");
summary.setSummaryData(summaryData.toString());
return summary;
}
private void addSummaryData(String key, float value, String unit) {
private void addSummaryData(String key, float value, String unit, String group) {
if (value> 0) {
try {
JSONObject innerData= new JSONObject();
innerData.put("value", value);
innerData.put("unit", unit);
summaryData.put(key, innerData);
} catch (JSONException ignore) {
}
}
}
private void addSummaryData(String key, short value, String unit) {
if (value> 0) {
try {
JSONObject innerData= new JSONObject();
innerData.put("value", value);
innerData.put("unit", unit);
innerData.put("group", group);
summaryData.put(key, innerData);
} catch (JSONException ignore) {
}
}
}
private void addSummaryData(String key, int value, String unit) {
if (value> 0) {
try {
JSONObject innerData= new JSONObject();
innerData.put("value", value);
innerData.put("unit", unit);
summaryData.put(key, innerData);
} catch (JSONException ignore) {
}
}
}
@Override
protected String getLastSyncTimeKey() {

View File

@ -43,7 +43,7 @@
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:text="Start:" />
android:text="@string/activity_detail_start_label" />
<TextView
android:id="@+id/starttime"
@ -66,7 +66,7 @@
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:text="End:" />
android:text="@string/activity_detail_end_label" />
<TextView
android:id="@+id/endtime"
@ -91,7 +91,7 @@
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:text="Duration:" />
android:text="@string/activity_detail_duration_label" />
<TextView
android:id="@+id/duration"
@ -123,7 +123,7 @@
android:id="@+id/showTrack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show GPS Track" />
android:text="@string/activity_detail_show_gps_label" />
</LinearLayout>

View File

@ -699,7 +699,8 @@
<string name="activity_type_walking">Walking</string>
<string name="activity_type_swimming">Swimming</string>
<string name="activity_type_unknown">Unknown activity</string>
<string name="activity_summaries">Activities</string>
<string name="activity_summaries">Sport Activities</string>
<string name="activity_summary_detail">Sport Activity Detail</string>
<string name="activity_type_biking">Biking</string>
<string name="activity_type_treadmill">Treadmill</string>
<string name="activity_type_exercise">Exercise</string>
@ -903,6 +904,69 @@
<string name="ignore_bonded_devices">Ignore bonded devices</string>
<string name="ignore_bonded_devices_description">Enabling this option will ignore devices that have been bonded/paired already when scanning</string>
<string name="error_location_enabled_mandatory">Location must be turned on to scan for devices</string>
<!-- activity summary labels-->
<string name="distanceMeters">Distance</string>
<string name="ascentMeters">Uphill</string>
<string name="descentMeters">Downhill</string>
<string name="maxAltitude">Maximum</string>
<string name="minAltitude">Minimum</string>
<string name="steps">Steps</string>
<string name="activeSeconds">Active</string>
<string name="caloriesBurnt">Calories</string>
<string name="maxSpeed">Maximum</string>
<string name="minPace">Slowest Pace</string>
<string name="maxPace">Fastest Pace</string>
<string name="totalStride">Total stride</string>
<string name="averageHR">Heartrate</string>
<string name="averageKMPaceSeconds">Pace</string>
<string name="averageStride">Average Stride</string>
<string name="averageStrokeDistance">Average Stroke Distance</string>
<string name="averageStrokesPerSecond">Average Strokes</string>
<string name="averageLapPace">Average Lap Pace</string>
<string name="strokes">Strokes</string>
<string name="swolfIndex">swolfIndex</string>
<string name="swimStyle">swimStyle</string>
<string name="laps">Laps</string>
<string name="ascentSeconds">Ascenting</string>
<string name="descentSeconds">Descenting</string>
<string name="flatSeconds">Flat</string>
<!-- activity summary units-->
<string name="meters">m</string>
<string name="cm">cm</string>
<string name="steps_unit">steps</string>
<string name="meters_second">m/s</string>
<string name="km_h">km/h</string>
<string name="strokes_second">str/s</string>
<string name="strokes_unit">str</string>
<string name="seconds">sec</string>
<string name="swolf_index">swolf index</string>
<string name="swim_style">swim style</string>
<string name="laps_unit">laps</string>
<string name="calories_unit">Kcal</string>
<string name="seconds_km">sec/km</string>
<string name="minutes_km">min/km</string>
<string name="bpm">bpm</string>
<!-- activity summary groups-->
<string name="Strokes">Strokes</string>
<string name="Swimming">Swimming</string>
<string name="Distance">Distance</string>
<string name="Elevation">Elevation</string>
<string name="Speed">Speed</string>
<string name="Activity">Activity</string>
<string name="Steps">Steps</string>
<!-- Sports Activity Detail -->
<string name="activity_detail_start_label">Start:</string>
<string name="activity_detail_end_label">End:</string>
<string name="activity_detail_duration_label">Duration:</string>
<string name="activity_detail_show_gps_label">Show GPS Track</string>
<plurals name="widget_alarm_target_hours">
<item quantity="one">%d hour</item>