Fixed MainActivity rotation issue

This commit is contained in:
Ziver Koc 2014-08-06 14:21:22 +02:00
parent bb9e55e12d
commit 531de6250d

View file

@ -40,6 +40,7 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
/** Fragments **/
private StatusFragment statusFragment;
private MenuItem action_execute;
private boolean backButtonPressed = false;
private static UeControlExecutor currentExecutor;
@ -64,26 +65,28 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
getFragmentManager().findFragmentById(R.id.status_fragment);
getFragmentManager().beginTransaction()
.replace(R.id.container, BehaviourListFragment.newInstance()).commit();
action_execute = (MenuItem) findViewById(R.id.action_execute);
currentExecutor = new UeControlExecutor();
currentExecutor.setThroughputListener(statusFragment.getThroughputListener());
File input = new File(this.getFilesDir(), BEHAVIOUR_SAVE_FILE);
if(input.exists()) {
try {
log.debug("Reading saved state");
currentExecutor.read(input.getAbsolutePath());
}catch(Exception e){
Toast.makeText(this, "Unable to load saved state", Toast.LENGTH_SHORT).show();
log.error(null, e);
if(currentExecutor == null) {
currentExecutor = new UeControlExecutor();
currentExecutor.setThroughputListener(statusFragment.getThroughputListener());
File input = new File(this.getFilesDir(), BEHAVIOUR_SAVE_FILE);
if (input.exists()) {
try {
log.debug("Reading saved state");
currentExecutor.read(input.getAbsolutePath());
} catch (Exception e) {
Toast.makeText(this, "Unable to load saved state", Toast.LENGTH_SHORT).show();
log.error(null, e);
}
} else {
log.debug("No saved state found, creating default behaviours");
currentExecutor.addBehaviour(new UeBehaviourSleep());
currentExecutor.addBehaviour(new UeBehaviourSurfing());
currentExecutor.addBehaviour(new UeBehaviourSleep(4000));
}
}
else{
log.debug("No saved state found, creating default behaviours");
currentExecutor.addBehaviour(new UeBehaviourSleep());
currentExecutor.addBehaviour(new UeBehaviourSurfing());
currentExecutor.addBehaviour(new UeBehaviourSleep(4000));
}
updateExecutionState();
}
@ -141,20 +144,7 @@ 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();
item.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();
item.setTitle(R.string.action_stop);
}
updateExecutionState();
return true;
}
else if (id == R.id.action_edit) {
@ -173,6 +163,23 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
return super.onOptionsItemSelected(item);
}
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);
else
csvLogger = null;
currentExecutor.execute();
action_execute.setTitle(R.string.action_stop);
}
}
@Override
public void onBackPressed() {