Commits

Anonymous committed 8dde2fd

Disable sensor feed to UI activity when UI not visible.

Comments (0)

Files changed (2)

src/to/rcpt/icarus/IcarusLogger.java

 		PrintWriter file;
 		SensorManager sensors;
 		Sensor sensor;
-		Context context;
 		
 		public SensorEventAdapter(Context c, Sensor s) {
 	        sensors = (SensorManager) c.getSystemService(Context.SENSOR_SERVICE);
 	        sensor = s;
-	        context = c;
 		}
 		
 		public void onAccuracyChanged(Sensor sensor, int accuracy) {

src/to/rcpt/icarus/IcarusMission.java

 package to.rcpt.icarus;
 
+import java.util.LinkedList;
 import java.util.List;
 
 import android.app.Activity;
 import android.widget.ToggleButton;
 
 public class IcarusMission extends Activity implements LocationListener {
+	private SensorManager sensors;
+
 	private class SensorEventAdapter implements SensorEventListener {
 		private TextView tv;
+		private Sensor s;
 		
-		public SensorEventAdapter(TextView tv) {
+		public SensorEventAdapter(Sensor s, TextView tv) {
 			this.tv = tv;
+			this.s = s;
 		}
 		
 		public void onSensorChanged(SensorEvent event) {
 		public void onAccuracyChanged(Sensor sensor, int accuracy) {
 			// like, WTF cares?
 		}
+		
+		public void startSensor() {			
+        	sensors.registerListener(this, s, SensorManager.SENSOR_DELAY_UI);
+		}
+		
+		public void stopSensor() {
+			sensors.unregisterListener(this);
+		}
 	}
 
 	LocationManager gps;
 	private TextView tv;
 	private LinearLayout ll;
 	private int index;
+	private List<SensorEventAdapter> adapterlist;
+	private String provider;
 	
     @Override
     public void onCreate(Bundle savedInstanceState) {
         tv.setText("Hello, Icarus");
         ll.addView(tv, index++);
         setContentView(ll);
-        SensorManager sensors = (SensorManager) this.getSystemService(SENSOR_SERVICE);
+        sensors = (SensorManager) this.getSystemService(SENSOR_SERVICE);
+        adapterlist = new LinkedList<SensorEventAdapter>();
         List<Sensor> sensorList = sensors.getSensorList(Sensor.TYPE_ALL);
         for(Sensor s: sensorList) {
         	tv = new TextView(this);
         	tv.setText(s.getName());
         	ll.addView(tv, index++);
-        	sensors.registerListener(new SensorEventAdapter(tv), s, 
-        			SensorManager.SENSOR_DELAY_UI);
+        	adapterlist.add(new SensorEventAdapter(s, tv));
         }
 		gps = (LocationManager) this.getSystemService(LOCATION_SERVICE);
 		Criteria c = new Criteria();
 		c.setAccuracy(Criteria.ACCURACY_FINE);
 		c.setAltitudeRequired(true);
 		c.setSpeedRequired(true);
-		String provider = gps.getBestProvider(c, true);
+		provider = gps.getBestProvider(c, true);
+    }
+    
+    @Override
+    public void onResume() {
+    	super.onResume();
 		gps.requestLocationUpdates(provider, 0, 0, this);
+    	for (SensorEventAdapter s: adapterlist) {
+    		s.startSensor();
+    	}
+    }
+    
+    @Override
+    public void onPause() {
+    	super.onPause();
+		gps.removeUpdates(this);
+    	for (SensorEventAdapter s: adapterlist) {
+    		s.stopSensor();
+    	}
     }
 
 	public void onLocationChanged(Location l) {