diff --git a/app/build.gradle b/app/build.gradle index 462ed50..1fe9e97 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "org.y20k.trackbook" minSdkVersion 22 targetSdkVersion 24 - versionCode 2 - versionName "0.9.1 (The Great Gig in the Sky)" + versionCode 3 + versionName "0.9.2 (The Great Gig in the Sky)" vectorDrawables.useSupportLibrary = true } buildTypes { @@ -23,7 +23,7 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:24.2.0' - compile 'com.android.support:design:24.2.0' + compile 'com.android.support:appcompat-v7:24.2.1' + compile 'com.android.support:design:24.2.1' compile 'org.osmdroid:osmdroid-android:5.2@aar' } diff --git a/app/src/main/java/org/y20k/trackbook/TrackerService.java b/app/src/main/java/org/y20k/trackbook/TrackerService.java index 824fd45..8883f4a 100644 --- a/app/src/main/java/org/y20k/trackbook/TrackerService.java +++ b/app/src/main/java/org/y20k/trackbook/TrackerService.java @@ -19,6 +19,10 @@ package org.y20k.trackbook; import android.app.Service; import android.content.Context; 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.LocationListener; import android.location.LocationManager; @@ -41,7 +45,7 @@ import java.util.List; /** * TrackerService class */ -public class TrackerService extends Service implements TrackbookKeys { +public class TrackerService extends Service implements TrackbookKeys, SensorEventListener { /* Define log tag */ private static final String LOG_TAG = TrackerService.class.getSimpleName(); @@ -51,6 +55,8 @@ public class TrackerService extends Service implements TrackbookKeys { private Track mTrack; private CountDownTimer mTimer; private LocationManager mLocationManager; + private SensorManager mSensorManager; + private float mStepCountOffset; private LocationListener mGPSListener = null; private LocationListener mNetworkListener = null; private Location mCurrentBestLocation; @@ -61,6 +67,9 @@ public class TrackerService extends Service implements TrackbookKeys { // acquire reference to Location Manager mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); + // acquire reference to Sensor Manager + mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE); + // checking for empty intent if (intent == null) { LogHelper.v(LOG_TAG, "Null-Intent received. Stopping self."); @@ -91,7 +100,7 @@ public class TrackerService extends Service implements TrackbookKeys { @Override public void onTick(long millisUntilFinished) { // update track duration - mTrack.setTrackDuration(EIGHT_HOURS_IN_MILLISECONDS - millisUntilFinished); + mTrack.setDuration(EIGHT_HOURS_IN_MILLISECONDS - millisUntilFinished); // try to add WayPoint to Track addWayPointToTrack(); // update notification @@ -106,11 +115,22 @@ public class TrackerService extends Service implements TrackbookKeys { }; 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 NotificationHelper.show(this,mTrack); // create gps and network location listeners startFindingLocation(); + } // ACTION STOP @@ -125,6 +145,7 @@ public class TrackerService extends Service implements TrackbookKeys { // remove listeners stopFindingLocation(); + mSensorManager.unregisterListener(this); } // 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 stopFindingLocation(); + mSensorManager.unregisterListener(this); // cancel notification 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 */ private void addWayPointToTrack() { @@ -241,5 +284,4 @@ public class TrackerService extends Service implements TrackbookKeys { } - } diff --git a/app/src/main/java/org/y20k/trackbook/core/Track.java b/app/src/main/java/org/y20k/trackbook/core/Track.java index 96ef6c4..a48e4d0 100644 --- a/app/src/main/java/org/y20k/trackbook/core/Track.java +++ b/app/src/main/java/org/y20k/trackbook/core/Track.java @@ -25,6 +25,7 @@ import org.y20k.trackbook.helpers.LogHelper; import org.y20k.trackbook.helpers.TrackbookKeys; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Locale; @@ -41,13 +42,17 @@ public class Track implements TrackbookKeys, Parcelable { /* Main class variables */ private final List mWayPoints; private float mTrackLength; - private long mTrackDuration; + private long mDuration; + private float mStepCount; + private int mUnitSystem; /* Constructor */ public Track() { mWayPoints = new ArrayList(); mTrackLength = 0; + mStepCount = 0; + mUnitSystem = getUnitSystem(Locale.getDefault()); } @@ -55,6 +60,8 @@ public class Track implements TrackbookKeys, Parcelable { protected Track(Parcel in) { mWayPoints = in.createTypedArrayList(WayPoint.CREATOR); mTrackLength = in.readFloat(); + mStepCount = in.readFloat(); + mUnitSystem = in.readInt(); } @@ -102,11 +109,16 @@ public class Track implements TrackbookKeys, Parcelable { /* Setter for duration of track */ - public void setTrackDuration(long trackDuration) { - mTrackDuration = trackDuration; + public void setDuration(long duration) { + mDuration = duration; } + /* Setter for step count of track */ + public void setStepCount(float stepCount) { + mStepCount = stepCount; + } + /* Getter for mWayPoints */ public List getWayPoints() { return mWayPoints; @@ -121,15 +133,25 @@ public class Track implements TrackbookKeys, Parcelable { /* Getter for duration of track */ public String getTrackDuration() { - return LocationHelper.convertToReadableTime(mTrackDuration, true); + return LocationHelper.convertToReadableTime(mDuration, true); } /* Getter for distance of track */ 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) { parcel.writeTypedList(mWayPoints); 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 imperialSystemCountries = Arrays.asList("US", "LR", "MM"); + String countryCode = locale.getCountry(); + + if (imperialSystemCountries.contains(countryCode)){ + return IMPERIAL; + } else { + return METRIC; + } } diff --git a/app/src/main/java/org/y20k/trackbook/helpers/LogHelper.java b/app/src/main/java/org/y20k/trackbook/helpers/LogHelper.java index 221e7a2..a4bcb00 100644 --- a/app/src/main/java/org/y20k/trackbook/helpers/LogHelper.java +++ b/app/src/main/java/org/y20k/trackbook/helpers/LogHelper.java @@ -26,7 +26,7 @@ import android.util.Log; */ 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) { // include logging only in debug versions diff --git a/app/src/main/java/org/y20k/trackbook/helpers/TrackbookKeys.java b/app/src/main/java/org/y20k/trackbook/helpers/TrackbookKeys.java index 3f098be..dada765 100644 --- a/app/src/main/java/org/y20k/trackbook/helpers/TrackbookKeys.java +++ b/app/src/main/java/org/y20k/trackbook/helpers/TrackbookKeys.java @@ -78,5 +78,7 @@ public interface TrackbookKeys { int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124; int TRACKER_SERVICE_NOTIFICATION_ID = 1; int INFOSHEET_CONTENT_ABOUT = 1; + int METRIC = 1; + int IMPERIAL = 2; }