From 95dfc88f952c3c962acc41532d2b0cb72ac97ddd Mon Sep 17 00:00:00 2001 From: James Cotton Date: Wed, 8 Aug 2012 12:32:10 -0500 Subject: [PATCH] AndroidGCS: Move the PFD functionality into a fragment and then programmatically instantiate that from the test PFD activity --- androidgcs/AndroidManifest.xml | 2 +- .../openpilot/androidgcs/AttitudeView.java | 2 - .../org/openpilot/androidgcs/HomePage.java | 2 +- .../src/org/openpilot/androidgcs/PFD.java | 84 ++++++++++--------- .../org/openpilot/androidgcs/PfdActivity.java | 58 +++++++++++++ 5 files changed, 103 insertions(+), 45 deletions(-) create mode 100644 androidgcs/src/org/openpilot/androidgcs/PfdActivity.java diff --git a/androidgcs/AndroidManifest.xml b/androidgcs/AndroidManifest.xml index 2015bc979..1fbb16f55 100644 --- a/androidgcs/AndroidManifest.xml +++ b/androidgcs/AndroidManifest.xml @@ -24,7 +24,7 @@ - + diff --git a/androidgcs/src/org/openpilot/androidgcs/AttitudeView.java b/androidgcs/src/org/openpilot/androidgcs/AttitudeView.java index b570f933c..d8aecc5e1 100644 --- a/androidgcs/src/org/openpilot/androidgcs/AttitudeView.java +++ b/androidgcs/src/org/openpilot/androidgcs/AttitudeView.java @@ -83,13 +83,11 @@ public class AttitudeView extends View { private float roll; public void setRoll(double roll) { this.roll = (float) roll; - postInvalidate(); } private float pitch; public void setPitch(double d) { this.pitch = (float) d; - postInvalidate(); } @Override diff --git a/androidgcs/src/org/openpilot/androidgcs/HomePage.java b/androidgcs/src/org/openpilot/androidgcs/HomePage.java index e6a33f083..12833df2f 100644 --- a/androidgcs/src/org/openpilot/androidgcs/HomePage.java +++ b/androidgcs/src/org/openpilot/androidgcs/HomePage.java @@ -48,7 +48,7 @@ public class HomePage extends ObjectManagerActivity { pfd.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { - startActivity(new Intent(HomePage.this, PFD.class)); + startActivity(new Intent(HomePage.this, PfdActivity.class)); } }); diff --git a/androidgcs/src/org/openpilot/androidgcs/PFD.java b/androidgcs/src/org/openpilot/androidgcs/PFD.java index 3832be41a..fa4480180 100644 --- a/androidgcs/src/org/openpilot/androidgcs/PFD.java +++ b/androidgcs/src/org/openpilot/androidgcs/PFD.java @@ -2,8 +2,9 @@ ****************************************************************************** * @file PFD.java * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012. - * @brief Shows the PFD activity. + * @brief The PFD display fragment * @see The GNU Public License (GPL) Version 3 + * *****************************************************************************/ /* * This program is free software; you can redistribute it and/or modify @@ -24,58 +25,59 @@ package org.openpilot.androidgcs; import org.openpilot.uavtalk.UAVObject; +import org.openpilot.uavtalk.UAVObjectManager; import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; -public class PFD extends ObjectManagerActivity { +public class PFD extends ObjectManagerFragment { - final long MIN_UPDATE_PERIOD = 50; - long lastUpdateMs; - double heading; - double roll; - double pitch; + private static final String TAG = ObjectManagerFragment.class + .getSimpleName(); + private static final int LOGLEVEL = 0; + // private static boolean WARN = LOGLEVEL > 1; + private static final boolean DEBUG = LOGLEVEL > 0; - /** - * Update the UI whenever the attitude is updated - */ + // @Override @Override - protected void objectUpdated(UAVObject obj) { - // Throttle the UI redraws. Eventually this should maybe come from a periodic task - if ((System.currentTimeMillis() - lastUpdateMs) < MIN_UPDATE_PERIOD) - return; - if (obj.getName().compareTo("AttitudeActual") != 0) - return; - - lastUpdateMs = System.currentTimeMillis(); - - heading = obj.getField("Yaw").getDouble(); - pitch = obj.getField("Pitch").getDouble(); - roll = obj.getField("Roll").getDouble(); - - /* - * CompassView compass = (CompassView) findViewById(R.id.compass_view); - * compass.setBearing((int) heading); compass.invalidate(); - */ - - AttitudeView attitude = (AttitudeView) findViewById(R.id.attitude_view); - attitude.setRoll(roll); - attitude.setPitch(pitch); - attitude.invalidate(); + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.pfd, container, false); } @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.pfd); - } + public void onOPConnected(UAVObjectManager objMngr) { + super.onOPConnected(objMngr); + if (DEBUG) + Log.d(TAG, "On connected"); - @Override - void onOPConnected() { - super.onOPConnected(); - - // Connect the update method to AttitudeActual UAVObject obj = objMngr.getObject("AttitudeActual"); if (obj != null) registerObjectUpdates(obj); + objectUpdated(obj); } + + /** + * Called whenever any objects subscribed to via registerObjects + */ + @Override + protected void objectUpdated(UAVObject obj) { + if (DEBUG) + Log.d(TAG, "Updated"); + + double pitch = obj.getField("Pitch").getDouble(); + double roll = obj.getField("Roll").getDouble(); + + AttitudeView attitude = (AttitudeView) getActivity().findViewById( + R.id.attitude_view); + attitude.setRoll(roll); + attitude.setPitch(pitch); + attitude.invalidate(); + + } + } diff --git a/androidgcs/src/org/openpilot/androidgcs/PfdActivity.java b/androidgcs/src/org/openpilot/androidgcs/PfdActivity.java new file mode 100644 index 000000000..6374c7b82 --- /dev/null +++ b/androidgcs/src/org/openpilot/androidgcs/PfdActivity.java @@ -0,0 +1,58 @@ +/** + ****************************************************************************** + * @file PfdActivity.java + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012. + * @brief Shows the PFD activity. + * @see The GNU Public License (GPL) Version 3 + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package org.openpilot.androidgcs; + +import org.openpilot.androidgcs.fragments.PFD; + +import android.app.FragmentTransaction; +import android.os.Bundle; +import android.view.View; +import android.widget.AbsListView; +import android.widget.LinearLayout; + +public class PfdActivity extends ObjectManagerActivity { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(createUI()); + } + + private View createUI() { + LinearLayout layout = new LinearLayout(this); + + layout.setOrientation(LinearLayout.HORIZONTAL); + layout.setLayoutParams(new LinearLayout.LayoutParams( + AbsListView.LayoutParams.MATCH_PARENT, + AbsListView.LayoutParams.MATCH_PARENT)); + layout.setId(0x101); + { + FragmentTransaction fragmentTransaction = getFragmentManager() + .beginTransaction(); + fragmentTransaction.add(0x101, new PFD()); + fragmentTransaction.commit(); + } + return layout; + } +}