Fixed Run button, again!

Added reset button.
Improved some things.
This commit is contained in:
Ziver Koc 2014-08-06 17:01:01 +02:00
parent f5e95e683c
commit 331b244a0e
6 changed files with 31 additions and 11 deletions

View file

@ -50,12 +50,6 @@ public abstract class UeBehaviour implements Serializable{
return !running;
}
/**
* @return true is this behaviour is currently running
*/
public boolean isRunning(){
return running;
}
public void setExecutionListener(BehaviourExecutionListener l){
execListener = l;

View file

@ -64,7 +64,7 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
public synchronized void execute(){
if(thread == null || !thread.isAlive()) {
if(!isRunning()) {
terminate = false;
thread = new Thread(this);
thread.start();
@ -78,12 +78,25 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
terminate = true;
if(currentlyActive != null)
currentlyActive.terminate();
// Wait for the execution to finnish
if(isRunning()){
synchronized (thread){
try { thread.wait(); }catch(InterruptedException e) {log.error(null,e);}
}
}
}
public synchronized void reset(){
terminate();
currentlyActive = null;
}
public boolean isRunning(){
if(currentlyActive == null)
return false;
return currentlyActive.isRunning();
return thread != null && thread.isAlive();
//if(currentlyActive == null)
// return false;
//return currentlyActive.isRunning();
}
public void run(){
@ -107,7 +120,7 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
log.info("Running behaviour: " + currentlyActive.getName());
currentlyActive.run();
if(isRunning()) {
if(!terminate) {
int index = behaviours.indexOf(currentlyActive) + 1;
if (index < behaviours.size())
currentlyActive = behaviours.get(index);
@ -116,6 +129,7 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
}
}
log.info("Execution completed");
synchronized (thread){thread.notifyAll();}
}

View file

@ -98,6 +98,8 @@ public class EditActivity extends ListActivity implements AdapterView.OnItemClic
File input = new File(this.getFilesDir(), MainActivity.BEHAVIOUR_SAVE_FILE);
executor.save(input.getAbsolutePath());
log.debug("Saved current state");
log.debug("Resetting executor");
executor.reset();
}catch(Exception e){log.error(null, e);}
super.onPause();
}

View file

@ -164,6 +164,10 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
updateExecutionState();
return true;
}
else if (id == R.id.action_reset) {
currentExecutor.reset();
updateExecutionState();
}
else if (id == R.id.action_edit) {
if(!currentExecutor.isRunning()) {
Intent intent = new Intent(this, EditActivity.class);