second try: moved metric/imperial conversion to its own helper class
This commit is contained in:
parent
9e3f2b6e67
commit
69d902eb49
6 changed files with 157 additions and 123 deletions
|
@ -20,6 +20,11 @@ android {
|
|||
shrinkResources true
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
debug {
|
||||
// minifyEnabled true
|
||||
// shrinkResources true
|
||||
// proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -35,6 +35,7 @@ import android.support.v4.app.FragmentActivity;
|
|||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
|
@ -55,6 +56,8 @@ import org.y20k.trackbook.core.Track;
|
|||
import org.y20k.trackbook.helpers.DialogHelper;
|
||||
import org.y20k.trackbook.helpers.DropdownAdapter;
|
||||
import org.y20k.trackbook.helpers.ExportHelper;
|
||||
import org.y20k.trackbook.helpers.LengthUnitHelper;
|
||||
import org.y20k.trackbook.helpers.LocationHelper;
|
||||
import org.y20k.trackbook.helpers.LogHelper;
|
||||
import org.y20k.trackbook.helpers.MapHelper;
|
||||
import org.y20k.trackbook.helpers.StorageHelper;
|
||||
|
@ -85,6 +88,7 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
|
|||
private ConstraintLayout mTrackManagementLayout;
|
||||
private Spinner mDropdown;
|
||||
private View mStatisticsSheet;
|
||||
private View mStatisticsView;
|
||||
private TextView mDistanceView;
|
||||
private TextView mStepsView;
|
||||
private TextView mWaypointsView;
|
||||
|
@ -206,7 +210,7 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
|
|||
deleteButton.setOnClickListener(getDeleteButtonListener());
|
||||
|
||||
// get views for statistics sheet
|
||||
View statisticsView = mRootView.findViewById(R.id.statistics_view);
|
||||
mStatisticsView = mRootView.findViewById(R.id.statistics_view);
|
||||
mStatisticsSheet = mRootView.findViewById(R.id.statistics_sheet);
|
||||
mDistanceView = (TextView) mRootView.findViewById(R.id.statistics_data_distance);
|
||||
mStepsView = (TextView) mRootView.findViewById(R.id.statistics_data_steps);
|
||||
|
@ -220,8 +224,6 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
|
|||
mNegativeElevationView = (TextView) mRootView.findViewById(R.id.statistics_data_negative_elevation);
|
||||
mElevationDataViews = (Group) mRootView.findViewById(R.id.elevation_data);
|
||||
|
||||
// attach listners for taps on elevation views
|
||||
attachTapListeners();
|
||||
|
||||
// display map and statistics
|
||||
if (savedInstanceState != null) {
|
||||
|
@ -241,7 +243,7 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
|
|||
mStatisticsSheetBehavior = BottomSheetBehavior.from(mStatisticsSheet);
|
||||
mStatisticsSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
|
||||
mStatisticsSheetBehavior.setBottomSheetCallback(getStatisticsSheetCallback());
|
||||
statisticsView.setOnClickListener(new View.OnClickListener() {
|
||||
mStatisticsView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if (mStatisticsSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
|
||||
|
@ -252,6 +254,12 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
|
|||
}
|
||||
});
|
||||
|
||||
// attach listener for taps on elevation views
|
||||
attachTapListenerToElevationViews();
|
||||
|
||||
// // attach listener for taps on statistics // TODO uncomment
|
||||
// attachTapListenerToStatisticsSheet();
|
||||
|
||||
return mRootView;
|
||||
}
|
||||
|
||||
|
@ -369,17 +377,14 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
|
|||
stepsTaken = String.valueOf(Math.round(mTrack.getStepCount()));
|
||||
}
|
||||
|
||||
// populate views
|
||||
mDistanceView.setText(mTrack.getTrackDistanceString());
|
||||
// populate length views
|
||||
displayCurrentLengthUnits();
|
||||
// populate other views
|
||||
mStepsView.setText(stepsTaken);
|
||||
mWaypointsView.setText(String.valueOf(mTrack.getWayPoints().size()));
|
||||
mDurationView.setText(mTrack.getTrackDurationString());
|
||||
mDurationView.setText(LocationHelper.convertToReadableTime(mTrack.getTrackDuration(), true));
|
||||
mRecordingStartView.setText(recordingStart);
|
||||
mRecordingStopView.setText(recordingStop);
|
||||
mPositiveElevationView.setText(mTrack.getPositiveElevationString());
|
||||
mNegativeElevationView.setText(mTrack.getNegativeElevationString());
|
||||
mMaxAltitudeView.setText(mTrack.getMaxAltitudeString());
|
||||
mMinAltitudeView.setText(mTrack.getMinAltitudeString());
|
||||
|
||||
// show/hide elevation views depending on file format version
|
||||
if (mTrack.getTrackFormatVersion() > 1 && mTrack.getMinAltitude() > 0) {
|
||||
|
@ -431,6 +436,27 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
|
|||
}
|
||||
|
||||
|
||||
/* Displays views in statistic sheet according to current locale */
|
||||
private void displayCurrentLengthUnits() {
|
||||
mDistanceView.setText(LengthUnitHelper.convertDistanceToString(mTrack.getTrackDistance()));
|
||||
mPositiveElevationView.setText(LengthUnitHelper.convertDistanceToString(mTrack.getPositiveElevation()));
|
||||
mNegativeElevationView.setText(LengthUnitHelper.convertDistanceToString(mTrack.getNegativeElevation()));
|
||||
mMaxAltitudeView.setText(LengthUnitHelper.convertDistanceToString(mTrack.getMaxAltitude()));
|
||||
mMinAltitudeView.setText(LengthUnitHelper.convertDistanceToString(mTrack.getMinAltitude()));
|
||||
}
|
||||
|
||||
|
||||
/* Switches views in statistic sheet between Metric and Imperial */
|
||||
private void displayOppositeLengthUnits() {
|
||||
int oppositeLengthUnit = LengthUnitHelper.getOppositeUnitSystem();
|
||||
mDistanceView.setText(LengthUnitHelper.convertDistanceToString(mTrack.getTrackDistance(), oppositeLengthUnit));
|
||||
mPositiveElevationView.setText(LengthUnitHelper.convertDistanceToString(mTrack.getPositiveElevation(), oppositeLengthUnit));
|
||||
mNegativeElevationView.setText(LengthUnitHelper.convertDistanceToString(mTrack.getNegativeElevation(), oppositeLengthUnit));
|
||||
mMaxAltitudeView.setText(LengthUnitHelper.convertDistanceToString(mTrack.getMaxAltitude(), oppositeLengthUnit));
|
||||
mMinAltitudeView.setText(LengthUnitHelper.convertDistanceToString(mTrack.getMinAltitude(), oppositeLengthUnit));
|
||||
}
|
||||
|
||||
|
||||
/* Deletes currently visible track */
|
||||
private void deleteCurrentTrack() {
|
||||
|
||||
|
@ -511,7 +537,7 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
|
|||
int dialogNegativeButton = R.string.dialog_default_action_cancel;
|
||||
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
|
||||
String recordingStartDate = df.format(mTrack.getRecordingStart());
|
||||
String dialogMessage = getString(R.string.dialog_delete_content) + " " + recordingStartDate + " | " + mTrack.getTrackDistanceString();
|
||||
String dialogMessage = getString(R.string.dialog_delete_content) + " " + recordingStartDate + " | " + LengthUnitHelper.convertDistanceToString(mTrack.getTrackDistance());
|
||||
|
||||
// show delete dialog - results are handles by onActivityResult
|
||||
DialogFragment dialogFragment = DialogHelper.newInstance(dialogTitle, dialogMessage, dialogPositiveButton, dialogNegativeButton);
|
||||
|
@ -539,13 +565,13 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
|
|||
if (ExportHelper.gpxFileExists(mTrack)) {
|
||||
// CASE: OVERWRITE - GPX file exists
|
||||
dialogTitle = R.string.dialog_export_title_overwrite;
|
||||
dialogMessage = getString(R.string.dialog_export_content_overwrite) + " (" + recordingStartDate + " | " + mTrack.getTrackDistanceString() + ")";
|
||||
dialogMessage = getString(R.string.dialog_export_content_overwrite) + " (" + recordingStartDate + " | " + LengthUnitHelper.convertDistanceToString(mTrack.getTrackDistance()) + ")";
|
||||
dialogPositiveButton = R.string.dialog_export_action_overwrite;
|
||||
dialogNegativeButton = R.string.dialog_default_action_cancel;
|
||||
} else {
|
||||
// CASE: EXPORT - GPX file does NOT yet exits
|
||||
dialogTitle = R.string.dialog_export_title_export;
|
||||
dialogMessage = getString(R.string.dialog_export_content_export) + " (" + recordingStartDate + " | " + mTrack.getTrackDistanceString() + ")";
|
||||
dialogMessage = getString(R.string.dialog_export_content_export) + " (" + recordingStartDate + " | " + LengthUnitHelper.convertDistanceToString(mTrack.getTrackDistance()) + ")";
|
||||
dialogPositiveButton = R.string.dialog_export_action_export;
|
||||
dialogNegativeButton = R.string.dialog_default_action_cancel;
|
||||
}
|
||||
|
@ -559,8 +585,8 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
|
|||
}
|
||||
|
||||
|
||||
/* Add tap listners to elevation data views */
|
||||
private void attachTapListeners() {
|
||||
/* Add tap listener to elevation data views */
|
||||
private void attachTapListenerToElevationViews() {
|
||||
int referencedIds[] = mElevationDataViews.getReferencedIds();
|
||||
for (int id : referencedIds) {
|
||||
mRootView.findViewById(id).setOnClickListener(new View.OnClickListener() {
|
||||
|
@ -574,6 +600,31 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O
|
|||
}
|
||||
|
||||
|
||||
/* Add tap listener to statistics sheet */
|
||||
private void attachTapListenerToStatisticsSheet() {
|
||||
mStatisticsView.setOnTouchListener(new View.OnTouchListener() {
|
||||
@Override
|
||||
public boolean onTouch(View v, MotionEvent event) {
|
||||
if(event.getAction() == MotionEvent.ACTION_DOWN){
|
||||
displayOppositeLengthUnits();
|
||||
return true;
|
||||
} else if (event.getAction() == MotionEvent.ACTION_UP) {
|
||||
displayCurrentLengthUnits();
|
||||
|
||||
if (mStatisticsSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
|
||||
mStatisticsSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
|
||||
} else {
|
||||
mStatisticsSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inner class: Loads track from external storage using AsyncTask
|
||||
*/
|
||||
|
|
|
@ -25,11 +25,9 @@ import org.y20k.trackbook.helpers.LocationHelper;
|
|||
import org.y20k.trackbook.helpers.TrackbookKeys;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -275,40 +273,9 @@ public class Track implements TrackbookKeys, Parcelable {
|
|||
}
|
||||
|
||||
|
||||
/* Getter for string representation of maximum altitude of recording */
|
||||
public String getMaxAltitudeString() {
|
||||
return convertDistanceToString(mMaxAltitude);
|
||||
}
|
||||
|
||||
|
||||
/* Getter for string representation of lowest altitude of recording */
|
||||
public String getMinAltitudeString() {
|
||||
return convertDistanceToString(mMinAltitude);
|
||||
}
|
||||
|
||||
|
||||
/* Getter for string representation of positive elevation of recording (cumulative altitude difference) */
|
||||
public String getPositiveElevationString() {
|
||||
return convertDistanceToString(mPositiveElevation);
|
||||
}
|
||||
|
||||
|
||||
/* Getter for string representation of negative elevation of recording (cumulative altitude difference) */
|
||||
public String getNegativeElevationString() {
|
||||
return convertDistanceToString(mNegativeElevation);
|
||||
}
|
||||
|
||||
|
||||
/* Getter for string representation of track duration */
|
||||
public String getTrackDurationString() {
|
||||
return LocationHelper.convertToReadableTime(mDuration, true);
|
||||
}
|
||||
|
||||
|
||||
/* Getter for string representation of track distance */
|
||||
public String getTrackDistanceString() {
|
||||
double trackDistance = (double) mWayPoints.get(mWayPoints.size()-1).getDistanceToStartingPoint();
|
||||
return convertDistanceToString(trackDistance);
|
||||
/* Getter recorded distance */
|
||||
public Double getTrackDistance() {
|
||||
return (double) mWayPoints.get(mWayPoints.size()-1).getDistanceToStartingPoint();
|
||||
}
|
||||
|
||||
|
||||
|
@ -318,53 +285,6 @@ public class Track implements TrackbookKeys, Parcelable {
|
|||
}
|
||||
|
||||
|
||||
// /* Adds distance to given location to length of track */
|
||||
// private float addDistanceToTrack(Location location) {
|
||||
// // get number of previously recorded WayPoints
|
||||
// int wayPointCount = mWayPoints.size();
|
||||
//
|
||||
// // at least two data points are needed
|
||||
// if (wayPointCount >= 1) {
|
||||
// // add up distance
|
||||
// Location lastLocation = mWayPoints.get(wayPointCount - 1).getLocation();
|
||||
// return mTrackLength + lastLocation.distanceTo(location);
|
||||
// }
|
||||
//
|
||||
// return 0f;
|
||||
// }
|
||||
|
||||
|
||||
/* Converts a given distance value to a readable string */
|
||||
private String convertDistanceToString(double distance) {
|
||||
// check for locale and set unit system accordingly
|
||||
String unit;
|
||||
if (getUnitSystem(Locale.getDefault()) == IMPERIAL) {
|
||||
// convert distance to feet
|
||||
distance = distance * 3.28084f;
|
||||
// set measurement unit
|
||||
unit = "ft";
|
||||
} else {
|
||||
// set measurement unit
|
||||
unit = "m";
|
||||
}
|
||||
return String.format (Locale.ENGLISH, "%.0f", distance) + unit;
|
||||
}
|
||||
|
||||
|
||||
/* Determines which unit system the device is using (metric or imperial) */
|
||||
private int getUnitSystem(Locale locale) {
|
||||
// America (US), Liberia (LR), Myanmar(MM) use the imperial system
|
||||
List<String> imperialSystemCountries = Arrays.asList("US", "LR", "MM");
|
||||
String countryCode = locale.getCountry();
|
||||
|
||||
if (imperialSystemCountries.contains(countryCode)){
|
||||
return IMPERIAL;
|
||||
} else {
|
||||
return METRIC;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
/**
|
||||
* LengthUnitHelper.java
|
||||
* Implements the LengthUnitHelper class
|
||||
* A LengthUnitHelper offers helper methods for dealing with unit systems and locales
|
||||
*
|
||||
* This file is part of
|
||||
* TRACKBOOK - Movement Recorder for Android
|
||||
*
|
||||
* Copyright (c) 2016-18 - Y20K.org
|
||||
* Licensed under the MIT-License
|
||||
* http://opensource.org/licenses/MIT
|
||||
*
|
||||
* Trackbook uses osmdroid - OpenStreetMap-Tools for Android
|
||||
* https://github.com/osmdroid/osmdroid
|
||||
*/
|
||||
|
||||
package org.y20k.trackbook.helpers;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
|
||||
/**
|
||||
* LengthUnitHelper class
|
||||
*/
|
||||
public final class LengthUnitHelper implements TrackbookKeys {
|
||||
|
||||
|
||||
/* Converts for the default locale a distance value to a readable string */
|
||||
public static String convertDistanceToString(double distance) {
|
||||
return convertDistanceToString(distance, getUnitSystem(Locale.getDefault()));
|
||||
}
|
||||
|
||||
|
||||
/* Converts for the given uni System a distance value to a readable string */
|
||||
public static String convertDistanceToString(double distance, int unitSystem) {
|
||||
// check for locale and set unit system accordingly
|
||||
String unit;
|
||||
if (unitSystem == IMPERIAL) {
|
||||
// convert distance to feet
|
||||
distance = distance * 3.28084f;
|
||||
// set measurement unit
|
||||
unit = "ft";
|
||||
} else {
|
||||
// set measurement unit
|
||||
unit = "m";
|
||||
}
|
||||
return String.format (Locale.ENGLISH, "%.0f", distance) + unit;
|
||||
}
|
||||
|
||||
|
||||
/* Determines which unit system the device is using (metric or imperial) */
|
||||
private static int getUnitSystem(Locale locale) {
|
||||
// America (US), Liberia (LR), Myanmar(MM) use the imperial system
|
||||
List<String> imperialSystemCountries = Arrays.asList("US", "LR", "MM");
|
||||
String countryCode = locale.getCountry();
|
||||
|
||||
if (imperialSystemCountries.contains(countryCode)){
|
||||
return IMPERIAL;
|
||||
} else {
|
||||
return METRIC;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Returns the opposite unit system based on the current locale */
|
||||
public static int getOppositeUnitSystem() {
|
||||
int unitSystem = getUnitSystem(Locale.getDefault());
|
||||
if (unitSystem == METRIC){
|
||||
return IMPERIAL;
|
||||
} else {
|
||||
return METRIC;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -163,8 +163,8 @@ public final class NotificationHelper implements TrackbookKeys {
|
|||
|
||||
/* Build context text for notification builder */
|
||||
private static String getContextString(Context context, Track track) {
|
||||
return context.getString(R.string.notification_content_distance) + ": " + track.getTrackDistanceString() + " | " +
|
||||
context.getString(R.string.notification_content_duration) + ": " + track.getTrackDurationString();
|
||||
return context.getString(R.string.notification_content_distance) + ": " + LengthUnitHelper.convertDistanceToString(track.getTrackDistance()) + " | " +
|
||||
context.getString(R.string.notification_content_duration) + ": " + LocationHelper.convertToReadableTime(track.getTrackDuration(), true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -22,7 +22,6 @@ import android.support.annotation.Nullable;
|
|||
import android.support.v4.os.EnvironmentCompat;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.gson.FieldNamingPolicy;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
|
@ -119,7 +118,7 @@ public class StorageHelper implements TrackbookKeys {
|
|||
File file = new File(mFolder.toString() + "/" + fileName);
|
||||
|
||||
// convert track to JSON
|
||||
Gson gson = getCustomGson(true);
|
||||
Gson gson = getCustomGson();
|
||||
String json = gson.toJson(track);
|
||||
|
||||
// write track
|
||||
|
@ -234,15 +233,8 @@ public class StorageHelper implements TrackbookKeys {
|
|||
}
|
||||
fileContent = sb.toString();
|
||||
|
||||
// identify for ugly JSON files
|
||||
boolean pretty = true;
|
||||
if (fileContent.startsWith("{\"b\"")) {
|
||||
LogHelper.w(LOG_TAG, "Trackbook file is not formatted correctly."); // todo remove
|
||||
pretty = false;
|
||||
}
|
||||
|
||||
// prepare custom Gson and return Track object
|
||||
Gson gson = getCustomGson(pretty);
|
||||
Gson gson = getCustomGson();
|
||||
return gson.fromJson(fileContent, TrackBuilder.class).toTrack();
|
||||
|
||||
} catch (IOException e) {
|
||||
|
@ -253,13 +245,9 @@ public class StorageHelper implements TrackbookKeys {
|
|||
|
||||
|
||||
/* Creates a Gson object */
|
||||
private Gson getCustomGson(boolean pretty) {
|
||||
private Gson getCustomGson() {
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.setDateFormat("M/d/yy hh:mm a");
|
||||
if (pretty) {
|
||||
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY);
|
||||
gsonBuilder.setPrettyPrinting();
|
||||
}
|
||||
return gsonBuilder.create();
|
||||
}
|
||||
|
||||
|
@ -347,13 +335,6 @@ public class StorageHelper implements TrackbookKeys {
|
|||
}
|
||||
});
|
||||
|
||||
// log sorting result // TODO comment out for release
|
||||
// String fileList = "";
|
||||
// for (File file : files) {
|
||||
// fileList = fileList + file.getName() + "\n";
|
||||
// }
|
||||
// LogHelper.v(LOG_TAG, "+++ List of files +++\n" + fileList);
|
||||
|
||||
// hand back sorted array of files
|
||||
return files;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue