Code cleanup: add name to Timer thread HandlerThread

This commit is contained in:
Jimmy Shih
2013-06-13 15:22:11 -07:00
parent 501dbd1746
commit b720cf26ee
4 changed files with 61 additions and 47 deletions
@@ -134,9 +134,10 @@ public class TrackDataHub implements DataSourceListener {
started = false;
dataSourceManager.unregisterAllListeners();
handlerThread.getLooper().quit();
handlerThread = null;
if (handlerThread != null) {
handlerThread.getLooper().quit();
handlerThread = null;
}
handler = null;
dataSource = null;
dataSourceManager = null;
@@ -133,8 +133,9 @@ public class TrackRecordingService extends Service {
private Location lastLocation;
private boolean currentSegmentHasLocation;
// Timer to periodically invoke checkLocationListener
private final Timer timer = new Timer();
// TimerTask to register location listener
private TimerTask timerTask;
private Timer timer;
// Handler for the timer to post a runnable to the main thread
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() {
@Override
public void onDisconnected() {}
@@ -301,10 +293,19 @@ public class TrackRecordingService extends Service {
// onSharedPreferenceChanged might not set recordingTrackId.
recordingTrackId = PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;
// Require announcementExecutor and splitExecutor to be created.
// Require voiceExecutor and splitExecutor to be created.
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
@@ -357,10 +358,15 @@ public class TrackRecordingService extends Service {
showNotification(false);
sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
checkLocationListener.cancel();
checkLocationListener = null;
timer.cancel();
timer.purge();
if (timerTask != null) {
timerTask.cancel();
timerTask = null;
}
if (timer != null) {
timer.cancel();
timer.purge();
timer = null;
}
unregisterLocationListener();
try {
@@ -39,30 +39,8 @@ public abstract class SensorManager {
private SensorState sensorState = SensorState.NONE;
private long sensorStateTimestamp = System.currentTimeMillis();
/**
* A time task to check sensor connection.
*/
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();
private TimerTask timerTask;
private Timer timer;
/**
* Returns true if the sensor is enabled.
@@ -89,14 +67,43 @@ public abstract class SensorManager {
*/
public void startSensor() {
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.
*/
public void stopSensor() {
timer.cancel();
if (timerTask != null) {
timerTask.cancel();
timerTask = null;
}
if (timer != null) {
timer.cancel();
timer.purge();
timer = null;
}
tearDownChannel();
}
@@ -68,7 +68,7 @@ public class TimerTaskExecutor {
periodicTask.run(trackRecordingService);
}
};
timer = new Timer();
timer = new Timer("TimerTaskExecutorTimer");
long next = System.currentTimeMillis() + interval - (tripStatistics.getTotalTime() % interval);
timer.scheduleAtFixedRate(timerTask, new Date(next), interval);
}