altosdroid: Check state.gps != null before using it
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TabLanded.java
1 /*
2  * Copyright © 2013 Mike Beattie <mike@ethernal.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.AltosDroid;
19
20 import org.altusmetrum.altoslib_1.*;
21
22 import android.app.Activity;
23 import android.os.Bundle;
24 import android.support.v4.app.Fragment;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.TextView;
29 import android.location.Location;
30
31 public class TabLanded extends Fragment implements AltosDroidTab {
32         AltosDroid mAltosDroid;
33
34         private TextView mBearingView;
35         private TextView mDistanceView;
36         private TextView mLatitudeView;
37         private TextView mLongitudeView;
38         private TextView mMaxHeightView;
39         private TextView mMaxSpeedView;
40         private TextView mMaxAccelView;
41
42
43         @Override
44         public void onAttach(Activity activity) {
45                 super.onAttach(activity);
46                 mAltosDroid = (AltosDroid) activity;
47                 mAltosDroid.registerTab(this);
48         }
49
50         @Override
51         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
52                 View v = inflater.inflate(R.layout.tab_landed, container, false);
53
54                 mBearingView   = (TextView) v.findViewById(R.id.bearing_value);
55                 mDistanceView  = (TextView) v.findViewById(R.id.distance_value);
56                 mLatitudeView  = (TextView) v.findViewById(R.id.lat_value);
57                 mLongitudeView = (TextView) v.findViewById(R.id.lon_value);
58                 mMaxHeightView = (TextView) v.findViewById(R.id.max_height_value);
59                 mMaxSpeedView  = (TextView) v.findViewById(R.id.max_speed_value);
60                 mMaxAccelView  = (TextView) v.findViewById(R.id.max_accel_value);
61
62                 return v;
63         }
64
65         @Override
66         public void onDestroy() {
67                 super.onDestroy();
68                 mAltosDroid.unregisterTab(this);
69                 mAltosDroid = null;
70         }
71
72         public void update_ui(AltosState state, AltosGreatCircle from_receiver, Location receiver) {
73                 if (from_receiver != null) {
74                         mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
75                         mDistanceView.setText(String.format("%6.0f m", from_receiver.distance));
76                 }
77                 if (state.gps != null) {
78                         mLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
79                         mLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
80                 }
81                 mMaxHeightView.setText(String.format("%6.0f m", state.max_height));
82                 mMaxAccelView.setText(String.format("%6.0f m/s²", state.max_acceleration));
83                 mMaxSpeedView.setText(String.format("%6.0f m/s", state.max_speed()));
84         }
85
86 }