Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / android / src / net / sf / openrocket / android / CurrentRocket.java
diff --git a/android/src/net/sf/openrocket/android/CurrentRocket.java b/android/src/net/sf/openrocket/android/CurrentRocket.java
new file mode 100644 (file)
index 0000000..3644606
--- /dev/null
@@ -0,0 +1,154 @@
+package net.sf.openrocket.android;\r
+\r
+import static net.sf.openrocket.android.events.Events.*;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+import net.sf.openrocket.aerodynamics.WarningSet;\r
+import net.sf.openrocket.document.OpenRocketDocument;\r
+import net.sf.openrocket.document.Simulation;\r
+import net.sf.openrocket.document.StorageOptions;\r
+import net.sf.openrocket.file.openrocket.OpenRocketSaver;\r
+import net.sf.openrocket.rocketcomponent.Rocket;\r
+import android.content.Context;\r
+import android.content.Intent;\r
+import android.net.Uri;\r
+import android.support.v4.content.LocalBroadcastManager;\r
+\r
+public class CurrentRocket {\r
+\r
+       private Uri fileUri;\r
+\r
+       private OpenRocketDocument rocketDocument;\r
+       private WarningSet warnings;\r
+\r
+       private boolean isModified = false;\r
+       private Set<Integer> runningSims = new HashSet<Integer>();\r
+\r
+       /**\r
+        * @return the rocketDocument\r
+        */\r
+       public OpenRocketDocument getRocketDocument() {\r
+               return rocketDocument;\r
+       }\r
+\r
+       private void notifySimsChanged( Context context ) {\r
+               Intent msg = new Intent(MESSAGE_ACTION);\r
+               msg.putExtra(TYPE, SIMS_CHANGED);\r
+\r
+               LocalBroadcastManager.getInstance(context).sendBroadcast(msg);\r
+       }\r
+\r
+       private void notifySimComplete( Context context ) {\r
+               Intent msg = new Intent(MESSAGE_ACTION);\r
+               msg.putExtra(TYPE, SIM_COMPLETE);\r
+\r
+               LocalBroadcastManager.getInstance(context).sendBroadcast(msg);\r
+       }\r
+\r
+       private void notifyMotorConfigChanged( Context context ) {\r
+               Intent msg = new Intent(MESSAGE_ACTION);\r
+               msg.putExtra(TYPE, CONFIGS_CHANGED);\r
+\r
+               LocalBroadcastManager.getInstance(context).sendBroadcast(msg);\r
+       }\r
+\r
+       public synchronized void lockSimulation( Context context, int simulationId ) {\r
+               runningSims.add(simulationId);\r
+               // TODO - someday we might want to know about this:\r
+               // notifySimsChanged( context );\r
+       }\r
+\r
+       public synchronized void unlockSimulation( Context context, int simulationId ) {\r
+               this.isModified = true;\r
+               runningSims.remove(simulationId);\r
+               notifySimComplete(context);\r
+       }\r
+\r
+       public synchronized Set<Integer> lockedSimulations() {\r
+               return new HashSet<Integer>(runningSims);\r
+       }\r
+\r
+       public synchronized void addNewSimulation( Context context ) {\r
+               isModified = true;\r
+               Rocket rocket = rocketDocument.getRocket();\r
+               Simulation newSim = new Simulation(rocket);\r
+               newSim.setName(rocketDocument.getNextSimulationName());\r
+               rocketDocument.addSimulation(newSim);\r
+               notifySimsChanged(context);\r
+       }\r
+\r
+       public synchronized void deleteSimulation( Context context, int simulationPos ) {\r
+               isModified = true;\r
+               rocketDocument.removeSimulation( simulationPos );\r
+               notifySimsChanged(context);\r
+       }\r
+\r
+       public synchronized String addNewMotorConfig( Context context ) {\r
+               isModified = true;\r
+               String configId = rocketDocument.getRocket().newMotorConfigurationID();\r
+               notifyMotorConfigChanged(context);\r
+               return configId;\r
+       }\r
+       \r
+       public synchronized void deleteMotorConfig( Context context, String config ) {\r
+               rocketDocument.getRocket().removeMotorConfigurationID(config);\r
+               notifyMotorConfigChanged(context);\r
+       }\r
+       \r
+       /**\r
+        * @param rocketDocument the rocketDocument to set\r
+        */\r
+       public void setRocketDocument(OpenRocketDocument rocketDocument) {\r
+               this.rocketDocument = rocketDocument;\r
+               synchronized ( this ) {\r
+                       isModified = false;\r
+               }\r
+       }\r
+\r
+       public WarningSet getWarnings() {\r
+               return warnings;\r
+       }\r
+\r
+       public void setWarnings(WarningSet warnings) {\r
+               this.warnings = warnings;\r
+       }\r
+\r
+       public Uri getFileUri() {\r
+               return fileUri;\r
+       }\r
+\r
+       public void setFileUri(Uri fileUri) {\r
+               this.fileUri = fileUri;\r
+       }\r
+\r
+       public boolean isModified() {\r
+               return this.isModified;\r
+       }\r
+\r
+       public boolean canSave() {\r
+               return this.isModified && this.runningSims.isEmpty();\r
+       }\r
+\r
+       public void saveOpenRocketDocument() throws IOException {\r
+\r
+               // Translate the fileUri if it happens to be a .rkt file.\r
+\r
+               String filename = fileUri.getPath();\r
+\r
+               if ( ! filename.endsWith(".ork") ) {\r
+                       filename = filename.concat(".ork");\r
+               }\r
+\r
+               OpenRocketSaver saver = new OpenRocketSaver();\r
+               StorageOptions options = new StorageOptions();\r
+               options.setCompressionEnabled(true);\r
+               options.setSimulationTimeSkip(StorageOptions.SIMULATION_DATA_ALL);\r
+               saver.save(new File(filename),rocketDocument,options);\r
+               isModified = false;\r
+       }\r
+\r
+}\r