X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=android%2Fsrc%2Fnet%2Fsf%2Fopenrocket%2Fandroid%2FCurrentRocket.java;h=36446063955e82b39a326779b03bf061e96acaa7;hb=77f2457dc781c8c517ddef157c18491ad770f6c6;hp=187d46e2f40d52d2b21ed9577befa40e43d92b71;hpb=907e5f821880bb0ba39f4cddeb3dfa05f3dfd83b;p=debian%2Fopenrocket diff --git a/android/src/net/sf/openrocket/android/CurrentRocket.java b/android/src/net/sf/openrocket/android/CurrentRocket.java index 187d46e2..36446063 100644 --- a/android/src/net/sf/openrocket/android/CurrentRocket.java +++ b/android/src/net/sf/openrocket/android/CurrentRocket.java @@ -1,14 +1,22 @@ package net.sf.openrocket.android; +import static net.sf.openrocket.android.events.Events.*; + import java.io.File; import java.io.IOException; +import java.util.HashSet; +import java.util.Set; import net.sf.openrocket.aerodynamics.WarningSet; import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.document.Simulation; +import net.sf.openrocket.document.StorageOptions; import net.sf.openrocket.file.openrocket.OpenRocketSaver; import net.sf.openrocket.rocketcomponent.Rocket; +import android.content.Context; +import android.content.Intent; import android.net.Uri; +import android.support.v4.content.LocalBroadcastManager; public class CurrentRocket { @@ -17,12 +25,9 @@ public class CurrentRocket { private OpenRocketDocument rocketDocument; private WarningSet warnings; - private RocketChangedEventHandler handler; - - public void setHandler( RocketChangedEventHandler handler ) { - this.handler = handler; - } - + private boolean isModified = false; + private Set runningSims = new HashSet(); + /** * @return the rocketDocument */ @@ -30,38 +35,78 @@ public class CurrentRocket { return rocketDocument; } - public void notifySimsChanged() { - if ( handler != null ) { - handler.simsChangedMessage(); - } + private void notifySimsChanged( Context context ) { + Intent msg = new Intent(MESSAGE_ACTION); + msg.putExtra(TYPE, SIMS_CHANGED); + + LocalBroadcastManager.getInstance(context).sendBroadcast(msg); + } + + private void notifySimComplete( Context context ) { + Intent msg = new Intent(MESSAGE_ACTION); + msg.putExtra(TYPE, SIM_COMPLETE); + + LocalBroadcastManager.getInstance(context).sendBroadcast(msg); } - public void addNewSimulation() { + private void notifyMotorConfigChanged( Context context ) { + Intent msg = new Intent(MESSAGE_ACTION); + msg.putExtra(TYPE, CONFIGS_CHANGED); + + LocalBroadcastManager.getInstance(context).sendBroadcast(msg); + } + + public synchronized void lockSimulation( Context context, int simulationId ) { + runningSims.add(simulationId); + // TODO - someday we might want to know about this: + // notifySimsChanged( context ); + } + + public synchronized void unlockSimulation( Context context, int simulationId ) { + this.isModified = true; + runningSims.remove(simulationId); + notifySimComplete(context); + } + + public synchronized Set lockedSimulations() { + return new HashSet(runningSims); + } + + public synchronized void addNewSimulation( Context context ) { + isModified = true; Rocket rocket = rocketDocument.getRocket(); - // FIXME - hopefully the change to the Simulation object will be reverted soon. - Simulation newSim = new Simulation(rocketDocument, rocket); + Simulation newSim = new Simulation(rocket); newSim.setName(rocketDocument.getNextSimulationName()); rocketDocument.addSimulation(newSim); - notifySimsChanged(); + notifySimsChanged(context); } - - public void deleteSimulation( int simulationPos ) { + + public synchronized void deleteSimulation( Context context, int simulationPos ) { + isModified = true; rocketDocument.removeSimulation( simulationPos ); - notifySimsChanged(); + notifySimsChanged(context); } - - public String addNewMotorConfig() { + + public synchronized String addNewMotorConfig( Context context ) { + isModified = true; String configId = rocketDocument.getRocket().newMotorConfigurationID(); - if ( handler != null ) { - handler.configsChangedMessage(); - } + notifyMotorConfigChanged(context); return configId; } + + public synchronized void deleteMotorConfig( Context context, String config ) { + rocketDocument.getRocket().removeMotorConfigurationID(config); + notifyMotorConfigChanged(context); + } + /** * @param rocketDocument the rocketDocument to set */ public void setRocketDocument(OpenRocketDocument rocketDocument) { this.rocketDocument = rocketDocument; + synchronized ( this ) { + isModified = false; + } } public WarningSet getWarnings() { @@ -80,11 +125,30 @@ public class CurrentRocket { this.fileUri = fileUri; } - public void saveOpenRocketDocument() throws IOException { - OpenRocketSaver saver = new OpenRocketSaver(); - saver.save(new File(fileUri.getPath()),rocketDocument); + public boolean isModified() { + return this.isModified; + } + public boolean canSave() { + return this.isModified && this.runningSims.isEmpty(); } + public void saveOpenRocketDocument() throws IOException { + + // Translate the fileUri if it happens to be a .rkt file. + + String filename = fileUri.getPath(); + + if ( ! filename.endsWith(".ork") ) { + filename = filename.concat(".ork"); + } + + OpenRocketSaver saver = new OpenRocketSaver(); + StorageOptions options = new StorageOptions(); + options.setCompressionEnabled(true); + options.setSimulationTimeSkip(StorageOptions.SIMULATION_DATA_ALL); + saver.save(new File(filename),rocketDocument,options); + isModified = false; + } }