1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-13 11:17:33 +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" /> android:windowSoftInputMode="stateHidden" />
<activity <activity
android:name=".activities.ActivitySummaryDetail" android:name=".activities.ActivitySummaryDetail"
android:label="Activity Summary Detail" android:label="@string/activity_summary_detail"
android:screenOrientation="portrait" android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" /> android:windowSoftInputMode="stateHidden" />

View File

@ -134,7 +134,7 @@ public class ActivitySummariesActivity extends AbstractListActivity<BaseActivity
if (item != null) { if (item != null) {
ActivitySummary summary = (ActivitySummary) item; ActivitySummary summary = (ActivitySummary) item;
try { try {
showDetail(summary); showActivityDetail(summary);
} catch (Exception e) { } 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); 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(); refresh();
} }
private void showDetail(ActivitySummary summary){ private void showActivityDetail(ActivitySummary summary){
Intent ActivitySummaryDetailIntent = new Intent(this, ActivitySummaryDetail.class); 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); ActivitySummaryDetailIntent.putExtra(GBDevice.EXTRA_DEVICE, mGBDevice);
startActivity(ActivitySummaryDetailIntent); startActivity(ActivitySummaryDetailIntent);
} }

View File

@ -25,6 +25,7 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -32,6 +33,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.concurrent.TimeUnit; 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.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts; import nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; 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.model.DailyTotals;
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils; import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
@ -57,14 +57,10 @@ public class ActivitySummaryDetail extends AbstractGBActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_summary_details); setContentView(R.layout.activity_summary_details);
Intent intent = getIntent(); Intent intent = getIntent();
mGBDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE); mGBDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
ActivitySummary summary = (ActivitySummary) intent.getSerializableExtra("summary"); final String gpxTrack = intent.getStringExtra("GpxTrack");
final String gpxTrack = summary.getGpxTrack();
Button show_track_btn = (Button) findViewById(R.id.showTrack); Button show_track_btn = (Button) findViewById(R.id.showTrack);
show_track_btn.setVisibility(View.GONE); show_track_btn.setVisibility(View.GONE);
@ -81,66 +77,135 @@ public class ActivitySummaryDetail extends AbstractGBActivity {
}); });
} }
LOG.debug("petr summary: " + summary + "," + summary.getSummaryData()); String activitykindname = ActivityKind.asString(intent.getIntExtra("ActivityKind",0), getApplicationContext());
String activitykind = ActivityKind.asString(summary.getActivityKind(), getApplicationContext()); Date starttime = (Date) intent.getSerializableExtra("StartTime");
Date endtime = (Date) intent.getSerializableExtra("EndTime");
String starttime = DateTimeUtils.formatDateTime(summary.getStartTime()); String starttimeS = DateTimeUtils.formatDateTime(starttime);
String endtime = DateTimeUtils.formatDateTime(summary.getEndTime()); String endtimeS = DateTimeUtils.formatDateTime(endtime);
long startTs = summary.getStartTime().getTime() / 1000; long startTs = starttime.getTime() / 1000;
long endTs = summary.getEndTime().getTime() / 1000; long endTs = endtime.getTime() / 1000;
long durationms = (summary.getEndTime().getTime() - summary.getStartTime().getTime()); String durationhms = DateTimeUtils.formatDurationHoursMinutes((endtime.getTime() - starttime.getTime()), TimeUnit.MILLISECONDS);
String durationhms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(durationms), //int steps = getSteps((int) startTs, (int) endTs);
TimeUnit.MILLISECONDS.toMinutes(durationms) % TimeUnit.HOURS.toMinutes(1), //unused now, as we use the more extensive summaryData
TimeUnit.MILLISECONDS.toSeconds(durationms) % TimeUnit.MINUTES.toSeconds(1));
int steps = getSteps((int) startTs, (int) endTs);
ImageView activity_icon = (ImageView) findViewById(R.id.item_image); 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); TextView activity_kind = (TextView) findViewById(R.id.activitykind);
activity_kind.setText(activitykind); activity_kind.setText(activitykindname);
TextView start_time = (TextView) findViewById(R.id.starttime); TextView start_time = (TextView) findViewById(R.id.starttime);
start_time.setText(starttime); start_time.setText(starttimeS);
TextView end_time = (TextView) findViewById(R.id.endtime); TextView end_time = (TextView) findViewById(R.id.endtime);
end_time.setText(endtime); end_time.setText(endtimeS);
TextView activity_duration = (TextView) findViewById(R.id.duration); TextView activity_duration = (TextView) findViewById(R.id.duration);
activity_duration.setText(durationhms); activity_duration.setText(durationhms);
JSONObject summaryData = null; JSONObject summaryData = null;
String sumData = summary.getSummaryData(); String sumData = intent.getStringExtra("SummaryData");
if (sumData != null) { if (sumData != null) {
try { try {
summaryData = new JSONObject(sumData); summaryData = new JSONObject(sumData);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); LOG.error("SportsActivity", e);
} }
} }
StringBuilder summaryDatastr = new StringBuilder();
if (summaryData == null) return; if (summaryData == null) return;
Iterator<String> keys = summaryData.keys(); JSONObject listOfSummaries = makeSummaryList(summaryData);
LOG.debug("petr summary JSON:" + summaryData + keys); 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()) { while (keys.hasNext()) {
String key = keys.next(); String key = keys.next();
LOG.debug("petr key:" + key);
try { try {
LOG.debug("petr" + key + ": " + summaryData.get(key) + "\n"); LOG.error("SportsActivity:" + key + ": " + summaryData.get(key) + "\n");
DecimalFormat df = new DecimalFormat("#.##");
JSONObject innerData = (JSONObject) summaryData.get(key); JSONObject innerData = (JSONObject) summaryData.get(key);
Object value = innerData.get("value"); Object value = innerData.get("value");
String unit = innerData.getString("unit"); String unit = innerData.getString("unit");
summaryDatastr.append(String.format("%s: %s %s\n", key, df.format(value), unit)); String group = innerData.getString("group");
} catch (JSONException e) {
e.printStackTrace(); if (!list.has(group)) {
} list.put(group,new JSONArray());
}
TextView details = (TextView) findViewById(R.id.details);
details.setText(summaryDatastr.toString());
} }
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) {
LOG.error("SportsActivity", e);
}
}
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) { private int getSteps(int tsStart, int tsEnd) {
try (DBHandler handler = GBApplication.acquireDB()) { try (DBHandler handler = GBApplication.acquireDB()) {

View File

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

View File

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

View File

@ -699,7 +699,8 @@
<string name="activity_type_walking">Walking</string> <string name="activity_type_walking">Walking</string>
<string name="activity_type_swimming">Swimming</string> <string name="activity_type_swimming">Swimming</string>
<string name="activity_type_unknown">Unknown activity</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_biking">Biking</string>
<string name="activity_type_treadmill">Treadmill</string> <string name="activity_type_treadmill">Treadmill</string>
<string name="activity_type_exercise">Exercise</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">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="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> <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"> <plurals name="widget_alarm_target_hours">
<item quantity="one">%d hour</item> <item quantity="one">%d hour</item>