2 * Copyright © 2015 Keith Packard <keithp@keithp.com>
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.
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.
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.
18 package org.altusmetrum.AltosDroid;
24 import org.altusmetrum.AltosDroid.R;
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.*;
40 import org.altusmetrum.altoslib_7.*;
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.
48 public class PreloadMapActivity extends Activity implements AltosLaunchSiteListener, AltosMapInterface, AltosMapLoaderListener {
50 private ArrayAdapter<AltosLaunchSite> known_sites_adapter;
52 private CheckBox hybrid;
53 private CheckBox satellite;
54 private CheckBox roadmap;
55 private CheckBox terrain;
57 private Spinner min_zoom;
58 private Spinner max_zoom;
59 private Spinner tile_radius;
61 private EditText latitude;
62 private EditText longitude;
64 private ProgressBar progress;
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() {
72 progress.setProgress(0);
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() {
81 progress.setProgress(cur);
86 public void loader_done(int max) {
87 AltosDebug.debug("loader_done max %d\n", max);
88 this.runOnUiThread(new Runnable() {
90 progress.setProgress(0);
96 /* AltosLaunchSiteListener interface */
97 public void notify_launch_sites(final List<AltosLaunchSite> sites) {
98 this.runOnUiThread(new Runnable() {
100 for (AltosLaunchSite site : sites)
101 known_sites_adapter.add(site);
107 AltosMapLoader loader;
109 class PreloadMapImage implements AltosImage {
110 public void flush() {
113 public PreloadMapImage(File file) {
114 AltosDebug.debug("preload file %s\n", file.toString());
118 public AltosMapPath new_path() {
122 public AltosMapLine new_line() {
126 public AltosImage load_image(File file) throws Exception {
127 return new PreloadMapImage(file);
130 public AltosMapMark new_mark(double lat, double lon, int state) {
134 class PreloadMapTile extends AltosMapTile {
135 public void paint(AltosMapTransform t) {
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);
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);
149 return AltosMap.px_size;
152 public int height() {
153 return AltosMap.px_size;
156 public void repaint() {
159 public void repaint(AltosRectangle damage) {
162 public void set_zoom_label(String label) {
163 AltosDebug.debug("zoom label %s\n", label);
166 private double text(EditText view) throws ParseException {
167 return AltosParse.parse_double_locale(view.getEditableText().toString());
170 private double latitude() throws ParseException {
171 return text(latitude);
174 private double longitude() throws ParseException {
175 return text(longitude);
178 private int value(Spinner spinner) {
179 return (Integer) spinner.getSelectedItem();
182 private int min_z() {
183 return value(min_zoom);
186 private int max_z() {
187 return value(max_zoom);
190 private int radius() {
191 return value(tile_radius);
194 private int bit(CheckBox box, int value) {
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));
207 private void load() {
209 double lat = latitude();
210 double lon = longitude();
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) {
223 private void add_numbers(Spinner spinner, int min, int max, int def) {
225 ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item);
230 for (int i = min; i <= max; i++) {
231 adapter.add(new Integer(i));
237 spinner.setAdapter(adapter);
238 spinner.setSelection(spinner_def);
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());
246 latitude.setText(new StringBuffer(String.format("%12.6f", site.latitude)));
247 longitude.setText(new StringBuffer(String.format("%12.6f", site.longitude)));
249 public void onNothingSelected(AdapterView<?> parent) {
252 public SiteListListener() {
257 protected void onCreate(Bundle savedInstanceState) {
258 super.onCreate(savedInstanceState);
260 AltosDebug.debug("preload map onCreate");
263 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
264 setContentView(R.layout.map_preload);
266 // Set result CANCELED incase the user backs out
267 setResult(Activity.RESULT_CANCELED);
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) {
277 latitude = (EditText) findViewById(R.id.preload_latitude);
278 longitude = (EditText) findViewById(R.id.preload_longitude);
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);
285 hybrid.setChecked(true);
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);
298 progress = (ProgressBar) findViewById(R.id.preload_progress);
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);
304 known_sites_adapter = new ArrayAdapter<AltosLaunchSite>(this, android.R.layout.simple_spinner_item);
306 known_sites_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
308 known_sites_spinner.setAdapter(known_sites_adapter);
309 known_sites_spinner.setOnItemSelectedListener(new SiteListListener());
311 map = new AltosMap(this);
313 loader = new AltosMapLoader(map, this);
315 new AltosLaunchSites(this);
319 protected void onDestroy() {