altosdroid: Import initial versions of XML and Java for Tab content
[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_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.ImageView;
29 import android.widget.TextView;
30
31 public class TabPad extends Fragment implements AltosDroidTab {
32         AltosDroid mAltosDroid;
33
34         private TextView mBatteryVoltageView;
35         private GoNoGoLights mBatteryLights;
36         private TextView mApogeeVoltageView;
37         private GoNoGoLights mApogeeLights;
38         private TextView mMainVoltageView;
39         private GoNoGoLights mMainLights;
40         private TextView mDataLoggingView;
41         private GoNoGoLights mDataLoggingLights;
42         private TextView mGPSLockedView;
43         private GoNoGoLights mGPSLockedLights;
44         private TextView mGPSReadyView;
45         private GoNoGoLights mGPSReadyLights;
46         private TextView mPadLatitudeView;
47         private TextView mPadLongitudeView;
48         private TextView mPadAltitudeView;
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_pad, container, false);
60                 mBatteryVoltageView = (TextView) v.findViewById(R.id.battery_voltage_value);
61                 mBatteryLights = new GoNoGoLights((ImageView) v.findViewById(R.id.battery_redled),
62                                                   (ImageView) v.findViewById(R.id.battery_greenled),
63                                                   getResources());
64
65                 mApogeeVoltageView = (TextView) v.findViewById(R.id.apogee_voltage_value);
66                 mApogeeLights = new GoNoGoLights((ImageView) v.findViewById(R.id.apogee_redled),
67                                                  (ImageView) v.findViewById(R.id.apogee_greenled),
68                                                  getResources());
69
70                 mMainVoltageView = (TextView) v.findViewById(R.id.main_voltage_value);
71                 mMainLights = new GoNoGoLights((ImageView) v.findViewById(R.id.main_redled),
72                                                (ImageView) v.findViewById(R.id.main_greenled),
73                                                getResources());
74
75                 mDataLoggingView = (TextView) v.findViewById(R.id.logging_value);
76                 mDataLoggingLights = new GoNoGoLights((ImageView) v.findViewById(R.id.logging_redled),
77                                                       (ImageView) v.findViewById(R.id.logging_greenled),
78                                                       getResources());
79
80                 mGPSLockedView = (TextView) v.findViewById(R.id.gps_locked_value);
81                 mGPSLockedLights = new GoNoGoLights((ImageView) v.findViewById(R.id.gps_locked_redled),
82                                                     (ImageView) v.findViewById(R.id.gps_locked_greenled),
83                                                     getResources());
84
85                 mGPSReadyView = (TextView) v.findViewById(R.id.gps_ready_value);
86                 mGPSReadyLights = new GoNoGoLights((ImageView) v.findViewById(R.id.gps_ready_redled),
87                                                    (ImageView) v.findViewById(R.id.gps_ready_greenled),
88                                                    getResources());
89
90                 mPadLatitudeView = (TextView) v.findViewById(R.id.pad_lat_value);
91                 mPadLongitudeView = (TextView) v.findViewById(R.id.pad_lon_value);
92                 mPadAltitudeView = (TextView) v.findViewById(R.id.pad_alt_value);
93         return v;
94         }
95
96         @Override
97         public void onDestroy() {
98                 super.onDestroy();
99                 mAltosDroid.unregisterTab(this);
100                 mAltosDroid = null;
101         }
102
103         public void update_ui(AltosState state) {
104                 mBatteryVoltageView.setText(String.format("%4.2f V", state.battery));
105                 mBatteryLights.set(state.battery > 3.7);
106
107                 mApogeeVoltageView.setText(String.format("%4.2f V", state.drogue_sense));
108                 mApogeeLights.set(state.drogue_sense > 3.2);
109
110                 mMainVoltageView.setText(String.format("%4.2f V", state.main_sense));
111                 mMainLights.set(state.main_sense > 3.2);
112
113                 if (state.data.flight != 0) {
114                         if (state.data.state <= AltosLib.ao_flight_pad)
115                                 mDataLoggingView.setText("Ready to record");
116                         else if (state.data.state < AltosLib.ao_flight_landed)
117                                 mDataLoggingView.setText("Recording data");
118                         else
119                                 mDataLoggingView.setText("Recorded data");
120                 } else {
121                         mDataLoggingView.setText("Storage full");
122                 }
123                 mDataLoggingLights.set(state.data.flight != 0);
124
125                 mGPSLockedView.setText(String.format("%4d sats", state.gps.nsat));
126                 mGPSLockedLights.set(state.gps.locked && state.gps.nsat >= 4);
127
128                 if (state.gps_ready)
129                         mGPSReadyView.setText("Ready");
130                 else
131                         mGPSReadyView.setText(String.format("Waiting %d", state.gps_waiting));
132                 mGPSReadyLights.set(state.gps_ready);
133
134                 mPadLatitudeView.setText(AltosDroid.pos(state.pad_lat, "N", "S"));
135                 mPadLongitudeView.setText(AltosDroid.pos(state.pad_lon, "W", "E"));
136                 mPadAltitudeView.setText(String.format("%4.0f m", state.pad_alt));
137         }
138
139 }