import android.widget.*;
import android.app.AlertDialog;
import android.location.Location;
+import android.location.LocationManager;
+import android.location.LocationListener;
import android.hardware.usb.*;
import android.graphics.*;
import android.graphics.drawable.*;
-import org.altusmetrum.altoslib_7.*;
+import org.altusmetrum.altoslib_10.*;
-public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
+public class AltosDroid extends FragmentActivity implements AltosUnitsListener, LocationListener {
// Actions sent to the telemetry server at startup time
private int mAgeNewColor;
private int mAgeOldColor;
+ public static final String tab_pad_name = "pad";
+ public static final String tab_flight_name = "flight";
+ public static final String tab_recover_name = "recover";
+ public static final String tab_map_name = "map";
+
// field to display the version at the bottom of the screen
private TextView mVersion;
private double frequency;
private int telemetry_rate;
+ public Location location = null;
+
// Tabs
TabHost mTabHost;
AltosViewPager mViewPager;
switch (msg.what) {
case MSG_STATE:
- AltosDebug.debug("MSG_STATE");
if (msg.obj == null) {
AltosDebug.debug("telemetry_state null!");
return;
ad.update_state((TelemetryState) msg.obj);
break;
case MSG_UPDATE_AGE:
- AltosDebug.debug("MSG_UPDATE_AGE");
ad.update_age();
break;
}
state = newest_state;
}
- update_ui(telemetry_state, state, telemetry_state.location);
+ update_ui(telemetry_state, state);
start_timer();
}
}
}
- void update_ui(TelemetryState telem_state, AltosState state, Location location) {
+ void update_ui(TelemetryState telem_state, AltosState state) {
int prev_state = AltosLib.ao_flight_invalid;
AltosGreatCircle from_receiver = null;
if (saved_state != null)
- prev_state = saved_state.state;
+ prev_state = saved_state.state();
if (state != null) {
set_screen_on(state_age(state));
- if (state.state == AltosLib.ao_flight_stateless) {
+ if (state.state() == AltosLib.ao_flight_stateless) {
boolean prev_locked = false;
boolean locked = false;
if (prev_locked != locked) {
String currentTab = mTabHost.getCurrentTabTag();
if (locked) {
- if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("flight");
+ if (currentTab.equals(tab_pad_name)) mTabHost.setCurrentTabByTag(tab_flight_name);
} else {
- if (currentTab.equals("flight")) mTabHost.setCurrentTabByTag("pad");
+ if (currentTab.equals(tab_flight_name)) mTabHost.setCurrentTabByTag(tab_pad_name);
}
}
} else {
- if (prev_state != state.state) {
+ if (prev_state != state.state()) {
String currentTab = mTabHost.getCurrentTabTag();
- switch (state.state) {
+ switch (state.state()) {
case AltosLib.ao_flight_boost:
- if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("flight");
+ if (currentTab.equals(tab_pad_name)) mTabHost.setCurrentTabByTag(tab_flight_name);
break;
case AltosLib.ao_flight_landed:
- if (currentTab.equals("flight")) mTabHost.setCurrentTabByTag("recover");
+ if (currentTab.equals(tab_flight_name)) mTabHost.setCurrentTabByTag(tab_recover_name);
break;
case AltosLib.ao_flight_stateless:
- if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("flight");
+ if (currentTab.equals(tab_pad_name)) mTabHost.setCurrentTabByTag(tab_flight_name);
break;
}
}
else
mFlightView.setText(String.format("%d", state.flight));
}
- if (saved_state == null || state.state != saved_state.state) {
- if (state.state == AltosLib.ao_flight_stateless) {
+ if (saved_state == null || state.state() != saved_state.state()) {
+ if (state.state() == AltosLib.ao_flight_stateless) {
mStateLayout.setVisibility(View.GONE);
} else {
mStateView.setText(state.state_name());
for (AltosDroidTab mTab : mTabs)
mTab.update_ui(telem_state, state, from_receiver, location, mTab == mTabsAdapter.currentItem());
- if (state != null && mAltosVoice != null)
- mAltosVoice.tell(state, from_receiver);
+ if (mAltosVoice != null)
+ mAltosVoice.tell(telem_state, state, from_receiver, location, (AltosDroidTab) mTabsAdapter.currentItem());
saved_state = state;
}
mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
- mTabsAdapter.addTab(mTabHost.newTabSpec("pad").setIndicator(create_tab_view("Pad")), TabPad.class, null);
- mTabsAdapter.addTab(mTabHost.newTabSpec("flight").setIndicator(create_tab_view("Flight")), TabFlight.class, null);
- mTabsAdapter.addTab(mTabHost.newTabSpec("recover").setIndicator(create_tab_view("Recover")), TabRecover.class, null);
- mTabsAdapter.addTab(mTabHost.newTabSpec("map").setIndicator(create_tab_view("Map")), TabMap.class, null);
+ mTabsAdapter.addTab(mTabHost.newTabSpec(tab_pad_name).setIndicator(create_tab_view("Pad")), TabPad.class, null);
+ mTabsAdapter.addTab(mTabHost.newTabSpec(tab_flight_name).setIndicator(create_tab_view("Flight")), TabFlight.class, null);
+ mTabsAdapter.addTab(mTabHost.newTabSpec(tab_recover_name).setIndicator(create_tab_view("Recover")), TabRecover.class, null);
+ mTabsAdapter.addTab(mTabHost.newTabSpec(tab_map_name).setIndicator(create_tab_view("Map")), TabMap.class, null);
// Display the Version
mVersion = (TextView) findViewById(R.id.version);
mAgeOldColor = getResources().getColor(R.color.old_color);
}
- private boolean ensureBluetooth() {
+ private void ensureBluetooth() {
// Get local Bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- // If the adapter is null, then Bluetooth is not supported
- if (mBluetoothAdapter == null) {
- Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
- return false;
- }
-
- if (!mBluetoothAdapter.isEnabled()) {
+ /* if there is a BT adapter and it isn't turned on, then turn it on */
+ if (mBluetoothAdapter != null && !mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, AltosDroid.REQUEST_ENABLE_BT);
}
-
- return true;
}
private boolean check_usb() {
return;
}
AltosDebug.debug("Starting by looking for bluetooth devices");
- if (ensureBluetooth())
- return;
- finish();
+ ensureBluetooth();
}
}
super.onStart();
AltosDebug.debug("++ ON START ++");
+ set_switch_time();
+
noticeIntent(getIntent());
// Start Telemetry Service
public void onResume() {
super.onResume();
AltosDebug.debug("+ ON RESUME +");
+
+ // Listen for GPS and Network position updates
+ LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
+ locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
+
+ location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
+
+ AltosDebug.debug("Resume, location is %f,%f\n",
+ location.getLatitude(),
+ location.getLongitude());
+
+ update_ui(telemetry_state, saved_state);
}
@Override
public void onPause() {
super.onPause();
AltosDebug.debug("- ON PAUSE -");
+ // Stop listening for location updates
+ ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);
}
@Override
public void onStop() {
super.onStop();
AltosDebug.debug("-- ON STOP --");
-
- doUnbindService();
- if (mAltosVoice != null) {
- mAltosVoice.stop();
- mAltosVoice = null;
- }
}
@Override
super.onDestroy();
AltosDebug.debug("--- ON DESTROY ---");
- if (mAltosVoice != null) mAltosVoice.stop();
+ doUnbindService();
+ if (mAltosVoice != null) {
+ mAltosVoice.stop();
+ mAltosVoice = null;
+ }
stop_timer();
}
if (resultCode == Activity.RESULT_OK) {
// Bluetooth is now enabled, so set up a chat session
//setupChat();
+ AltosDebug.debug("BT enabled");
+ bluetoothEnabled(data);
} else {
// User did not enable Bluetooth or an error occured
- AltosDebug.error("BT not enabled");
- stopService(new Intent(AltosDroid.this, TelemetryService.class));
- Toast.makeText(this, R.string.bt_not_enabled, Toast.LENGTH_SHORT).show();
- finish();
+ AltosDebug.debug("BT not enabled");
}
break;
case REQUEST_MAP_TYPE:
}
}
+ private void bluetoothEnabled(Intent data) {
+ try {
+ mService.send(Message.obtain(null, TelemetryService.MSG_BLUETOOTH_ENABLED, null));
+ } catch (RemoteException e) {
+ AltosDebug.debug("send BT enabled message failed");
+ }
+ }
+
private void connectDevice(Intent data) {
// Attempt to connect to the device
try {
update_state(null);
}
+ void touch_trackers(Integer[] serials) {
+ AlertDialog.Builder builder_tracker = new AlertDialog.Builder(this);
+ builder_tracker.setTitle("Select Tracker");
+ final String[] trackers = new String[serials.length + 1];
+ trackers[0] = "Auto";
+ for (int i = 0; i < serials.length; i++)
+ trackers[i+1] = String.format("%d", serials[i]);
+ builder_tracker.setItems(trackers,
+ new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int item) {
+ if (item == 0)
+ select_tracker(0);
+ else
+ select_tracker(Integer.parseInt(trackers[item]));
+ }
+ });
+ AlertDialog alert_tracker = builder_tracker.create();
+ alert_tracker.show();
+ }
+
void delete_track(int serial) {
try {
mService.send(Message.obtain(null, TelemetryService.MSG_DELETE_SERIAL, (Integer) serial));
Intent serverIntent = null;
switch (item.getItemId()) {
case R.id.connect_scan:
- if (ensureBluetooth()) {
- // Launch the DeviceListActivity to see devices and do scan
- serverIntent = new Intent(this, DeviceListActivity.class);
- startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
- }
+ ensureBluetooth();
+ // Launch the DeviceListActivity to see devices and do scan
+ serverIntent = new Intent(this, DeviceListActivity.class);
+ startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
return true;
case R.id.disconnect:
/* Disconnect the device
}
static String direction(AltosGreatCircle from_receiver,
- Location receiver) {
+ Location receiver) {
+ if (from_receiver == null)
+ return null;
+
+ if (receiver == null)
+ return null;
+
if (!receiver.hasBearing())
return null;
else if (iheading < -179 || 179 < iheading)
return "backwards";
else if (iheading < 0)
- return String.format("left %d", -iheading);
+ return String.format("left %d°", -iheading);
else
- return String.format("right %d", iheading);
+ return String.format("right %d°", iheading);
+ }
+
+ public void onLocationChanged(Location location) {
+ this.location = location;
+ AltosDebug.debug("Location changed to %f,%f",
+ location.getLatitude(),
+ location.getLongitude());
+ update_ui(telemetry_state, saved_state);
+ }
+
+ public void onStatusChanged(String provider, int status, Bundle extras) {
+ AltosDebug.debug("Location status now %d\n", status);
+ }
+
+ public void onProviderEnabled(String provider) {
+ AltosDebug.debug("Location provider enabled %s\n", provider);
+ }
+
+ public void onProviderDisabled(String provider) {
+ AltosDebug.debug("Location provider disabled %s\n", provider);
}
}