2016-08-29 12:50:41 +00:00
|
|
|
/**
|
|
|
|
* TrackerService.java
|
|
|
|
* Implements the app's movement tracker service
|
|
|
|
* The TrackerService creates a Track object and displays a notification
|
|
|
|
*
|
|
|
|
* This file is part of
|
|
|
|
* TRACKBOOK - Movement Recorder for Android
|
|
|
|
*
|
2018-01-12 16:42:47 +00:00
|
|
|
* Copyright (c) 2016-18 - Y20K.org
|
2016-08-29 12:50:41 +00:00
|
|
|
* 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;
|
|
|
|
|
2018-01-21 21:28:32 +00:00
|
|
|
import android.app.ActivityManager;
|
2017-08-15 08:23:03 +00:00
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
2016-08-29 12:50:41 +00:00
|
|
|
import android.app.Service;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2017-01-09 16:56:45 +00:00
|
|
|
import android.content.SharedPreferences;
|
2016-09-19 16:46:25 +00:00
|
|
|
import android.database.ContentObserver;
|
2016-09-15 11:38:51 +00:00
|
|
|
import android.hardware.Sensor;
|
|
|
|
import android.hardware.SensorEvent;
|
|
|
|
import android.hardware.SensorEventListener;
|
|
|
|
import android.hardware.SensorManager;
|
2016-08-29 12:50:41 +00:00
|
|
|
import android.location.Location;
|
|
|
|
import android.location.LocationListener;
|
|
|
|
import android.location.LocationManager;
|
2017-01-09 16:56:45 +00:00
|
|
|
import android.os.AsyncTask;
|
2016-08-29 12:50:41 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.CountDownTimer;
|
2016-09-19 16:46:25 +00:00
|
|
|
import android.os.Handler;
|
2016-08-29 12:50:41 +00:00
|
|
|
import android.os.IBinder;
|
2017-01-09 16:56:45 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2016-08-29 12:50:41 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2017-08-15 08:23:03 +00:00
|
|
|
import android.support.v4.app.NotificationCompat;
|
2016-09-01 11:45:46 +00:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2016-09-19 16:46:25 +00:00
|
|
|
import android.widget.Toast;
|
2016-08-29 12:50:41 +00:00
|
|
|
|
|
|
|
import org.y20k.trackbook.core.Track;
|
2016-09-01 11:45:46 +00:00
|
|
|
import org.y20k.trackbook.core.WayPoint;
|
2016-08-30 13:22:19 +00:00
|
|
|
import org.y20k.trackbook.helpers.LocationHelper;
|
2016-09-01 11:45:46 +00:00
|
|
|
import org.y20k.trackbook.helpers.LogHelper;
|
2016-09-06 15:27:04 +00:00
|
|
|
import org.y20k.trackbook.helpers.NotificationHelper;
|
2017-01-09 16:56:45 +00:00
|
|
|
import org.y20k.trackbook.helpers.StorageHelper;
|
2016-08-29 12:50:41 +00:00
|
|
|
import org.y20k.trackbook.helpers.TrackbookKeys;
|
|
|
|
|
2018-01-21 21:28:32 +00:00
|
|
|
import java.util.Iterator;
|
2016-09-01 11:45:46 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2016-08-29 12:50:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TrackerService class
|
|
|
|
*/
|
2016-09-15 11:38:51 +00:00
|
|
|
public class TrackerService extends Service implements TrackbookKeys, SensorEventListener {
|
2016-08-29 12:50:41 +00:00
|
|
|
|
|
|
|
/* Define log tag */
|
|
|
|
private static final String LOG_TAG = TrackerService.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
|
|
/* Main class variables */
|
|
|
|
private Track mTrack;
|
|
|
|
private CountDownTimer mTimer;
|
|
|
|
private LocationManager mLocationManager;
|
2016-09-15 11:38:51 +00:00
|
|
|
private SensorManager mSensorManager;
|
|
|
|
private float mStepCountOffset;
|
2016-09-01 11:45:46 +00:00
|
|
|
private LocationListener mGPSListener = null;
|
|
|
|
private LocationListener mNetworkListener = null;
|
2016-09-19 16:46:25 +00:00
|
|
|
private SettingsContentObserver mSettingsContentObserver;
|
2016-09-01 11:45:46 +00:00
|
|
|
private Location mCurrentBestLocation;
|
2017-08-15 08:23:03 +00:00
|
|
|
private Notification mNotification;
|
|
|
|
private NotificationCompat.Builder mNotificationBuilder;
|
|
|
|
private NotificationManager mNotificationManager;
|
2016-09-19 16:46:25 +00:00
|
|
|
private boolean mTrackerServiceRunning;
|
|
|
|
private boolean mLocationSystemSetting;
|
2016-08-29 12:50:41 +00:00
|
|
|
|
2017-01-11 20:52:45 +00:00
|
|
|
|
2016-08-29 12:50:41 +00:00
|
|
|
@Override
|
2017-05-22 12:39:28 +00:00
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
2016-08-29 12:50:41 +00:00
|
|
|
|
2017-08-15 08:23:03 +00:00
|
|
|
// prepare notification channel and get NotificationManager
|
|
|
|
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
NotificationHelper.createNotificationChannel(this);
|
2017-01-17 15:27:43 +00:00
|
|
|
|
2016-08-30 14:49:54 +00:00
|
|
|
// acquire reference to Location Manager
|
|
|
|
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
|
|
|
|
|
2016-09-15 11:38:51 +00:00
|
|
|
// acquire reference to Sensor Manager
|
|
|
|
mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
|
|
|
|
|
2016-09-19 16:46:25 +00:00
|
|
|
// get state of location system setting
|
|
|
|
mLocationSystemSetting = LocationHelper.checkLocationSystemSetting(getApplicationContext());
|
|
|
|
|
|
|
|
// create content observer for changes in System Settings
|
2017-05-22 12:39:28 +00:00
|
|
|
mSettingsContentObserver = new SettingsContentObserver(new Handler());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
2016-09-19 16:46:25 +00:00
|
|
|
|
|
|
|
// check if user did turn off location in device settings
|
|
|
|
if (!mLocationSystemSetting) {
|
2017-05-22 12:39:28 +00:00
|
|
|
LogHelper.i(LOG_TAG, "Location Setting is turned off.");
|
2016-09-19 16:46:25 +00:00
|
|
|
Toast.makeText(getApplicationContext(), R.string.toast_message_location_offline, Toast.LENGTH_LONG).show();
|
|
|
|
stopTracking();
|
|
|
|
return START_STICKY;
|
2016-08-29 12:50:41 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 21:28:32 +00:00
|
|
|
// RESTART CHECK: checking for empty intent - try to get saved track
|
2017-08-15 08:23:03 +00:00
|
|
|
if (intent == null || intent.getAction() == null) {
|
2018-01-21 21:54:34 +00:00
|
|
|
LogHelper.w(LOG_TAG, "Null-Intent received. Trying to restart tracking.");
|
2018-01-21 21:28:32 +00:00
|
|
|
StorageHelper storageHelper = new StorageHelper(this);
|
|
|
|
if (storageHelper.tempFileExists()) {
|
|
|
|
mTrack = storageHelper.loadTrack(FILE_TEMP_TRACK);
|
|
|
|
// restart tracking
|
|
|
|
startTracking(intent, false);
|
|
|
|
}
|
2016-09-20 11:51:17 +00:00
|
|
|
}
|
2016-08-30 14:49:54 +00:00
|
|
|
|
2016-09-19 16:46:25 +00:00
|
|
|
// ACTION START
|
|
|
|
else if (intent.getAction().equals(ACTION_START) && mLocationSystemSetting) {
|
2018-01-21 21:28:32 +00:00
|
|
|
startTracking(intent, true);
|
2016-09-20 11:51:17 +00:00
|
|
|
}
|
2016-09-12 12:31:37 +00:00
|
|
|
|
2016-09-19 16:46:25 +00:00
|
|
|
// ACTION STOP
|
|
|
|
else if (intent.getAction().equals(ACTION_STOP) || !mLocationSystemSetting) {
|
2017-08-15 08:23:03 +00:00
|
|
|
mTrackerServiceRunning = false;
|
2017-06-11 19:46:20 +00:00
|
|
|
if (mTrack != null && mTimer != null) {
|
2017-05-31 08:30:29 +00:00
|
|
|
stopTracking();
|
2017-08-15 08:23:03 +00:00
|
|
|
} else {
|
|
|
|
// handle error - save state
|
2018-01-21 21:54:34 +00:00
|
|
|
saveTrackerServiceState(mTrackerServiceRunning, FAB_STATE_DEFAULT);
|
2017-05-31 08:30:29 +00:00
|
|
|
}
|
2017-08-15 08:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ACTION DISMISS
|
|
|
|
else if (intent.getAction().equals(ACTION_DISMISS)) {
|
|
|
|
// save state
|
2018-01-21 21:54:34 +00:00
|
|
|
saveTrackerServiceState(mTrackerServiceRunning, FAB_STATE_DEFAULT);
|
2017-08-15 08:23:03 +00:00
|
|
|
// dismiss notification
|
|
|
|
mNotificationManager.cancel(TRACKER_SERVICE_NOTIFICATION_ID); // todo check if necessary?
|
|
|
|
stopForeground(true);
|
|
|
|
}
|
2017-01-09 16:56:45 +00:00
|
|
|
|
2017-08-15 08:23:03 +00:00
|
|
|
// ACTION TRACK REQUEST
|
|
|
|
else if (intent.getAction().equals(ACTION_TRACK_REQUEST)) {
|
|
|
|
// send track via broadcast
|
|
|
|
sendTrackUpdate();
|
2016-08-29 12:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// START_STICKY is used for services that are explicitly started and stopped as needed
|
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
2016-09-01 11:45:46 +00:00
|
|
|
LogHelper.v(LOG_TAG, "onDestroy called.");
|
2016-08-29 12:50:41 +00:00
|
|
|
|
2017-08-15 08:23:03 +00:00
|
|
|
if (mTrackerServiceRunning) {
|
|
|
|
stopTracking();
|
|
|
|
}
|
2016-08-30 13:22:19 +00:00
|
|
|
|
2017-10-16 08:20:59 +00:00
|
|
|
// remove TrackerService from foreground state
|
2016-08-29 12:50:41 +00:00
|
|
|
stopForeground(true);
|
2016-08-30 14:49:54 +00:00
|
|
|
|
|
|
|
super.onDestroy();
|
2016-08-29 12:50:41 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 13:22:19 +00:00
|
|
|
|
2016-09-15 11:38:51 +00:00
|
|
|
@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) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-11 20:52:45 +00:00
|
|
|
/* Start tracking location */
|
2018-01-21 21:28:32 +00:00
|
|
|
private void startTracking(@Nullable Intent intent, boolean createNewTrack) {
|
2016-09-19 16:46:25 +00:00
|
|
|
LogHelper.v(LOG_TAG, "Service received command: START");
|
|
|
|
|
2018-01-21 21:28:32 +00:00
|
|
|
// create a new track -- if necessary
|
|
|
|
if (createNewTrack) {
|
|
|
|
mTrack = new Track();
|
|
|
|
}
|
2016-09-19 16:46:25 +00:00
|
|
|
|
|
|
|
// get last location
|
2018-01-21 21:28:32 +00:00
|
|
|
if (intent != null && intent.hasExtra(EXTRA_LAST_LOCATION)) {
|
2016-09-19 16:46:25 +00:00
|
|
|
mCurrentBestLocation = intent.getParcelableExtra(EXTRA_LAST_LOCATION);
|
|
|
|
}
|
|
|
|
// get last location - fallback
|
|
|
|
if (mCurrentBestLocation == null) {
|
|
|
|
mCurrentBestLocation = LocationHelper.determineLastKnownLocation(mLocationManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add last location as WayPoint to track
|
|
|
|
addWayPointToTrack();
|
|
|
|
|
2017-08-15 08:23:03 +00:00
|
|
|
// put up notification
|
|
|
|
mNotificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANEL_ID_RECORDING_CHANNEL);
|
|
|
|
mNotification = NotificationHelper.getNotification(this, mNotificationBuilder, mTrack, true);
|
|
|
|
mNotificationManager.notify(TRACKER_SERVICE_NOTIFICATION_ID, mNotification); // todo check if necessary in pre Android O
|
|
|
|
|
2016-09-19 16:46:25 +00:00
|
|
|
// set timer to retrieve new locations and to prevent endless tracking
|
2018-01-21 21:54:34 +00:00
|
|
|
// SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
// final long previouslyRecordedDuration = settings.getLong(PREFS_CURRENT_TRACK_DURATION, 0);// todo describe
|
|
|
|
final long previouslyRecordedDuration = mTrack.getTrackDuration();// todo describe
|
2016-09-19 16:46:25 +00:00
|
|
|
mTimer = new CountDownTimer(EIGHT_HOURS_IN_MILLISECONDS, FIFTEEN_SECONDS_IN_MILLISECONDS) {
|
|
|
|
@Override
|
|
|
|
public void onTick(long millisUntilFinished) {
|
|
|
|
// update track duration
|
2018-01-21 21:54:34 +00:00
|
|
|
long duration = EIGHT_HOURS_IN_MILLISECONDS - millisUntilFinished + previouslyRecordedDuration;
|
2017-01-10 14:57:03 +00:00
|
|
|
mTrack.setDuration(duration);
|
2016-09-19 16:46:25 +00:00
|
|
|
// try to add WayPoint to Track
|
|
|
|
addWayPointToTrack();
|
|
|
|
// update notification
|
2017-08-15 08:23:03 +00:00
|
|
|
mNotification = NotificationHelper.getUpdatedNotification(TrackerService.this, mNotificationBuilder, mTrack);
|
|
|
|
mNotificationManager.notify(TRACKER_SERVICE_NOTIFICATION_ID, mNotification);
|
2018-01-21 21:54:34 +00:00
|
|
|
// save a temp file in case the service has been killed by the system
|
|
|
|
SaveTempTrackAsyncHelper saveTempTrackAsyncHelper = new SaveTempTrackAsyncHelper();
|
|
|
|
saveTempTrackAsyncHelper.execute();
|
2016-09-19 16:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFinish() {
|
2017-08-15 08:23:03 +00:00
|
|
|
// stop tracking after eight hours
|
|
|
|
stopTracking();
|
2016-09-19 16:46:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
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 {
|
2017-05-22 12:39:28 +00:00
|
|
|
LogHelper.i(LOG_TAG, "Pedometer Sensor not available");
|
2016-09-19 16:46:25 +00:00
|
|
|
mTrack.setStepCount(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// create gps and network location listeners
|
|
|
|
startFindingLocation();
|
|
|
|
|
|
|
|
// register content observer for changes in System Settings
|
2017-08-15 08:23:03 +00:00
|
|
|
this.getContentResolver().registerContentObserver(android.provider.Settings.Secure.CONTENT_URI, true, mSettingsContentObserver);
|
|
|
|
|
|
|
|
// start service in foreground
|
|
|
|
startForeground(TRACKER_SERVICE_NOTIFICATION_ID, mNotification);
|
2016-09-19 16:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-11 20:52:45 +00:00
|
|
|
/* Stop tracking location */
|
2016-09-19 16:46:25 +00:00
|
|
|
private void stopTracking() {
|
|
|
|
LogHelper.v(LOG_TAG, "Service received command: STOP");
|
|
|
|
|
|
|
|
// store current date and time
|
2017-01-09 16:56:45 +00:00
|
|
|
mTrack.setRecordingEnd();
|
2016-09-19 16:46:25 +00:00
|
|
|
|
|
|
|
// stop timer
|
|
|
|
mTimer.cancel();
|
|
|
|
|
2017-01-25 13:19:50 +00:00
|
|
|
// broadcast an updated track
|
2017-01-19 12:36:49 +00:00
|
|
|
sendTrackUpdate();
|
|
|
|
|
2017-01-09 16:56:45 +00:00
|
|
|
// save a temp file in case the activity has been killed
|
|
|
|
SaveTempTrackAsyncHelper saveTempTrackAsyncHelper = new SaveTempTrackAsyncHelper();
|
|
|
|
saveTempTrackAsyncHelper.execute();
|
|
|
|
|
2016-09-19 16:46:25 +00:00
|
|
|
// change notification
|
2017-08-15 08:23:03 +00:00
|
|
|
mNotificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANEL_ID_RECORDING_CHANNEL);
|
|
|
|
mNotification = NotificationHelper.getNotification(this, mNotificationBuilder, mTrack, false);
|
|
|
|
mNotificationManager.notify(TRACKER_SERVICE_NOTIFICATION_ID, mNotification);
|
2016-09-19 16:46:25 +00:00
|
|
|
|
|
|
|
// remove listeners
|
|
|
|
stopFindingLocation();
|
|
|
|
mSensorManager.unregisterListener(this);
|
|
|
|
|
|
|
|
// disable content observer for changes in System Settings
|
|
|
|
this.getContentResolver().unregisterContentObserver(mSettingsContentObserver);
|
2017-08-15 08:23:03 +00:00
|
|
|
|
2017-10-16 08:20:59 +00:00
|
|
|
// remove TrackerService from foreground state
|
2017-08-15 08:23:03 +00:00
|
|
|
stopForeground(false);
|
2016-09-19 16:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-01 11:45:46 +00:00
|
|
|
/* Adds a new WayPoint to current track */
|
2016-09-09 20:28:12 +00:00
|
|
|
private void addWayPointToTrack() {
|
2016-09-01 11:45:46 +00:00
|
|
|
|
2018-01-21 21:28:32 +00:00
|
|
|
// 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
|
|
|
|
|
|
|
|
|
2016-09-01 11:45:46 +00:00
|
|
|
// create new WayPoint
|
|
|
|
WayPoint newWayPoint = null;
|
|
|
|
|
|
|
|
// get number of previously tracked WayPoints
|
|
|
|
int trackSize = mTrack.getWayPoints().size();
|
|
|
|
|
2016-09-01 12:16:32 +00:00
|
|
|
if (trackSize == 0) {
|
|
|
|
// add first location to track
|
|
|
|
newWayPoint = mTrack.addWayPoint(mCurrentBestLocation);
|
|
|
|
} else {
|
2016-09-12 12:31:37 +00:00
|
|
|
// get last WayPoint and compare it to current location
|
2016-09-01 12:16:32 +00:00
|
|
|
Location lastWayPoint = mTrack.getWayPointLocation(trackSize - 1);
|
2016-09-28 12:57:06 +00:00
|
|
|
|
2016-09-29 10:54:57 +00:00
|
|
|
// default value for average speed
|
2016-09-28 12:57:06 +00:00
|
|
|
float averageSpeed = 0f;
|
2016-09-29 10:54:57 +00:00
|
|
|
|
|
|
|
// compute average speed if new location come from network provider
|
|
|
|
if (trackSize > 1 && mCurrentBestLocation.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
|
2016-09-28 12:57:06 +00:00
|
|
|
Location firstWayPoint = mTrack.getWayPointLocation(0);
|
|
|
|
float distance = firstWayPoint.distanceTo(lastWayPoint);
|
|
|
|
long timeDifference = lastWayPoint.getElapsedRealtimeNanos() - firstWayPoint.getElapsedRealtimeNanos();
|
2017-08-15 08:23:03 +00:00
|
|
|
averageSpeed = distance / ((float) timeDifference / ONE_NANOSECOND);
|
2016-09-28 12:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (LocationHelper.isNewWayPoint(lastWayPoint, mCurrentBestLocation, averageSpeed)) {
|
2016-09-01 11:45:46 +00:00
|
|
|
// if new, add current best location to track
|
|
|
|
newWayPoint = mTrack.addWayPoint(mCurrentBestLocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send local broadcast if new WayPoint added
|
|
|
|
if (newWayPoint != null) {
|
2017-01-17 15:27:43 +00:00
|
|
|
sendTrackUpdate();
|
2016-09-01 11:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-17 15:27:43 +00:00
|
|
|
/* Broadcasts a track update */
|
|
|
|
private void sendTrackUpdate() {
|
2017-05-22 12:39:28 +00:00
|
|
|
if (mTrack != null) {
|
|
|
|
Intent i = new Intent();
|
|
|
|
i.setAction(ACTION_TRACK_UPDATED);
|
|
|
|
i.putExtra(EXTRA_TRACK, mTrack);
|
|
|
|
i.putExtra(EXTRA_LAST_LOCATION, mCurrentBestLocation);
|
|
|
|
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
|
|
|
|
}
|
2017-01-17 15:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-30 13:22:19 +00:00
|
|
|
/* Creates a location listener */
|
|
|
|
private LocationListener createLocationListener() {
|
|
|
|
return new LocationListener() {
|
|
|
|
public void onLocationChanged(Location location) {
|
2016-09-01 11:45:46 +00:00
|
|
|
// check if the new location is better
|
|
|
|
if (LocationHelper.isBetterLocation(location, mCurrentBestLocation)) {
|
|
|
|
// save location
|
|
|
|
mCurrentBestLocation = location;
|
2016-08-30 13:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
2017-08-15 08:23:03 +00:00
|
|
|
LogHelper.v(LOG_TAG, "Location provider status change: " + provider + " | " + status);
|
2016-08-30 13:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onProviderEnabled(String provider) {
|
2017-08-15 08:23:03 +00:00
|
|
|
LogHelper.v(LOG_TAG, "Location provider enabled: " + provider);
|
2016-08-30 13:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onProviderDisabled(String provider) {
|
2017-08-15 08:23:03 +00:00
|
|
|
LogHelper.v(LOG_TAG, "Location provider disabled: " + provider);
|
2016-08-30 13:22:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-08-30 14:49:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Creates gps and network location listeners */
|
2016-09-01 11:45:46 +00:00
|
|
|
private void startFindingLocation() {
|
2016-08-30 14:49:54 +00:00
|
|
|
|
|
|
|
// register location listeners and request updates
|
2016-09-19 16:46:25 +00:00
|
|
|
List locationProviders = mLocationManager.getAllProviders();
|
2016-09-01 11:45:46 +00:00
|
|
|
if (locationProviders.contains(LocationManager.GPS_PROVIDER)) {
|
|
|
|
mGPSListener = createLocationListener();
|
2016-09-20 18:24:17 +00:00
|
|
|
mTrackerServiceRunning = true;
|
|
|
|
}
|
|
|
|
if (locationProviders.contains(LocationManager.NETWORK_PROVIDER)) {
|
2016-09-01 11:45:46 +00:00
|
|
|
mNetworkListener = createLocationListener();
|
2016-09-20 18:24:17 +00:00
|
|
|
mTrackerServiceRunning = true;
|
2016-08-30 14:49:54 +00:00
|
|
|
}
|
2016-09-01 11:45:46 +00:00
|
|
|
LocationHelper.registerLocationListeners(mLocationManager, mGPSListener, mNetworkListener);
|
2018-01-21 21:54:34 +00:00
|
|
|
saveTrackerServiceState(mTrackerServiceRunning, FAB_STATE_RECORDING);
|
2016-08-30 14:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-06 15:27:04 +00:00
|
|
|
/* Removes gps and network location listeners */
|
|
|
|
private void stopFindingLocation() {
|
|
|
|
// remove listeners
|
|
|
|
LocationHelper.removeLocationListeners(mLocationManager, mGPSListener, mNetworkListener);
|
2016-09-19 16:46:25 +00:00
|
|
|
mTrackerServiceRunning = false;
|
2018-01-21 21:54:34 +00:00
|
|
|
saveTrackerServiceState(mTrackerServiceRunning, FAB_STATE_SAVE);
|
2016-09-06 15:27:04 +00:00
|
|
|
|
2016-09-16 15:45:10 +00:00
|
|
|
// notify MainActivityMapFragment
|
2016-09-06 15:27:04 +00:00
|
|
|
Intent i = new Intent();
|
|
|
|
i.setAction(ACTION_TRACKING_STOPPED);
|
|
|
|
i.putExtra(EXTRA_TRACK, mTrack);
|
|
|
|
i.putExtra(EXTRA_LAST_LOCATION, mCurrentBestLocation);
|
|
|
|
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
|
2016-09-19 16:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-15 08:23:03 +00:00
|
|
|
/* Saves state of Tracker Service and floating Action Button */
|
2018-01-21 21:54:34 +00:00
|
|
|
private void saveTrackerServiceState(boolean trackerServiceRunning, int fabState) {
|
|
|
|
// private void saveTrackerServiceState(boolean trackerServiceRunning, long currentTrackDuration, int fabState) {
|
2017-08-15 08:23:03 +00:00
|
|
|
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
SharedPreferences.Editor editor = settings.edit();
|
|
|
|
editor.putBoolean(PREFS_TRACKER_SERVICE_RUNNING, trackerServiceRunning);
|
2018-01-21 21:54:34 +00:00
|
|
|
// editor.putLong(PREFS_CURRENT_TRACK_DURATION, currentTrackDuration); // todo remove
|
2017-08-15 08:23:03 +00:00
|
|
|
editor.putInt(PREFS_FAB_STATE, fabState);
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-19 16:46:25 +00:00
|
|
|
/**
|
|
|
|
* Inner class: SettingsContentObserver is a custom ContentObserver for changes in Android Settings
|
|
|
|
*/
|
|
|
|
public class SettingsContentObserver extends ContentObserver {
|
|
|
|
|
|
|
|
public SettingsContentObserver(Handler handler) {
|
|
|
|
super(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean deliverSelfNotifications() {
|
|
|
|
return super.deliverSelfNotifications();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChange(boolean selfChange) {
|
|
|
|
super.onChange(selfChange);
|
|
|
|
LogHelper.v(LOG_TAG, "System Setting change detected.");
|
|
|
|
|
|
|
|
// check if location setting was changed
|
|
|
|
boolean previousLocationSystemSetting = mLocationSystemSetting;
|
|
|
|
mLocationSystemSetting = LocationHelper.checkLocationSystemSetting(getApplicationContext());
|
|
|
|
if (previousLocationSystemSetting != mLocationSystemSetting && !mLocationSystemSetting && mTrackerServiceRunning) {
|
|
|
|
LogHelper.v(LOG_TAG, "Location Setting turned off while tracking service running.");
|
2017-05-31 08:30:29 +00:00
|
|
|
if (mTrack != null) {
|
|
|
|
stopTracking();
|
|
|
|
}
|
2016-09-19 16:46:25 +00:00
|
|
|
stopForeground(true);
|
|
|
|
}
|
|
|
|
}
|
2016-09-06 15:27:04 +00:00
|
|
|
|
|
|
|
}
|
2017-01-09 16:56:45 +00:00
|
|
|
/**
|
|
|
|
* End of inner class
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inner class: Saves track to external storage using AsyncTask
|
|
|
|
*/
|
|
|
|
private class SaveTempTrackAsyncHelper extends AsyncTask<Void, Void, Void> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... voids) {
|
|
|
|
LogHelper.v(LOG_TAG, "Saving temporary track object in background.");
|
|
|
|
// save track object
|
2017-01-19 12:36:49 +00:00
|
|
|
StorageHelper storageHelper = new StorageHelper(TrackerService.this);
|
2017-02-15 14:29:21 +00:00
|
|
|
storageHelper.saveTrack(mTrack, FILE_TEMP_TRACK);
|
2017-01-09 16:56:45 +00:00
|
|
|
return null;
|
|
|
|
}
|
2016-09-06 15:27:04 +00:00
|
|
|
|
2017-01-09 16:56:45 +00:00
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void aVoid) {
|
|
|
|
super.onPostExecute(aVoid);
|
|
|
|
LogHelper.v(LOG_TAG, "Saving finished.");
|
|
|
|
}
|
|
|
|
}
|
2017-01-17 15:27:43 +00:00
|
|
|
/**
|
|
|
|
* End of inner class
|
|
|
|
*/
|
2016-09-19 16:46:25 +00:00
|
|
|
|
2017-08-15 08:23:03 +00:00
|
|
|
}
|