2020-08-07 09:21:14 +02:00
|
|
|
/* Copyright (C) 2015-2020 abettenburg, Andreas Shimokawa, Carsten Pfeiffer,
|
|
|
|
Daniele Gobbetti, Lem Dulfo
|
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2020-08-09 13:11:10 +02:00
|
|
|
import org.json.JSONArray;
|
2020-08-08 16:22:55 +02:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2020-08-07 09:21:14 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2020-08-08 16:22:55 +02:00
|
|
|
import java.text.DecimalFormat;
|
2020-08-09 13:11:10 +02:00
|
|
|
import java.util.Date;
|
2020-08-08 16:22:55 +02:00
|
|
|
import java.util.Iterator;
|
2020-08-07 09:21:14 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ActivityAnalysis;
|
|
|
|
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.DailyTotals;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
|
|
|
|
|
|
|
public class ActivitySummaryDetail extends AbstractGBActivity {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ActivitySummaryDetail.class);
|
|
|
|
private GBDevice mGBDevice;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_summary_details);
|
|
|
|
Intent intent = getIntent();
|
2020-08-09 01:40:59 +02:00
|
|
|
mGBDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
|
2020-08-07 09:21:14 +02:00
|
|
|
|
2020-08-09 13:11:10 +02:00
|
|
|
final String gpxTrack = intent.getStringExtra("GpxTrack");
|
2020-08-07 09:21:14 +02:00
|
|
|
Button show_track_btn = (Button) findViewById(R.id.showTrack);
|
|
|
|
show_track_btn.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
if (gpxTrack != null) {
|
|
|
|
show_track_btn.setVisibility(View.VISIBLE);
|
|
|
|
show_track_btn.setOnClickListener(new View.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
|
|
|
try {
|
2020-08-09 01:40:59 +02:00
|
|
|
AndroidUtils.viewFile(gpxTrack, Intent.ACTION_VIEW, ActivitySummaryDetail.this);
|
2020-08-07 09:21:14 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(getApplicationContext(), "Unable to display GPX track: " + e.getMessage(), Toast.LENGTH_LONG, GB.ERROR, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-08-08 16:22:55 +02:00
|
|
|
|
2020-08-09 13:11:10 +02:00
|
|
|
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
|
2020-08-07 09:21:14 +02:00
|
|
|
|
|
|
|
ImageView activity_icon = (ImageView) findViewById(R.id.item_image);
|
2020-08-09 13:11:10 +02:00
|
|
|
activity_icon.setImageResource(ActivityKind.getIconId(intent.getIntExtra("ActivityKind",0)));
|
2020-08-07 09:21:14 +02:00
|
|
|
TextView activity_kind = (TextView) findViewById(R.id.activitykind);
|
2020-08-09 13:11:10 +02:00
|
|
|
activity_kind.setText(activitykindname);
|
2020-08-07 09:21:14 +02:00
|
|
|
TextView start_time = (TextView) findViewById(R.id.starttime);
|
2020-08-09 13:11:10 +02:00
|
|
|
start_time.setText(starttimeS);
|
2020-08-07 09:21:14 +02:00
|
|
|
TextView end_time = (TextView) findViewById(R.id.endtime);
|
2020-08-09 13:11:10 +02:00
|
|
|
end_time.setText(endtimeS);
|
2020-08-07 09:21:14 +02:00
|
|
|
TextView activity_duration = (TextView) findViewById(R.id.duration);
|
|
|
|
activity_duration.setText(durationhms);
|
2020-08-08 16:22:55 +02:00
|
|
|
|
|
|
|
JSONObject summaryData = null;
|
2020-08-09 13:11:10 +02:00
|
|
|
String sumData = intent.getStringExtra("SummaryData");
|
2020-08-09 01:40:59 +02:00
|
|
|
if (sumData != null) {
|
|
|
|
try {
|
|
|
|
summaryData = new JSONObject(sumData);
|
|
|
|
} catch (JSONException e) {
|
2020-08-09 13:11:10 +02:00
|
|
|
LOG.error("SportsActivity", e);
|
2020-08-08 16:22:55 +02:00
|
|
|
}
|
2020-08-09 01:40:59 +02:00
|
|
|
}
|
2020-08-08 16:22:55 +02:00
|
|
|
|
2020-08-09 01:40:59 +02:00
|
|
|
if (summaryData == null) return;
|
2020-08-08 16:22:55 +02:00
|
|
|
|
2020-08-09 13:11:10 +02:00
|
|
|
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();
|
2020-08-08 16:22:55 +02:00
|
|
|
Iterator<String> keys = summaryData.keys();
|
2020-08-09 13:11:10 +02:00
|
|
|
LOG.error("SportsActivity JSON:" + summaryData + keys);
|
2020-08-08 16:22:55 +02:00
|
|
|
|
2020-08-09 01:40:59 +02:00
|
|
|
while (keys.hasNext()) {
|
2020-08-08 16:22:55 +02:00
|
|
|
String key = keys.next();
|
|
|
|
|
|
|
|
try {
|
2020-08-09 13:11:10 +02:00
|
|
|
LOG.error("SportsActivity:" + key + ": " + summaryData.get(key) + "\n");
|
2020-08-08 16:22:55 +02:00
|
|
|
JSONObject innerData = (JSONObject) summaryData.get(key);
|
|
|
|
Object value = innerData.get("value");
|
|
|
|
String unit = innerData.getString("unit");
|
2020-08-09 13:11:10 +02:00
|
|
|
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);
|
2020-08-08 16:22:55 +02:00
|
|
|
} catch (JSONException e) {
|
2020-08-09 13:11:10 +02:00
|
|
|
LOG.error("SportsActivity", e);
|
2020-08-08 16:22:55 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-09 13:11:10 +02:00
|
|
|
return list;
|
2020-08-07 09:21:14 +02:00
|
|
|
}
|
|
|
|
|
2020-08-09 13:11:10 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2020-08-07 09:21:14 +02:00
|
|
|
|
|
|
|
private int getSteps(int tsStart, int tsEnd) {
|
|
|
|
try (DBHandler handler = GBApplication.acquireDB()) {
|
|
|
|
DailyTotals dt = new DailyTotals();
|
|
|
|
ActivityAnalysis analysis = new ActivityAnalysis();
|
|
|
|
ActivityAmounts amountsSteps;
|
|
|
|
amountsSteps = analysis.calculateActivityAmounts(dt.getSamples(handler, mGBDevice, tsStart, tsEnd));
|
|
|
|
return (int) dt.getTotalsStepsForActivityAmounts(amountsSteps);
|
|
|
|
} catch (Exception e) {
|
|
|
|
GB.toast("Error loading activity steps.", Toast.LENGTH_SHORT, GB.ERROR, e);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|