From 10dd89f5694d85071843de2e1df017abdc6e66dd Mon Sep 17 00:00:00 2001 From: Kevin Ruland Date: Sun, 8 Jan 2012 00:53:35 +0000 Subject: [PATCH] Change OpenRocketViewer to tab layout with three tabs: overview, components, and simulations. Overview is populated with some high level data about the rocket - length, mass, cg, etc. Components will be populated with the tree of rocket components. Simulations contains the list of stored simulations. Added length and mass preferences and wired into the application object initialization of the OpenRocket UnitGroup statics. There is currently an issue in the Simulation list in that the motors are not being displayed. This is because currently the motor dao is not wired into the application. --- android/res/layout/openrocketviewer.xml | 123 ++++++++++++++++-- android/res/values/strings.xml | 32 ++++- android/res/xml/preferences.xml | 16 +++ .../sf/openrocket/android/Application.java | 11 ++ .../android/PreferencesActivity.java | 36 ++++- .../android/rocket/OpenRocketViewer.java | 100 ++++++++++++-- 6 files changed, 294 insertions(+), 24 deletions(-) diff --git a/android/res/layout/openrocketviewer.xml b/android/res/layout/openrocketviewer.xml index 12634b90b..6b6a8a2cd 100644 --- a/android/res/layout/openrocketviewer.xml +++ b/android/res/layout/openrocketviewer.xml @@ -1,18 +1,119 @@ - - - + android:layout_height="match_parent" + android:background="@android:color/black" > + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/android/res/values/strings.xml b/android/res/values/strings.xml index 5ff104495..d79944fdb 100644 --- a/android/res/values/strings.xml +++ b/android/res/values/strings.xml @@ -95,6 +95,34 @@ Submit Download from ThrustCurve Series 1 - Series 2 - + Series 2 + PreferenceUnitLengthOption + + Millimeters + Centimeters + Meters + Inches + Feet + + + mm + cm + m + in + ft + + PreferenceUnitMassOption + + Grams + Kilograms + Ounces + Pounds + + + g + kg + oz + lb + + \ No newline at end of file diff --git a/android/res/xml/preferences.xml b/android/res/xml/preferences.xml index 05fc59c83..1a37a8df0 100644 --- a/android/res/xml/preferences.xml +++ b/android/res/xml/preferences.xml @@ -14,4 +14,20 @@ android:summary="Set the grouping in Motor Browser" android:title="Motor Browser Group" /> + + \ No newline at end of file diff --git a/src/net/sf/openrocket/android/Application.java b/src/net/sf/openrocket/android/Application.java index 61b368c74..6628e5fbc 100644 --- a/src/net/sf/openrocket/android/Application.java +++ b/src/net/sf/openrocket/android/Application.java @@ -2,6 +2,8 @@ package net.sf.openrocket.android; import java.util.Locale; +import android.preference.PreferenceManager; + import net.sf.openrocket.database.ThrustCurveMotorSetDatabase; import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.l10n.DebugTranslator; @@ -54,6 +56,15 @@ public class Application extends android.app.Application { initialize(); } + /* (non-Javadoc) + * @see android.app.Application#onCreate() + */ + @Override + public void onCreate() { + super.onCreate(); + PreferencesActivity.initializePreferences(this, PreferenceManager.getDefaultSharedPreferences(this)); + } + /** * @return the rocketDocument */ diff --git a/src/net/sf/openrocket/android/PreferencesActivity.java b/src/net/sf/openrocket/android/PreferencesActivity.java index 4ce05878f..66f4d7788 100644 --- a/src/net/sf/openrocket/android/PreferencesActivity.java +++ b/src/net/sf/openrocket/android/PreferencesActivity.java @@ -1,14 +1,48 @@ package net.sf.openrocket.android; import net.sf.openrocket.R; +import net.sf.openrocket.unit.UnitGroup; +import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; -public class PreferencesActivity extends android.preference.PreferenceActivity { +public class PreferencesActivity extends android.preference.PreferenceActivity +implements SharedPreferences.OnSharedPreferenceChangeListener { @Override protected void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); addPreferencesFromResource(R.xml.preferences); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + prefs.registerOnSharedPreferenceChangeListener(this); + } + + /* (non-Javadoc) + * @see android.content.SharedPreferences.OnSharedPreferenceChangeListener#onSharedPreferenceChanged(android.content.SharedPreferences, java.lang.String) + */ + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + + initializePreferences(getApplication(), PreferenceManager.getDefaultSharedPreferences(this)); + } + + /** + * This method is to be called from Application setup to pull the saved preference + * values into the various datastructures used in OpenRocket. + * This method is located in this class because it is probably best to have as much + * of the code in the same place as possible. + * @param sharedPreferences + */ + public static void initializePreferences( android.app.Application app, SharedPreferences sharedPreferences ) { + + String unitLength = app.getResources().getString(R.string.PreferenceUnitLengthOption); + String len = sharedPreferences.getString(unitLength, "cm"); + UnitGroup.UNITS_LENGTH.setDefaultUnit( len ); + + String unitMass = app.getResources().getString(R.string.PreferenceUnitMassOption); + String mass = sharedPreferences.getString(unitMass, "g"); + UnitGroup.UNITS_MASS.setDefaultUnit( mass ); + } } diff --git a/src/net/sf/openrocket/android/rocket/OpenRocketViewer.java b/src/net/sf/openrocket/android/rocket/OpenRocketViewer.java index aa0083455..1037d1b89 100644 --- a/src/net/sf/openrocket/android/rocket/OpenRocketViewer.java +++ b/src/net/sf/openrocket/android/rocket/OpenRocketViewer.java @@ -10,11 +10,19 @@ import net.sf.openrocket.android.motor.MotorHierarchicalBrowser; import net.sf.openrocket.android.simulation.SimulationViewer; import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.document.Simulation; +import net.sf.openrocket.rocketcomponent.Rocket; +import net.sf.openrocket.rocketcomponent.RocketComponent; +import net.sf.openrocket.rocketcomponent.RocketUtils; +import net.sf.openrocket.unit.Unit; +import net.sf.openrocket.unit.UnitGroup; +import net.sf.openrocket.util.Coordinate; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; +import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; +import android.preference.PreferenceManager; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; @@ -26,15 +34,18 @@ import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; +import android.widget.TabHost; import android.widget.TextView; -public class OpenRocketViewer extends Activity { +public class OpenRocketViewer extends Activity +implements SharedPreferences.OnSharedPreferenceChangeListener +{ private static final String TAG = "OpenRocketViewer"; private ProgressDialog progress; - private TextView header; + private ListView componentList; private ListView simulationList; private Application app; @@ -54,8 +65,31 @@ public class OpenRocketViewer extends Activity { setContentView(R.layout.openrocketviewer); - header = (TextView) findViewById(R.id.heading); - simulationList = (ListView) findViewById(R.id.rocketSimulations); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + prefs.registerOnSharedPreferenceChangeListener(this); + + TabHost tabs=(TabHost)findViewById(R.id.openrocketviewerTabHost); + + tabs.setup(); + + TabHost.TabSpec spec=tabs.newTabSpec("tag1"); + + spec.setContent(R.id.openrocketviewerOverview); + spec.setIndicator("Overview"); + tabs.addTab(spec); + + spec=tabs.newTabSpec("tag2"); + spec.setContent(R.id.openrocketviewerComponentList); + spec.setIndicator("Components"); + tabs.addTab(spec); + + spec=tabs.newTabSpec("tag3"); + spec.setContent(R.id.openrocketviewerSimulationList); + spec.setIndicator("Simulations"); + tabs.addTab(spec); + + componentList = (ListView) findViewById(R.id.openrocketviewerComponentList); + simulationList = (ListView) findViewById(R.id.openrocketviewerSimulationList); Intent i = getIntent(); Uri file = i.getData(); @@ -118,23 +152,48 @@ public class OpenRocketViewer extends Activity { } + /* (non-Javadoc) + * @see android.content.SharedPreferences.OnSharedPreferenceChangeListener#onSharedPreferenceChanged(android.content.SharedPreferences, java.lang.String) + */ + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + // just in case the user changed the units, we redraw. + PreferencesActivity.initializePreferences(getApplication(), PreferenceManager.getDefaultSharedPreferences(this)); + updateContents(); + } + private void updateContents() { - OpenRocketDocument rocket = app.getRocketDocument(); - header.setText( rocket.getRocket().getName()); + OpenRocketDocument rocketDocument = app.getRocketDocument(); + Rocket rocket = rocketDocument.getRocket(); + + setTitle(rocket.getName()); + + Unit LengthUnit = UnitGroup.UNITS_LENGTH.getDefaultUnit(); + Unit MassUnit = UnitGroup.UNITS_MASS.getDefaultUnit(); + + Coordinate cg = RocketUtils.getCG(rocket); + double length = RocketUtils.getLength(rocket); + ((TextView) findViewById(R.id.openrocketviewerRocketName)).setText( rocket.getName()); + ((TextView)findViewById(R.id.openrocketviewerDesigner)).setText(rocket.getDesigner()); + ((TextView)findViewById(R.id.openrocketviewerCG)).setText(LengthUnit.toStringUnit(cg.x) ); + ((TextView)findViewById(R.id.openrocketviewerLength)).setText(LengthUnit.toStringUnit(length)); + ((TextView)findViewById(R.id.openrocketviewerMass)).setText(MassUnit.toStringUnit(cg.weight)); + ((TextView)findViewById(R.id.openrocketviewerStageCount)).setText(String.valueOf(rocket.getStageCount())); + ((TextView)findViewById(R.id.openrocketviewerComment)).setText(rocket.getComment()); - ArrayAdapter sims = new ArrayAdapter(this,android.R.layout.simple_list_item_1,rocket.getSimulations()) { + ArrayAdapter sims = new ArrayAdapter(this,android.R.layout.simple_list_item_2,rocketDocument.getSimulations()) { @Override - public View getView(int position, View convertView, - ViewGroup parent) { + public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if ( v == null ) { LayoutInflater li = getLayoutInflater(); - v = li.inflate(android.R.layout.simple_list_item_1,null); + v = li.inflate(android.R.layout.simple_list_item_2,null); } Simulation sim = this.getItem(position); ((TextView)v.findViewById(android.R.id.text1)).setText( sim.getName() ); + ((TextView)v.findViewById(android.R.id.text2)).setText( "motors: " + sim.getConfiguration().getMotorConfigurationDescription() + " apogee: " + sim.getSimulatedData().getMaxAltitude() + "m time: " + sim.getSimulatedData().getFlightTime() + "s"); return v; } @@ -151,6 +210,27 @@ public class OpenRocketViewer extends Activity { }); simulationList.setAdapter(sims); + ArrayAdapter comps = new ArrayAdapter(this, android.R.layout.simple_list_item_1,rocket.getChildren()) { + + /* (non-Javadoc) + * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup) + */ + @Override + public View getView(int position, View convertView, ViewGroup parent) { + View v = convertView; + if ( v == null ) { + LayoutInflater li = getLayoutInflater(); + v = li.inflate(android.R.layout.simple_list_item_1,null); + } + RocketComponent comp = this.getItem(position); + ((TextView)v.findViewById(android.R.id.text1)).setText( comp.getName() ); + return v; + } + + + }; + componentList.setAdapter(comps); + if ( progress.isShowing() ) { progress.dismiss(); }