altosdroid: Check state.gps != null before using it
authorKeith Packard <keithp@keithp.com>
Fri, 12 Apr 2013 08:00:36 +0000 (01:00 -0700)
committerKeith Packard <keithp@keithp.com>
Fri, 12 Apr 2013 08:00:36 +0000 (01:00 -0700)
Avoid crashing.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java
altosdroid/src/org/altusmetrum/AltosDroid/AltosDroidTab.java
altosdroid/src/org/altusmetrum/AltosDroid/TabAscent.java
altosdroid/src/org/altusmetrum/AltosDroid/TabDescent.java
altosdroid/src/org/altusmetrum/AltosDroid/TabLanded.java
altosdroid/src/org/altusmetrum/AltosDroid/TabMap.java
altosdroid/src/org/altusmetrum/AltosDroid/TabPad.java

index b1d080dbb9d67aee5eb986d1e3e4b4d894dab943..cf4227cae7895e8c713e16ee45207a177ed3b766 100644 (file)
@@ -233,9 +233,12 @@ public class AltosDroid extends FragmentActivity {
                AltosGreatCircle from_receiver = null;
 
                if (saved_location != null && state.gps != null && state.gps.locked) {
                AltosGreatCircle from_receiver = null;
 
                if (saved_location != null && state.gps != null && state.gps.locked) {
+                       double altitude = 0;
+                       if (saved_location.hasAltitude())
+                               altitude = saved_location.getAltitude();
                        from_receiver = new AltosGreatCircle(saved_location.getLatitude(),
                                                             saved_location.getLongitude(),
                        from_receiver = new AltosGreatCircle(saved_location.getLatitude(),
                                                             saved_location.getLongitude(),
-                                                            saved_location.getAltitude(),
+                                                            altitude,
                                                             state.gps.lat,
                                                             state.gps.lon,
                                                             state.gps.alt);
                                                             state.gps.lat,
                                                             state.gps.lon,
                                                             state.gps.alt);
@@ -248,7 +251,7 @@ public class AltosDroid extends FragmentActivity {
                mRSSIView.setText(String.format("%d", state.data.rssi));
 
                for (AltosDroidTab mTab : mTabs)
                mRSSIView.setText(String.format("%d", state.data.rssi));
 
                for (AltosDroidTab mTab : mTabs)
-                       mTab.update_ui(state, from_receiver);
+                       mTab.update_ui(state, from_receiver, saved_location);
 
                mAltosVoice.tell(state);
        }
 
                mAltosVoice.tell(state);
        }
index 2b5cdae77b885cf94010839d0fb31187488ef86e..6ebb47f74e3b1ff9f3218f5655c5e1d43d6e2237 100644 (file)
@@ -18,7 +18,8 @@
 package org.altusmetrum.AltosDroid;
 
 import org.altusmetrum.altoslib_1.*;
 package org.altusmetrum.AltosDroid;
 
 import org.altusmetrum.altoslib_1.*;
+import android.location.Location;
 
 public interface AltosDroidTab {
 
 public interface AltosDroidTab {
-       public void update_ui(AltosState state, AltosGreatCircle from_receiver);
+       public void update_ui(AltosState state, AltosGreatCircle from_receiver, Location receiver);
 }
 }
index ce677c57b49545b5af2c300b37f94f5914a64c86..de3bc3d29f5cefd8e5e16e2e643eff1017be410e 100644 (file)
@@ -27,6 +27,7 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.TextView;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.TextView;
+import android.location.Location;
 
 public class TabAscent extends Fragment implements AltosDroidTab {
        AltosDroid mAltosDroid;
 
 public class TabAscent extends Fragment implements AltosDroidTab {
        AltosDroid mAltosDroid;
@@ -84,7 +85,7 @@ public class TabAscent extends Fragment implements AltosDroidTab {
                mAltosDroid = null;
        }
 
                mAltosDroid = null;
        }
 
-       public void update_ui(AltosState state, AltosGreatCircle from_receiver) {
+       public void update_ui(AltosState state, AltosGreatCircle from_receiver, Location receiver) {
                mHeightView.setText(String.format("%6.0f m", state.height));
                mMaxHeightView.setText(String.format("%6.0f m", state.max_height));
                mSpeedView.setText(String.format("%6.0f m/s", state.speed()));
                mHeightView.setText(String.format("%6.0f m", state.height));
                mMaxHeightView.setText(String.format("%6.0f m", state.max_height));
                mSpeedView.setText(String.format("%6.0f m/s", state.speed()));
@@ -92,8 +93,10 @@ public class TabAscent extends Fragment implements AltosDroidTab {
                mAccelView.setText(String.format("%6.0f m/s²", state.acceleration));
                mMaxAccelView.setText(String.format("%6.0f m/s²", state.max_acceleration));
 
                mAccelView.setText(String.format("%6.0f m/s²", state.acceleration));
                mMaxAccelView.setText(String.format("%6.0f m/s²", state.max_acceleration));
 
-               mLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
-               mLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
+               if (state.gps != null) {
+                       mLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
+                       mLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
+               }
 
                mApogeeVoltageView.setText(String.format("%4.2f V", state.drogue_sense));
                mApogeeLights.set(state.drogue_sense > 3.2);
 
                mApogeeVoltageView.setText(String.format("%4.2f V", state.drogue_sense));
                mApogeeLights.set(state.drogue_sense > 3.2);
index b0c6539cf48a29673b36b5c68b5ec6ef75d16ce2..698e89fc83474c26caf62492be0f89ba0f5a79f7 100644 (file)
@@ -27,6 +27,7 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.TextView;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.TextView;
+import android.location.Location;
 
 public class TabDescent extends Fragment implements AltosDroidTab {
        AltosDroid mAltosDroid;
 
 public class TabDescent extends Fragment implements AltosDroidTab {
        AltosDroid mAltosDroid;
@@ -88,7 +89,7 @@ public class TabDescent extends Fragment implements AltosDroidTab {
                mAltosDroid = null;
        }
 
                mAltosDroid = null;
        }
 
-       public void update_ui(AltosState state, AltosGreatCircle from_receiver) {
+       public void update_ui(AltosState state, AltosGreatCircle from_receiver, Location receiver) {
                mSpeedView.setText(String.format("%6.0f m/s", state.speed()));
                mHeightView.setText(String.format("%6.0f m", state.height));
                if (from_receiver != null) {
                mSpeedView.setText(String.format("%6.0f m/s", state.speed()));
                mHeightView.setText(String.format("%6.0f m", state.height));
                if (from_receiver != null) {
@@ -104,8 +105,10 @@ public class TabDescent extends Fragment implements AltosDroidTab {
                        mCompassView.setText("<unknown>");
                        mDistanceView.setText("<unknown>");
                }
                        mCompassView.setText("<unknown>");
                        mDistanceView.setText("<unknown>");
                }
-               mLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
-               mLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
+               if (state.gps != null) {
+                       mLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
+                       mLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
+               }
 
                mApogeeVoltageView.setText(String.format("%4.2f V", state.drogue_sense));
                mApogeeLights.set(state.drogue_sense > 3.2);
 
                mApogeeVoltageView.setText(String.format("%4.2f V", state.drogue_sense));
                mApogeeLights.set(state.drogue_sense > 3.2);
index 93a42334efd872856d339efc2043febd3b95cb34..c346dc991763cdfec4e69a45ea1f0e9146ead798 100644 (file)
@@ -26,6 +26,7 @@ import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
+import android.location.Location;
 
 public class TabLanded extends Fragment implements AltosDroidTab {
        AltosDroid mAltosDroid;
 
 public class TabLanded extends Fragment implements AltosDroidTab {
        AltosDroid mAltosDroid;
@@ -68,13 +69,15 @@ public class TabLanded extends Fragment implements AltosDroidTab {
                mAltosDroid = null;
        }
 
                mAltosDroid = null;
        }
 
-       public void update_ui(AltosState state, AltosGreatCircle from_receiver) {
+       public void update_ui(AltosState state, AltosGreatCircle from_receiver, Location receiver) {
                if (from_receiver != null) {
                        mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
                        mDistanceView.setText(String.format("%6.0f m", from_receiver.distance));
                }
                if (from_receiver != null) {
                        mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
                        mDistanceView.setText(String.format("%6.0f m", from_receiver.distance));
                }
-               mLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
-               mLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
+               if (state.gps != null) {
+                       mLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
+                       mLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
+               }
                mMaxHeightView.setText(String.format("%6.0f m", state.max_height));
                mMaxAccelView.setText(String.format("%6.0f m/s²", state.max_acceleration));
                mMaxSpeedView.setText(String.format("%6.0f m/s", state.max_speed()));
                mMaxHeightView.setText(String.format("%6.0f m", state.max_height));
                mMaxAccelView.setText(String.format("%6.0f m/s²", state.max_acceleration));
                mMaxSpeedView.setText(String.format("%6.0f m/s", state.max_speed()));
index 607ded46693fc8de67dee0f0b3b1199650cafc3b..371fd9c1c617f92e3166f0254209842b8da671a5 100644 (file)
@@ -40,6 +40,7 @@ import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
+import android.location.Location;
 
 public class TabMap extends Fragment implements AltosDroidTab {
        AltosDroid mAltosDroid;
 
 public class TabMap extends Fragment implements AltosDroidTab {
        AltosDroid mAltosDroid;
@@ -50,6 +51,7 @@ public class TabMap extends Fragment implements AltosDroidTab {
 
        private Marker mRocketMarker;
        private Marker mPadMarker;
 
        private Marker mRocketMarker;
        private Marker mPadMarker;
+       private Marker mReceiverMarker;
        private Polyline mPolyline;
 
        private TextView mDistanceView;
        private Polyline mPolyline;
 
        private TextView mDistanceView;
@@ -128,6 +130,12 @@ public class TabMap extends Fragment implements AltosDroidTab {
                                                           .visible(false)
                                        );
 
                                                           .visible(false)
                                        );
 
+                       mReceiverMarker = mMap.addMarker(
+                                       new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
+                                                          .position(new LatLng(0,0))
+                                                          .visible(false)
+                                       );
+
                        mPolyline = mMap.addPolyline(
                                        new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
                                                             .width(3)
                        mPolyline = mMap.addPolyline(
                                        new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
                                                             .width(3)
@@ -139,25 +147,34 @@ public class TabMap extends Fragment implements AltosDroidTab {
                }
        }
 
                }
        }
 
-       public void update_ui(AltosState state, AltosGreatCircle from_receiver) {
+       public void update_ui(AltosState state, AltosGreatCircle from_receiver, Location receiver) {
                if (state.from_pad != null) {
                        mDistanceView.setText(String.format("%6.0f m", state.from_pad.distance));
                        mBearingView.setText(String.format("%3.0f°", state.from_pad.bearing));
                }
                if (state.from_pad != null) {
                        mDistanceView.setText(String.format("%6.0f m", state.from_pad.distance));
                        mBearingView.setText(String.format("%3.0f°", state.from_pad.bearing));
                }
-               mLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
-               mLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
+               if (state.gps != null) {
+                       mLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
+                       mLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
+               }
 
                if (mapLoaded) {
 
                if (mapLoaded) {
-                       mRocketMarker.setPosition(new LatLng(state.gps.lat, state.gps.lon));
-                       mRocketMarker.setVisible(true);
+                       if (state.gps != null) {
+                               mRocketMarker.setPosition(new LatLng(state.gps.lat, state.gps.lon));
+                               mRocketMarker.setVisible(true);
 
 
-                       mPolyline.setPoints(Arrays.asList(new LatLng(state.pad_lat, state.pad_lon), new LatLng(state.gps.lat, state.gps.lon)));
-                       mPolyline.setVisible(true);
+                               mPolyline.setPoints(Arrays.asList(new LatLng(state.pad_lat, state.pad_lon), new LatLng(state.gps.lat, state.gps.lon)));
+                               mPolyline.setVisible(true);
+                       }
 
                        if (state.state == AltosLib.ao_flight_pad) {
                                mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
                                mPadMarker.setVisible(true);
                        }
 
                        if (state.state == AltosLib.ao_flight_pad) {
                                mPadMarker.setPosition(new LatLng(state.pad_lat, state.pad_lon));
                                mPadMarker.setVisible(true);
                        }
+
+                       if (receiver != null) {
+                               mReceiverMarker.setPosition(new LatLng(receiver.getLatitude(), receiver.getLongitude()));
+                               mReceiverMarker.setVisible(true);
+                       }
                }
        }
 
                }
        }
 
index 6906324dd594fd5de27a9e346b5f4011ce1b5cae..5070ec0bb0a33d765a33bc067a2f9f285bf2a105 100644 (file)
@@ -27,6 +27,7 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.TextView;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.TextView;
+import android.location.Location;
 
 public class TabPad extends Fragment implements AltosDroidTab {
        AltosDroid mAltosDroid;
 
 public class TabPad extends Fragment implements AltosDroidTab {
        AltosDroid mAltosDroid;
@@ -100,7 +101,7 @@ public class TabPad extends Fragment implements AltosDroidTab {
                mAltosDroid = null;
        }
 
                mAltosDroid = null;
        }
 
-       public void update_ui(AltosState state, AltosGreatCircle from_receiver) {
+       public void update_ui(AltosState state, AltosGreatCircle from_receiver, Location receiver) {
                mBatteryVoltageView.setText(String.format("%4.2f V", state.battery));
                mBatteryLights.set(state.battery > 3.7);
 
                mBatteryVoltageView.setText(String.format("%4.2f V", state.battery));
                mBatteryLights.set(state.battery > 3.7);
 
@@ -122,18 +123,24 @@ public class TabPad extends Fragment implements AltosDroidTab {
                }
                mDataLoggingLights.set(state.data.flight != 0);
 
                }
                mDataLoggingLights.set(state.data.flight != 0);
 
-               mGPSLockedView.setText(String.format("%4d sats", state.gps.nsat));
-               mGPSLockedLights.set(state.gps.locked && state.gps.nsat >= 4);
-
-               if (state.gps_ready)
-                       mGPSReadyView.setText("Ready");
-               else
-                       mGPSReadyView.setText(String.format("Waiting %d", state.gps_waiting));
-               mGPSReadyLights.set(state.gps_ready);
+               if (state.gps != null) {
+                       mGPSLockedView.setText(String.format("%4d sats", state.gps.nsat));
+                       mGPSLockedLights.set(state.gps.locked && state.gps.nsat >= 4);
+                       if (state.gps_ready)
+                               mGPSReadyView.setText("Ready");
+                       else
+                               mGPSReadyView.setText(String.format("Waiting %d", state.gps_waiting));
+                       mGPSReadyLights.set(state.gps_ready);
+               }
 
 
-               mPadLatitudeView.setText(AltosDroid.pos(state.pad_lat, "N", "S"));
-               mPadLongitudeView.setText(AltosDroid.pos(state.pad_lon, "W", "E"));
-               mPadAltitudeView.setText(String.format("%4.0f m", state.pad_alt));
+               if (receiver != null) {
+                       double altitude = 0;
+                       if (receiver.hasAltitude())
+                               altitude = receiver.getAltitude();
+                       mPadLatitudeView.setText(AltosDroid.pos(receiver.getLatitude(), "N", "S"));
+                       mPadLongitudeView.setText(AltosDroid.pos(receiver.getLongitude(), "W", "E"));
+                       mPadAltitudeView.setText(String.format("%4.0f m", altitude));
+               }
        }
 
 }
        }
 
 }