trackbook displays distance in feet on US devices. It now also tracks steps in the background

This commit is contained in:
y20k 2016-09-15 13:38:51 +02:00
parent d47fcb6ab3
commit d0359a8400
5 changed files with 96 additions and 14 deletions

View file

@ -8,8 +8,8 @@ android {
applicationId "org.y20k.trackbook" applicationId "org.y20k.trackbook"
minSdkVersion 22 minSdkVersion 22
targetSdkVersion 24 targetSdkVersion 24
versionCode 2 versionCode 3
versionName "0.9.1 (The Great Gig in the Sky)" versionName "0.9.2 (The Great Gig in the Sky)"
vectorDrawables.useSupportLibrary = true vectorDrawables.useSupportLibrary = true
} }
buildTypes { buildTypes {
@ -23,7 +23,7 @@ android {
dependencies { dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0' compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.0' compile 'com.android.support:design:24.2.1'
compile 'org.osmdroid:osmdroid-android:5.2@aar' compile 'org.osmdroid:osmdroid-android:5.2@aar'
} }

View file

@ -19,6 +19,10 @@ package org.y20k.trackbook;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location; import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
import android.location.LocationManager; import android.location.LocationManager;
@ -41,7 +45,7 @@ import java.util.List;
/** /**
* TrackerService class * TrackerService class
*/ */
public class TrackerService extends Service implements TrackbookKeys { public class TrackerService extends Service implements TrackbookKeys, SensorEventListener {
/* Define log tag */ /* Define log tag */
private static final String LOG_TAG = TrackerService.class.getSimpleName(); private static final String LOG_TAG = TrackerService.class.getSimpleName();
@ -51,6 +55,8 @@ public class TrackerService extends Service implements TrackbookKeys {
private Track mTrack; private Track mTrack;
private CountDownTimer mTimer; private CountDownTimer mTimer;
private LocationManager mLocationManager; private LocationManager mLocationManager;
private SensorManager mSensorManager;
private float mStepCountOffset;
private LocationListener mGPSListener = null; private LocationListener mGPSListener = null;
private LocationListener mNetworkListener = null; private LocationListener mNetworkListener = null;
private Location mCurrentBestLocation; private Location mCurrentBestLocation;
@ -61,6 +67,9 @@ public class TrackerService extends Service implements TrackbookKeys {
// acquire reference to Location Manager // acquire reference to Location Manager
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// acquire reference to Sensor Manager
mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
// checking for empty intent // checking for empty intent
if (intent == null) { if (intent == null) {
LogHelper.v(LOG_TAG, "Null-Intent received. Stopping self."); LogHelper.v(LOG_TAG, "Null-Intent received. Stopping self.");
@ -91,7 +100,7 @@ public class TrackerService extends Service implements TrackbookKeys {
@Override @Override
public void onTick(long millisUntilFinished) { public void onTick(long millisUntilFinished) {
// update track duration // update track duration
mTrack.setTrackDuration(EIGHT_HOURS_IN_MILLISECONDS - millisUntilFinished); mTrack.setDuration(EIGHT_HOURS_IN_MILLISECONDS - millisUntilFinished);
// try to add WayPoint to Track // try to add WayPoint to Track
addWayPointToTrack(); addWayPointToTrack();
// update notification // update notification
@ -106,11 +115,22 @@ public class TrackerService extends Service implements TrackbookKeys {
}; };
mTimer.start(); mTimer.start();
// initialize step counter
mStepCountOffset = 0;
Sensor stepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if (stepCounter != null) {
mSensorManager.registerListener(this, stepCounter, SensorManager.SENSOR_DELAY_UI);
} else {
LogHelper.v(LOG_TAG, "Pedometer Sensor not available");
mTrack.setStepCount(-1);
}
// put up notification // put up notification
NotificationHelper.show(this,mTrack); NotificationHelper.show(this,mTrack);
// create gps and network location listeners // create gps and network location listeners
startFindingLocation(); startFindingLocation();
} }
// ACTION STOP // ACTION STOP
@ -125,6 +145,7 @@ public class TrackerService extends Service implements TrackbookKeys {
// remove listeners // remove listeners
stopFindingLocation(); stopFindingLocation();
mSensorManager.unregisterListener(this);
} }
// START_STICKY is used for services that are explicitly started and stopped as needed // START_STICKY is used for services that are explicitly started and stopped as needed
@ -145,6 +166,7 @@ public class TrackerService extends Service implements TrackbookKeys {
// remove listeners // remove listeners
stopFindingLocation(); stopFindingLocation();
mSensorManager.unregisterListener(this);
// cancel notification // cancel notification
stopForeground(true); stopForeground(true);
@ -153,6 +175,27 @@ public class TrackerService extends Service implements TrackbookKeys {
} }
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
// save the step count offset / previously recorded steps
if (mStepCountOffset == 0) {
mStepCountOffset = sensorEvent.values[0] - 1;
}
// calculate step count
float stepCount = sensorEvent.values[0] - mStepCountOffset;
// set step count in track
mTrack.setStepCount(stepCount);
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
/* Adds a new WayPoint to current track */ /* Adds a new WayPoint to current track */
private void addWayPointToTrack() { private void addWayPointToTrack() {
@ -241,5 +284,4 @@ public class TrackerService extends Service implements TrackbookKeys {
} }
} }

View file

@ -25,6 +25,7 @@ import org.y20k.trackbook.helpers.LogHelper;
import org.y20k.trackbook.helpers.TrackbookKeys; import org.y20k.trackbook.helpers.TrackbookKeys;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -41,13 +42,17 @@ public class Track implements TrackbookKeys, Parcelable {
/* Main class variables */ /* Main class variables */
private final List<WayPoint> mWayPoints; private final List<WayPoint> mWayPoints;
private float mTrackLength; private float mTrackLength;
private long mTrackDuration; private long mDuration;
private float mStepCount;
private int mUnitSystem;
/* Constructor */ /* Constructor */
public Track() { public Track() {
mWayPoints = new ArrayList<WayPoint>(); mWayPoints = new ArrayList<WayPoint>();
mTrackLength = 0; mTrackLength = 0;
mStepCount = 0;
mUnitSystem = getUnitSystem(Locale.getDefault());
} }
@ -55,6 +60,8 @@ public class Track implements TrackbookKeys, Parcelable {
protected Track(Parcel in) { protected Track(Parcel in) {
mWayPoints = in.createTypedArrayList(WayPoint.CREATOR); mWayPoints = in.createTypedArrayList(WayPoint.CREATOR);
mTrackLength = in.readFloat(); mTrackLength = in.readFloat();
mStepCount = in.readFloat();
mUnitSystem = in.readInt();
} }
@ -102,11 +109,16 @@ public class Track implements TrackbookKeys, Parcelable {
/* Setter for duration of track */ /* Setter for duration of track */
public void setTrackDuration(long trackDuration) { public void setDuration(long duration) {
mTrackDuration = trackDuration; mDuration = duration;
} }
/* Setter for step count of track */
public void setStepCount(float stepCount) {
mStepCount = stepCount;
}
/* Getter for mWayPoints */ /* Getter for mWayPoints */
public List<WayPoint> getWayPoints() { public List<WayPoint> getWayPoints() {
return mWayPoints; return mWayPoints;
@ -121,15 +133,25 @@ public class Track implements TrackbookKeys, Parcelable {
/* Getter for duration of track */ /* Getter for duration of track */
public String getTrackDuration() { public String getTrackDuration() {
return LocationHelper.convertToReadableTime(mTrackDuration, true); return LocationHelper.convertToReadableTime(mDuration, true);
} }
/* Getter for distance of track */ /* Getter for distance of track */
public String getTrackDistance() { public String getTrackDistance() {
float trackDistance = mWayPoints.get(mWayPoints.size()-1).getDistanceToStartingPoint(); float trackDistance;
String unit;
return String.format (Locale.ENGLISH, "%.0f", trackDistance) + "m"; if (mUnitSystem == IMPERIAL) {
// get track distance and convert to feet
trackDistance = mWayPoints.get(mWayPoints.size()-1).getDistanceToStartingPoint() * 3.28084f;
unit = "ft";
} else {
// get track distance
trackDistance = mWayPoints.get(mWayPoints.size()-1).getDistanceToStartingPoint();
unit = "m";
}
return String.format (Locale.ENGLISH, "%.0f", trackDistance) + unit;
} }
@ -164,6 +186,22 @@ public class Track implements TrackbookKeys, Parcelable {
public void writeToParcel(Parcel parcel, int i) { public void writeToParcel(Parcel parcel, int i) {
parcel.writeTypedList(mWayPoints); parcel.writeTypedList(mWayPoints);
parcel.writeFloat(mTrackLength); parcel.writeFloat(mTrackLength);
parcel.writeFloat(mStepCount);
parcel.writeInt(mUnitSystem);
}
/* 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;
}
} }

View file

@ -26,7 +26,7 @@ import android.util.Log;
*/ */
public final class LogHelper { public final class LogHelper {
private final static boolean mTesting = false; private final static boolean mTesting = true;
public static void d(final String tag, String message) { public static void d(final String tag, String message) {
// include logging only in debug versions // include logging only in debug versions

View file

@ -78,5 +78,7 @@ public interface TrackbookKeys {
int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124; int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
int TRACKER_SERVICE_NOTIFICATION_ID = 1; int TRACKER_SERVICE_NOTIFICATION_ID = 1;
int INFOSHEET_CONTENT_ABOUT = 1; int INFOSHEET_CONTENT_ABOUT = 1;
int METRIC = 1;
int IMPERIAL = 2;
} }