forked from upstream-mirrors/OpenTracks
Code cleanup: add name to Timer thread HandlerThread
This commit is contained in:
@@ -134,9 +134,10 @@ public class TrackDataHub implements DataSourceListener {
|
|||||||
started = false;
|
started = false;
|
||||||
|
|
||||||
dataSourceManager.unregisterAllListeners();
|
dataSourceManager.unregisterAllListeners();
|
||||||
handlerThread.getLooper().quit();
|
if (handlerThread != null) {
|
||||||
|
handlerThread.getLooper().quit();
|
||||||
handlerThread = null;
|
handlerThread = null;
|
||||||
|
}
|
||||||
handler = null;
|
handler = null;
|
||||||
dataSource = null;
|
dataSource = null;
|
||||||
dataSourceManager = null;
|
dataSourceManager = null;
|
||||||
|
|||||||
@@ -133,8 +133,9 @@ public class TrackRecordingService extends Service {
|
|||||||
private Location lastLocation;
|
private Location lastLocation;
|
||||||
private boolean currentSegmentHasLocation;
|
private boolean currentSegmentHasLocation;
|
||||||
|
|
||||||
// Timer to periodically invoke checkLocationListener
|
// TimerTask to register location listener
|
||||||
private final Timer timer = new Timer();
|
private TimerTask timerTask;
|
||||||
|
private Timer timer;
|
||||||
|
|
||||||
// Handler for the timer to post a runnable to the main thread
|
// Handler for the timer to post a runnable to the main thread
|
||||||
private final Handler handler = new Handler();
|
private final Handler handler = new Handler();
|
||||||
@@ -247,15 +248,6 @@ public class TrackRecordingService extends Service {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private TimerTask checkLocationListener = new TimerTask() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (isRecording() && !isPaused()) {
|
|
||||||
registerLocationListener();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private final ConnectionCallbacks activityRecognitionCallbacks = new ConnectionCallbacks() {
|
private final ConnectionCallbacks activityRecognitionCallbacks = new ConnectionCallbacks() {
|
||||||
@Override
|
@Override
|
||||||
public void onDisconnected() {}
|
public void onDisconnected() {}
|
||||||
@@ -301,10 +293,19 @@ public class TrackRecordingService extends Service {
|
|||||||
// onSharedPreferenceChanged might not set recordingTrackId.
|
// onSharedPreferenceChanged might not set recordingTrackId.
|
||||||
recordingTrackId = PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;
|
recordingTrackId = PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;
|
||||||
|
|
||||||
// Require announcementExecutor and splitExecutor to be created.
|
// Require voiceExecutor and splitExecutor to be created.
|
||||||
sharedPreferenceChangeListener.onSharedPreferenceChanged(sharedPreferences, null);
|
sharedPreferenceChangeListener.onSharedPreferenceChanged(sharedPreferences, null);
|
||||||
|
|
||||||
timer.schedule(checkLocationListener, 0, ONE_MINUTE);
|
timerTask = new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (isRecording() && !isPaused()) {
|
||||||
|
registerLocationListener();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
timer = new Timer("TrackRecordingServiceTimer");
|
||||||
|
timer.schedule(timerTask, 0, ONE_MINUTE);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Try to restart the previous recording track in case the service has been
|
* Try to restart the previous recording track in case the service has been
|
||||||
@@ -357,10 +358,15 @@ public class TrackRecordingService extends Service {
|
|||||||
showNotification(false);
|
showNotification(false);
|
||||||
|
|
||||||
sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
||||||
checkLocationListener.cancel();
|
if (timerTask != null) {
|
||||||
checkLocationListener = null;
|
timerTask.cancel();
|
||||||
timer.cancel();
|
timerTask = null;
|
||||||
timer.purge();
|
}
|
||||||
|
if (timer != null) {
|
||||||
|
timer.cancel();
|
||||||
|
timer.purge();
|
||||||
|
timer = null;
|
||||||
|
}
|
||||||
unregisterLocationListener();
|
unregisterLocationListener();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -39,30 +39,8 @@ public abstract class SensorManager {
|
|||||||
private SensorState sensorState = SensorState.NONE;
|
private SensorState sensorState = SensorState.NONE;
|
||||||
private long sensorStateTimestamp = System.currentTimeMillis();
|
private long sensorStateTimestamp = System.currentTimeMillis();
|
||||||
|
|
||||||
/**
|
private TimerTask timerTask;
|
||||||
* A time task to check sensor connection.
|
private Timer timer;
|
||||||
*/
|
|
||||||
private TimerTask checkSensorConnectionTimeTask = new TimerTask() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
switch (getSensorState()) {
|
|
||||||
case CONNECTING:
|
|
||||||
if (System.currentTimeMillis() - sensorStateTimestamp > MAX_SENSOR_STATE_AGE) {
|
|
||||||
Log.i(TAG, "Retry setUpChannel");
|
|
||||||
setUpChannel();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case NONE:
|
|
||||||
case DISCONNECTED:
|
|
||||||
setUpChannel();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private final Timer timer = new Timer();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the sensor is enabled.
|
* Returns true if the sensor is enabled.
|
||||||
@@ -89,14 +67,43 @@ public abstract class SensorManager {
|
|||||||
*/
|
*/
|
||||||
public void startSensor() {
|
public void startSensor() {
|
||||||
setUpChannel();
|
setUpChannel();
|
||||||
timer.schedule(checkSensorConnectionTimeTask, RETRY_PERIOD, RETRY_PERIOD);
|
timerTask = new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
switch (getSensorState()) {
|
||||||
|
case CONNECTING:
|
||||||
|
if (System.currentTimeMillis() - sensorStateTimestamp > MAX_SENSOR_STATE_AGE) {
|
||||||
|
Log.i(TAG, "Retry setUpChannel");
|
||||||
|
setUpChannel();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case NONE:
|
||||||
|
case DISCONNECTED:
|
||||||
|
setUpChannel();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// CONNECTED or SENDING
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
timer = new Timer("SensorManagerTimer");
|
||||||
|
timer.schedule(timerTask, RETRY_PERIOD, RETRY_PERIOD);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stops the sensor.
|
* Stops the sensor.
|
||||||
*/
|
*/
|
||||||
public void stopSensor() {
|
public void stopSensor() {
|
||||||
timer.cancel();
|
if (timerTask != null) {
|
||||||
|
timerTask.cancel();
|
||||||
|
timerTask = null;
|
||||||
|
}
|
||||||
|
if (timer != null) {
|
||||||
|
timer.cancel();
|
||||||
|
timer.purge();
|
||||||
|
timer = null;
|
||||||
|
}
|
||||||
tearDownChannel();
|
tearDownChannel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ public class TimerTaskExecutor {
|
|||||||
periodicTask.run(trackRecordingService);
|
periodicTask.run(trackRecordingService);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
timer = new Timer();
|
timer = new Timer("TimerTaskExecutorTimer");
|
||||||
long next = System.currentTimeMillis() + interval - (tripStatistics.getTotalTime() % interval);
|
long next = System.currentTimeMillis() + interval - (tripStatistics.getTotalTime() % interval);
|
||||||
timer.scheduleAtFixedRate(timerTask, new Date(next), interval);
|
timer.scheduleAtFixedRate(timerTask, new Date(next), interval);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user