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;
import static net.sf.openrocket.android.events.Events.*;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
@ -11,7 +13,10 @@ 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 {
@ -20,15 +25,9 @@ public class CurrentRocket {
private OpenRocketDocument rocketDocument;
private WarningSet warnings;
private RocketChangedEventHandler handler;
private boolean isModified = false;
private Set<Integer> runningSims = new HashSet<Integer>();
public void setHandler( RocketChangedEventHandler handler ) {
this.handler = handler;
}
/**
* @return the rocketDocument
*/
@ -36,60 +35,66 @@ public class CurrentRocket {
return rocketDocument;
}
public void notifySimsChanged() {
synchronized ( this ) {
isModified = true;
}
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);
}
public void notifySimComplete() {
synchronized ( this ) {
isModified = true;
}
if ( handler != null ) {
handler.simCompleteMessage();
}
private void notifySimComplete( Context context ) {
Intent msg = new Intent(MESSAGE_ACTION);
msg.putExtra(TYPE, SIM_COMPLETE);
LocalBroadcastManager.getInstance(context).sendBroadcast(msg);
}
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);
// 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);
notifySimComplete(context);
}
public synchronized Set<Integer> lockedSimulations() {
return new HashSet<Integer>(runningSims);
}
public void addNewSimulation() {
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);
newSim.setName(rocketDocument.getNextSimulationName());
rocketDocument.addSimulation(newSim);
notifySimsChanged();
notifySimsChanged(context);
}
public void deleteSimulation( int simulationPos ) {
rocketDocument.removeSimulation( simulationPos );
notifySimsChanged();
}
public String addNewMotorConfig() {
synchronized ( this ) {
public synchronized void deleteSimulation( Context context, int simulationPos ) {
isModified = true;
rocketDocument.removeSimulation( simulationPos );
notifySimsChanged(context);
}
public synchronized String addNewMotorConfig( Context context ) {
isModified = true;
String configId = rocketDocument.getRocket().newMotorConfigurationID();
if ( handler != null ) {
handler.configsChangedMessage();
}
notifyMotorConfigChanged(context);
return configId;
}
/**
* @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() {
CurrentRocketHolder.getCurrentRocket().addNewMotorConfig();
CurrentRocketHolder.getCurrentRocket().addNewMotorConfig(getActivity());
}
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.android.ActivityHelpers;
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.SimulationViewActivity;
import net.sf.openrocket.android.simulation.SimulationViewFragment;
@ -48,6 +49,9 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
private String autoSaveEnabledKey;
private boolean autoSaveEnabled = false;
private RocketChangedEventHandler handler = new RocketChangedEventHandler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -80,7 +84,7 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
@Override
protected void onPause() {
CurrentRocketHolder.getCurrentRocket().setHandler(null);
handler.unregister(this);
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
@ -95,8 +99,7 @@ implements Simulations.OnSimulationSelectedListener, OpenRocketSaverFragment.OnO
pref.registerOnSharedPreferenceChangeListener(this);
RocketChangedEventHandler handler = new RocketChangedEventHandler();
CurrentRocketHolder.getCurrentRocket().setHandler(handler);
handler.register(this);
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
protected void doSimComplete() {

View File

@ -23,6 +23,8 @@ public class SimulationService extends IntentService {
public static void executeSimulationTask( Context c, SimulationTask t ) {
AndroidLogWrapper.d(SimulationService.class, "Submitting simulation " + t.simulationId );
CurrentRocketHolder.getCurrentRocket().lockSimulation( c, t.simulationId );
Intent intent = new Intent( c, SimulationService.class );
intent.putExtra("net.sf.openrocket.simulationtask", t);
c.startService(intent);
@ -39,8 +41,7 @@ public class SimulationService extends IntentService {
Simulation sim = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getSimulation(t.simulationId);
AndroidLogWrapper.d(SimulationService.class, "simulating " + t.simulationId );
sim.simulate();
CurrentRocketHolder.getCurrentRocket().unlockSimulation(t.simulationId);
CurrentRocketHolder.getCurrentRocket().notifySimsChanged();
CurrentRocketHolder.getCurrentRocket().unlockSimulation(this, t.simulationId);
}
catch (SimulationException simex) {
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( ) {
CurrentRocketHolder.getCurrentRocket().deleteSimulation(simulationId);
CurrentRocketHolder.getCurrentRocket().deleteSimulation(getActivity(), simulationId);
getDialog().dismiss();
}

View File

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