Commits

Anonymous committed 0fa3cd7

acquire WakeLock to prevent CPU sleep

  • Participants
  • Parent commits f787081

Comments (0)

Files changed (4)

File AndroidManifest.xml

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
 <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
+
+
+
+
+
+
+
+<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
+
 </manifest> 

File src/to/rcpt/icarus/DataSource.java

 public class DataSource implements LocationListener {
 
 	public int gsmState;
-	public List<Sensor> sensorList;
 	
 	private SensorManager sensors;
 	private List<SensorEventAdapter> adapterlist;
 			for(float f: event.values) {
 				s += " " + f;
 			}
-			this.ds.logDataEvent(
-					event.sensor.getName(), 
-					s,
-					event.timestamp);
+			this.ds.logDataEvent(event.sensor.getName(), s, 0);
 		}
 
 		public void onAccuracyChanged(Sensor sensor, int accuracy) {
         // 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) {
+        for(Sensor s: sensors.getSensorList(Sensor.TYPE_ALL)) {
         	adapterlist.add(new SensorEventAdapter(s, this));
         }
 
 		c.setAltitudeRequired(true);
 		c.setSpeedRequired(true);
 		provider = gps.getBestProvider(c, true);
-
 	}
 	
 	public void resume() {
 	}
 	
 	public void logDataEvent(String name, String data, long ts) {
-		this.mission.logDataEvent(name, data, ts);
+		mission.logDataEvent(name, data, ts, this.gsmState == ServiceState.STATE_IN_SERVICE);
 	}
 
 	public void onLocationChanged(Location l) {

File src/to/rcpt/icarus/IcarusLogger.java

 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;
-import android.telephony.ServiceState;
 import android.telephony.SmsManager;
 
-public class IcarusLogger implements CompoundButton.OnCheckedChangeListener, LocationListener {
-	private class SensorEventAdapter implements SensorEventListener {
-		PrintWriter file;
-		SensorManager sensors;
-		Sensor sensor;
-		
-		public SensorEventAdapter(Context c, Sensor s) {
-	        sensors = (SensorManager) c.getSystemService(Context.SENSOR_SERVICE);
-	        sensor = s;
-		}
-		
-		public void onAccuracyChanged(Sensor sensor, int accuracy) {
-		}
+public class IcarusLogger implements CompoundButton.OnCheckedChangeListener {
 
-		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();
-		}
-	}
+	final String logfileName = "IcarusLog";
+	final String destPhoneNumber = "4154836353";
+	final long sendInterval = 60000;
+
+	private PrintWriter file;
+	private SmsManager smsmanager;
+	private long lastSent;
 	
-	List<SensorEventAdapter> sensorlogs;
-	private LocationManager gps;
-	private String provider;
-	private PrintWriter gpsfile;
-	private SmsManager smsmanager;
-	private ServiceState servicestate;
-
-	private long lastSent;
-	final long sendInterval = 60000;
+	private boolean logging;
 	
-	public IcarusLogger(Context context) {
-		// TODO: abstract setup code with IcarusMission
+	public IcarusLogger() {
 		smsmanager = SmsManager.getDefault();
-		servicestate = new ServiceState();
 		lastSent = 0;
-		
-        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) {
-			startGpsLogging();
-			for(SensorEventAdapter s: sensorlogs) {
-				s.startLogging();
-			}
-		} else {
-			stopGpsLogging();
-			for(SensorEventAdapter s: sensorlogs) {
-				s.stopLogging();
-			}
-		}
+		logging = false;
 	}
 
-	public void startGpsLogging() {
+	public void startLogging() {
 		try {
-			File f = new File("/sdcard", provider);
+			File f = new File("/sdcard", logfileName);
 			try {
 				f.createNewFile();
 			} catch(IOException e) {
 				throw new RuntimeException(e);
 			}
-			gpsfile = new PrintWriter(new FileOutputStream(f, true));
+			file = new PrintWriter(new FileOutputStream(f, true));
 		} catch(FileNotFoundException e) {
 			throw new RuntimeException(e);
 		}
-		gps.requestLocationUpdates(provider, 0, 0, this);
+		logging = true;
+	}
+		
+	public void stopLogging() {
+		if (logging)
+			return;
+		file.close();
+		logging = false;
 	}
 	
-	public void stopGpsLogging() {
-		gps.removeUpdates(this);
-		gpsfile.close();
-	}
-	
-	public void onLocationChanged(Location l) {
-		long curTime = l.getTime();
-		String update = curTime + " " + l.getLatitude() + "," + l.getLongitude() + " " + l.getAltitude();
-		
+	public void logDataEvent(String name, String data, long ts, boolean hasService) {
+		if (!logging)
+			return;
+
 		// update the file
-		gpsfile.println(update);
+		file.println(name + ": " + data + " " + ts);
+
+		if (name != "gps") // only send SMS for gps coords
+			return;
 
 		// send SMS if okay to do so
-		boolean hasService = servicestate.getState() == ServiceState.STATE_IN_SERVICE;
-		hasService = true;
-		boolean sendIntervalExceeded = lastSent < (l.getTime()-sendInterval);
+		boolean sendIntervalExceeded = lastSent < (ts-sendInterval);
 		if (hasService && sendIntervalExceeded) {
-			smsmanager.sendTextMessage("4154836353", null, update, null, null);
-			lastSent = curTime;
+			smsmanager.sendTextMessage(destPhoneNumber, null, data, null, null);
+			lastSent = ts;
 		}
 	}
 
-	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
-		
+	public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
+		logging = isChecked;
+		if (isChecked)
+			this.startLogging();
+		else
+			this.stopLogging();
 	}
 	
-	
 }

File src/to/rcpt/icarus/IcarusMission.java

 
 import java.util.HashMap;
 
+import android.content.Context;
 import android.app.Activity;
-import android.hardware.Sensor;
+import android.os.PowerManager;
 import android.os.Bundle;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 import android.view.View;
 import android.view.View.OnClickListener;
 
-import to.rcpt.icarus.DataSource;
-
 public class IcarusMission extends Activity {
 
 	private class ExitActivityListener implements OnClickListener {
 	private HashMap<String, TextView> dataViews;
 	
 	private DataSource ds;
+	private IcarusLogger logger;
+	private PowerManager pm;
+	private PowerManager.WakeLock wl;
 	
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        
-        this.ds = new DataSource(this);
+
+        logger = new IcarusLogger();
+		ds = new DataSource(this);
+		
+		pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
+		wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "IcarusMission");
 
     	index = 0;
         ll = new LinearLayout(this);
         ToggleButton b = new ToggleButton(this);
         b.setTextOff("Start mission");
         b.setTextOn("Stop mission");
-        b.setOnCheckedChangeListener(new IcarusLogger(this));
+        b.setOnCheckedChangeListener(logger);
         ll.addView(b, index++);
         
         tv = new TextView(this);
         dataViews = new HashMap<String, TextView>();
     }
     
-    @Override
     public void onResume() {
     	super.onResume();
-		this.ds.resume();
+    	ds.resume();
+    	wl.acquire();
     }
     
-    @Override
     public void onPause() {
     	super.onPause();
-		this.ds.pause();
+    	ds.pause();
     }
     
-    public void logDataEvent(String name, String data, long ts) {
-    	tv.setText("Timestamp: "+ts);
+    public void onDestroy() {
+    	super.onDestroy();
+    	logger.stopLogging();
+    	if (wl.isHeld())
+    		wl.release();
+    }
+    
+    public void logDataEvent(String name, String data, long ts, boolean hasService) {
+    	logger.logDataEvent(name, data, ts, hasService);
+    	if (ts > 0)
+    		tv.setText("Timestamp: "+ts);
     	TextView t;
     	if (!dataViews.containsKey(name))
     	{