- void update_ui(AltosState state) {
- mCallsignView.setText(state.data.callsign);
- mStateView.setText(state.data.state());
- double speed = state.speed;
- if (!state.ascent)
- speed = state.baro_speed;
- mSpeedView.setText(String.format("%6.0f", speed));
- mAccelView.setText(String.format("%6.0f", state.acceleration));
- mRangeView.setText(String.format("%6.0f", state.range));
- mAltitudeView.setText(String.format("%6.0f", state.height));
- mAzimuthView.setText(String.format("%3.0f", state.elevation));
- if (state.from_pad != null)
- mBearingView.setText(String.format("%3.0f", state.from_pad.bearing));
- mLatitudeView.setText(pos(state.gps.lat, "N", "S"));
- mLongitudeView.setText(pos(state.gps.lon, "W", "E"));
- }
-
- String pos(double p, String pos, String neg) {
+ public void registerTab(AltosDroidTab mTab) {
+ mTabs.add(mTab);
+ }
+
+ public void unregisterTab(AltosDroidTab mTab) {
+ mTabs.remove(mTab);
+ }
+
+ public void units_changed(boolean imperial_units) {
+ for (AltosDroidTab mTab : mTabs)
+ mTab.units_changed(imperial_units);
+ }
+
+ void update_title(TelemetryState telemetry_state) {
+ switch (telemetry_state.connect) {
+ case TelemetryState.CONNECT_CONNECTED:
+ if (telemetry_state.config != null) {
+ String str = String.format("S/N %d %6.3f MHz", telemetry_state.config.serial,
+ telemetry_state.frequency);
+ if (telemetry_state.telemetry_rate != AltosLib.ao_telemetry_rate_38400)
+ str = str.concat(String.format(" %d bps",
+ AltosLib.ao_telemetry_rate_values[telemetry_state.telemetry_rate]));
+ mTitle.setText(str);
+ } else {
+ mTitle.setText(R.string.title_connected_to);
+ }
+ break;
+ case TelemetryState.CONNECT_CONNECTING:
+ if (telemetry_state.address != null)
+ mTitle.setText(String.format("Connecting to %s...", telemetry_state.address.name));
+ else
+ mTitle.setText("Connecting to something...");
+ break;
+ case TelemetryState.CONNECT_DISCONNECTED:
+ case TelemetryState.CONNECT_NONE:
+ mTitle.setText(R.string.title_not_connected);
+ break;
+ }
+ }
+
+ void start_timer() {
+ if (timer == null) {
+ timer = new Timer();
+ timer.scheduleAtFixedRate(new TimerTask(){ public void run() {onTimerTick();}}, 1000L, 1000L);
+ }
+ }
+
+ void stop_timer() {
+ if (timer != null) {
+ timer.cancel();
+ timer.purge();
+ timer = null;
+ }
+ }
+
+ boolean registered_units_listener;
+
+ void update_state(TelemetryState telemetry_state) {
+
+ if (!registered_units_listener) {
+ registered_units_listener = true;
+ AltosPreferences.register_units_listener(this);
+ }
+
+ update_title(telemetry_state);
+ update_ui(telemetry_state.state, telemetry_state.location);
+ if (telemetry_state.connect == TelemetryState.CONNECT_CONNECTED)
+ start_timer();
+ }
+
+ 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_age() {
+ if (saved_state != null)
+ mAgeView.setText(String.format("%d", (System.currentTimeMillis() - saved_state.received_time + 500) / 1000));
+ }
+
+ void update_ui(AltosState state, Location location) {
+
+ int prev_state = AltosLib.ao_flight_invalid;
+
+ AltosGreatCircle from_receiver = null;
+
+ if (saved_state != null)
+ prev_state = saved_state.state;
+
+ if (state != null) {
+ if (state.state == AltosLib.ao_flight_stateless) {
+ boolean prev_locked = false;
+ boolean locked = false;
+
+ if(state.gps != null)
+ locked = state.gps.locked;
+ if (saved_state != null && saved_state.gps != null)
+ prev_locked = saved_state.gps.locked;
+ if (prev_locked != locked) {
+ String currentTab = mTabHost.getCurrentTabTag();
+ if (locked) {
+ if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("descent");
+ } else {
+ if (currentTab.equals("descent")) mTabHost.setCurrentTabByTag("pad");
+ }
+ }
+ } else {
+ if (prev_state != state.state) {
+ String currentTab = mTabHost.getCurrentTabTag();
+ switch (state.state) {
+ case AltosLib.ao_flight_boost:
+ if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("ascent");
+ break;
+ case AltosLib.ao_flight_drogue:
+ if (currentTab.equals("ascent")) mTabHost.setCurrentTabByTag("descent");
+ break;
+ 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;
+ }
+ }
+ }
+
+ if (location != null && state.gps != null && state.gps.locked) {
+ double altitude = 0;
+ if (location.hasAltitude())
+ altitude = location.getAltitude();
+ from_receiver = new AltosGreatCircle(location.getLatitude(),
+ location.getLongitude(),
+ altitude,
+ state.gps.lat,
+ state.gps.lon,
+ state.gps.alt);
+ }
+
+ if (saved_state == null || !same_string(saved_state.callsign, state.callsign)) {
+ mCallsignView.setText(state.callsign);
+ }
+ if (saved_state == null || state.serial != saved_state.serial) {
+ mSerialView.setText(String.format("%d", state.serial));
+ }
+ if (saved_state == null || state.flight != saved_state.flight) {
+ if (state.flight == AltosLib.MISSING)
+ mFlightView.setText("");
+ else
+ mFlightView.setText(String.format("%d", state.flight));
+ }
+ 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());
+ mStateLayout.setVisibility(View.VISIBLE);
+ }
+ }
+ if (saved_state == null || state.rssi != saved_state.rssi) {
+ mRSSIView.setText(String.format("%d", state.rssi));
+ }
+ }
+
+ for (AltosDroidTab mTab : mTabs)
+ mTab.update_ui(state, from_receiver, location, mTab == mTabsAdapter.currentItem());
+
+ if (state != null && mAltosVoice != null)
+ mAltosVoice.tell(state, from_receiver);
+
+ saved_state = state;
+ }
+
+ private void onTimerTick() {
+ try {
+ mMessenger.send(Message.obtain(null, MSG_UPDATE_AGE));
+ } catch (RemoteException e) {
+ }
+ }
+
+ static String pos(double p, String pos, String neg) {