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
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 - 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;
|
|
|
|
|
|
|
|
import android.app.Service;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.location.Location;
|
|
|
|
import android.location.LocationListener;
|
|
|
|
import android.location.LocationManager;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.CountDownTimer;
|
|
|
|
import android.os.IBinder;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.y20k.trackbook.core.Track;
|
2016-08-30 13:22:19 +00:00
|
|
|
import org.y20k.trackbook.helpers.LocationHelper;
|
2016-08-29 12:50:41 +00:00
|
|
|
import org.y20k.trackbook.helpers.TrackbookKeys;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TrackerService class
|
|
|
|
*/
|
|
|
|
public class TrackerService extends Service implements TrackbookKeys {
|
|
|
|
|
|
|
|
/* 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-08-30 13:22:19 +00:00
|
|
|
private LocationListener mGPSListener;
|
|
|
|
private LocationListener mNetworkListener;
|
2016-08-29 12:50:41 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
|
2016-08-30 14:49:54 +00:00
|
|
|
// acquire reference to Location Manager
|
|
|
|
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
|
|
|
|
|
2016-08-29 12:50:41 +00:00
|
|
|
// checking for empty intent
|
|
|
|
if (intent == null) {
|
|
|
|
Log.v(LOG_TAG, "Null-Intent received. Stopping self.");
|
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ACTION START
|
|
|
|
else if (intent.getAction().equals(ACTION_START)) {
|
|
|
|
Log.v(LOG_TAG, "Service received command: START");
|
|
|
|
|
|
|
|
// create a new track
|
2016-08-30 13:22:19 +00:00
|
|
|
mTrack = new Track(getApplicationContext());
|
2016-08-29 12:50:41 +00:00
|
|
|
|
2016-08-30 14:49:54 +00:00
|
|
|
// create gps and network location listeners
|
|
|
|
createListeners();
|
2016-08-29 12:50:41 +00:00
|
|
|
|
2016-08-30 14:49:54 +00:00
|
|
|
// set a timer to prevent endless tracking
|
2016-08-29 12:50:41 +00:00
|
|
|
mTimer = new CountDownTimer(CONSTANT_MAXIMAL_DURATION, CONSTANT_TRACKING_INTERVAL) {
|
|
|
|
@Override
|
|
|
|
public void onTick(long l) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFinish() {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ACTION STOP
|
|
|
|
else if (intent.getAction().equals(ACTION_STOP)) {
|
|
|
|
Log.v(LOG_TAG, "Service received command: STOP");
|
2016-08-30 14:49:54 +00:00
|
|
|
|
|
|
|
// remove listeners
|
|
|
|
removeListeners();
|
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-08-30 14:49:54 +00:00
|
|
|
Log.v(LOG_TAG, "onDestroy called.");
|
2016-08-29 12:50:41 +00:00
|
|
|
|
2016-08-30 13:22:19 +00:00
|
|
|
// remove listeners
|
2016-08-30 14:49:54 +00:00
|
|
|
removeListeners();
|
2016-08-30 13:22:19 +00:00
|
|
|
|
2016-08-29 12:50:41 +00:00
|
|
|
// cancel notification
|
|
|
|
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
|
|
|
|
|
|
|
/* Creates a location listener */
|
|
|
|
private LocationListener createLocationListener() {
|
|
|
|
return new LocationListener() {
|
|
|
|
public void onLocationChanged(Location location) {
|
|
|
|
|
|
|
|
// get number of tracked WayPoints
|
|
|
|
int trackSize = mTrack.getWayPoints().size();
|
|
|
|
|
|
|
|
if (trackSize >= 2) {
|
|
|
|
Location lastWayPoint = mTrack.getWayPointLocation(trackSize-2);
|
|
|
|
if (LocationHelper.isNewWayPoint(lastWayPoint, location)) {
|
|
|
|
// add new location to track
|
|
|
|
mTrack.addWayPoint(location);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// add first location to track
|
|
|
|
mTrack.addWayPoint(location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
|
|
|
// TODO do something
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onProviderEnabled(String provider) {
|
|
|
|
// TODO do something
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onProviderDisabled(String provider) {
|
|
|
|
// TODO do something
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-08-30 14:49:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Creates gps and network location listeners */
|
|
|
|
private void createListeners() {
|
|
|
|
Log.v(LOG_TAG, "Setting up location listeners.");
|
|
|
|
|
|
|
|
// listeners that responds to location updates
|
|
|
|
mGPSListener = createLocationListener();
|
|
|
|
mNetworkListener = createLocationListener();
|
|
|
|
|
|
|
|
// register location listeners and request updates
|
|
|
|
try {
|
|
|
|
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGPSListener);
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mNetworkListener);
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Removes gps and network location listeners */
|
|
|
|
private void removeListeners() {
|
|
|
|
Log.v(LOG_TAG, "Removing location listeners.");
|
|
|
|
|
|
|
|
// remove gps listener
|
|
|
|
if (mGPSListener != null) {
|
|
|
|
Log.v(LOG_TAG, "Removing GPS location listener.");
|
|
|
|
try {
|
|
|
|
mLocationManager.removeUpdates(mGPSListener);
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
// catches permission problems
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove network listener
|
|
|
|
if (mNetworkListener != null) {
|
|
|
|
Log.v(LOG_TAG, "Removing network location listener.");
|
|
|
|
try {
|
|
|
|
mLocationManager.removeUpdates(mNetworkListener);
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
// catches permission problems
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-29 12:50:41 +00:00
|
|
|
}
|