altosdroid: Add quit. Restart. Show freq in title.
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TabPad.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 TabPad extends AltosDroidTab {
33         AltosDroid mAltosDroid;
34
35         private TextView mBatteryVoltageView;
36         private TextView mBatteryVoltageLabel;
37         private GoNoGoLights mBatteryLights;
38         private TextView mApogeeVoltageView;
39         private TextView mApogeeVoltageLabel;
40         private GoNoGoLights mApogeeLights;
41         private TextView mMainVoltageView;
42         private TextView mMainVoltageLabel;
43         private GoNoGoLights mMainLights;
44         private TextView mDataLoggingView;
45         private GoNoGoLights mDataLoggingLights;
46         private TextView mGPSLockedView;
47         private GoNoGoLights mGPSLockedLights;
48         private TextView mGPSReadyView;
49         private GoNoGoLights mGPSReadyLights;
50         private TextView mPadLatitudeView;
51         private TextView mPadLongitudeView;
52         private TextView mPadAltitudeView;
53
54         @Override
55         public void onAttach(Activity activity) {
56                 super.onAttach(activity);
57                 mAltosDroid = (AltosDroid) activity;
58                 mAltosDroid.registerTab(this);
59         }
60
61         @Override
62         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
63                 View v = inflater.inflate(R.layout.tab_pad, container, false);
64                 mBatteryVoltageView = (TextView) v.findViewById(R.id.battery_voltage_value);
65                 mBatteryVoltageLabel = (TextView) v.findViewById(R.id.battery_voltage_label);
66                 mBatteryLights = new GoNoGoLights((ImageView) v.findViewById(R.id.battery_redled),
67                                                   (ImageView) v.findViewById(R.id.battery_greenled),
68                                                   getResources());
69
70                 mApogeeVoltageView = (TextView) v.findViewById(R.id.apogee_voltage_value);
71                 mApogeeVoltageLabel = (TextView) v.findViewById(R.id.apogee_voltage_label);
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                 mMainVoltageLabel = (TextView) v.findViewById(R.id.main_voltage_label);
78                 mMainLights = new GoNoGoLights((ImageView) v.findViewById(R.id.main_redled),
79                                                (ImageView) v.findViewById(R.id.main_greenled),
80                                                getResources());
81
82                 mDataLoggingView = (TextView) v.findViewById(R.id.logging_value);
83                 mDataLoggingLights = new GoNoGoLights((ImageView) v.findViewById(R.id.logging_redled),
84                                                       (ImageView) v.findViewById(R.id.logging_greenled),
85                                                       getResources());
86
87                 mGPSLockedView = (TextView) v.findViewById(R.id.gps_locked_value);
88                 mGPSLockedLights = new GoNoGoLights((ImageView) v.findViewById(R.id.gps_locked_redled),
89                                                     (ImageView) v.findViewById(R.id.gps_locked_greenled),
90                                                     getResources());
91
92                 mGPSReadyView = (TextView) v.findViewById(R.id.gps_ready_value);
93                 mGPSReadyLights = new GoNoGoLights((ImageView) v.findViewById(R.id.gps_ready_redled),
94                                                    (ImageView) v.findViewById(R.id.gps_ready_greenled),
95                                                    getResources());
96
97                 mPadLatitudeView = (TextView) v.findViewById(R.id.pad_lat_value);
98                 mPadLongitudeView = (TextView) v.findViewById(R.id.pad_lon_value);
99                 mPadAltitudeView = (TextView) v.findViewById(R.id.pad_alt_value);
100         return v;
101         }
102
103         @Override
104         public void onDestroy() {
105                 super.onDestroy();
106                 mAltosDroid.unregisterTab(this);
107                 mAltosDroid = null;
108         }
109
110         public String tab_name() { return "pad"; }
111
112         public void show(AltosState state, AltosGreatCircle from_receiver, Location receiver) {
113                 if (state != null) {
114                         mBatteryVoltageView.setText(AltosDroid.number("%4.2f V", state.battery_voltage));
115                         mBatteryLights.set(state.battery_voltage >= AltosLib.ao_battery_good, state.battery_voltage == AltosLib.MISSING);
116                         if (state.apogee_voltage == AltosLib.MISSING) {
117                                 mApogeeVoltageView.setVisibility(View.GONE);
118                                 mApogeeVoltageLabel.setVisibility(View.GONE);
119                         } else {
120                                 mApogeeVoltageView.setText(AltosDroid.number("%4.2f V", state.apogee_voltage));
121                                 mApogeeVoltageView.setVisibility(View.VISIBLE);
122                                 mApogeeVoltageLabel.setVisibility(View.VISIBLE);
123                         }
124                         mApogeeLights.set(state.apogee_voltage >= AltosLib.ao_igniter_good, state.apogee_voltage == AltosLib.MISSING);
125                         if (state.main_voltage == AltosLib.MISSING) {
126                                 mMainVoltageView.setVisibility(View.GONE);
127                                 mMainVoltageLabel.setVisibility(View.GONE);
128                         } else {
129                                 mMainVoltageView.setText(AltosDroid.number("%4.2f V", state.main_voltage));
130                                 mMainVoltageView.setVisibility(View.VISIBLE);
131                                 mMainVoltageLabel.setVisibility(View.VISIBLE);
132                         }
133                         mMainLights.set(state.main_voltage >= AltosLib.ao_igniter_good, state.main_voltage == AltosLib.MISSING);
134
135                         if (state.flight != 0) {
136                                 if (state.state <= AltosLib.ao_flight_pad)
137                                         mDataLoggingView.setText("Ready to record");
138                                 else if (state.state < AltosLib.ao_flight_landed)
139                                         mDataLoggingView.setText("Recording data");
140                                 else
141                                         mDataLoggingView.setText("Recorded data");
142                         } else {
143                                 mDataLoggingView.setText("Storage full");
144                         }
145                         mDataLoggingLights.set(state.flight != 0, state.flight == AltosLib.MISSING);
146
147                         if (state.gps != null) {
148                                 int soln = state.gps.nsat;
149                                 int nsat = state.gps.cc_gps_sat != null ? state.gps.cc_gps_sat.length : 0;
150                                 mGPSLockedView.setText(String.format("%4d in soln, %4d in view", soln, nsat));
151                                 mGPSLockedLights.set(state.gps.locked && state.gps.nsat >= 4, false);
152                                 if (state.gps_ready)
153                                         mGPSReadyView.setText("Ready");
154                                 else
155                                         mGPSReadyView.setText(AltosDroid.integer("Waiting %d", state.gps_waiting));
156                         } else
157                                 mGPSLockedLights.set(false, true);
158                         mGPSReadyLights.set(state.gps_ready, state.gps == null);
159                 }
160
161                 if (receiver != null) {
162                         double altitude = 0;
163                         if (receiver.hasAltitude())
164                                 altitude = receiver.getAltitude();
165                         mPadLatitudeView.setText(AltosDroid.pos(receiver.getLatitude(), "N", "S"));
166                         mPadLongitudeView.setText(AltosDroid.pos(receiver.getLongitude(), "W", "E"));
167                         mPadAltitudeView.setText(AltosDroid.number("%4.0f m", altitude));
168                 }
169         }
170
171 }