altosdroid: Save selected map type in AltosDroid object
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / PreloadMapActivity.java
1 /*
2  * Copyright © 2015 Keith Packard <keithp@keithp.com>
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 java.util.*;
21 import java.io.*;
22 import java.text.*;
23
24 import org.altusmetrum.AltosDroid.R;
25
26 import android.app.Activity;
27 import android.bluetooth.BluetoothAdapter;
28 import android.bluetooth.BluetoothDevice;
29 import android.content.BroadcastReceiver;
30 import android.content.Context;
31 import android.content.Intent;
32 import android.content.IntentFilter;
33 import android.os.Bundle;
34 import android.view.View;
35 import android.view.Window;
36 import android.view.View.OnClickListener;
37 import android.widget.*;
38 import android.widget.AdapterView.*;
39
40 import org.altusmetrum.altoslib_7.*;
41
42 /**
43  * This Activity appears as a dialog. It lists any paired devices and
44  * devices detected in the area after discovery. When a device is chosen
45  * by the user, the MAC address of the device is sent back to the parent
46  * Activity in the result Intent.
47  */
48 public class PreloadMapActivity extends Activity implements AltosLaunchSiteListener, AltosMapInterface, AltosMapLoaderListener {
49
50         private ArrayAdapter<AltosLaunchSite> known_sites_adapter;
51
52         private CheckBox        hybrid;
53         private CheckBox        satellite;
54         private CheckBox        roadmap;
55         private CheckBox        terrain;
56
57         private Spinner         min_zoom;
58         private Spinner         max_zoom;
59         private Spinner         tile_radius;
60
61         private EditText        latitude;
62         private EditText        longitude;
63
64         private ProgressBar     progress;
65
66         /* AltosMapLoaderListener interfaces */
67         public void loader_start(final int max) {
68                 AltosDebug.debug("loader_start max %d\n", max);
69                 this.runOnUiThread(new Runnable() {
70                                 public void run() {
71                                         progress.setMax(max);
72                                         progress.setProgress(0);
73                                 }
74                         });
75         }
76
77         public void loader_notify(final int cur, final int max, final String name) {
78                 AltosDebug.debug("loader_notify cur %4d max %4d %s\n", cur, max, name);
79                 this.runOnUiThread(new Runnable() {
80                                 public void run() {
81                                         progress.setProgress(cur);
82                                 }
83                         });
84         }
85
86         public void loader_done(int max) {
87                 AltosDebug.debug("loader_done max %d\n", max);
88                 this.runOnUiThread(new Runnable() {
89                                 public void run() {
90                                         progress.setProgress(0);
91                                         finish();
92                                 }
93                         });
94         }
95
96         /* AltosLaunchSiteListener interface */
97         public void notify_launch_sites(final List<AltosLaunchSite> sites) {
98                 this.runOnUiThread(new Runnable() {
99                                 public void run() {
100                                         for (AltosLaunchSite site : sites)
101                                                 known_sites_adapter.add(site);
102                                 }
103                         });
104         }
105
106         AltosMap        map;
107         AltosMapLoader  loader;
108
109         class PreloadMapImage implements AltosImage {
110                 public void flush() {
111                 }
112
113                 public PreloadMapImage(File file) {
114                         AltosDebug.debug("preload file %s\n", file.toString());
115                 }
116         }
117
118         public AltosMapPath new_path() {
119                 return null;
120         }
121
122         public AltosMapLine new_line() {
123                 return null;
124         }
125
126         public AltosImage load_image(File file) throws Exception {
127                 return new PreloadMapImage(file);
128         }
129
130         public AltosMapMark new_mark(double lat, double lon, int state) {
131                 return null;
132         }
133
134         class PreloadMapTile extends AltosMapTile {
135                 public void paint(AltosMapTransform t) {
136                 }
137
138                 public PreloadMapTile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
139                         super(listener, upper_left, center, zoom, maptype, px_size, 2);
140                 }
141
142         }
143
144         public AltosMapTile new_tile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
145                 return new PreloadMapTile(listener, upper_left, center, zoom, maptype, px_size);
146         }
147
148         public int width() {
149                 return AltosMap.px_size;
150         }
151
152         public int height() {
153                 return AltosMap.px_size;
154         }
155
156         public void repaint() {
157         }
158
159         public void repaint(AltosRectangle damage) {
160         }
161
162         public void set_zoom_label(String label) {
163                 AltosDebug.debug("zoom label %s\n", label);
164         }
165
166         private double text(EditText view) throws ParseException {
167                 return AltosParse.parse_double_locale(view.getEditableText().toString());
168         }
169
170         private double latitude() throws ParseException {
171                 return text(latitude);
172         }
173
174         private double longitude() throws ParseException {
175                 return text(longitude);
176         }
177
178         private int value(Spinner spinner) {
179                 return (Integer) spinner.getSelectedItem();
180         }
181
182         private int min_z() {
183                 return value(min_zoom);
184         }
185
186         private int max_z() {
187                 return value(max_zoom);
188         }
189
190         private int radius() {
191                 return value(tile_radius);
192         }
193
194         private int bit(CheckBox box, int value) {
195                 if (box.isChecked())
196                         return 1 << value;
197                 return 0;
198         }
199
200         private int types() {
201                 return (bit(hybrid, AltosMap.maptype_hybrid) |
202                         bit(satellite, AltosMap.maptype_satellite) |
203                         bit(roadmap, AltosMap.maptype_roadmap) |
204                         bit(terrain, AltosMap.maptype_terrain));
205         }
206
207         private void load() {
208                 try {
209                         double  lat = latitude();
210                         double  lon = longitude();
211                         int     min = min_z();
212                         int     max = max_z();
213                         int     r = radius();
214                         int     t = types();
215
216                         AltosDebug.debug("load lat %12.6f lon %12.6f min %d max %d r %d types %x\n",
217                                          lat, lon, min, max, r, t);
218                         loader.load(lat, lon, min, max, r, t);
219                 } catch (ParseException e) {
220                 }
221         }
222
223         private void add_numbers(Spinner spinner, int min, int max, int def) {
224
225                 ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item);
226
227                 int     spinner_def = 0;
228                 int     pos = 0;
229
230                 for (int i = min; i <= max; i++) {
231                         adapter.add(new Integer(i));
232                         if (i == def)
233                                 spinner_def = pos;
234                         pos++;
235                 }
236
237                 spinner.setAdapter(adapter);
238                 spinner.setSelection(spinner_def);
239         }
240
241         class SiteListListener implements OnItemSelectedListener {
242                 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
243                         AltosLaunchSite site = (AltosLaunchSite) parent.getItemAtPosition(pos);
244                         AltosDebug.debug("Site selected: %s\n", site.toString());
245
246                         latitude.setText(new StringBuffer(String.format("%12.6f", site.latitude)));
247                         longitude.setText(new StringBuffer(String.format("%12.6f", site.longitude)));
248                 }
249                 public void onNothingSelected(AdapterView<?> parent) {
250                 }
251
252                 public SiteListListener() {
253                 }
254         }
255
256         @Override
257         protected void onCreate(Bundle savedInstanceState) {
258                 super.onCreate(savedInstanceState);
259
260                 AltosDebug.debug("preload map onCreate");
261
262                 // Setup the window
263                 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
264                 setContentView(R.layout.map_preload);
265
266                 // Set result CANCELED incase the user backs out
267                 setResult(Activity.RESULT_CANCELED);
268
269                 // Initialize the button to perform device discovery
270                 Button loadButton = (Button) findViewById(R.id.preload_load);
271                 loadButton.setOnClickListener(new OnClickListener() {
272                         public void onClick(View v) {
273                                 load();
274                         }
275                 });
276
277                 latitude = (EditText) findViewById(R.id.preload_latitude);
278                 longitude = (EditText) findViewById(R.id.preload_longitude);
279
280                 hybrid = (CheckBox) findViewById(R.id.preload_hybrid);
281                 satellite = (CheckBox) findViewById(R.id.preload_satellite);
282                 roadmap = (CheckBox) findViewById(R.id.preload_roadmap);
283                 terrain = (CheckBox) findViewById(R.id.preload_terrain);
284
285                 hybrid.setChecked(true);
286
287                 min_zoom = (Spinner) findViewById(R.id.preload_min_zoom);
288                 add_numbers(min_zoom,
289                             AltosMap.min_zoom - AltosMap.default_zoom,
290                             AltosMap.max_zoom - AltosMap.default_zoom, -2);
291                 max_zoom = (Spinner) findViewById(R.id.preload_max_zoom);
292                 add_numbers(max_zoom,
293                             AltosMap.min_zoom - AltosMap.default_zoom,
294                             AltosMap.max_zoom - AltosMap.default_zoom, 2);
295                 tile_radius = (Spinner) findViewById(R.id.preload_tile_radius);
296                 add_numbers(tile_radius, 1, 5, 3);
297
298                 progress = (ProgressBar) findViewById(R.id.preload_progress);
299
300                 // Initialize array adapters. One for already paired devices and
301                 // one for newly discovered devices
302                 Spinner known_sites_spinner = (Spinner) findViewById(R.id.preload_site_list);
303
304                 known_sites_adapter = new ArrayAdapter<AltosLaunchSite>(this, android.R.layout.simple_spinner_item);
305
306                 known_sites_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
307
308                 known_sites_spinner.setAdapter(known_sites_adapter);
309                 known_sites_spinner.setOnItemSelectedListener(new SiteListListener());
310
311                 map = new AltosMap(this);
312
313                 loader = new AltosMapLoader(map, this);
314
315                 new AltosLaunchSites(this);
316         }
317
318         @Override
319         protected void onDestroy() {
320                 super.onDestroy();
321         }
322 }