X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altosdroid%2Fsrc%2Forg%2Faltusmetrum%2FAltosDroid%2FAltosDroid.java;h=db1ca6916c643fc448ad69639e860fdf8e0f058c;hp=92287476b7dbf56fc654c408e5ddeb8c5ba41fd4;hb=bc3fbcb35090be3856284ccf4d908ebf39d02bec;hpb=488a527267decece48e6682e0e0c7fc29cbed329 diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index 92287476..db1ca691 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -37,24 +37,27 @@ import android.os.Message; import android.os.Messenger; import android.os.RemoteException; import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; import android.util.DisplayMetrics; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.Window; +import android.view.View; import android.widget.TabHost; import android.widget.TextView; +import android.widget.RelativeLayout; import android.widget.Toast; import android.app.AlertDialog; import android.location.Location; -import org.altusmetrum.altoslib_2.*; +import org.altusmetrum.altoslib_5.*; public class AltosDroid extends FragmentActivity { // Debugging - private static final String TAG = "AltosDroid"; - private static final boolean D = true; + static final String TAG = "AltosDroid"; + static final boolean D = true; // Message types received by our Handler public static final int MSG_STATE_CHANGE = 1; @@ -62,11 +65,15 @@ public class AltosDroid extends FragmentActivity { public static final int MSG_UPDATE_AGE = 3; public static final int MSG_LOCATION = 4; public static final int MSG_CRC_ERROR = 5; + public static final int MSG_FREQUENCY = 6; + public static final int MSG_TELEMETRY_RATE = 7; // Intent request codes private static final int REQUEST_CONNECT_DEVICE = 1; private static final int REQUEST_ENABLE_BT = 2; + public static FragmentManager fm; + // Layout Views private TextView mTitle; @@ -75,12 +82,16 @@ public class AltosDroid extends FragmentActivity { private TextView mRSSIView; private TextView mSerialView; private TextView mFlightView; + private RelativeLayout mStateLayout; private TextView mStateView; private TextView mAgeView; // field to display the version at the bottom of the screen private TextView mVersion; + private double frequency; + private int telemetry_rate; + // Tabs TabHost mTabHost; AltosViewPager mViewPager; @@ -98,9 +109,6 @@ public class AltosDroid extends FragmentActivity { private Messenger mService = null; final Messenger mMessenger = new Messenger(new IncomingHandler(this)); - // Preferences - private AltosDroidPreferences prefs = null; - // TeleBT Config data private AltosConfigData mConfigData = null; // Local Bluetooth adapter @@ -122,11 +130,7 @@ public class AltosDroid extends FragmentActivity { if(D) Log.d(TAG, "MSG_STATE_CHANGE: " + msg.arg1); switch (msg.arg1) { case TelemetryService.STATE_CONNECTED: - ad.mConfigData = (AltosConfigData) msg.obj; - String str = String.format(" %s S/N: %d", ad.mConfigData.product, ad.mConfigData.serial); - ad.mTitle.setText(R.string.title_connected_to); - ad.mTitle.append(str); - Toast.makeText(ad.getApplicationContext(), "Connected to " + str, Toast.LENGTH_SHORT).show(); + ad.set_config_data((AltosConfigData) msg.obj); break; case TelemetryService.STATE_CONNECTING: ad.mTitle.setText(R.string.title_connecting); @@ -135,6 +139,9 @@ public class AltosDroid extends FragmentActivity { case TelemetryService.STATE_NONE: ad.mConfigData = null; ad.mTitle.setText(R.string.title_not_connected); + String active_device = AltosDroidPreferences.active_device(); + if (active_device != null) + ad.connectDevice(active_device); break; } break; @@ -145,11 +152,18 @@ public class AltosDroid extends FragmentActivity { ad.set_location((Location) msg.obj); break; case MSG_CRC_ERROR: + break; case MSG_UPDATE_AGE: if (ad.saved_state != null) { ad.mAgeView.setText(String.format("%d", (System.currentTimeMillis() - ad.saved_state.received_time + 500) / 1000)); } break; + case MSG_FREQUENCY: + ad.set_frequency((Double) msg.obj); + break; + case MSG_TELEMETRY_RATE: + ad.set_telemetry_rate((Integer) msg.obj); + break; } } }; @@ -206,13 +220,63 @@ public class AltosDroid extends FragmentActivity { void set_location(Location location) { saved_location = location; + Log.d(TAG, "set_location"); update_ui(saved_state); } + void set_title() { + if (mConfigData != null) { + String str = String.format("S/N %d %6.3f MHz", mConfigData.serial, frequency); + + if (telemetry_rate != AltosLib.ao_telemetry_rate_38400) + str = str.concat(String.format(" %d bps", AltosLib.ao_telemetry_rate_values[telemetry_rate])); + mTitle.setText(str); + } + } + + void set_frequency(double frequency) { + if (D) Log.d(TAG, String.format("AltosDroid: set_frequency %f\n", frequency)); + this.frequency = frequency; + set_title(); + } + + void set_telemetry_rate(int telemetry_rate) { + if (D) Log.d(TAG, String.format("AltosDroid: set_telemetry_rate %d\n", telemetry_rate)); + this.telemetry_rate = telemetry_rate; + set_title(); + } + + void set_config_data(AltosConfigData config_data) { + mConfigData = config_data; + set_title(); + } + + boolean same_string(String a, String b) { + if (a == null) { + if (b == null) + return true; + return false; + } else { + if (b == null) + return false; + return a.equals(b); + } + } + void update_ui(AltosState state) { - if (state != null && saved_state != null) { - if (saved_state.state != state.state) { + + Log.d(TAG, "update_ui"); + + int prev_state = AltosLib.ao_flight_invalid; + + if (saved_state != null) + prev_state = saved_state.state; + + if (state != null) { + Log.d(TAG, String.format("prev state %d new state %d\n", prev_state, state.state)); + if (prev_state != state.state) { String currentTab = mTabHost.getCurrentTabTag(); + Log.d(TAG, "switch state"); switch (state.state) { case AltosLib.ao_flight_boost: if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("ascent"); @@ -223,10 +287,12 @@ public class AltosDroid extends FragmentActivity { case AltosLib.ao_flight_landed: if (currentTab.equals("descent")) mTabHost.setCurrentTabByTag("landed"); break; + case AltosLib.ao_flight_stateless: + if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("descent"); + break; } } } - saved_state = state; AltosGreatCircle from_receiver = null; @@ -243,18 +309,40 @@ public class AltosDroid extends FragmentActivity { } if (state != null) { - mCallsignView.setText(state.callsign); - mSerialView.setText(String.format("%d", state.serial)); - mFlightView.setText(String.format("%d", state.flight)); - mStateView.setText(state.state_name()); - mRSSIView.setText(String.format("%d", state.rssi)); + if (saved_state == null || !same_string(saved_state.callsign, state.callsign)) { + Log.d(TAG, "update callsign"); + mCallsignView.setText(state.callsign); + } + if (saved_state == null || state.serial != saved_state.serial) { + Log.d(TAG, "update serial"); + mSerialView.setText(String.format("%d", state.serial)); + } + if (saved_state == null || state.flight != saved_state.flight) { + Log.d(TAG, "update flight"); + mFlightView.setText(String.format("%d", state.flight)); + } + if (saved_state == null || state.state != saved_state.state) { + Log.d(TAG, "update state"); + if (state.state == AltosLib.ao_flight_stateless) { + mStateLayout.setVisibility(View.GONE); + } else { + mStateView.setText(state.state_name()); + mStateLayout.setVisibility(View.VISIBLE); + } + } + if (saved_state == null || state.rssi != saved_state.rssi) { + Log.d(TAG, "update rssi"); + mRSSIView.setText(String.format("%d", state.rssi)); + } } for (AltosDroidTab mTab : mTabs) - mTab.update_ui(state, from_receiver, saved_location); + mTab.update_ui(state, from_receiver, saved_location, mTab == mTabsAdapter.currentItem()); if (state != null) mAltosVoice.tell(state); + + saved_state = state; } private void onTimerTick() { @@ -294,6 +382,8 @@ public class AltosDroid extends FragmentActivity { super.onCreate(savedInstanceState); if(D) Log.e(TAG, "+++ ON CREATE +++"); + fm = getSupportFragmentManager(); + // Get local Bluetooth adapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); @@ -305,8 +395,7 @@ public class AltosDroid extends FragmentActivity { } // Initialise preferences - prefs = new AltosDroidPreferences(this); - AltosPreferences.init(prefs); + AltosDroidPreferences.init(this); // Set up the window layout requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); @@ -365,6 +454,7 @@ public class AltosDroid extends FragmentActivity { mRSSIView = (TextView) findViewById(R.id.rssi_value); mSerialView = (TextView) findViewById(R.id.serial_value); mFlightView = (TextView) findViewById(R.id.flight_value); + mStateLayout = (RelativeLayout) findViewById(R.id.state_container); mStateView = (TextView) findViewById(R.id.state_value); mAgeView = (TextView) findViewById(R.id.age_value); @@ -387,6 +477,7 @@ public class AltosDroid extends FragmentActivity { startService(new Intent(AltosDroid.this, TelemetryService.class)); doBindService(); + } @Override @@ -442,9 +533,7 @@ public class AltosDroid extends FragmentActivity { } } - private void connectDevice(Intent data) { - // Get the device MAC address - String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); + private void connectDevice(String address) { // Get the BLuetoothDevice object BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); // Attempt to connect to the device @@ -455,6 +544,13 @@ public class AltosDroid extends FragmentActivity { } } + private void connectDevice(Intent data) { + // Get the device MAC address + String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); + AltosDroidPreferences.set_active_device(address); + connectDevice(address); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); @@ -476,6 +572,33 @@ public class AltosDroid extends FragmentActivity { } } + void setBaud(int baud) { + try { + mService.send(Message.obtain(null, TelemetryService.MSG_SETBAUD, baud)); + } catch (RemoteException e) { + } + } + + void setBaud(String baud) { + try { + int value = Integer.parseInt(baud); + int rate = AltosLib.ao_telemetry_rate_38400; + switch (value) { + case 2400: + rate = AltosLib.ao_telemetry_rate_2400; + break; + case 9600: + rate = AltosLib.ao_telemetry_rate_9600; + break; + case 38400: + rate = AltosLib.ao_telemetry_rate_38400; + break; + } + setBaud(rate); + } catch (NumberFormatException e) { + } + } + @Override public boolean onOptionsItemSelected(MenuItem item) { Intent serverIntent = null; @@ -485,6 +608,11 @@ public class AltosDroid extends FragmentActivity { serverIntent = new Intent(this, DeviceListActivity.class); startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE); return true; + case R.id.quit: + Log.d(TAG, "R.id.quit"); + stopService(new Intent(AltosDroid.this, TelemetryService.class)); + finish(); + return true; case R.id.select_freq: // Set the TBT radio frequency @@ -501,16 +629,36 @@ public class AltosDroid extends FragmentActivity { "Channel 9 (435.450MHz)" }; - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle("Pick a frequency"); - builder.setItems(frequencies, + AlertDialog.Builder builder_freq = new AlertDialog.Builder(this); + builder_freq.setTitle("Pick a frequency"); + builder_freq.setItems(frequencies, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { setFrequency(frequencies[item]); } }); - AlertDialog alert = builder.create(); - alert.show(); + AlertDialog alert_freq = builder_freq.create(); + alert_freq.show(); + return true; + case R.id.select_rate: + // Set the TBT baud rate + + final String[] rates = { + "38400", + "9600", + "2400", + }; + + AlertDialog.Builder builder_rate = new AlertDialog.Builder(this); + builder_rate.setTitle("Pick a baud rate"); + builder_rate.setItems(rates, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int item) { + setBaud(rates[item]); + } + }); + AlertDialog alert_rate = builder_rate.create(); + alert_rate.show(); return true; } return false;