Use a reference count with SensorManagers created by SensorManagerFactory

This commit is contained in:
ml1050
2012-02-04 00:47:55 +01:00
parent 073c8a3c98
commit d105c58309
4 changed files with 94 additions and 57 deletions
@@ -168,8 +168,6 @@ public class SensorStateActivity extends Activity {
// If no temp sensor manager is present, create one, and start it. // If no temp sensor manager is present, create one, and start it.
if (tempSensorManager == null) { if (tempSensorManager == null) {
tempSensorManager = SensorManagerFactory.getSensorManager(this); tempSensorManager = SensorManagerFactory.getSensorManager(this);
if (tempSensorManager != null)
tempSensorManager.onStartTrack();
} }
// If a temp sensor manager is available, use states from temp sensor // If a temp sensor manager is available, use states from temp sensor
@@ -226,7 +224,7 @@ public class SensorStateActivity extends Activity {
*/ */
private void stopTempSensorManager() { private void stopTempSensorManager() {
if (tempSensorManager != null) { if (tempSensorManager != null) {
tempSensorManager.shutdown(); SensorManagerFactory.releaseSensorManager(tempSensorManager);
tempSensorManager = null; tempSensorManager = null;
} }
} }
@@ -358,7 +358,7 @@ public class TrackRecordingService extends Service {
unregisterLocationListener(); unregisterLocationListener();
shutdownTaskExecutors(); shutdownTaskExecutors();
if (sensorManager != null) { if (sensorManager != null) {
sensorManager.shutdown(); SensorManagerFactory.releaseSensorManager(sensorManager);
sensorManager = null; sensorManager = null;
} }
@@ -602,9 +602,6 @@ public class TrackRecordingService extends Service {
showNotification(); showNotification();
registerLocationListener(); registerLocationListener();
sensorManager = SensorManagerFactory.getSensorManager(this); sensorManager = SensorManagerFactory.getSensorManager(this);
if (sensorManager != null) {
sensorManager.onStartTrack();
}
// Reset the number of auto-resume retries. // Reset the number of auto-resume retries.
setAutoResumeTrackRetries(0); setAutoResumeTrackRetries(0);
@@ -1038,7 +1035,7 @@ public class TrackRecordingService extends Service {
prefManager.setRecordingTrack(recordingTrackId = -1); prefManager.setRecordingTrack(recordingTrackId = -1);
if (sensorManager != null) { if (sensorManager != null) {
sensorManager.shutdown(); SensorManagerFactory.releaseSensorManager(sensorManager);
sensorManager = null; sensorManager = null;
} }
@@ -35,7 +35,7 @@ public class SensorManagerFactory {
} }
/** /**
* Get a new sensor manager. * Get and start a new sensor manager.
* @param context Context to fetch system preferences. * @param context Context to fetch system preferences.
* @return The sensor manager that corresponds to the sensor type setting. * @return The sensor manager that corresponds to the sensor type setting.
*/ */
@@ -52,18 +52,59 @@ public class SensorManagerFactory {
Log.i(Constants.TAG, "Creating sensor of type: " + sensor); Log.i(Constants.TAG, "Creating sensor of type: " + sensor);
if (sensor == null) { if (sensor == null) {
reset();
return null; return null;
} else if (sensor.equals(context.getString(R.string.sensor_type_value_ant))) { }
return new AntDirectSensorManager(context); if (sensor.equals(activeSensorType)) {
Log.i(Constants.TAG, "Returning existing sensor manager.");
refCount++;
return activeSensorManager;
}
reset();
if (sensor.equals(context.getString(R.string.sensor_type_value_ant))) {
activeSensorManager = new AntDirectSensorManager(context);
} else if (sensor.equals(context.getString(R.string.sensor_type_value_srm_ant_bridge))) { } else if (sensor.equals(context.getString(R.string.sensor_type_value_srm_ant_bridge))) {
return new AntSrmBridgeSensorManager(context); activeSensorManager = new AntSrmBridgeSensorManager(context);
} else if (sensor.equals(context.getString(R.string.sensor_type_value_zephyr))) { } else if (sensor.equals(context.getString(R.string.sensor_type_value_zephyr))) {
return new ZephyrSensorManager(context); activeSensorManager = new ZephyrSensorManager(context);
} else if (sensor.equals(context.getString(R.string.sensor_type_value_polar))) { } else if (sensor.equals(context.getString(R.string.sensor_type_value_polar))) {
return new PolarSensorManager(context); activeSensorManager = new PolarSensorManager(context);
} else { } else {
Log.w(Constants.TAG, "Unable to find sensor type: " + sensor); Log.w(Constants.TAG, "Unable to find sensor type: " + sensor);
return null; return null;
} }
activeSensorType = sensor;
refCount = 1;
activeSensorManager.onStartTrack();
return activeSensorManager;
} }
/**
* Finish using a sensor manager.
*/
public static void releaseSensorManager(SensorManager sensorManager) {
Log.i(Constants.TAG, "releaseSensorManager: " + activeSensorType + " " + refCount);
if (sensorManager != activeSensorManager) {
Log.e(Constants.TAG, "invalid parameter to releaseSensorManager");
}
if (--refCount > 0) {
return;
}
reset();
}
private static void reset() {
activeSensorType = null;
if (activeSensorManager != null) {
activeSensorManager.shutdown();
}
activeSensorManager = null;
refCount = 0;
}
private static String activeSensorType = null;
private static SensorManager activeSensorManager = null;
private static int refCount = 0;
} }
@@ -52,5 +52,6 @@ public class SensorManagerFactoryTest extends AndroidTestCase {
SensorManager sm = SensorManagerFactory.getSensorManager(getContext()); SensorManager sm = SensorManagerFactory.getSensorManager(getContext());
assertNotNull(sm); assertNotNull(sm);
assertTrue(c.isInstance(sm)); assertTrue(c.isInstance(sm));
SensorManagerFactory.releaseSensorManager(sm);
} }
} }