f1f1b6de853e6996034d23d305b8b51a909fb2a4
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosDroidTab.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_7.*;
21 import android.location.Location;
22 import android.app.Activity;
23 import android.graphics.Color;
24 import android.os.Bundle;
25 import android.support.v4.app.Fragment;
26 import android.support.v4.app.FragmentTransaction;
27 import android.support.v4.app.FragmentManager;
28 import android.location.Location;
29 import android.widget.TextView;
30
31 public abstract class AltosDroidTab extends Fragment implements AltosUnitsListener {
32         AltosState              last_state;
33         AltosGreatCircle        last_from_receiver;
34         Location                last_receiver;
35
36         public abstract void show(AltosState state, AltosGreatCircle from_receiver, Location receiver);
37
38         public abstract String tab_name();
39
40         public void set_map_type(int map_type) {
41         }
42
43         public void units_changed(boolean imperial_units) {
44                 if (!isHidden() && last_state != null)
45                         show(last_state, last_from_receiver, last_receiver);
46         }
47
48         public void set_value(TextView text_view,
49                               AltosUnits units,
50                               int width,
51                               double value) {
52                 if (value == AltosLib.MISSING)
53                         text_view.setText("");
54                 else
55                         text_view.setText(units.show(width, value));
56         }
57
58         public void set_visible(boolean visible) {
59                 FragmentTransaction     ft = AltosDroid.fm.beginTransaction();
60                 AltosDebug.debug("set visible %b %s\n", visible, tab_name());
61                 if (visible) {
62                         AltosState              state = last_state;
63                         AltosGreatCircle        from_receiver = last_from_receiver;
64                         Location                receiver = last_receiver;
65
66                         ft.show(this);
67                         show(state, from_receiver, receiver);
68                 } else
69                         ft.hide(this);
70                 ft.commitAllowingStateLoss();
71         }
72
73         @Override
74         public void onResume() {
75                 super.onResume();
76                 AltosDebug.debug("onResume tab %s\n", tab_name());
77                 set_visible(true);
78         }
79
80         public void update_ui(AltosState state, AltosGreatCircle from_receiver, Location receiver, boolean is_current) {
81                 last_state = state;
82                 last_from_receiver = from_receiver;
83                 last_receiver = receiver;
84                 if (is_current)
85                         show(state, from_receiver, receiver);
86                 else
87                         return;
88         }
89 }