X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=altosdroid%2Fsrc%2Forg%2Faltusmetrum%2FAltosDroid%2FAltosDroid.java;h=c9c38d9819a6e7ac798d3a12a800fdcf93dfe300;hb=59dfe661fcb504f390d9726378c676f2b5b005f3;hp=b1d080dbb9d67aee5eb986d1e3e4b4d894dab943;hpb=9212ce268f3a4a9f3f019f23f6eef8b57207d340;p=fw%2Faltos diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index b1d080db..c9c38d98 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -37,7 +37,8 @@ import android.os.Message; import android.os.Messenger; import android.os.RemoteException; import android.support.v4.app.FragmentActivity; -import android.support.v4.view.ViewPager; +import android.support.v4.app.FragmentManager; +import android.util.DisplayMetrics; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; @@ -49,12 +50,12 @@ import android.widget.Toast; import android.app.AlertDialog; import android.location.Location; -import org.altusmetrum.altoslib_1.*; +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; @@ -67,6 +68,8 @@ public class AltosDroid extends FragmentActivity { 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; @@ -82,10 +85,11 @@ public class AltosDroid extends FragmentActivity { private TextView mVersion; // Tabs - TabHost mTabHost; - AltosViewPager mViewPager; - TabsAdapter mTabsAdapter; + TabHost mTabHost; + AltosViewPager mViewPager; + TabsAdapter mTabsAdapter; ArrayList mTabs = new ArrayList(); + int tabHeight; // Timer and Saved flight state for Age calculation private Timer timer = new Timer(); @@ -126,7 +130,6 @@ public class AltosDroid extends FragmentActivity { ad.mTitle.setText(R.string.title_connected_to); ad.mTitle.append(str); Toast.makeText(ad.getApplicationContext(), "Connected to " + str, Toast.LENGTH_SHORT).show(); - ad.mAltosVoice.speak("Connected"); break; case TelemetryService.STATE_CONNECTING: ad.mTitle.setText(R.string.title_connecting); @@ -145,9 +148,10 @@ 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.report_time + 500) / 1000)); + ad.mAgeView.setText(String.format("%d", (System.currentTimeMillis() - ad.saved_state.received_time + 500) / 1000)); } break; } @@ -206,15 +210,29 @@ public class AltosDroid extends FragmentActivity { void set_location(Location location) { saved_location = location; - if (saved_state != null) { - update_ui(saved_state); + Log.d(TAG, "set_location"); + update_ui(saved_state); + } + + 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 (saved_state != null) { + + Log.d(TAG, "update_ui"); + if (state != null && saved_state != null) { if (saved_state.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"); @@ -228,29 +246,51 @@ public class AltosDroid extends FragmentActivity { } } } - saved_state = state; AltosGreatCircle from_receiver = null; - if (saved_location != null && state.gps != null && state.gps.locked) { + if (state != null && saved_location != null && state.gps != null && state.gps.locked) { + double altitude = 0; + if (saved_location.hasAltitude()) + altitude = saved_location.getAltitude(); from_receiver = new AltosGreatCircle(saved_location.getLatitude(), saved_location.getLongitude(), - saved_location.getAltitude(), + altitude, state.gps.lat, state.gps.lon, state.gps.alt); } - mCallsignView.setText(state.data.callsign); - mSerialView.setText(String.format("%d", state.data.serial)); - mFlightView.setText(String.format("%d", state.data.flight)); - mStateView.setText(state.data.state()); - mRSSIView.setText(String.format("%d", state.data.rssi)); + if (state != null) { + 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"); + mStateView.setText(state.state_name()); + } + 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); + mTab.update_ui(state, from_receiver, saved_location, mTab == mTabsAdapter.currentItem()); - mAltosVoice.tell(state); + if (state != null) + mAltosVoice.tell(state); + + saved_state = state; } private void onTimerTick() { @@ -262,13 +302,27 @@ public class AltosDroid extends FragmentActivity { static String pos(double p, String pos, String neg) { String h = pos; + if (p == AltosLib.MISSING) + return ""; if (p < 0) { h = neg; p = -p; } int deg = (int) Math.floor(p); double min = (p - Math.floor(p)) * 60.0; - return String.format("%d° %9.6f\" %s", deg, min, h); + return String.format("%d°%9.4f\" %s", deg, min, h); + } + + static String number(String format, double value) { + if (value == AltosLib.MISSING) + return ""; + return String.format(format, value); + } + + static String integer(String format, int value) { + if (value == AltosLib.MISSING) + return ""; + return String.format(format, value); } @Override @@ -276,6 +330,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(); @@ -295,6 +351,7 @@ public class AltosDroid extends FragmentActivity { setContentView(R.layout.altosdroid); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); + // Create the Tabs and ViewPager mTabHost = (TabHost)findViewById(android.R.id.tabhost); mTabHost.setup(); @@ -310,6 +367,27 @@ public class AltosDroid extends FragmentActivity { mTabsAdapter.addTab(mTabHost.newTabSpec("map").setIndicator("Map"), TabMap.class, null); + // Scale the size of the Tab bar for different screen densities + // This probably won't be needed when we start supporting ICS+ tabs. + DisplayMetrics metrics = new DisplayMetrics(); + getWindowManager().getDefaultDisplay().getMetrics(metrics); + int density = metrics.densityDpi; + + if (density==DisplayMetrics.DENSITY_XHIGH) + tabHeight = 65; + else if (density==DisplayMetrics.DENSITY_HIGH) + tabHeight = 45; + else if (density==DisplayMetrics.DENSITY_MEDIUM) + tabHeight = 35; + else if (density==DisplayMetrics.DENSITY_LOW) + tabHeight = 25; + else + tabHeight = 65; + + for (int i = 0; i < 5; i++) + mTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = tabHeight; + + // Set up the custom title mTitle = (TextView) findViewById(R.id.title_left_text); mTitle.setText(R.string.app_name); @@ -374,7 +452,7 @@ public class AltosDroid extends FragmentActivity { super.onDestroy(); if(D) Log.e(TAG, "--- ON DESTROY ---"); - mAltosVoice.stop(); + if (mAltosVoice != null) mAltosVoice.stop(); } public void onActivityResult(int requestCode, int resultCode, Intent data) { @@ -436,6 +514,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; @@ -461,16 +566,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;