2 * Copyright © 2016 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;
20 import java.lang.ref.WeakReference;
22 import org.altusmetrum.AltosDroid.R;
24 import android.app.Activity;
25 import android.bluetooth.*;
26 import android.content.*;
28 import android.view.*;
29 import android.view.View.*;
30 import android.widget.*;
31 import android.widget.AdapterView.*;
33 import org.altusmetrum.altoslib_11.*;
35 public class SetupActivity extends Activity {
36 private Spinner select_rate;
37 private Spinner set_units;
38 private Spinner map_type;
39 private Spinner map_source;
40 private Button manage_frequencies;
41 private Button preload_maps;
44 private boolean is_bound;
45 private Messenger service = null;
47 public final static String EXTRA_SETUP_CHANGES = "setup_changes";
49 private ServiceConnection connection = new ServiceConnection() {
50 public void onServiceConnected(ComponentName className, IBinder binder) {
51 service = new Messenger(binder);
54 public void onServiceDisconnected(ComponentName className) {
55 // This is called when the connection with the service has been unexpectedly disconnected - process crashed.
60 void doBindService() {
61 bindService(new Intent(this, TelemetryService.class), connection, Context.BIND_AUTO_CREATE);
65 void doUnbindService() {
67 // If we have received the service, and hence registered with it, then now is the time to unregister.
68 unbindService(connection);
73 static final String[] rates = {
79 static final String[] map_types = {
86 static final int[] map_type_values = {
87 AltosMap.maptype_hybrid,
88 AltosMap.maptype_satellite,
89 AltosMap.maptype_roadmap,
90 AltosMap.maptype_terrain,
93 static final String[] map_sources = {
98 private int set_telemetry_rate;
99 private int set_map_source;
100 private int set_map_type;
101 private boolean set_imperial_units;
103 private int changes = 0;
105 private void add_change(int change) {
109 private void done() {
110 Intent intent = new Intent();
111 if ((changes & AltosDroid.SETUP_BAUD) != 0)
112 AltosPreferences.set_telemetry_rate(1, set_telemetry_rate);
113 if ((changes & AltosDroid.SETUP_UNITS) != 0)
114 AltosPreferences.set_imperial_units(set_imperial_units);
115 if ((changes & AltosDroid.SETUP_MAP_SOURCE) != 0)
116 AltosDroidPreferences.set_map_source(set_map_source);
117 if ((changes & AltosDroid.SETUP_MAP_TYPE) != 0)
118 AltosPreferences.set_map_type(set_map_type);
119 intent.putExtra(EXTRA_SETUP_CHANGES, changes);
120 setResult(Activity.RESULT_OK, intent);
124 private void add_strings(Spinner spinner, String[] strings, int def) {
125 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
127 for (int i = 0; i < strings.length; i++)
128 adapter.add(strings[i]);
130 spinner.setAdapter(adapter);
132 spinner.setSelection(def);
135 private int default_rate_pos() {
136 int default_rate = AltosPreferences.telemetry_rate(1);
138 for (int pos = 0; pos < rates.length; pos++) {
139 if (string_to_rate(rates[pos]) == default_rate)
145 private void setBaud(int baud) {
147 service.send(Message.obtain(null, TelemetryService.MSG_SETBAUD, baud));
148 set_telemetry_rate = baud;
149 add_change(AltosDroid.SETUP_BAUD);
150 } catch (RemoteException e) {
154 private int string_to_rate(String baud) {
155 int rate = AltosLib.ao_telemetry_rate_38400;
157 int value = Integer.parseInt(baud);
160 rate = AltosLib.ao_telemetry_rate_2400;
163 rate = AltosLib.ao_telemetry_rate_9600;
166 rate = AltosLib.ao_telemetry_rate_38400;
169 } catch (NumberFormatException e) {
174 private void setBaud(String baud) {
175 setBaud(string_to_rate(baud));
178 private void select_rate(int pos) {
182 static final String[] units = {
187 private int default_units_pos() {
188 boolean imperial = AltosPreferences.imperial_units();
195 private void set_units(int pos) {
198 set_imperial_units = false;
201 set_imperial_units = true;
204 add_change(AltosDroid.SETUP_UNITS);
207 private int default_map_type_pos() {
208 int default_map_type = AltosPreferences.map_type();
210 for (int pos = 0; pos < map_types.length; pos++)
211 if (map_type_values[pos] == default_map_type)
216 private void select_map_type(int pos) {
217 set_map_type = map_type_values[pos];
218 add_change(AltosDroid.SETUP_MAP_TYPE);
221 private int default_map_source_pos() {
222 int default_source = AltosDroidPreferences.map_source();
224 switch (default_source) {
225 case AltosDroidPreferences.MAP_SOURCE_OFFLINE:
232 private void select_map_source(int pos) {
235 set_map_source = AltosDroidPreferences.MAP_SOURCE_ONLINE;
238 set_map_source = AltosDroidPreferences.MAP_SOURCE_OFFLINE;
241 add_change(AltosDroid.SETUP_MAP_SOURCE);
244 private void manage_frequencies(){
245 Intent intent = new Intent(this, ManageFrequenciesActivity.class);
246 startActivity(intent);
249 private void preload_maps(){
250 Intent intent = new Intent(this, PreloadMapActivity.class);
251 startActivity(intent);
255 protected void onCreate(Bundle savedInstanceState) {
256 super.onCreate(savedInstanceState);
259 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
260 setContentView(R.layout.setup);
262 select_rate = (Spinner) findViewById(R.id.select_rate);
263 add_strings(select_rate, rates, default_rate_pos());
264 select_rate.setOnItemSelectedListener(new OnItemSelectedListener() {
265 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
268 public void onNothingSelected(AdapterView<?> parent) {
272 set_units = (Spinner) findViewById(R.id.set_units);
273 add_strings(set_units, units, default_units_pos());
274 set_units.setOnItemSelectedListener(new OnItemSelectedListener() {
275 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
278 public void onNothingSelected(AdapterView<?> parent) {
282 map_type = (Spinner) findViewById(R.id.map_type);
283 add_strings(map_type, map_types, default_map_type_pos());
284 map_type.setOnItemSelectedListener(new OnItemSelectedListener() {
285 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
286 select_map_type(pos);
288 public void onNothingSelected(AdapterView<?> parent) {
292 map_source = (Spinner) findViewById(R.id.map_source);
293 add_strings(map_source, map_sources, default_map_source_pos());
294 map_source.setOnItemSelectedListener(new OnItemSelectedListener() {
295 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
296 select_map_source(pos);
298 public void onNothingSelected(AdapterView<?> parent) {
303 manage_frequencies = (Button) findViewById(R.id.manage_frequencies);
304 manage_frequencies.setOnClickListener(new OnClickListener() {
305 public void onClick(View v) {
306 manage_frequencies();
310 preload_maps = (Button) findViewById(R.id.preload_maps);
311 preload_maps.setOnClickListener(new OnClickListener() {
312 public void onClick(View v) {
317 done = (Button) findViewById(R.id.done);
318 done.setOnClickListener(new OnClickListener() {
319 public void onClick(View v) {
324 // Set result for when the user backs out
325 setResult(Activity.RESULT_CANCELED);
329 protected void onStart() {
335 protected void onStop() {