altosdroid: Send LOCATION and CRC_ERROR messages to UI.
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TelemetryService.java
index 5ff00a680a5d50c974419242a3be74fe054f8126..98b7d32ff68eff665732cf6685dac49f3244c129 100644 (file)
@@ -29,7 +29,8 @@ import android.app.PendingIntent;
 import android.app.Service;
 import android.bluetooth.BluetoothDevice;
 import android.content.Intent;
-//import android.os.Bundle;
+import android.content.Context;
+import android.os.Bundle;
 import android.os.IBinder;
 import android.os.Handler;
 import android.os.Message;
@@ -37,9 +38,35 @@ import android.os.Messenger;
 import android.os.RemoteException;
 import android.util.Log;
 import android.widget.Toast;
+import android.location.Location;
+import android.location.LocationManager;
+import android.location.LocationListener;
 
 import org.altusmetrum.altoslib_1.*;
 
+class AltosLocationListener implements LocationListener {
+       TelemetryService service;
+       boolean fine;
+
+       public void onLocationChanged(Location location) {
+               service.sendLocation(location);
+       }
+
+       public void onStatusChanged(String provider, int status, Bundle extras) {
+       }
+
+       public void onProviderEnabled(String provider) {
+       }
+
+       public void onProviderDisabled(String provider) {
+       }
+
+       public AltosLocationListener(TelemetryService service, boolean fine) {
+               this.fine = fine;
+               this.service = service;
+       }
+}
+
 public class TelemetryService extends Service {
 
        private static final String TAG = "TelemetryService";
@@ -53,6 +80,8 @@ public class TelemetryService extends Service {
        static final int MSG_DISCONNECTED      = 6;
        static final int MSG_TELEMETRY         = 7;
        static final int MSG_SETFREQUENCY      = 8;
+       static final int MSG_LOCATION          = 9;
+       static final int MSG_CRC_ERROR         = 10;
 
        public static final int STATE_NONE       = 0;
        public static final int STATE_READY      = 1;
@@ -81,6 +110,17 @@ public class TelemetryService extends Service {
        // internally track state of bluetooth connection
        private int state = STATE_NONE;
 
+       // location listeners
+
+       private AltosLocationListener gpsListener;
+       private AltosLocationListener netListener;
+       
+       // Last data seen; send to UI when it starts
+
+       private AltosState last_state;
+       private Location last_location;
+       private int last_crc_errors;
+
        // Handler of incoming messages from clients.
        static class IncomingHandler extends Handler {
                private final WeakReference<TelemetryService> service;
@@ -100,6 +140,12 @@ public class TelemetryService extends Service {
                                        s.mClients.remove(msg.replyTo);
                                }
                                if (D) Log.d(TAG, "Client bound to service");
+                               if (s.last_state != null)
+                                       s.sendTelemetry(s.last_state);
+                               if (s.last_location != null)
+                                       s.sendLocation(s.last_location);
+                               if (s.last_crc_errors != 0)
+                                       s.sendCrcErrors(s.last_crc_errors);
                                break;
                        case MSG_UNREGISTER_CLIENT:
                                s.mClients.remove(msg.replyTo);
@@ -143,6 +189,21 @@ public class TelemetryService extends Service {
                }
        }
 
+       public void sendTelemetry(AltosState state) {
+               last_state = state;
+               mHandler.obtainMessage(MSG_TELEMETRY, state).sendToTarget();
+       }
+
+       public void sendLocation(Location location) {
+               last_location = location;
+               mHandler.obtainMessage(MSG_LOCATION, location).sendToTarget();
+       }
+
+       public void sendCrcErrors(int crc_errors) {
+               last_crc_errors = crc_errors;
+               mHandler.obtainMessage(MSG_CRC_ERROR, new Integer(crc_errors)).sendToTarget();
+       }
+
        private void sendMessageToClients(Message m) {
                for (int i=mClients.size()-1; i>=0; i--) {
                        try {
@@ -220,7 +281,7 @@ public class TelemetryService extends Service {
 
                setState(STATE_CONNECTED);
 
-               mTelemetryReader = new TelemetryReader(mAltosBluetooth, mHandler);
+               mTelemetryReader = new TelemetryReader(this, mAltosBluetooth, mHandler);
                mTelemetryReader.start();
                
                mTelemetryLogger = new TelemetryLogger(this, mAltosBluetooth);
@@ -249,6 +310,14 @@ public class TelemetryService extends Service {
                // Start our timer - first event in 10 seconds, then every 10 seconds after that.
                timer.scheduleAtFixedRate(new TimerTask(){ public void run() {onTimerTick();}}, 10000L, 10000L);
 
+               // Listen for GPS and Network position updates
+               gpsListener = new AltosLocationListener(this, true);
+               netListener = new AltosLocationListener(this, false);
+
+               LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
+               
+               locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsListener);
+               locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, netListener);
        }
 
        @Override
@@ -281,6 +350,11 @@ public class TelemetryService extends Service {
        @Override
        public void onDestroy() {
 
+               // Stop listening for location updates
+               LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
+               locationManager.removeUpdates(gpsListener);
+               locationManager.removeUpdates(netListener);
+
                // Stop the bluetooth Comms threads
                stopAltosBluetooth();