2171afa461cc62f310b2531a1c387a891b873b94
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TabDescent.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_5.*;
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.ImageView;
29 import android.widget.TextView;
30 import android.location.Location;
31
32 public class TabDescent extends Fragment implements AltosDroidTab {
33         AltosDroid mAltosDroid;
34
35         private TextView mSpeedView;
36         private TextView mHeightView;
37         private TextView mElevationView;
38         private TextView mRangeView;
39         private TextView mBearingView;
40         private TextView mCompassView;
41         private TextView mDistanceView;
42         private TextView mLatitudeView;
43         private TextView mLongitudeView;
44         private TextView mApogeeVoltageView;
45         private GoNoGoLights mApogeeLights;
46         private TextView mMainVoltageView;
47         private GoNoGoLights mMainLights;
48
49
50         @Override
51         public void onAttach(Activity activity) {
52                 super.onAttach(activity);
53                 mAltosDroid = (AltosDroid) activity;
54                 mAltosDroid.registerTab(this);
55         }
56
57         @Override
58         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
59                 View v = inflater.inflate(R.layout.tab_descent, container, false);
60
61                 mSpeedView     = (TextView) v.findViewById(R.id.speed_value);
62                 mHeightView    = (TextView) v.findViewById(R.id.height_value);
63                 mElevationView = (TextView) v.findViewById(R.id.elevation_value);
64                 mRangeView     = (TextView) v.findViewById(R.id.range_value);
65                 mBearingView   = (TextView) v.findViewById(R.id.bearing_value);
66                 mCompassView   = (TextView) v.findViewById(R.id.compass_value);
67                 mDistanceView  = (TextView) v.findViewById(R.id.distance_value);
68                 mLatitudeView  = (TextView) v.findViewById(R.id.lat_value);
69                 mLongitudeView = (TextView) v.findViewById(R.id.lon_value);
70
71                 mApogeeVoltageView = (TextView) v.findViewById(R.id.apogee_voltage_value);
72                 mApogeeLights = new GoNoGoLights((ImageView) v.findViewById(R.id.apogee_redled),
73                                                  (ImageView) v.findViewById(R.id.apogee_greenled),
74                                                  getResources());
75
76                 mMainVoltageView = (TextView) v.findViewById(R.id.main_voltage_value);
77                 mMainLights = new GoNoGoLights((ImageView) v.findViewById(R.id.main_redled),
78                                                (ImageView) v.findViewById(R.id.main_greenled),
79                                                getResources());
80
81                 return v;
82         }
83
84
85         @Override
86         public void onDestroy() {
87                 super.onDestroy();
88                 mAltosDroid.unregisterTab(this);
89                 mAltosDroid = null;
90         }
91
92         public void update_ui(AltosState state, AltosGreatCircle from_receiver, Location receiver) {
93                 if (state != null) {
94                         mSpeedView.setText(AltosDroid.number("%6.0f m/s", state.speed()));
95                         mHeightView.setText(AltosDroid.number("%6.0f m", state.height()));
96                         if (from_receiver != null) {
97                                 mElevationView.setText(AltosDroid.number("%3.0f°", from_receiver.elevation));
98                                 mRangeView.setText(AltosDroid.number("%6.0f m", from_receiver.range));
99                                 mBearingView.setText(AltosDroid.number("%3.0f°", from_receiver.bearing));
100                                 mCompassView.setText(from_receiver.bearing_words(AltosGreatCircle.BEARING_LONG));
101                                 mDistanceView.setText(AltosDroid.number("%6.0f m", from_receiver.distance));
102                         } else { 
103                                 mElevationView.setText("<unknown>");
104                                 mRangeView.setText("<unknown>");
105                                 mBearingView.setText("<unknown>");
106                                 mCompassView.setText("<unknown>");
107                                 mDistanceView.setText("<unknown>");
108                         }
109                         if (state.gps != null) {
110                                 mLatitudeView.setText(AltosDroid.pos(state.gps.lat, "N", "S"));
111                                 mLongitudeView.setText(AltosDroid.pos(state.gps.lon, "W", "E"));
112                         }
113
114                         mApogeeVoltageView.setText(AltosDroid.number("%4.2f V", state.apogee_voltage));
115                         mApogeeLights.set(state.apogee_voltage > 3.2, state.apogee_voltage == AltosLib.MISSING);
116
117                         mMainVoltageView.setText(AltosDroid.number("%4.2f V", state.main_voltage));
118                         mMainLights.set(state.main_voltage > 3.2, state.main_voltage == AltosLib.MISSING);
119                 }
120         }
121
122 }