Revised the notification mechanism for background processes (simulations) to use LocalBroadcastManager instead of the Handler tricks.

This commit is contained in:
Kevin Ruland 2012-07-10 03:40:44 +00:00
parent 568b7aeb5d
commit bde3ea3701
9 changed files with 119 additions and 120 deletions

View File

@ -1,5 +1,7 @@
package net.sf.openrocket.android; package net.sf.openrocket.android;
import static net.sf.openrocket.android.events.Events.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
@ -11,7 +13,10 @@ import net.sf.openrocket.document.Simulation;
import net.sf.openrocket.document.StorageOptions; import net.sf.openrocket.document.StorageOptions;
import net.sf.openrocket.file.openrocket.OpenRocketSaver; import net.sf.openrocket.file.openrocket.OpenRocketSaver;
import net.sf.openrocket.rocketcomponent.Rocket; import net.sf.openrocket.rocketcomponent.Rocket;
import android.content.Context;
import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.support.v4.content.LocalBroadcastManager;
public class CurrentRocket { public class CurrentRocket {
@ -20,15 +25,9 @@ public class CurrentRocket {
private OpenRocketDocument rocketDocument; private OpenRocketDocument rocketDocument;
private WarningSet warnings; private WarningSet warnings;
private RocketChangedEventHandler handler;
private boolean isModified = false; private boolean isModified = false;
private Set<Integer> runningSims = new HashSet<Integer>(); private Set<Integer> runningSims = new HashSet<Integer>();
public void setHandler( RocketChangedEventHandler handler ) {
this.handler = handler;
}
/** /**
* @return the rocketDocument * @return the rocketDocument
*/ */
@ -36,60 +35,66 @@ public class CurrentRocket {
return rocketDocument; return rocketDocument;
} }
public void notifySimsChanged() { private void notifySimsChanged( Context context ) {
synchronized ( this ) { Intent msg = new Intent(MESSAGE_ACTION);
isModified = true; msg.putExtra(TYPE, SIMS_CHANGED);
}
if ( handler != null ) { LocalBroadcastManager.getInstance(context).sendBroadcast(msg);
handler.simsChangedMessage();
}
} }
public void notifySimComplete() { private void notifySimComplete( Context context ) {
synchronized ( this ) { Intent msg = new Intent(MESSAGE_ACTION);
isModified = true; msg.putExtra(TYPE, SIM_COMPLETE);
}
if ( handler != null ) { LocalBroadcastManager.getInstance(context).sendBroadcast(msg);
handler.simCompleteMessage();
}
} }
public synchronized void lockSimulation( int simulationId ) { 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); runningSims.add(simulationId);
// TODO - someday we might want to know about this:
// notifySimsChanged( context );
} }
public synchronized void unlockSimulation( int simulationId ) { public synchronized void unlockSimulation( Context context, int simulationId ) {
this.isModified = true;
runningSims.remove(simulationId); runningSims.remove(simulationId);
notifySimComplete(context);
} }
public synchronized Set<Integer> lockedSimulations() { public synchronized Set<Integer> lockedSimulations() {
return new HashSet<Integer>(runningSims); return new HashSet<Integer>(runningSims);
} }
public void addNewSimulation() { public synchronized void addNewSimulation( Context context ) {
isModified = true;
Rocket rocket = rocketDocument.getRocket(); Rocket rocket = rocketDocument.getRocket();
// FIXME - hopefully the change to the Simulation object will be reverted soon. // FIXME - hopefully the change to the Simulation object will be reverted soon.
Simulation newSim = new Simulation(rocketDocument, rocket); Simulation newSim = new Simulation(rocketDocument, rocket);
newSim.setName(rocketDocument.getNextSimulationName()); newSim.setName(rocketDocument.getNextSimulationName());
rocketDocument.addSimulation(newSim); rocketDocument.addSimulation(newSim);
notifySimsChanged(); notifySimsChanged(context);
} }
public void deleteSimulation( int simulationPos ) { public synchronized void deleteSimulation( Context context, int simulationPos ) {
rocketDocument.removeSimulation( simulationPos );
notifySimsChanged();
}
public String addNewMotorConfig() {
synchronized ( this ) {
isModified = true; isModified = true;
rocketDocument.removeSimulation( simulationPos );
notifySimsChanged(context);
} }
public synchronized String addNewMotorConfig( Context context ) {
isModified = true;
String configId = rocketDocument.getRocket().newMotorConfigurationID(); String configId = rocketDocument.getRocket().newMotorConfigurationID();
if ( handler != null ) { notifyMotorConfigChanged(context);
handler.configsChangedMessage();
}
return configId; return configId;
} }
/** /**
* @param rocketDocument the rocketDocument to set * @param rocketDocument the rocketDocument to set
*/ */

View File

@ -1,67 +0,0 @@
package net.sf.openrocket.android;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
public abstract class RocketChangedEventHandler extends Handler {
public RocketChangedEventHandler() {
}
public RocketChangedEventHandler(Callback callback) {
super(callback);
}
public RocketChangedEventHandler(Looper looper) {
super(looper);
}
public RocketChangedEventHandler(Looper looper, Callback callback) {
super(looper, callback);
}
public static final int MOTOR_CONFIGS_CHANGED = 1;
public static final int SIMS_CHANGED = 2;
public static final int SIM_COMPLETE = 3;
public void simCompleteMessage() {
Message m = this.obtainMessage(SIM_COMPLETE);
this.sendMessage(m);
}
public void simsChangedMessage() {
Message m = this.obtainMessage(SIMS_CHANGED);
this.sendMessage(m);
}
public void configsChangedMessage() {
Message m = this.obtainMessage(MOTOR_CONFIGS_CHANGED);
this.sendMessage(m);
}
@Override
public void handleMessage(Message msg) {
int what = msg.what;
switch ( what ) {
case SIMS_CHANGED:
doSimsChanged();
break;
case MOTOR_CONFIGS_CHANGED:
doMotorConfigsChanged();
break;
case SIM_COMPLETE:
doSimComplete();
break;
default:
super.handleMessage(msg);
}
}
protected abstract void doSimComplete();
protected abstract void doSimsChanged();
protected abstract void doMotorConfigsChanged();
}

View File

@ -0,0 +1,43 @@
package net.sf.openrocket.android.events;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
public abstract class ChangeEventBroadcastReceiver extends BroadcastReceiver {
public void register( Context context ) {
LocalBroadcastManager.getInstance(context).registerReceiver( this,
new IntentFilter(Events.MESSAGE_ACTION) );
}
public void unregister( Context context ) {
LocalBroadcastManager.getInstance(context).unregisterReceiver(this);
}
@Override
public void onReceive(Context context, Intent intent) {
int type = intent.getIntExtra(Events.TYPE, -1);
switch( type ) {
case Events.CONFIGS_CHANGED:
doMotorConfigsChanged();
break;
case Events.SIMS_CHANGED:
doSimsChanged();
break;
case Events.SIM_COMPLETE:
doSimComplete();
break;
}
}
protected abstract void doSimComplete();
protected abstract void doSimsChanged();
protected abstract void doMotorConfigsChanged();
}

View File

@ -0,0 +1,14 @@
package net.sf.openrocket.android.events;
public abstract class Events {
public final static int SIMS_CHANGED=0;
public final static int SIM_COMPLETE=1;
public final static int CONFIGS_CHANGED=2;
public final static String MESSAGE_ACTION = "net.sf.openrocket.Message";
public final static String TYPE = "type";
}

View File

@ -70,7 +70,7 @@ public class Configurations extends ExpandableListFragment {
} }
private void addConfiguration() { private void addConfiguration() {
CurrentRocketHolder.getCurrentRocket().addNewMotorConfig(); CurrentRocketHolder.getCurrentRocket().addNewMotorConfig(getActivity());
} }
private static class MotorMountInfo { private static class MotorMountInfo {

View File

@ -4,6 +4,7 @@ package net.sf.openrocket.android.rocket;
import net.sf.openrocket.R; import net.sf.openrocket.R;
import net.sf.openrocket.android.ActivityHelpers; import net.sf.openrocket.android.ActivityHelpers;
import net.sf.openrocket.android.CurrentRocketHolder; import net.sf.openrocket.android.CurrentRocketHolder;
import net.sf.openrocket.android.events.ChangeEventBroadcastReceiver;
import net.sf.openrocket.android.simulation.SimulationChart; import net.sf.openrocket.android.simulation.SimulationChart;
import net.sf.openrocket.android.simulation.SimulationViewActivity; import net.sf.openrocket.android.simulation.SimulationViewActivity;
import net.sf.openrocket.android.simulation.SimulationViewFragment; import net.sf.openrocket.android.simulation.SimulationViewFragment;
@ -48,6 +49,9 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
private String autoSaveEnabledKey; private String autoSaveEnabledKey;
private boolean autoSaveEnabled = false; private boolean autoSaveEnabled = false;
private RocketChangedEventHandler handler = new RocketChangedEventHandler();
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -80,7 +84,7 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
@Override @Override
protected void onPause() { protected void onPause() {
CurrentRocketHolder.getCurrentRocket().setHandler(null); handler.unregister(this);
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.unregisterOnSharedPreferenceChangeListener(this); pref.unregisterOnSharedPreferenceChangeListener(this);
super.onPause(); super.onPause();
@ -95,8 +99,7 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
pref.registerOnSharedPreferenceChangeListener(this); pref.registerOnSharedPreferenceChangeListener(this);
RocketChangedEventHandler handler = new RocketChangedEventHandler(); handler.register(this);
CurrentRocketHolder.getCurrentRocket().setHandler(handler);
super.onResume(); super.onResume();
} }
@ -220,7 +223,7 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
} }
} }
private class RocketChangedEventHandler extends net.sf.openrocket.android.RocketChangedEventHandler { private class RocketChangedEventHandler extends ChangeEventBroadcastReceiver {
@Override @Override
protected void doSimComplete() { protected void doSimComplete() {

View File

@ -23,6 +23,8 @@ public class SimulationService extends IntentService {
public static void executeSimulationTask( Context c, SimulationTask t ) { public static void executeSimulationTask( Context c, SimulationTask t ) {
AndroidLogWrapper.d(SimulationService.class, "Submitting simulation " + t.simulationId ); AndroidLogWrapper.d(SimulationService.class, "Submitting simulation " + t.simulationId );
CurrentRocketHolder.getCurrentRocket().lockSimulation( c, t.simulationId );
Intent intent = new Intent( c, SimulationService.class ); Intent intent = new Intent( c, SimulationService.class );
intent.putExtra("net.sf.openrocket.simulationtask", t); intent.putExtra("net.sf.openrocket.simulationtask", t);
c.startService(intent); c.startService(intent);
@ -39,8 +41,7 @@ public class SimulationService extends IntentService {
Simulation sim = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getSimulation(t.simulationId); Simulation sim = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getSimulation(t.simulationId);
AndroidLogWrapper.d(SimulationService.class, "simulating " + t.simulationId ); AndroidLogWrapper.d(SimulationService.class, "simulating " + t.simulationId );
sim.simulate(); sim.simulate();
CurrentRocketHolder.getCurrentRocket().unlockSimulation(t.simulationId); CurrentRocketHolder.getCurrentRocket().unlockSimulation(this, t.simulationId);
CurrentRocketHolder.getCurrentRocket().notifySimsChanged();
} }
catch (SimulationException simex) { catch (SimulationException simex) {
Toast.makeText(this, "Error in simulation:" + simex.getMessage(), Toast.LENGTH_LONG ).show(); Toast.makeText(this, "Error in simulation:" + simex.getMessage(), Toast.LENGTH_LONG ).show();

View File

@ -109,7 +109,7 @@ public class SimulationEditFragment extends SherlockDialogFragment {
} }
public void onDelete( ) { public void onDelete( ) {
CurrentRocketHolder.getCurrentRocket().deleteSimulation(simulationId); CurrentRocketHolder.getCurrentRocket().deleteSimulation(getActivity(), simulationId);
getDialog().dismiss(); getDialog().dismiss();
} }

View File

@ -169,7 +169,7 @@ implements SharedPreferences.OnSharedPreferenceChangeListener
} }
private void addSimulation() { private void addSimulation() {
CurrentRocketHolder.getCurrentRocket().addNewSimulation(); CurrentRocketHolder.getCurrentRocket().addNewSimulation(getActivity());
} }
} }