summaryrefslogtreecommitdiff
path: root/altosdroid/src/org/altusmetrum/AltosDroid/PreloadMapActivity.java
blob: e6ce38093a78d657ff74d576e7dfc2cb3f21c188 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * Copyright © 2015 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package org.altusmetrum.AltosDroid;

import java.util.*;
import java.io.*;
import java.text.*;

import org.altusmetrum.AltosDroid.R;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.AdapterView.*;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import android.location.Criteria;

import org.altusmetrum.altoslib_9.*;

/**
 * This Activity appears as a dialog. It lists any paired devices and
 * devices detected in the area after discovery. When a device is chosen
 * by the user, the MAC address of the device is sent back to the parent
 * Activity in the result Intent.
 */
public class PreloadMapActivity extends Activity implements AltosLaunchSiteListener, AltosMapInterface, AltosMapLoaderListener, LocationListener {

	private ArrayAdapter<AltosLaunchSite> known_sites_adapter;

	private CheckBox	hybrid;
	private CheckBox	satellite;
	private CheckBox	roadmap;
	private CheckBox	terrain;

	private Spinner		known_sites_spinner;
	private Spinner		min_zoom;
	private Spinner		max_zoom;
	private TextView	radius_label;
	private Spinner		radius;

	private EditText	latitude;
	private EditText	longitude;

	private ProgressBar	progress;

	/* AltosMapLoaderListener interfaces */
	public void loader_start(final int max) {
		this.runOnUiThread(new Runnable() {
				public void run() {
					progress.setMax(max);
					progress.setProgress(0);
				}
			});
	}

	public void loader_notify(final int cur, final int max, final String name) {
		this.runOnUiThread(new Runnable() {
				public void run() {
					progress.setProgress(cur);
				}
			});
	}

	public void loader_done(int max) {
		this.runOnUiThread(new Runnable() {
				public void run() {
					progress.setProgress(0);
					finish();
				}
			});
	}

	/* AltosLaunchSiteListener interface */
	public void notify_launch_sites(final List<AltosLaunchSite> sites) {
		this.runOnUiThread(new Runnable() {
				public void run() {
					for (AltosLaunchSite site : sites)
						known_sites_adapter.add(site);
				}
			});
	}

	AltosMap	map;
	AltosMapLoader	loader;

	class PreloadMapImage implements AltosImage {
		public void flush() {
		}

		public PreloadMapImage(File file) {
		}
	}

	public AltosMapPath new_path() {
		return null;
	}

	public AltosMapLine new_line() {
		return null;
	}

	public AltosImage load_image(File file) throws Exception {
		return new PreloadMapImage(file);
	}

	public AltosMapMark new_mark(double lat, double lon, int state) {
		return null;
	}

	class PreloadMapTile extends AltosMapTile {
		public void paint(AltosMapTransform t) {
		}

		public PreloadMapTile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
			super(listener, upper_left, center, zoom, maptype, px_size, 2);
		}

	}

	public AltosMapTile new_tile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
		return new PreloadMapTile(listener, upper_left, center, zoom, maptype, px_size);
	}

	public int width() {
		return AltosMap.px_size;
	}

	public int height() {
		return AltosMap.px_size;
	}

	public void repaint() {
	}

	public void repaint(AltosRectangle damage) {
	}

	public void set_zoom_label(String label) {
	}

	public void select_object(AltosLatLon latlon) {
	}

	public void debug(String format, Object ... arguments) {
		AltosDebug.debug(format, arguments);
	}

	/* LocationProvider interface */

	AltosLaunchSite	current_location_site;

	public void onLocationChanged(Location location) {
		AltosDebug.debug("location changed");
		if (current_location_site == null) {
			AltosLaunchSite selected_item = (AltosLaunchSite) known_sites_spinner.getSelectedItem();

			current_location_site = new AltosLaunchSite("Current Location", location.getLatitude(), location.getLongitude());
			known_sites_adapter.insert(current_location_site, 0);

			if (selected_item != null)
				known_sites_spinner.setSelection(known_sites_adapter.getPosition(selected_item));
			else {
				latitude.setText(new StringBuffer(String.format("%12.6f", current_location_site.latitude)));
				longitude.setText(new StringBuffer(String.format("%12.6f", current_location_site.longitude)));
			}
		} else {
			current_location_site.latitude = location.getLatitude();
			current_location_site.longitude = location.getLongitude();
		}
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
	}

	public void onProviderEnabled(String provider) {
	}

	public void onProviderDisabled(String provider) {
	}

	private double text(EditText view) throws ParseException {
		return AltosParse.parse_double_locale(view.getEditableText().toString());
	}

	private double latitude() throws ParseException {
		return text(latitude);
	}

	private double longitude() throws ParseException {
		return text(longitude);
	}

	private int value(Spinner spinner) {
		return (Integer) spinner.getSelectedItem();
	}

	private int min_z() {
		return value(min_zoom);
	}

	private int max_z() {
		return value(max_zoom);
	}

	private double value_distance(Spinner spinner) {
		return (Double) spinner.getSelectedItem();
	}

	private double radius() {
		double r = value_distance(radius);
		if (AltosPreferences.imperial_units())
			r = AltosConvert.distance.inverse(r);
		else
			r = r * 1000;
		return r;
	}

	private int bit(CheckBox box, int value) {
		if (box.isChecked())
			return 1 << value;
		return 0;
	}

	private int types() {
		return (bit(hybrid, AltosMap.maptype_hybrid) |
			bit(satellite, AltosMap.maptype_satellite) |
			bit(roadmap, AltosMap.maptype_roadmap) |
			bit(terrain, AltosMap.maptype_terrain));
	}

	private void load() {
		try {
			double	lat = latitude();
			double	lon = longitude();
			int	min = min_z();
			int	max = max_z();
			double	r = radius();
			int	t = types();

			AltosDebug.debug("PreloadMap load %f %f %d %d %f %d\n",
					 lat, lon, min, max, r, t);
			loader.load(lat, lon, min, max, r, t);
		} catch (ParseException e) {
			AltosDebug.debug("PreloadMap load raised exception %s", e.toString());
		}
	}

	private void add_numbers(Spinner spinner, int min, int max, int def) {

		ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item);

		int	spinner_def = 0;
		int	pos = 0;

		for (int i = min; i <= max; i++) {
			adapter.add(new Integer(i));
			if (i == def)
				spinner_def = pos;
			pos++;
		}

		spinner.setAdapter(adapter);
		spinner.setSelection(spinner_def);
	}


	private void add_distance(Spinner spinner, double[] distances_km, double def_km, double[] distances_mi, double def_mi) {

		ArrayAdapter<Double> adapter = new ArrayAdapter<Double>(this, android.R.layout.simple_spinner_item);

		int	spinner_def = 0;
		int	pos = 0;

		double[] distances;
		double	def;
		if (AltosPreferences.imperial_units()) {
			distances = distances_mi;
			def = def_mi;
		} else {
			distances = distances_km;
			def = def_km;
		}

		for (int i = 0; i < distances.length; i++) {
			adapter.add(distances[i]);
			if (distances[i] == def)
				spinner_def = pos;
			pos++;
		}

		spinner.setAdapter(adapter);
		spinner.setSelection(spinner_def);
	}



	class SiteListListener implements OnItemSelectedListener {
		public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
			AltosLaunchSite	site = (AltosLaunchSite) parent.getItemAtPosition(pos);
			latitude.setText(new StringBuffer(String.format("%12.6f", site.latitude)));
			longitude.setText(new StringBuffer(String.format("%12.6f", site.longitude)));
		}
		public void onNothingSelected(AdapterView<?> parent) {
		}

		public SiteListListener() {
		}
	}

	double[]	radius_mi = { 1, 2, 5, 10, 20 };
	double		radius_def_mi = 2;
	double[]	radius_km = { 1, 2, 5, 10, 20, 30 };
	double		radius_def_km = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Setup the window
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.map_preload);

		// Set result CANCELED incase the user backs out
		setResult(Activity.RESULT_CANCELED);

		// Initialize the button to perform device discovery
		Button loadButton = (Button) findViewById(R.id.preload_load);
		loadButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				load();
			}
		});

		latitude = (EditText) findViewById(R.id.preload_latitude);
		longitude = (EditText) findViewById(R.id.preload_longitude);

		hybrid = (CheckBox) findViewById(R.id.preload_hybrid);
		satellite = (CheckBox) findViewById(R.id.preload_satellite);
		roadmap = (CheckBox) findViewById(R.id.preload_roadmap);
		terrain = (CheckBox) findViewById(R.id.preload_terrain);

		hybrid.setChecked(true);

		min_zoom = (Spinner) findViewById(R.id.preload_min_zoom);
		add_numbers(min_zoom,
			    AltosMap.min_zoom - AltosMap.default_zoom,
			    AltosMap.max_zoom - AltosMap.default_zoom, -2);
		max_zoom = (Spinner) findViewById(R.id.preload_max_zoom);
		add_numbers(max_zoom,
			    AltosMap.min_zoom - AltosMap.default_zoom,
			    AltosMap.max_zoom - AltosMap.default_zoom, 2);
		radius_label = (TextView) findViewById(R.id.preload_radius_label);
		radius = (Spinner) findViewById(R.id.preload_radius);
		if (AltosPreferences.imperial_units())
			radius_label.setText("Radius (miles)");
		else
			radius_label.setText("Radius (km)");
		add_distance(radius, radius_km, radius_def_km, radius_mi, radius_def_mi);

		progress = (ProgressBar) findViewById(R.id.preload_progress);

		// Initialize array adapters. One for already paired devices and
		// one for newly discovered devices
		known_sites_spinner = (Spinner) findViewById(R.id.preload_site_list);

		known_sites_adapter = new ArrayAdapter<AltosLaunchSite>(this, android.R.layout.simple_spinner_item);

		known_sites_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		known_sites_spinner.setAdapter(known_sites_adapter);
		known_sites_spinner.setOnItemSelectedListener(new SiteListListener());

		map = new AltosMap(this);

		loader = new AltosMapLoader(map, this);

		// Listen for GPS and Network position updates
		LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);

		new AltosLaunchSites(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		// Stop listening for location updates
		((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);
	}
}