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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.AltosDroid;
25 import org.altusmetrum.AltosDroid.R;
27 import android.app.Activity;
28 import android.bluetooth.BluetoothAdapter;
29 import android.bluetooth.BluetoothDevice;
30 import android.content.BroadcastReceiver;
31 import android.content.Context;
32 import android.content.Intent;
33 import android.content.IntentFilter;
34 import android.os.Bundle;
35 import android.view.View;
36 import android.view.Window;
37 import android.view.View.OnClickListener;
38 import android.widget.*;
39 import android.widget.AdapterView.*;
40 import android.location.Location;
41 import android.location.LocationManager;
42 import android.location.LocationListener;
43 import android.location.Criteria;
45 import org.altusmetrum.altoslib_12.*;
48 * This Activity appears as a dialog. It lists any paired devices and
49 * devices detected in the area after discovery. When a device is chosen
50 * by the user, the MAC address of the device is sent back to the parent
51 * Activity in the result Intent.
53 public class PreloadMapActivity extends Activity implements AltosLaunchSiteListener, AltosMapLoaderListener, LocationListener {
55 private ArrayAdapter<AltosLaunchSite> known_sites_adapter;
57 private CheckBox hybrid;
58 private CheckBox satellite;
59 private CheckBox roadmap;
60 private CheckBox terrain;
62 private Spinner known_sites_spinner;
63 private Spinner min_zoom;
64 private Spinner max_zoom;
65 private TextView radius_label;
66 private Spinner radius;
68 private EditText latitude;
69 private EditText longitude;
71 private ProgressBar progress;
73 private AltosMapLoader loader;
75 long loader_notify_time;
77 /* AltosMapLoaderListener interfaces */
78 public void loader_start(final int max) {
79 loader_notify_time = System.currentTimeMillis();
81 this.runOnUiThread(new Runnable() {
84 progress.setProgress(0);
89 public void loader_notify(final int cur, final int max, final String name) {
90 long now = System.currentTimeMillis();
92 if (now - loader_notify_time < 100)
95 loader_notify_time = now;
97 this.runOnUiThread(new Runnable() {
99 progress.setProgress(cur);
104 public void loader_done(int max) {
106 this.runOnUiThread(new Runnable() {
108 progress.setProgress(0);
114 public void debug(String format, Object ... arguments) {
115 AltosDebug.debug(format, arguments);
118 /* AltosLaunchSiteListener interface */
120 public void notify_launch_sites(final List<AltosLaunchSite> sites) {
121 this.runOnUiThread(new Runnable() {
123 for (AltosLaunchSite site : sites)
124 known_sites_adapter.add(site);
129 /* LocationProvider interface */
131 AltosLaunchSite current_location_site;
133 public void onLocationChanged(Location location) {
134 AltosDebug.debug("location changed");
135 if (current_location_site == null) {
136 AltosLaunchSite selected_item = (AltosLaunchSite) known_sites_spinner.getSelectedItem();
138 current_location_site = new AltosLaunchSite("Current Location", location.getLatitude(), location.getLongitude());
139 known_sites_adapter.insert(current_location_site, 0);
141 if (selected_item != null)
142 known_sites_spinner.setSelection(known_sites_adapter.getPosition(selected_item));
144 latitude.setText(new StringBuffer(String.format("%12.6f", current_location_site.latitude)));
145 longitude.setText(new StringBuffer(String.format("%12.6f", current_location_site.longitude)));
148 current_location_site.latitude = location.getLatitude();
149 current_location_site.longitude = location.getLongitude();
153 public void onStatusChanged(String provider, int status, Bundle extras) {
156 public void onProviderEnabled(String provider) {
159 public void onProviderDisabled(String provider) {
162 private double text(EditText view) throws ParseException {
163 return AltosParse.parse_double_locale(view.getEditableText().toString());
166 private double latitude() throws ParseException {
167 return text(latitude);
170 private double longitude() throws ParseException {
171 return text(longitude);
174 private int value(Spinner spinner) {
175 return (Integer) spinner.getSelectedItem();
178 private int min_z() {
179 return value(min_zoom);
182 private int max_z() {
183 return value(max_zoom);
186 private double value_distance(Spinner spinner) {
187 return (Double) spinner.getSelectedItem();
190 private double radius() {
191 double r = value_distance(radius);
192 if (AltosPreferences.imperial_units())
193 r = AltosConvert.miles_to_meters(r);
199 private int bit(CheckBox box, int value) {
205 private int types() {
206 return (bit(hybrid, AltosMap.maptype_hybrid) |
207 bit(satellite, AltosMap.maptype_satellite) |
208 bit(roadmap, AltosMap.maptype_roadmap) |
209 bit(terrain, AltosMap.maptype_terrain));
212 private void load() {
217 double lat = latitude();
218 double lon = longitude();
224 AltosDebug.debug("PreloadMap load %f %f %d %d %f %d\n",
225 lat, lon, min, max, r, t);
226 loader = new AltosMapLoader(this, lat, lon, min, max, r, t, AltosMapOffline.scale);
227 } catch (ParseException e) {
228 AltosDebug.debug("PreloadMap load raised exception %s", e.toString());
232 private void add_numbers(Spinner spinner, int min, int max, int def) {
234 ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item);
239 for (int i = min; i <= max; i++) {
240 adapter.add(new Integer(i));
246 spinner.setAdapter(adapter);
247 spinner.setSelection(spinner_def);
251 private void add_distance(Spinner spinner, double[] distances_km, double def_km, double[] distances_mi, double def_mi) {
253 ArrayAdapter<Double> adapter = new ArrayAdapter<Double>(this, android.R.layout.simple_spinner_item);
260 if (AltosPreferences.imperial_units()) {
261 distances = distances_mi;
264 distances = distances_km;
268 for (int i = 0; i < distances.length; i++) {
269 adapter.add(distances[i]);
270 if (distances[i] == def)
275 spinner.setAdapter(adapter);
276 spinner.setSelection(spinner_def);
281 class SiteListListener implements OnItemSelectedListener {
282 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
283 AltosLaunchSite site = (AltosLaunchSite) parent.getItemAtPosition(pos);
284 latitude.setText(new StringBuffer(String.format("%12.6f", site.latitude)));
285 longitude.setText(new StringBuffer(String.format("%12.6f", site.longitude)));
287 public void onNothingSelected(AdapterView<?> parent) {
290 public SiteListListener() {
294 double[] radius_mi = { 1, 2, 5, 10, 20 };
295 double radius_def_mi = 2;
296 double[] radius_km = { 1, 2, 5, 10, 20, 30 };
297 double radius_def_km = 2;
300 protected void onCreate(Bundle savedInstanceState) {
301 super.onCreate(savedInstanceState);
304 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
305 setContentView(R.layout.map_preload);
307 // Set result CANCELED incase the user backs out
308 setResult(Activity.RESULT_CANCELED);
310 // Initialize the button to perform device discovery
311 Button loadButton = (Button) findViewById(R.id.preload_load);
312 loadButton.setOnClickListener(new OnClickListener() {
313 public void onClick(View v) {
318 latitude = (EditText) findViewById(R.id.preload_latitude);
319 longitude = (EditText) findViewById(R.id.preload_longitude);
321 hybrid = (CheckBox) findViewById(R.id.preload_hybrid);
322 satellite = (CheckBox) findViewById(R.id.preload_satellite);
323 roadmap = (CheckBox) findViewById(R.id.preload_roadmap);
324 terrain = (CheckBox) findViewById(R.id.preload_terrain);
326 hybrid.setChecked(true);
328 min_zoom = (Spinner) findViewById(R.id.preload_min_zoom);
329 add_numbers(min_zoom,
330 AltosMap.min_zoom - AltosMap.default_zoom,
331 AltosMap.max_zoom - AltosMap.default_zoom, -2);
332 max_zoom = (Spinner) findViewById(R.id.preload_max_zoom);
333 add_numbers(max_zoom,
334 AltosMap.min_zoom - AltosMap.default_zoom,
335 AltosMap.max_zoom - AltosMap.default_zoom, 2);
336 radius_label = (TextView) findViewById(R.id.preload_radius_label);
337 radius = (Spinner) findViewById(R.id.preload_radius);
338 if (AltosPreferences.imperial_units())
339 radius_label.setText("Radius (miles)");
341 radius_label.setText("Radius (km)");
342 add_distance(radius, radius_km, radius_def_km, radius_mi, radius_def_mi);
344 progress = (ProgressBar) findViewById(R.id.preload_progress);
346 // Initialize array adapters. One for already paired devices and
347 // one for newly discovered devices
348 known_sites_spinner = (Spinner) findViewById(R.id.preload_site_list);
350 known_sites_adapter = new ArrayAdapter<AltosLaunchSite>(this, android.R.layout.simple_spinner_item);
352 known_sites_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
354 known_sites_spinner.setAdapter(known_sites_adapter);
355 known_sites_spinner.setOnItemSelectedListener(new SiteListListener());
357 // Listen for GPS and Network position updates
358 LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
360 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
362 new AltosLaunchSites(this);
366 protected void onDestroy() {
372 // Stop listening for location updates
373 ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);