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

@ -4,6 +4,7 @@ import android.net.TrafficStats;
import android.os.Handler; import android.os.Handler;
import com.ericsson.uecontrol.core.util.AbstractElementAdapter; import com.ericsson.uecontrol.core.util.AbstractElementAdapter;
import com.ericsson.uecontrol.core.util.CSVWriter;
import com.ericsson.uecontrol.core.util.ThroughputCalculator; import com.ericsson.uecontrol.core.util.ThroughputCalculator;
import com.ericsson.uecontrol.gui.MainActivity; import com.ericsson.uecontrol.gui.MainActivity;
import com.google.gson.Gson; import com.google.gson.Gson;
@ -29,7 +30,8 @@ public class UeControlExecutor implements Runnable{
private UeBehaviour currentlyActive; private UeBehaviour currentlyActive;
private boolean terminate; private boolean terminate;
private Thread thread; private Thread thread;
private Gson gson; private String csvPath;
private CSVWriter csvLogger;
private Handler handler; private Handler handler;
private ThroughputUpdateRunnable throughputUpdateRunnable; private ThroughputUpdateRunnable throughputUpdateRunnable;
@ -40,10 +42,6 @@ public class UeControlExecutor implements Runnable{
behaviours = new ArrayList<UeBehaviour>(); behaviours = new ArrayList<UeBehaviour>();
handler = new Handler(); handler = new Handler();
throughputUpdateRunnable = new ThroughputUpdateRunnable(); throughputUpdateRunnable = new ThroughputUpdateRunnable();
gson = new GsonBuilder()
.registerTypeAdapter(UeBehaviour.class, new AbstractElementAdapter<UeBehaviour>())
.create();
} }
public void addBehaviour(UeBehaviour b){ public void addBehaviour(UeBehaviour b){
@ -51,6 +49,9 @@ public class UeControlExecutor implements Runnable{
} }
public void read(String file) throws Exception { public void read(String file) throws Exception {
Gson gson = new GsonBuilder()
.registerTypeAdapter(UeBehaviour.class, new AbstractElementAdapter<UeBehaviour>())
.create();
FileReader in = new FileReader(file); FileReader in = new FileReader(file);
Type type = new TypeToken<ArrayList<UeBehaviour>>(){}.getType(); Type type = new TypeToken<ArrayList<UeBehaviour>>(){}.getType();
ArrayList<UeBehaviour> newBehaviours = gson.fromJson(in, type); ArrayList<UeBehaviour> newBehaviours = gson.fromJson(in, type);
@ -60,6 +61,9 @@ public class UeControlExecutor implements Runnable{
in.close(); in.close();
} }
public void save(String file) throws IOException { public void save(String file) throws IOException {
Gson gson = new GsonBuilder()
.registerTypeAdapter(UeBehaviour.class, new AbstractElementAdapter<UeBehaviour>())
.create();
FileWriter out = new FileWriter(file); FileWriter out = new FileWriter(file);
Type type = new TypeToken<ArrayList<UeBehaviour>>(){}.getType(); Type type = new TypeToken<ArrayList<UeBehaviour>>(){}.getType();
log.debug("Saving behaviours: "+gson.toJson(behaviours, type)); log.debug("Saving behaviours: "+gson.toJson(behaviours, type));
@ -120,6 +124,8 @@ public class UeControlExecutor implements Runnable{
public void run(){ public void run(){
log.info("Starting execution"); log.info("Starting execution");
if(csvPath != null)
csvLogger = new CSVWriter(csvPath);
while(!terminate) { while(!terminate) {
if(behaviours.isEmpty()) { if(behaviours.isEmpty()) {
currentlyActive = null; currentlyActive = null;
@ -147,12 +153,20 @@ public class UeControlExecutor implements Runnable{
currentlyActive = null; currentlyActive = null;
} }
} }
if(csvLogger != null){
csvLogger.flush();
csvLogger = null;
}
log.info("Execution completed"); log.info("Execution completed");
synchronized (thread){thread.notifyAll();} synchronized (thread){thread.notifyAll();}
} }
/**
* Set the log path or null to disable logging
*/
public void setLogPath(String path){
csvPath = path;
}
public void setThroughputListener(ThroughputListener listener){ public void setThroughputListener(ThroughputListener listener){
throughputListener = listener; throughputListener = listener;
} }
@ -163,10 +177,13 @@ public class UeControlExecutor implements Runnable{
public List<UeBehaviour> getBehaviourList() { public List<UeBehaviour> getBehaviourList() {
return behaviours; return behaviours;
} }
public UeBehaviour getRunningBehaviour(){ public UeBehaviour getRunningBehaviour(){
return currentlyActive; return currentlyActive;
} }
public CSVWriter getCsvLogger() {
return csvLogger;
}
public static interface ThroughputListener{ public static interface ThroughputListener{
public void throughputUpdate(double downThroughput, double upThroughput); public void throughputUpdate(double downThroughput, double upThroughput);
@ -194,9 +211,16 @@ public class UeControlExecutor implements Runnable{
uploadSpeed.setHandledData(0); uploadSpeed.setHandledData(0);
downloadSpeed.setHandledData(0); downloadSpeed.setHandledData(0);
} }
if (throughputListener != null && uploadSpeed.isUpdated()) if (uploadSpeed.isUpdated()) {
throughputListener.throughputUpdate(downloadSpeed.getBitThroughput(), uploadSpeed.getBitThroughput()); if(csvLogger != null)
csvLogger.write(getRunningBehaviour().getName(),
downloadSpeed.getBitThroughput(),
uploadSpeed.getBitThroughput());
if (throughputListener != null)
throughputListener.throughputUpdate(
downloadSpeed.getBitThroughput(),
uploadSpeed.getBitThroughput());
}
// Rescedule this handler // Rescedule this handler
if(!terminate) if(!terminate)
handler.postDelayed(this, 100); handler.postDelayed(this, 100);

View file

@ -2,7 +2,7 @@ package com.ericsson.uecontrol.core.behaviour;
import com.ericsson.uecontrol.core.UeBehaviour; import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.core.util.UrlUtil; import com.ericsson.uecontrol.core.util.UrlUtil;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable; import com.ericsson.uecontrol.core.util.Configurator.Configurable;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;

View file

@ -1,6 +1,6 @@
package com.ericsson.uecontrol.core.behaviour; package com.ericsson.uecontrol.core.behaviour;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable; import com.ericsson.uecontrol.core.util.Configurator.Configurable;
import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;

View file

@ -1,7 +1,7 @@
package com.ericsson.uecontrol.core.behaviour; package com.ericsson.uecontrol.core.behaviour;
import com.ericsson.uecontrol.core.util.ThroughputCalculator; import com.ericsson.uecontrol.core.util.ThroughputCalculator;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable; import com.ericsson.uecontrol.core.util.Configurator.Configurable;
import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;

View file

@ -10,7 +10,7 @@ import android.telephony.SmsManager;
import com.ericsson.uecontrol.core.UeBehaviour; import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.gui.MainActivity; import com.ericsson.uecontrol.gui.MainActivity;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable; import com.ericsson.uecontrol.core.util.Configurator.Configurable;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;

View file

@ -1,8 +1,7 @@
package com.ericsson.uecontrol.core.behaviour; package com.ericsson.uecontrol.core.behaviour;
import android.util.Log;
import com.ericsson.uecontrol.core.UeBehaviour; import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable; import com.ericsson.uecontrol.core.util.Configurator.Configurable;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
/** /**

View file

@ -6,7 +6,7 @@ import android.telephony.TelephonyManager;
import com.ericsson.uecontrol.core.UeBehaviour; import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.core.util.CallUtil; import com.ericsson.uecontrol.core.util.CallUtil;
import com.ericsson.uecontrol.gui.MainActivity; import com.ericsson.uecontrol.gui.MainActivity;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable; import com.ericsson.uecontrol.core.util.Configurator.Configurable;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;

View file

@ -2,7 +2,7 @@ package com.ericsson.uecontrol.core.behaviour;
import com.ericsson.uecontrol.core.UeBehaviour; import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.core.util.UrlUtil; import com.ericsson.uecontrol.core.util.UrlUtil;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable; import com.ericsson.uecontrol.core.util.Configurator.Configurable;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;

View file

@ -3,7 +3,7 @@ package com.ericsson.uecontrol.core.behaviour;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import com.ericsson.uecontrol.core.UeBehaviour; import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable; import com.ericsson.uecontrol.core.util.Configurator.Configurable;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.dom4j.Attribute; import org.dom4j.Attribute;

View file

@ -1,4 +1,4 @@
package com.ericsson.uecontrol.gui.util; package com.ericsson.uecontrol.core.util;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -39,12 +39,9 @@ public class CSVWriter {
private File file; private File file;
private String comment; private String comment;
public CSVWriter(){ public CSVWriter(String path){
Context context = MainActivity.getContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
comment = ""; comment = "";
String path = prefs.getString("logging_path", Environment.getExternalStorageDirectory().getAbsolutePath()+"/uecontrol/");
if(!path.endsWith(File.separator)) path += File.separator; if(!path.endsWith(File.separator)) path += File.separator;
file = new File( file = new File(
path, path,
@ -58,9 +55,7 @@ public class CSVWriter {
line.append(header).append(DELIMITER); line.append(header).append(DELIMITER);
} }
writeLine(line.toString()); writeLine(line.toString());
// Add file to Media Scanner so it shows up when phone is connected with usb flush();
MainActivity.getContext().sendBroadcast(
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
} }
public void addComment(String str){ public void addComment(String str){
@ -104,6 +99,11 @@ public class CSVWriter {
return false; return false;
} }
public void flush(){
// Add file to Media Scanner so it shows up when phone is connected with usb
MainActivity.getContext().sendBroadcast(
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
}
//******************* INFORMATION FUNCTIONS ******************************************** //******************* INFORMATION FUNCTIONS ********************************************
public static String getRat(){ public static String getRat(){

View file

@ -1,4 +1,4 @@
package com.ericsson.uecontrol.gui.util; package com.ericsson.uecontrol.core.util;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;

View file

@ -1,4 +1,4 @@
package com.ericsson.uecontrol.gui.util; package com.ericsson.uecontrol.core.util;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;

View file

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

View file

@ -15,8 +15,8 @@ import android.widget.TextView;
import com.ericsson.uecontrol.R; import com.ericsson.uecontrol.R;
import com.ericsson.uecontrol.core.UeBehaviour; import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.gui.MainActivity; import com.ericsson.uecontrol.gui.MainActivity;
import com.ericsson.uecontrol.gui.util.Configurator; import com.ericsson.uecontrol.core.util.Configurator;
import com.ericsson.uecontrol.gui.util.Configurator.ConfigurationParam; import com.ericsson.uecontrol.core.util.Configurator.ConfigurationParam;
import java.util.HashMap; import java.util.HashMap;

View file

@ -16,7 +16,7 @@ import com.ericsson.uecontrol.R;
import com.ericsson.uecontrol.core.UeControlExecutor; import com.ericsson.uecontrol.core.UeControlExecutor;
import com.ericsson.uecontrol.core.util.ThroughputCalculator; import com.ericsson.uecontrol.core.util.ThroughputCalculator;
import com.ericsson.uecontrol.gui.MainActivity; import com.ericsson.uecontrol.gui.MainActivity;
import com.ericsson.uecontrol.gui.util.CSVWriter; import com.ericsson.uecontrol.core.util.CSVWriter;
import com.jjoe64.graphview.GraphView.GraphViewData; import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphViewSeries; import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.GraphViewSeries.GraphViewSeriesStyle; import com.jjoe64.graphview.GraphViewSeries.GraphViewSeriesStyle;
@ -96,7 +96,6 @@ public class StatusFragment extends Fragment {
upGraph.appendData(new GraphViewData(x, upThroughput), true, 120); upGraph.appendData(new GraphViewData(x, upThroughput), true, 120);
x++; x++;
MainActivity.logThroughput(downThroughput, upThroughput);
ExecNotification.update(downThroughput, upThroughput); ExecNotification.update(downThroughput, upThroughput);
startActivityTimer(); startActivityTimer();
} }