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 !running;
} }
/**
* @return true is this behaviour is currently running
*/
public boolean isRunning(){
return running;
}
public void setExecutionListener(BehaviourExecutionListener l){ public void setExecutionListener(BehaviourExecutionListener l){
execListener = l; execListener = l;

View file

@ -64,7 +64,7 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
public synchronized void execute(){ public synchronized void execute(){
if(thread == null || !thread.isAlive()) { if(!isRunning()) {
terminate = false; terminate = false;
thread = new Thread(this); thread = new Thread(this);
thread.start(); thread.start();
@ -78,12 +78,25 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
terminate = true; terminate = true;
if(currentlyActive != null) if(currentlyActive != null)
currentlyActive.terminate(); 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(){ public boolean isRunning(){
if(currentlyActive == null) return thread != null && thread.isAlive();
return false; //if(currentlyActive == null)
return currentlyActive.isRunning(); // return false;
//return currentlyActive.isRunning();
} }
public void run(){ public void run(){
@ -107,7 +120,7 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
log.info("Running behaviour: " + currentlyActive.getName()); log.info("Running behaviour: " + currentlyActive.getName());
currentlyActive.run(); currentlyActive.run();
if(isRunning()) { if(!terminate) {
int index = behaviours.indexOf(currentlyActive) + 1; int index = behaviours.indexOf(currentlyActive) + 1;
if (index < behaviours.size()) if (index < behaviours.size())
currentlyActive = behaviours.get(index); currentlyActive = behaviours.get(index);
@ -116,6 +129,7 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
} }
} }
log.info("Execution completed"); 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); File input = new File(this.getFilesDir(), MainActivity.BEHAVIOUR_SAVE_FILE);
executor.save(input.getAbsolutePath()); executor.save(input.getAbsolutePath());
log.debug("Saved current state"); log.debug("Saved current state");
log.debug("Resetting executor");
executor.reset();
}catch(Exception e){log.error(null, e);} }catch(Exception e){log.error(null, e);}
super.onPause(); super.onPause();
} }

View file

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

View file

@ -1,10 +1,15 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" > tools:context=".MainActivity" >
<item android:id="@+id/action_execute" <item android:id="@+id/action_execute"
android:title="@string/action_run" android:title="@string/action_run"
android:showAsAction="withText|ifRoom" /> android:showAsAction="withText|ifRoom" />
<item android:id="@+id/action_reset"
android:title="@string/action_reset"
android:orderInCategory="50"
android:showAsAction="never" />
<item android:id="@+id/action_edit" <item android:id="@+id/action_edit"
android:title="@string/action_edit" android:title="@string/action_edit"
android:orderInCategory="50" android:orderInCategory="50"

View file

@ -28,5 +28,6 @@
<string name="title_activity_about">About</string> <string name="title_activity_about">About</string>
<string name="pref_debug">Debug Logging</string> <string name="pref_debug">Debug Logging</string>
<string name="pref_debug_summ">Enable application debug logging</string> <string name="pref_debug_summ">Enable application debug logging</string>
<string name="action_reset">Reset</string>
</resources> </resources>