Bump java lib versions to 13
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.AltosDroid;
20
21 import java.util.*;
22 import java.io.*;
23 import java.text.*;
24
25 import org.altusmetrum.AltosDroid.R;
26
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;
44
45 import org.altusmetrum.altoslib_13.*;
46
47 /**
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.
52  */
53 public class PreloadMapActivity extends Activity implements AltosLaunchSiteListener, AltosMapLoaderListener, LocationListener {
54
55         private ArrayAdapter<AltosLaunchSite> known_sites_adapter;
56
57         private CheckBox        hybrid;
58         private CheckBox        satellite;
59         private CheckBox        roadmap;
60         private CheckBox        terrain;
61
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;
67
68         private EditText        latitude;
69         private EditText        longitude;
70
71         private ProgressBar     progress;
72
73         private AltosMapLoader  loader;
74
75         long    loader_notify_time;
76
77         /* AltosMapLoaderListener interfaces */
78         public void loader_start(final int max) {
79                 loader_notify_time = System.currentTimeMillis();
80
81                 this.runOnUiThread(new Runnable() {
82                                 public void run() {
83                                         progress.setMax(max);
84                                         progress.setProgress(0);
85                                 }
86                         });
87         }
88
89         public void loader_notify(final int cur, final int max, final String name) {
90                 long    now = System.currentTimeMillis();
91
92                 if (now - loader_notify_time < 100)
93                         return;
94
95                 loader_notify_time = now;
96
97                 this.runOnUiThread(new Runnable() {
98                                 public void run() {
99                                         progress.setProgress(cur);
100                                 }
101                         });
102         }
103
104         public void loader_done(int max) {
105                 loader = null;
106                 this.runOnUiThread(new Runnable() {
107                                 public void run() {
108                                         progress.setProgress(0);
109                                         finish();
110                                 }
111                         });
112         }
113
114         public void debug(String format, Object ... arguments) {
115                 AltosDebug.debug(format, arguments);
116         }
117
118         /* AltosLaunchSiteListener interface */
119
120         public void notify_launch_sites(final List<AltosLaunchSite> sites) {
121                 this.runOnUiThread(new Runnable() {
122                                 public void run() {
123                                         for (AltosLaunchSite site : sites)
124                                                 known_sites_adapter.add(site);
125                                 }
126                         });
127         }
128
129         /* LocationProvider interface */
130
131         AltosLaunchSite current_location_site;
132
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();
137
138                         current_location_site = new AltosLaunchSite("Current Location", location.getLatitude(), location.getLongitude());
139                         known_sites_adapter.insert(current_location_site, 0);
140
141                         if (selected_item != null)
142                                 known_sites_spinner.setSelection(known_sites_adapter.getPosition(selected_item));
143                         else {
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)));
146                         }
147                 } else {
148                         current_location_site.latitude = location.getLatitude();
149                         current_location_site.longitude = location.getLongitude();
150                 }
151         }
152
153         public void onStatusChanged(String provider, int status, Bundle extras) {
154         }
155
156         public void onProviderEnabled(String provider) {
157         }
158
159         public void onProviderDisabled(String provider) {
160         }
161
162         private double text(EditText view) throws ParseException {
163                 return AltosParse.parse_double_locale(view.getEditableText().toString());
164         }
165
166         private double latitude() throws ParseException {
167                 return text(latitude);
168         }
169
170         private double longitude() throws ParseException {
171                 return text(longitude);
172         }
173
174         private int value(Spinner spinner) {
175                 return (Integer) spinner.getSelectedItem();
176         }
177
178         private int min_z() {
179                 return value(min_zoom);
180         }
181
182         private int max_z() {
183                 return value(max_zoom);
184         }
185
186         private double value_distance(Spinner spinner) {
187                 return (Double) spinner.getSelectedItem();
188         }
189
190         private double radius() {
191                 double r = value_distance(radius);
192                 if (AltosPreferences.imperial_units())
193                         r = AltosConvert.miles_to_meters(r);
194                 else
195                         r = r * 1000;
196                 return r;
197         }
198
199         private int bit(CheckBox box, int value) {
200                 if (box.isChecked())
201                         return 1 << value;
202                 return 0;
203         }
204
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));
210         }
211
212         private void load() {
213                 if (loader != null)
214                         return;
215
216                 try {
217                         double  lat = latitude();
218                         double  lon = longitude();
219                         int     min = min_z();
220                         int     max = max_z();
221                         double  r = radius();
222                         int     t = types();
223
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());
229                 }
230         }
231
232         private void add_numbers(Spinner spinner, int min, int max, int def) {
233
234                 ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item);
235
236                 int     spinner_def = 0;
237                 int     pos = 0;
238
239                 for (int i = min; i <= max; i++) {
240                         adapter.add(new Integer(i));
241                         if (i == def)
242                                 spinner_def = pos;
243                         pos++;
244                 }
245
246                 spinner.setAdapter(adapter);
247                 spinner.setSelection(spinner_def);
248         }
249
250
251         private void add_distance(Spinner spinner, double[] distances_km, double def_km, double[] distances_mi, double def_mi) {
252
253                 ArrayAdapter<Double> adapter = new ArrayAdapter<Double>(this, android.R.layout.simple_spinner_item);
254
255                 int     spinner_def = 0;
256                 int     pos = 0;
257
258                 double[] distances;
259                 double  def;
260                 if (AltosPreferences.imperial_units()) {
261                         distances = distances_mi;
262                         def = def_mi;
263                 } else {
264                         distances = distances_km;
265                         def = def_km;
266                 }
267
268                 for (int i = 0; i < distances.length; i++) {
269                         adapter.add(distances[i]);
270                         if (distances[i] == def)
271                                 spinner_def = pos;
272                         pos++;
273                 }
274
275                 spinner.setAdapter(adapter);
276                 spinner.setSelection(spinner_def);
277         }
278
279
280
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)));
286                 }
287                 public void onNothingSelected(AdapterView<?> parent) {
288                 }
289
290                 public SiteListListener() {
291                 }
292         }
293
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;
298
299         @Override
300         protected void onCreate(Bundle savedInstanceState) {
301                 super.onCreate(savedInstanceState);
302
303                 // Setup the window
304                 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
305                 setContentView(R.layout.map_preload);
306
307                 // Set result CANCELED incase the user backs out
308                 setResult(Activity.RESULT_CANCELED);
309
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) {
314                                 load();
315                         }
316                 });
317
318                 latitude = (EditText) findViewById(R.id.preload_latitude);
319                 longitude = (EditText) findViewById(R.id.preload_longitude);
320
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);
325
326                 hybrid.setChecked(true);
327
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)");
340                 else
341                         radius_label.setText("Radius (km)");
342                 add_distance(radius, radius_km, radius_def_km, radius_mi, radius_def_mi);
343
344                 progress = (ProgressBar) findViewById(R.id.preload_progress);
345
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);
349
350                 known_sites_adapter = new ArrayAdapter<AltosLaunchSite>(this, android.R.layout.simple_spinner_item);
351
352                 known_sites_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
353
354                 known_sites_spinner.setAdapter(known_sites_adapter);
355                 known_sites_spinner.setOnItemSelectedListener(new SiteListListener());
356
357                 // Listen for GPS and Network position updates
358                 LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
359
360                 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
361
362                 new AltosLaunchSites(this);
363         }
364
365         @Override
366         protected void onDestroy() {
367                 super.onDestroy();
368
369                 if (loader != null)
370                         loader.abort();
371
372                 // Stop listening for location updates
373                 ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);
374         }
375 }