Fixed issue where Run button was showing wrong state

This commit is contained in:
Ziver Koc 2014-08-06 16:35:03 +02:00
parent 38c4a56a97
commit f5e95e683c
5 changed files with 69 additions and 30 deletions

8
.idea/dictionaries/ezivkoc.xml generated Executable file
View file

@ -0,0 +1,8 @@
<component name="ProjectDictionaryState">
<dictionary name="ezivkoc">
<words>
<w>ericsson</w>
<w>parsable</w>
</words>
</dictionary>
</component>

View file

@ -16,12 +16,15 @@ public abstract class UeBehaviour implements Serializable{
private transient BehaviourExecutionListener execListener;
private transient DataHandledListener datahandledListener;
public synchronized void preRun() {
running = true;
}
/**
* Starts to run the behaviour, this method will block until the execution is done
*/
public void run(){
running = true;
if(execListener != null) execListener.executionStarted();
try {
setProgress(0);
@ -101,7 +104,6 @@ public abstract class UeBehaviour implements Serializable{
public abstract String toString();
public static interface BehaviourExecutionListener {
public void executionStarted();
public void progressChanged(float progress);

View file

@ -63,19 +63,21 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
}
public void execute(){
public synchronized void execute(){
if(thread == null || !thread.isAlive()) {
terminate = false;
thread = new Thread(this);
thread.start();
synchronized (thread){
try { thread.wait(); }catch(InterruptedException e) {log.error(null,e);}
}
}
}
public void terminate(){
public synchronized void terminate(){
terminate = true;
if(currentlyActive != null);
if(currentlyActive != null)
currentlyActive.terminate();
currentlyActive = null;
}
public boolean isRunning(){
@ -85,15 +87,23 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
}
public void run(){
log.info("Starting execution");
while(!terminate) {
if(behaviours.isEmpty()) {
currentlyActive = null;
terminate();
synchronized (thread){thread.notifyAll();}
break;
}
else if (currentlyActive == null)
currentlyActive = behaviours.get(0);
// Notify threads that started execution that we are now running
synchronized (thread){
currentlyActive.preRun();
thread.notifyAll();
}
// Run behaviour
log.info("Running behaviour: " + currentlyActive.getName());
currentlyActive.run();
@ -112,7 +122,7 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
@Override
public void handledIncomingData(long size) {
if (!setRealDataUsage()){
// TrafficStat unsupported so try to us estimation instead
// TrafficStat unsupported so try to use estimation instead
downloadSpeed.setHandledData(size);
}
if (throughputListener != null && downloadSpeed.isUpdated())

View file

@ -22,7 +22,7 @@ public class UeBehaviourSendSMS extends UeBehaviour{
private static final Logger log = Logger.getLogger(UeBehaviourSendSMS.class);
private static final String INTENT_ACTION_SENT = "SMS_SENT";
private static final String INTENT_ACTION_DELIVERED = "SMS_DELIVERED";
private static final int SLEEP_PERIOD = 200;
private static final int SLEEP_PERIOD = 300;
private static enum MessageStatus{
UNKNOWN, MESSAGE_SENT, MESSAGE_DELIVERED, MESSAGE_SEND_ERROR, MESSAGE_CANCELED
@ -37,9 +37,15 @@ public class UeBehaviourSendSMS extends UeBehaviour{
private transient Exception exception;
private transient BroadcastReceiver sentBroadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context arg0, Intent arg1) { setSendingStatus(getResultCode()); }};
public void onReceive(Context arg0, Intent arg1) {
setSendingStatus(getResultCode());
}
};
private transient BroadcastReceiver deliveryBroadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context arg0, Intent arg1) { setDeliveryStatus(getResultCode()); }};
public void onReceive(Context arg0, Intent arg1) {
setDeliveryStatus(getResultCode());
}
};
@Override
protected void execute() throws Exception {
@ -61,18 +67,20 @@ public class UeBehaviourSendSMS extends UeBehaviour{
log.debug("Sending message");
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
this.setProgress(1/3);
this.setProgress(1/3f);
// Wait for the message sending
log.debug("Waiting for message delivery");
while(status != MessageStatus.MESSAGE_DELIVERED ||
status != MessageStatus.MESSAGE_CANCELED){
if(status == MessageStatus.MESSAGE_SENT)
this.setProgress(2/3);
this.setProgress(2/3f);
Thread.sleep(SLEEP_PERIOD);
}
this.setProgress(3/3);
this.setProgress(1f);
// Cleanup
log.debug("Cleanup");
context.unregisterReceiver(sentBroadcastReceiver);
context.unregisterReceiver(deliveryBroadcastReceiver);
}

View file

@ -64,10 +64,11 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
statusFragment = (StatusFragment)
getFragmentManager().findFragmentById(R.id.status_fragment);
getFragmentManager().beginTransaction()
.replace(R.id.container, BehaviourListFragment.newInstance()).commit();
action_execute = (MenuItem) findViewById(R.id.action_execute);
.replace(R.id.container, new BehaviourListFragment()).commit();
if(currentExecutor == null) {
log.info("Creating new instance of executor");
currentExecutor = new UeControlExecutor();
currentExecutor.setThroughputListener(statusFragment.getThroughputListener());
File input = new File(this.getFilesDir(), BEHAVIOUR_SAVE_FILE);
@ -86,6 +87,8 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
currentExecutor.addBehaviour(new UeBehaviourSleep(4000));
}
}
else
log.info("Using existing executor");
updateExecutionState();
}
@ -132,6 +135,8 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
action_execute = (MenuItem) menu.findItem(R.id.action_execute);
updateExecutionState();
return true;
}
@ -144,6 +149,18 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_execute) {
if(currentExecutor.isRunning()) {
currentExecutor.terminate();
csvLogger = null;
}
else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(prefs.getBoolean("logging", true))
csvLogger = new CSVWriter(this);
else
csvLogger = null;
currentExecutor.execute();
}
updateExecutionState();
return true;
}
@ -164,19 +181,11 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
}
private void updateExecutionState(){
if(currentExecutor.isRunning()){
currentExecutor.terminate();
action_execute.setTitle(R.string.action_run);
csvLogger = null;
}
else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(prefs.getBoolean("logging", true))
csvLogger = new CSVWriter(this);
if(action_execute != null) {
if (currentExecutor.isRunning())
action_execute.setTitle(R.string.action_stop);
else
csvLogger = null;
currentExecutor.execute();
action_execute.setTitle(R.string.action_stop);
action_execute.setTitle(R.string.action_run);
}
}
@ -184,6 +193,10 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
@Override
public void onBackPressed() {
if (backButtonPressed) {
if(currentExecutor != null){
log.info("Terminating executor");
currentExecutor.terminate();
}
super.onBackPressed();
return;
}
@ -200,8 +213,6 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
}
public static void logThroughput(double downThroughput, double upThroughput) {
if(csvLogger == null || currentExecutor == null || currentExecutor.getRunningBehaviour() == null)
return;