+ void doBindService() {
+ bindService(new Intent(this, TelemetryService.class), mConnection, Context.BIND_AUTO_CREATE);
+ mIsBound = true;
+ }
+
+ void doUnbindService() {
+ if (mIsBound) {
+ // If we have received the service, and hence registered with it, then now is the time to unregister.
+ if (mService != null) {
+ try {
+ Message msg = Message.obtain(null, TelemetryService.MSG_UNREGISTER_CLIENT);
+ msg.replyTo = mMessenger;
+ mService.send(msg);
+ } catch (RemoteException e) {
+ // There is nothing special we need to do if the service has crashed.
+ }
+ }
+ // Detach our existing connection.
+ unbindService(mConnection);
+ mIsBound = false;
+ }
+ }
+
+ public void registerTab(AltosDroidTab mTab) {
+ mTabs.add(mTab);
+ }
+
+ public void unregisterTab(AltosDroidTab mTab) {
+ mTabs.remove(mTab);
+ }
+
+ void set_location(Location location) {
+ saved_location = location;
+ 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) {
+
+ 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");
+ 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;
+ }
+ }
+ }
+
+ AltosGreatCircle from_receiver = null;
+
+ 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(),
+ altitude,
+ state.gps.lat,
+ state.gps.lon,
+ state.gps.alt);
+ }
+
+ 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, saved_location, mTab == mTabsAdapter.currentItem());
+
+ if (state != null)
+ mAltosVoice.tell(state);
+
+ 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) {
+ 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.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);
+ }