Commits

Anonymous committed f65ffa7

sms support, service state

  • Participants
  • Parent commits 8dde2fd

Comments (0)

Files changed (4)

File AndroidManifest.xml

       package="to.rcpt.icarus"
       android:versionCode="1"
       android:versionName="1.0">
-    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
+    <application android:label="@string/app_name" android:debuggable="true">
         <activity android:name=".IcarusMission"
                   android:label="@string/app_name">
             <intent-filter>
 
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
 <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>
 </manifest> 

File res/values/strings.xml

+<resources><string name="app_name">Icarus</string>
+</resources>

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

 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 {
 		}
 		
 		public void onAccuracyChanged(Sensor sensor, int accuracy) {
-			// TODO Auto-generated method stub
-			
 		}
 
 		public void onSensorChanged(SensorEvent event) {
 	private LocationManager gps;
 	private String provider;
 	private PrintWriter gpsfile;
+	private SmsManager smsmanager;
+	private ServiceState servicestate;
+
+	private long lastSent;
+	final long sendInterval = 60000;
 	
 	public IcarusLogger(Context context) {
 		// TODO: abstract setup code with IcarusMission
+		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>();
 		c.setAltitudeRequired(true);
 		c.setSpeedRequired(true);
 		provider = gps.getBestProvider(c, true);
+
 	}
 	
 	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 	}
 	
 	public void onLocationChanged(Location l) {
-		String update = l.getTime() + " " + l.getLatitude() + "," + l.getLongitude() + " " + l.getAltitude();
+		long curTime = l.getTime();
+		String update = curTime + " " + l.getLatitude() + "," + l.getLongitude() + " " + l.getAltitude();
+		
+		// update the file
 		gpsfile.println(update);
+
+		// send SMS if okay to do so
+		boolean hasService = servicestate.getState() == ServiceState.STATE_IN_SERVICE;
+		hasService = true;
+		boolean sendIntervalExceeded = lastSent < (l.getTime()-sendInterval);
+		if (hasService && sendIntervalExceeded) {
+			smsmanager.sendTextMessage("4154836353", null, update, null, null);
+			lastSent = curTime;
+		}
 	}
 
 	public void onProviderDisabled(String provider) {

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

 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.widget.LinearLayout;
 import android.widget.TextView;
 import android.widget.ToggleButton;
+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 {
+
 	private SensorManager sensors;
 
 	private class SensorEventAdapter implements SensorEventListener {
 			sensors.unregisterListener(this);
 		}
 	}
+	
+	private class ExitActivityListener implements OnClickListener {
+		private IcarusMission act;
+		public ExitActivityListener(IcarusMission a) {
+			act = a;
+		}
 
+		public void onClick(View v) {
+			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;
 	
     @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);
+
     	index = 0;
-        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++);
+
         setContentView(ll);
         sensors = (SensorManager) this.getSystemService(SENSOR_SERVICE);
         adapterlist = new LinkedList<SensorEventAdapter>();
 		c.setAltitudeRequired(true);
 		c.setSpeedRequired(true);
 		provider = gps.getBestProvider(c, true);
+
+		Button exitButton = new Button(this);
+        exitButton.setOnClickListener(new ExitActivityListener(this));
+        exitButton.setText("Exit");
+        ll.addView(exitButton, index++);        
     }
     
     @Override
 
 	public void onLocationChanged(Location l) {
 		TextView tv = (TextView) ll.getChildAt(1);
-		tv.setText(l.getTime() + " " + l.getLatitude() + "," + l.getLongitude() + " " + l.getAltitude());
+		tv.setText(l.getTime() + " " + l.getLatitude() + "," + l.getLongitude() + " " + l.getAltitude() + " " + gsmState);
 	}
 
 	public void onProviderDisabled(String provider) {