1 package net.sf.openrocket.android;
\r
3 import static net.sf.openrocket.android.events.Events.*;
\r
6 import java.io.IOException;
\r
7 import java.util.HashSet;
\r
8 import java.util.Set;
\r
10 import net.sf.openrocket.aerodynamics.WarningSet;
\r
11 import net.sf.openrocket.document.OpenRocketDocument;
\r
12 import net.sf.openrocket.document.Simulation;
\r
13 import net.sf.openrocket.document.StorageOptions;
\r
14 import net.sf.openrocket.file.openrocket.OpenRocketSaver;
\r
15 import net.sf.openrocket.rocketcomponent.Rocket;
\r
16 import android.content.Context;
\r
17 import android.content.Intent;
\r
18 import android.net.Uri;
\r
19 import android.support.v4.content.LocalBroadcastManager;
\r
21 public class CurrentRocket {
\r
23 private Uri fileUri;
\r
25 private OpenRocketDocument rocketDocument;
\r
26 private WarningSet warnings;
\r
28 private boolean isModified = false;
\r
29 private Set<Integer> runningSims = new HashSet<Integer>();
\r
32 * @return the rocketDocument
\r
34 public OpenRocketDocument getRocketDocument() {
\r
35 return rocketDocument;
\r
38 private void notifySimsChanged( Context context ) {
\r
39 Intent msg = new Intent(MESSAGE_ACTION);
\r
40 msg.putExtra(TYPE, SIMS_CHANGED);
\r
42 LocalBroadcastManager.getInstance(context).sendBroadcast(msg);
\r
45 private void notifySimComplete( Context context ) {
\r
46 Intent msg = new Intent(MESSAGE_ACTION);
\r
47 msg.putExtra(TYPE, SIM_COMPLETE);
\r
49 LocalBroadcastManager.getInstance(context).sendBroadcast(msg);
\r
52 private void notifyMotorConfigChanged( Context context ) {
\r
53 Intent msg = new Intent(MESSAGE_ACTION);
\r
54 msg.putExtra(TYPE, CONFIGS_CHANGED);
\r
56 LocalBroadcastManager.getInstance(context).sendBroadcast(msg);
\r
59 public synchronized void lockSimulation( Context context, int simulationId ) {
\r
60 runningSims.add(simulationId);
\r
61 // TODO - someday we might want to know about this:
\r
62 // notifySimsChanged( context );
\r
65 public synchronized void unlockSimulation( Context context, int simulationId ) {
\r
66 this.isModified = true;
\r
67 runningSims.remove(simulationId);
\r
68 notifySimComplete(context);
\r
71 public synchronized Set<Integer> lockedSimulations() {
\r
72 return new HashSet<Integer>(runningSims);
\r
75 public synchronized void addNewSimulation( Context context ) {
\r
77 Rocket rocket = rocketDocument.getRocket();
\r
78 Simulation newSim = new Simulation(rocket);
\r
79 newSim.setName(rocketDocument.getNextSimulationName());
\r
80 rocketDocument.addSimulation(newSim);
\r
81 notifySimsChanged(context);
\r
84 public synchronized void deleteSimulation( Context context, int simulationPos ) {
\r
86 rocketDocument.removeSimulation( simulationPos );
\r
87 notifySimsChanged(context);
\r
90 public synchronized String addNewMotorConfig( Context context ) {
\r
92 String configId = rocketDocument.getRocket().newMotorConfigurationID();
\r
93 notifyMotorConfigChanged(context);
\r
97 public synchronized void deleteMotorConfig( Context context, String config ) {
\r
98 rocketDocument.getRocket().removeMotorConfigurationID(config);
\r
99 notifyMotorConfigChanged(context);
\r
103 * @param rocketDocument the rocketDocument to set
\r
105 public void setRocketDocument(OpenRocketDocument rocketDocument) {
\r
106 this.rocketDocument = rocketDocument;
\r
107 synchronized ( this ) {
\r
108 isModified = false;
\r
112 public WarningSet getWarnings() {
\r
116 public void setWarnings(WarningSet warnings) {
\r
117 this.warnings = warnings;
\r
120 public Uri getFileUri() {
\r
124 public void setFileUri(Uri fileUri) {
\r
125 this.fileUri = fileUri;
\r
128 public boolean isModified() {
\r
129 return this.isModified;
\r
132 public boolean canSave() {
\r
133 return this.isModified && this.runningSims.isEmpty();
\r
136 public void saveOpenRocketDocument() throws IOException {
\r
138 // Translate the fileUri if it happens to be a .rkt file.
\r
140 String filename = fileUri.getPath();
\r
142 if ( ! filename.endsWith(".ork") ) {
\r
143 filename = filename.concat(".ork");
\r
146 OpenRocketSaver saver = new OpenRocketSaver();
\r
147 StorageOptions options = new StorageOptions();
\r
148 options.setCompressionEnabled(true);
\r
149 options.setSimulationTimeSkip(StorageOptions.SIMULATION_DATA_ALL);
\r
150 saver.save(new File(filename),rocketDocument,options);
\r
151 isModified = false;
\r