Commits

Anonymous committed 266f632

abstracting data source from presentation - IN PROGRESS PROBABLY BROKEN

Comments (0)

Files changed (2)

src/to/rcpt/icarus/DataSource.java

+package to.rcpt.icarus;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import android.content.Context;
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorEventListener;
+import android.hardware.SensorManager;
+import android.location.Criteria;
+import android.location.Location;
+import android.location.LocationListener;
+import android.location.LocationManager;
+import android.os.Bundle;
+import android.telephony.PhoneStateListener;
+import android.telephony.ServiceState;
+import android.telephony.TelephonyManager;
+
+public class DataSource implements LocationListener {
+
+	public int gsmState;
+	public List<Sensor> sensorList;
+	
+	private SensorManager sensors;
+	private List<SensorEventAdapter> adapterlist;
+
+	private LocationManager gps;
+	private String provider;
+
+	private class SensorEventAdapter implements SensorEventListener {
+		private DataSource ds;
+		private Sensor s;
+		
+		public SensorEventAdapter(Sensor s, DataSource ds) {
+			this.s = s;
+			this.ds = ds;
+		}
+		
+		public void onSensorChanged(SensorEvent event) {
+			String s = ""+event.timestamp;
+			for(float f: event.values) {
+				s += " " + f;
+			}
+			this.ds.logDataEvent(event.sensor.getName(), event.sensor.getName());
+		}
+
+		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);
+		}
+	}
+
+	private class StateListener extends PhoneStateListener {
+		private DataSource ds;
+		public StateListener(DataSource ds) {
+			this.ds = ds;
+		}
+		public void onServiceStateChanged(ServiceState serviceState) {
+			int gsmState = serviceState.getState();
+			this.ds.gsmState = gsmState;
+			this.ds.logDataEvent("gsmState changed", ""+gsmState);
+		}
+	}
+	
+	public DataSource(IcarusMission m) {
+		// set up service state listener
+        gsmState = -1;
+        TelephonyManager tm = (TelephonyManager) m.getSystemService(Context.TELEPHONY_SERVICE);
+        tm.listen(new StateListener(this), PhoneStateListener.LISTEN_SERVICE_STATE);
+
+        // set up sensor listener
+        sensors = (SensorManager) m.getSystemService(Context.SENSOR_SERVICE);
+        adapterlist = new LinkedList<SensorEventAdapter>();
+        sensorList = sensors.getSensorList(Sensor.TYPE_ALL);
+        for(Sensor s: sensorList) {
+        	adapterlist.add(new SensorEventAdapter(s, this));
+        }
+
+        // set up gps
+        gps = (LocationManager) m.getSystemService(Context.LOCATION_SERVICE);
+		Criteria c = new Criteria();
+		c.setAccuracy(Criteria.ACCURACY_FINE);
+		c.setAltitudeRequired(true);
+		c.setSpeedRequired(true);
+		provider = gps.getBestProvider(c, true);
+
+	}
+	
+	public void resume() {
+		gps.requestLocationUpdates(provider, 0, 0, this);
+    	for (SensorEventAdapter s: adapterlist) {
+    		s.startSensor();
+    	}	
+	}
+
+	public void pause() {
+		gps.removeUpdates(this);
+    	for (SensorEventAdapter s: adapterlist) {
+    		s.stopSensor();
+    	}
+	}
+	
+	public void logDataEvent(String name, String data) {
+		// TODO: send event to callback
+	}
+
+	public void onLocationChanged(Location l) {
+		this.logDataEvent("gps", l.getTime() + " " + l.getLatitude() + "," + l.getLongitude() + " " + l.getAltitude());
+	}
+
+	public void onProviderDisabled(String provider) {	
+	}
+
+	public void onProviderEnabled(String provider) {
+	}
+
+	public void onStatusChanged(String provider, int status, Bundle extras) {
+	}
+}

src/to/rcpt/icarus/IcarusMission.java

 package to.rcpt.icarus;
 
-import java.util.LinkedList;
-import java.util.List;
-
 import android.app.Activity;
-import android.content.Context;
 import android.hardware.Sensor;
-import android.hardware.SensorEvent;
-import android.hardware.SensorEventListener;
-import android.hardware.SensorManager;
-import android.location.Criteria;
-import android.location.Location;
-import android.location.LocationListener;
-import android.location.LocationManager;
 import android.os.Bundle;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 import android.widget.Button;
 import android.view.View;
 import android.view.View.OnClickListener;
-import android.telephony.ServiceState;
-import android.telephony.PhoneStateListener;
-import android.telephony.TelephonyManager;
 
-public class IcarusMission extends Activity implements LocationListener {
+import to.rcpt.icarus.DataSource;
 
-	private SensorManager sensors;
+public class IcarusMission extends Activity {
 
-	private class SensorEventAdapter implements SensorEventListener {
-		private TextView tv;
-		private Sensor s;
-		
-		public SensorEventAdapter(Sensor s, TextView tv) {
-			this.tv = tv;
-			this.s = s;
-		}
-		
-		public void onSensorChanged(SensorEvent event) {
-			String s = event.sensor.getName() + " " + event.timestamp;
-			for(float f: event.values) {
-				s += " " + f;
-			}
-			tv.setText(s);
-		}
-
-		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);
-		}
-	}
-	
 	private class ExitActivityListener implements OnClickListener {
 		private IcarusMission act;
 		public ExitActivityListener(IcarusMission a) {
 			act.finish();
 		}
 	}
-
-	private class StateListener extends PhoneStateListener {
-		private IcarusMission act;
-		public StateListener(IcarusMission a) {
-			act = a;
-		}
-		public void onServiceStateChanged(ServiceState serviceState) {
-			act.gsmState = serviceState.getState();
-		}
-	}
 	
-	LocationManager gps;
 	private TextView tv;
 	private LinearLayout ll;
 	private int index;
-	private List<SensorEventAdapter> adapterlist;
-	private String provider;
-	private int gsmState;
+	
+	private DataSource ds;
 	
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         
-        gsmState = -1;
-        TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
-        tm.listen(new StateListener(this), PhoneStateListener.LISTEN_SERVICE_STATE);
+        this.ds = new DataSource(this);
 
     	index = 0;
         ll = new LinearLayout(this);
         ll.addView(tv, index++);
 
         setContentView(ll);
-        sensors = (SensorManager) this.getSystemService(SENSOR_SERVICE);
-        adapterlist = new LinkedList<SensorEventAdapter>();
-        List<Sensor> sensorList = sensors.getSensorList(Sensor.TYPE_ALL);
-        for(Sensor s: sensorList) {
+        for(Sensor s: this.ds.sensorList) {
         	tv = new TextView(this);
         	tv.setText(s.getName());
         	ll.addView(tv, index++);
-        	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);
-		provider = gps.getBestProvider(c, true);
 
 		Button exitButton = new Button(this);
         exitButton.setOnClickListener(new ExitActivityListener(this));
     @Override
     public void onResume() {
     	super.onResume();
-		gps.requestLocationUpdates(provider, 0, 0, this);
-    	for (SensorEventAdapter s: adapterlist) {
-    		s.startSensor();
-    	}
+		this.ds.resume();
     }
     
     @Override
     public void onPause() {
     	super.onPause();
-		gps.removeUpdates(this);
-    	for (SensorEventAdapter s: adapterlist) {
-    		s.stopSensor();
-    	}
+		this.ds.pause();
     }
 
-	public void onLocationChanged(Location l) {
-		TextView tv = (TextView) ll.getChildAt(1);
-		tv.setText(l.getTime() + " " + l.getLatitude() + "," + l.getLongitude() + " " + l.getAltitude() + " " + gsmState);
-	}
-
-	public void onProviderDisabled(String provider) {
-		// TODO Auto-generated method stub
-		
-	}
-
-	public void onProviderEnabled(String provider) {
-		// TODO Auto-generated method stub
-		
-	}
-
-	public void onStatusChanged(String provider, int status, Bundle extras) {
-		// TODO Auto-generated method stub
-		
-	}
 }