Moved CSVWriter and Configurable classes to core package

This commit is contained in:
Ziver Koc 2015-01-16 11:54:33 +01:00
parent 850dbb661b
commit 8a00a547cf
15 changed files with 216 additions and 203 deletions

View file

@ -6,6 +6,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
@ -23,7 +24,6 @@ import com.ericsson.uecontrol.gui.fragments.ExecNotification;
import com.ericsson.uecontrol.gui.fragments.FileBrowserDialog;
import com.ericsson.uecontrol.gui.fragments.FileBrowserDialog.OnFileSelectionListener;
import com.ericsson.uecontrol.gui.fragments.StatusFragment;
import com.ericsson.uecontrol.gui.util.CSVWriter;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
@ -48,8 +48,7 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
private boolean backButtonPressed = false;
/* Static Data */
private static UeControlExecutor currentExecutor;
private static CSVWriter csvLogger;
private static UeControlExecutor executor;
private static int uid;
private static Context context;
@ -70,24 +69,24 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
setupDebugLogging();
// Setup Executor
if(currentExecutor == null) {
if(executor == null) {
log.info("Creating new instance of executor");
currentExecutor = new UeControlExecutor();
currentExecutor.setDeviceBasedThroughput(Boolean.parseBoolean(prefs.getString("throughput_type", "false")));
executor = new UeControlExecutor();
executor.setDeviceBasedThroughput(Boolean.parseBoolean(prefs.getString("throughput_type", "false")));
File input = new File(this.getFilesDir(), BEHAVIOUR_SAVE_FILE);
if (input.exists()) {
try {
log.debug("Reading saved state");
currentExecutor.read(input.getAbsolutePath());
executor.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));
executor.addBehaviour(new UeBehaviourSleep());
executor.addBehaviour(new UeBehaviourSurfing());
executor.addBehaviour(new UeBehaviourSleep(4000));
}
}
else {
@ -103,7 +102,7 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
getFragmentManager().findFragmentById(R.id.behaviour_list_fragment);
currentExecutor.setThroughputListener(statusFragment.getThroughputListener());
executor.setThroughputListener(statusFragment.getThroughputListener());
updateExecutionState();
}
@ -137,8 +136,8 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
}
else if(key.equals("throughput_type")){
log.info("Device Throughput set to: "+sharedPreferences.getString("throughput_type", "false"));
if(currentExecutor != null)
currentExecutor.setDeviceBasedThroughput(Boolean.parseBoolean(sharedPreferences.getString("throughput_type", "false")));
if(executor != null)
executor.setDeviceBasedThroughput(Boolean.parseBoolean(sharedPreferences.getString("throughput_type", "false")));
}
}
@ -168,37 +167,36 @@ 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;
if(executor.isRunning()) {
executor.terminate();
}
else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(prefs.getBoolean("logging", true))
csvLogger = new CSVWriter();
if(prefs.getBoolean("logging", false))
executor.setLogPath(prefs.getString("logging_path",
Environment.getExternalStorageDirectory().getAbsolutePath()+"/uecontrol/"));
else
csvLogger = null;
currentExecutor.execute();
executor.setLogPath(null);
executor.execute();
}
updateExecutionState();
return true;
}
else if (id == R.id.action_mark) {
if(csvLogger != null) {
csvLogger.addComment("--- Mark ---");
if (executor.getCsvLogger() != null) {
executor.getCsvLogger().addComment("--- Mark ---");
Toast.makeText(this, "Mark added to log", Toast.LENGTH_SHORT).show();
}
}
else if (id == R.id.action_reset) {
if(currentExecutor != null)
currentExecutor.reset();
if(executor != null)
executor.reset();
if(statusFragment != null)
statusFragment.reset();
csvLogger = null;
updateExecutionState();
}
else if (id == R.id.action_edit) {
if(!currentExecutor.isRunning()) {
if(!executor.isRunning()) {
Intent intent = new Intent(this, EditActivity.class);
startActivity(intent);
}
@ -225,12 +223,12 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
public void onFileSelection(String tag, File file){
try {
if(tag.equals("import")) {
currentExecutor.read(file.getAbsolutePath());
executor.read(file.getAbsolutePath());
if(behaviourListFragment != null)
behaviourListFragment.onResume();
}
else if(tag.equals("export")) {
currentExecutor.save(file.getAbsolutePath());
executor.save(file.getAbsolutePath());
}
} catch (Exception e) {
String msg = "Unable to import from file";
@ -245,10 +243,10 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
private void updateExecutionState(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(action_execute != null) {
if (currentExecutor.isRunning()) {
if (executor.isRunning()) {
action_execute.setTitle(R.string.action_stop);
ExecNotification.create();
if(action_mark != null && csvLogger != null)
if(action_mark != null && prefs.getBoolean("logging", false))
action_mark.setEnabled(true);
if(prefs.getBoolean("screen_on", false))
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
@ -266,9 +264,9 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
@Override
public void onBackPressed() {
if (backButtonPressed) {
if(currentExecutor != null){
if(executor != null){
log.info("Terminating executor");
currentExecutor.terminate();
executor.terminate();
}
super.onBackPressed();
return;
@ -286,15 +284,8 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
}
public static void logThroughput(double downThroughput, double upThroughput) {
if(csvLogger == null || currentExecutor == null || currentExecutor.getRunningBehaviour() == null)
return;
csvLogger.write(currentExecutor.getRunningBehaviour().getName(), downThroughput, upThroughput);
}
public static UeControlExecutor getExecutor() {
return currentExecutor;
return executor;
}
public static int getUID(){
return uid;