Commits

Anonymous committed 0e3c4ba

Sensor data logged to sdcard
Checkbox to control logging

Comments (0)

Files changed (3)

AndroidManifest.xml

 
 
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
+<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
 </manifest> 

src/to/rcpt/icarus/IcarusLogger.java

+package to.rcpt.icarus;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintWriter;
+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.widget.CompoundButton;
+
+public class IcarusLogger implements CompoundButton.OnCheckedChangeListener, LocationListener {
+	private class SensorEventAdapter implements SensorEventListener {
+		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) {
+			// TODO Auto-generated method stub
+			
+		}
+
+		public void onSensorChanged(SensorEvent event) {
+			String s = event.timestamp + "";
+			for(float f: event.values) {
+				s += " " + f;
+			}
+			file.println(s);
+		}
+		
+		public void startLogging() {
+			try {
+				File f = new File("/sdcard", sensor.getName());
+				try {
+					f.createNewFile();
+				} catch(IOException e) {
+					throw new RuntimeException(e);
+				}
+				file = new PrintWriter(new FileOutputStream(f, true));
+			} catch(FileNotFoundException e) {
+				throw new RuntimeException(e);
+			}
+        	sensors.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);			
+		}
+		
+		public void stopLogging() {
+			sensors.unregisterListener(this);
+			file.close();
+		}
+	}
+	
+	List<SensorEventAdapter> sensorlogs;
+	private LocationManager gps;
+	private String provider;
+	
+	public IcarusLogger(Context context) {
+		// TODO: abstract setup code with IcarusMission
+        SensorManager sensors = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
+        List<Sensor> sensorList = sensors.getSensorList(Sensor.TYPE_ALL);
+        sensorlogs = new LinkedList<SensorEventAdapter>();
+        for(Sensor s: sensorList) {
+        	sensorlogs.add(new SensorEventAdapter(context, s));
+        }
+		gps = (LocationManager) context.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 onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+		if (isChecked) {
+			gps.requestLocationUpdates(provider, 0, 0, this);
+			for(SensorEventAdapter s: sensorlogs) {
+				s.startLogging();
+			}
+		} else {
+			gps.removeUpdates(this);
+			for(SensorEventAdapter s: sensorlogs) {
+				s.stopLogging();
+			}
+		}
+	}
+
+	public void onLocationChanged(Location location) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	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
+		
+	}
+	
+	
+}

src/to/rcpt/icarus/IcarusMission.java

 import android.os.Bundle;
 import android.widget.LinearLayout;
 import android.widget.TextView;
+import android.widget.ToggleButton;
 
 public class IcarusMission extends Activity implements LocationListener {
 	private class SensorEventAdapter implements SensorEventListener {
         super.onCreate(savedInstanceState);
         ll = new LinearLayout(this);
         ll.setOrientation(LinearLayout.VERTICAL);
+        ToggleButton b = new ToggleButton(this);
+        b.setTextOff("Start mission");
+        b.setTextOn("Stop mission");
+        b.setOnCheckedChangeListener(new IcarusLogger(this));
+        ll.addView(b, index++);
         tv = new TextView(this);
         tv.setText("Hello, Icarus");
         ll.addView(tv, index++);
     }
 
 	public void onLocationChanged(Location l) {
-		TextView tv = (TextView) ll.getChildAt(0);
+		TextView tv = (TextView) ll.getChildAt(1);
 		tv.setText(l.getTime() + " " + l.getLatitude() + "," + l.getLongitude() + " " + l.getAltitude());
 	}