keep step count when restarting trackerservice (see #29)

This commit is contained in:
y20k 2018-01-22 15:17:33 +01:00
parent adc68605a4
commit 1584eac152
5 changed files with 19 additions and 60 deletions

View file

@ -294,10 +294,6 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
// dismiss notification // dismiss notification
startTrackerService(ACTION_DISMISS, null); startTrackerService(ACTION_DISMISS, null);
// Intent intent = new Intent(this, TrackerService.class); // todo remove
// intent.setAction(ACTION_DISMISS);
// startService(intent);
// hide Floating Action Button sub menu // hide Floating Action Button sub menu
showFloatingActionButtonMenu(false); showFloatingActionButtonMenu(false);
@ -474,15 +470,6 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
// start tracker service // start tracker service
startTrackerService(ACTION_START, lastLocation); startTrackerService(ACTION_START, lastLocation);
// Intent intent = new Intent(this, TrackerService.class); // todo remove
// intent.setAction(ACTION_START);
// intent.putExtra(EXTRA_LAST_LOCATION, lastLocation);
// if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
// startForegroundService(intent);
// } else {
// startService(intent);
// }
} else { } else {
Toast.makeText(this, getString(R.string.toast_message_location_services_not_ready), Toast.LENGTH_LONG).show(); Toast.makeText(this, getString(R.string.toast_message_location_services_not_ready), Toast.LENGTH_LONG).show();
// change state back // change state back
@ -502,19 +489,11 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
// stop tracker service // stop tracker service
startTrackerService(ACTION_STOP, null); startTrackerService(ACTION_STOP, null);
// Intent intent = new Intent(this, TrackerService.class); // todo remove
// intent.setAction(ACTION_STOP);
// startService(intent);
break; break;
case FAB_STATE_SAVE: case FAB_STATE_SAVE:
// toggle floating action button sub menu // toggle visibility floating action button sub menu
if (!mFloatingActionButtonSubMenuVisible) { showFloatingActionButtonMenu(!mFloatingActionButtonSubMenuVisible);
showFloatingActionButtonMenu(true);
} else {
showFloatingActionButtonMenu(false);
}
break; break;

View file

@ -294,7 +294,6 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
} }
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);

View file

@ -16,7 +16,6 @@
package org.y20k.trackbook; package org.y20k.trackbook;
import android.app.ActivityManager;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
@ -50,9 +49,10 @@ import org.y20k.trackbook.helpers.NotificationHelper;
import org.y20k.trackbook.helpers.StorageHelper; import org.y20k.trackbook.helpers.StorageHelper;
import org.y20k.trackbook.helpers.TrackbookKeys; import org.y20k.trackbook.helpers.TrackbookKeys;
import java.util.Iterator;
import java.util.List; import java.util.List;
import static android.hardware.Sensor.TYPE_STEP_COUNTER;
/** /**
* TrackerService class * TrackerService class
@ -184,9 +184,9 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
@Override @Override
public void onSensorChanged(SensorEvent sensorEvent) { public void onSensorChanged(SensorEvent sensorEvent) {
// save the step count offset / previously recorded steps // save the step count offset (steps previously recorded by the system) and add any steps recorded during this session in case the app was killed
if (mStepCountOffset == 0) { if (mStepCountOffset == 0) {
mStepCountOffset = sensorEvent.values[0] - 1; mStepCountOffset = sensorEvent.values[0] - 1 + mTrack.getStepCount();
} }
// calculate step count // calculate step count
@ -229,14 +229,14 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
mNotification = NotificationHelper.getNotification(this, mNotificationBuilder, mTrack, true); mNotification = NotificationHelper.getNotification(this, mNotificationBuilder, mTrack, true);
mNotificationManager.notify(TRACKER_SERVICE_NOTIFICATION_ID, mNotification); // todo check if necessary in pre Android O mNotificationManager.notify(TRACKER_SERVICE_NOTIFICATION_ID, mNotification); // todo check if necessary in pre Android O
// get duration of previously recorded track - in case this service has been restarted
final long previouslyRecordedDuration = mTrack.getTrackDuration();
// set timer to retrieve new locations and to prevent endless tracking // set timer to retrieve new locations and to prevent endless tracking
// SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
// final long previouslyRecordedDuration = settings.getLong(PREFS_CURRENT_TRACK_DURATION, 0);// todo describe
final long previouslyRecordedDuration = mTrack.getTrackDuration();// todo describe
mTimer = new CountDownTimer(EIGHT_HOURS_IN_MILLISECONDS, FIFTEEN_SECONDS_IN_MILLISECONDS) { mTimer = new CountDownTimer(EIGHT_HOURS_IN_MILLISECONDS, FIFTEEN_SECONDS_IN_MILLISECONDS) {
@Override @Override
public void onTick(long millisUntilFinished) { public void onTick(long millisUntilFinished) {
// update track duration // update track duration - and add duration from previously interrupted session
long duration = EIGHT_HOURS_IN_MILLISECONDS - millisUntilFinished + previouslyRecordedDuration; long duration = EIGHT_HOURS_IN_MILLISECONDS - millisUntilFinished + previouslyRecordedDuration;
mTrack.setDuration(duration); mTrack.setDuration(duration);
// try to add WayPoint to Track // try to add WayPoint to Track
@ -259,11 +259,13 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
// initialize step counter // initialize step counter
mStepCountOffset = 0; mStepCountOffset = 0;
Sensor stepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if (stepCounter != null) { boolean stepCounterAvailable;
mSensorManager.registerListener(this, stepCounter, SensorManager.SENSOR_DELAY_UI); stepCounterAvailable = mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(TYPE_STEP_COUNTER), SensorManager.SENSOR_DELAY_UI);
if (stepCounterAvailable) {
LogHelper.v(LOG_TAG, "Pedometer sensor available: Registering listener.");
} else { } else {
LogHelper.i(LOG_TAG, "Pedometer Sensor not available"); LogHelper.i(LOG_TAG, "Pedometer sensor not available.");
mTrack.setStepCount(-1); mTrack.setStepCount(-1);
} }
@ -315,25 +317,6 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
/* Adds a new WayPoint to current track */ /* Adds a new WayPoint to current track */
private void addWayPointToTrack() { private void addWayPointToTrack() {
// TODO REMOVE
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> l = am.getRunningServices(50);
Iterator<ActivityManager.RunningServiceInfo> i = l.iterator();
while (i.hasNext()) {
ActivityManager.RunningServiceInfo runningServiceInfo = i
.next();
if(runningServiceInfo.service.getClassName().contains("TrackerService")){
if(runningServiceInfo.foreground)
{
LogHelper.e(LOG_TAG, "Foreground State? YES");
}
}
}
// TODO REMOVE
// create new WayPoint // create new WayPoint
WayPoint newWayPoint = null; WayPoint newWayPoint = null;
@ -446,11 +429,9 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
/* Saves state of Tracker Service and floating Action Button */ /* Saves state of Tracker Service and floating Action Button */
private void saveTrackerServiceState(boolean trackerServiceRunning, int fabState) { private void saveTrackerServiceState(boolean trackerServiceRunning, int fabState) {
// private void saveTrackerServiceState(boolean trackerServiceRunning, long currentTrackDuration, int fabState) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit(); SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(PREFS_TRACKER_SERVICE_RUNNING, trackerServiceRunning); editor.putBoolean(PREFS_TRACKER_SERVICE_RUNNING, trackerServiceRunning);
// editor.putLong(PREFS_CURRENT_TRACK_DURATION, currentTrackDuration); // todo remove
editor.putInt(PREFS_FAB_STATE, fabState); editor.putInt(PREFS_FAB_STATE, fabState);
editor.apply(); editor.apply();
} }

View file

@ -26,7 +26,7 @@ import android.util.Log;
*/ */
public final class LogHelper { public final class LogHelper {
private final static boolean mTesting = true; private final static boolean mTesting = false;
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

@ -21,8 +21,8 @@ allprojects {
} }
project.ext { project.ext {
applicationId = 'org.y20k.trackbook' applicationId = 'org.y20k.trackbook'
versionCode = 16 versionCode = 17
versionName = '1.1.0' versionName = '1.1.1'
minSdkVersion = 22 minSdkVersion = 22
compileSdkVersion = 27 compileSdkVersion = 27
targetSdkVersion = 27 targetSdkVersion = 27