altosdroid: Track device location in app, not telemetry service
authorKeith Packard <keithp@keithp.com>
Fri, 22 Apr 2016 21:32:10 +0000 (17:32 -0400)
committerKeith Packard <keithp@keithp.com>
Fri, 22 Apr 2016 22:49:35 +0000 (18:49 -0400)
This means we get location even if there isn't a telemetry device
connected, making it possible to walk to old device locations

Signed-off-by: Keith Packard <keithp@keithp.com>
altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java
altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java
altosdroid/src/org/altusmetrum/AltosDroid/TelemetryState.java

index f317f31da5803b5002fff50e2743e44a00390920..855133253cd36e6ae142c3316698cae48fb7dc4d 100644 (file)
@@ -45,13 +45,15 @@ import android.view.*;
 import android.widget.*;
 import android.app.AlertDialog;
 import android.location.Location;
+import android.location.LocationManager;
+import android.location.LocationListener;
 import android.hardware.usb.*;
 import android.graphics.*;
 import android.graphics.drawable.*;
 
 import org.altusmetrum.altoslib_10.*;
 
-public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
+public class AltosDroid extends FragmentActivity implements AltosUnitsListener, LocationListener {
 
        // Actions sent to the telemetry server at startup time
 
@@ -98,6 +100,8 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
        private double frequency;
        private int telemetry_rate;
 
+       public Location location = null;
+
        // Tabs
        TabHost         mTabHost;
        AltosViewPager  mViewPager;
@@ -314,7 +318,7 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                                state = newest_state;
                }
 
-               update_ui(telemetry_state, state, telemetry_state.location);
+               update_ui(telemetry_state, state);
 
                start_timer();
        }
@@ -379,7 +383,7 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                }
        }
 
-       void update_ui(TelemetryState telem_state, AltosState state, Location location) {
+       void update_ui(TelemetryState telem_state, AltosState state) {
 
                int prev_state = AltosLib.ao_flight_invalid;
 
@@ -679,12 +683,26 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
        public void onResume() {
                super.onResume();
                AltosDebug.debug("+ ON RESUME +");
+
+               // Listen for GPS and Network position updates
+               LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
+               locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
+
+               location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
+
+               AltosDebug.debug("Resume, location is %f,%f\n",
+                                location.getLatitude(),
+                                location.getLongitude());
+
+               update_ui(telemetry_state, saved_state);
        }
 
        @Override
        public void onPause() {
                super.onPause();
                AltosDebug.debug("- ON PAUSE -");
+               // Stop listening for location updates
+               ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);
        }
 
        @Override
@@ -1015,7 +1033,7 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
        }
 
        static String direction(AltosGreatCircle from_receiver,
-                            Location receiver) {
+                               Location receiver) {
                if (from_receiver == null)
                        return null;
 
@@ -1044,4 +1062,24 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                else
                        return String.format("right %d°", iheading);
        }
+
+       public void onLocationChanged(Location location) {
+               this.location = location;
+               AltosDebug.debug("Location changed to %f,%f",
+                                location.getLatitude(),
+                                location.getLongitude());
+               update_ui(telemetry_state, saved_state);
+       }
+
+       public void onStatusChanged(String provider, int status, Bundle extras) {
+               AltosDebug.debug("Location status now %d\n", status);
+       }
+
+       public void onProviderEnabled(String provider) {
+               AltosDebug.debug("Location provider enabled %s\n", provider);
+       }
+
+       public void onProviderDisabled(String provider) {
+               AltosDebug.debug("Location provider disabled %s\n", provider);
+       }
 }
index 63592e2999d26e2ae09b66bd5e4a59b7cb1df32a..1834d55b2eb1fa4bb3280803b5fbc1a283b06f21 100644 (file)
@@ -38,14 +38,11 @@ import android.os.Messenger;
 import android.os.RemoteException;
 import android.os.Looper;
 import android.widget.Toast;
-import android.location.Location;
-import android.location.LocationManager;
-import android.location.LocationListener;
 import android.location.Criteria;
 
 import org.altusmetrum.altoslib_10.*;
 
-public class TelemetryService extends Service implements LocationListener {
+public class TelemetryService extends Service {
 
        static final int MSG_REGISTER_CLIENT   = 1;
        static final int MSG_UNREGISTER_CLIENT = 2;
@@ -484,11 +481,6 @@ public class TelemetryService extends Service implements LocationListener {
                                telemetry_state.states.put(serial, saved_state.state);
                        }
                }
-
-               // Listen for GPS and Network position updates
-               LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
-
-               locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
        }
 
        @Override
@@ -535,9 +527,6 @@ public class TelemetryService extends Service implements LocationListener {
        @Override
        public void onDestroy() {
 
-               // Stop listening for location updates
-               ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);
-
                // Stop the bluetooth Comms threads
                disconnect(true);
 
@@ -552,21 +541,4 @@ public class TelemetryService extends Service implements LocationListener {
        public IBinder onBind(Intent intent) {
                return messenger.getBinder();
        }
-
-
-       public void onLocationChanged(Location location) {
-               telemetry_state.location = location;
-               AltosDebug.debug("location changed");
-               send_to_clients();
-       }
-
-       public void onStatusChanged(String provider, int status, Bundle extras) {
-       }
-
-       public void onProviderEnabled(String provider) {
-       }
-
-       public void onProviderDisabled(String provider) {
-       }
-
 }
index ec9f479828a543ed09a7063a1b1f7d4a55ee6388..d3ccf0a9d8e576bab5e75c5cb96f2d0906790677 100644 (file)
@@ -30,7 +30,6 @@ public class TelemetryState {
        int             connect;
        DeviceAddress   address;
        AltosConfigData config;
-       Location        location;
        int             crc_errors;
        double          receiver_battery;
        double          frequency;
@@ -44,7 +43,6 @@ public class TelemetryState {
                connect = CONNECT_NONE;
                config = null;
                states = new HashMap<Integer,AltosState>();
-               location = null;
                crc_errors = 0;
                receiver_battery = AltosLib.MISSING;
                frequency = AltosPreferences.frequency(0);