Fixed issue where Run button was showing wrong state
This commit is contained in:
parent
38c4a56a97
commit
f5e95e683c
5 changed files with 69 additions and 30 deletions
8
.idea/dictionaries/ezivkoc.xml
generated
Executable file
8
.idea/dictionaries/ezivkoc.xml
generated
Executable file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<component name="ProjectDictionaryState">
|
||||||
|
<dictionary name="ezivkoc">
|
||||||
|
<words>
|
||||||
|
<w>ericsson</w>
|
||||||
|
<w>parsable</w>
|
||||||
|
</words>
|
||||||
|
</dictionary>
|
||||||
|
</component>
|
||||||
|
|
@ -16,12 +16,15 @@ public abstract class UeBehaviour implements Serializable{
|
||||||
private transient BehaviourExecutionListener execListener;
|
private transient BehaviourExecutionListener execListener;
|
||||||
private transient DataHandledListener datahandledListener;
|
private transient DataHandledListener datahandledListener;
|
||||||
|
|
||||||
|
|
||||||
|
public synchronized void preRun() {
|
||||||
|
running = true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts to run the behaviour, this method will block until the execution is done
|
* Starts to run the behaviour, this method will block until the execution is done
|
||||||
*/
|
*/
|
||||||
public void run(){
|
public void run(){
|
||||||
running = true;
|
|
||||||
|
|
||||||
if(execListener != null) execListener.executionStarted();
|
if(execListener != null) execListener.executionStarted();
|
||||||
try {
|
try {
|
||||||
setProgress(0);
|
setProgress(0);
|
||||||
|
|
@ -101,7 +104,6 @@ public abstract class UeBehaviour implements Serializable{
|
||||||
public abstract String toString();
|
public abstract String toString();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static interface BehaviourExecutionListener {
|
public static interface BehaviourExecutionListener {
|
||||||
public void executionStarted();
|
public void executionStarted();
|
||||||
public void progressChanged(float progress);
|
public void progressChanged(float progress);
|
||||||
|
|
|
||||||
|
|
@ -63,19 +63,21 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void execute(){
|
public synchronized void execute(){
|
||||||
if(thread == null || !thread.isAlive()) {
|
if(thread == null || !thread.isAlive()) {
|
||||||
terminate = false;
|
terminate = false;
|
||||||
thread = new Thread(this);
|
thread = new Thread(this);
|
||||||
thread.start();
|
thread.start();
|
||||||
|
synchronized (thread){
|
||||||
|
try { thread.wait(); }catch(InterruptedException e) {log.error(null,e);}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void terminate(){
|
public synchronized void terminate(){
|
||||||
terminate = true;
|
terminate = true;
|
||||||
if(currentlyActive != null);
|
if(currentlyActive != null)
|
||||||
currentlyActive.terminate();
|
currentlyActive.terminate();
|
||||||
currentlyActive = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isRunning(){
|
public boolean isRunning(){
|
||||||
|
|
@ -85,15 +87,23 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run(){
|
public void run(){
|
||||||
|
log.info("Starting execution");
|
||||||
while(!terminate) {
|
while(!terminate) {
|
||||||
if(behaviours.isEmpty()) {
|
if(behaviours.isEmpty()) {
|
||||||
currentlyActive = null;
|
currentlyActive = null;
|
||||||
terminate();
|
terminate();
|
||||||
|
synchronized (thread){thread.notifyAll();}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (currentlyActive == null)
|
else if (currentlyActive == null)
|
||||||
currentlyActive = behaviours.get(0);
|
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());
|
log.info("Running behaviour: " + currentlyActive.getName());
|
||||||
currentlyActive.run();
|
currentlyActive.run();
|
||||||
|
|
||||||
|
|
@ -112,7 +122,7 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
|
||||||
@Override
|
@Override
|
||||||
public void handledIncomingData(long size) {
|
public void handledIncomingData(long size) {
|
||||||
if (!setRealDataUsage()){
|
if (!setRealDataUsage()){
|
||||||
// TrafficStat unsupported so try to us estimation instead
|
// TrafficStat unsupported so try to use estimation instead
|
||||||
downloadSpeed.setHandledData(size);
|
downloadSpeed.setHandledData(size);
|
||||||
}
|
}
|
||||||
if (throughputListener != null && downloadSpeed.isUpdated())
|
if (throughputListener != null && downloadSpeed.isUpdated())
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ public class UeBehaviourSendSMS extends UeBehaviour{
|
||||||
private static final Logger log = Logger.getLogger(UeBehaviourSendSMS.class);
|
private static final Logger log = Logger.getLogger(UeBehaviourSendSMS.class);
|
||||||
private static final String INTENT_ACTION_SENT = "SMS_SENT";
|
private static final String INTENT_ACTION_SENT = "SMS_SENT";
|
||||||
private static final String INTENT_ACTION_DELIVERED = "SMS_DELIVERED";
|
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{
|
private static enum MessageStatus{
|
||||||
UNKNOWN, MESSAGE_SENT, MESSAGE_DELIVERED, MESSAGE_SEND_ERROR, MESSAGE_CANCELED
|
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 Exception exception;
|
||||||
|
|
||||||
private transient BroadcastReceiver sentBroadcastReceiver = new BroadcastReceiver() {
|
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() {
|
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
|
@Override
|
||||||
protected void execute() throws Exception {
|
protected void execute() throws Exception {
|
||||||
|
|
@ -61,18 +67,20 @@ public class UeBehaviourSendSMS extends UeBehaviour{
|
||||||
log.debug("Sending message");
|
log.debug("Sending message");
|
||||||
SmsManager sms = SmsManager.getDefault();
|
SmsManager sms = SmsManager.getDefault();
|
||||||
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
|
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
|
||||||
this.setProgress(1/3);
|
this.setProgress(1/3f);
|
||||||
|
|
||||||
// Wait for the message sending
|
// Wait for the message sending
|
||||||
|
log.debug("Waiting for message delivery");
|
||||||
while(status != MessageStatus.MESSAGE_DELIVERED ||
|
while(status != MessageStatus.MESSAGE_DELIVERED ||
|
||||||
status != MessageStatus.MESSAGE_CANCELED){
|
status != MessageStatus.MESSAGE_CANCELED){
|
||||||
if(status == MessageStatus.MESSAGE_SENT)
|
if(status == MessageStatus.MESSAGE_SENT)
|
||||||
this.setProgress(2/3);
|
this.setProgress(2/3f);
|
||||||
Thread.sleep(SLEEP_PERIOD);
|
Thread.sleep(SLEEP_PERIOD);
|
||||||
}
|
}
|
||||||
this.setProgress(3/3);
|
this.setProgress(1f);
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
|
log.debug("Cleanup");
|
||||||
context.unregisterReceiver(sentBroadcastReceiver);
|
context.unregisterReceiver(sentBroadcastReceiver);
|
||||||
context.unregisterReceiver(deliveryBroadcastReceiver);
|
context.unregisterReceiver(deliveryBroadcastReceiver);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,10 +64,11 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
|
||||||
statusFragment = (StatusFragment)
|
statusFragment = (StatusFragment)
|
||||||
getFragmentManager().findFragmentById(R.id.status_fragment);
|
getFragmentManager().findFragmentById(R.id.status_fragment);
|
||||||
getFragmentManager().beginTransaction()
|
getFragmentManager().beginTransaction()
|
||||||
.replace(R.id.container, BehaviourListFragment.newInstance()).commit();
|
.replace(R.id.container, new BehaviourListFragment()).commit();
|
||||||
action_execute = (MenuItem) findViewById(R.id.action_execute);
|
|
||||||
|
|
||||||
if(currentExecutor == null) {
|
if(currentExecutor == null) {
|
||||||
|
log.info("Creating new instance of executor");
|
||||||
currentExecutor = new UeControlExecutor();
|
currentExecutor = new UeControlExecutor();
|
||||||
currentExecutor.setThroughputListener(statusFragment.getThroughputListener());
|
currentExecutor.setThroughputListener(statusFragment.getThroughputListener());
|
||||||
File input = new File(this.getFilesDir(), BEHAVIOUR_SAVE_FILE);
|
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));
|
currentExecutor.addBehaviour(new UeBehaviourSleep(4000));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
log.info("Using existing executor");
|
||||||
updateExecutionState();
|
updateExecutionState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -132,6 +135,8 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
|
||||||
ActionBar actionBar = getActionBar();
|
ActionBar actionBar = getActionBar();
|
||||||
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
|
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
|
||||||
actionBar.setDisplayShowTitleEnabled(true);
|
actionBar.setDisplayShowTitleEnabled(true);
|
||||||
|
action_execute = (MenuItem) menu.findItem(R.id.action_execute);
|
||||||
|
updateExecutionState();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -144,6 +149,18 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
|
||||||
// as you specify a parent activity in AndroidManifest.xml.
|
// as you specify a parent activity in AndroidManifest.xml.
|
||||||
int id = item.getItemId();
|
int id = item.getItemId();
|
||||||
if (id == R.id.action_execute) {
|
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();
|
updateExecutionState();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -164,19 +181,11 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateExecutionState(){
|
private void updateExecutionState(){
|
||||||
if(currentExecutor.isRunning()){
|
if(action_execute != null) {
|
||||||
currentExecutor.terminate();
|
if (currentExecutor.isRunning())
|
||||||
action_execute.setTitle(R.string.action_run);
|
|
||||||
csvLogger = null;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
||||||
if(prefs.getBoolean("logging", true))
|
|
||||||
csvLogger = new CSVWriter(this);
|
|
||||||
else
|
|
||||||
csvLogger = null;
|
|
||||||
currentExecutor.execute();
|
|
||||||
action_execute.setTitle(R.string.action_stop);
|
action_execute.setTitle(R.string.action_stop);
|
||||||
|
else
|
||||||
|
action_execute.setTitle(R.string.action_run);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -184,6 +193,10 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
|
||||||
@Override
|
@Override
|
||||||
public void onBackPressed() {
|
public void onBackPressed() {
|
||||||
if (backButtonPressed) {
|
if (backButtonPressed) {
|
||||||
|
if(currentExecutor != null){
|
||||||
|
log.info("Terminating executor");
|
||||||
|
currentExecutor.terminate();
|
||||||
|
}
|
||||||
super.onBackPressed();
|
super.onBackPressed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -200,8 +213,6 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static void logThroughput(double downThroughput, double upThroughput) {
|
public static void logThroughput(double downThroughput, double upThroughput) {
|
||||||
if(csvLogger == null || currentExecutor == null || currentExecutor.getRunningBehaviour() == null)
|
if(csvLogger == null || currentExecutor == null || currentExecutor.getRunningBehaviour() == null)
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue