Added option for device or app level throughput.

Started on a file browser dialog for import/export
This commit is contained in:
Ziver Koc 2014-08-15 16:05:56 +02:00
parent 01896c15e5
commit 9f84e472eb
6 changed files with 80 additions and 29 deletions

View file

@ -34,19 +34,21 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
private UeBehaviour currentlyActive;
private boolean terminate;
private Thread thread;
private Gson gson;
private long previousRxBytes = TrafficStats.UNSUPPORTED;
private long previousTxBytes = TrafficStats.UNSUPPORTED;
private boolean deviceBasedThroughput;
private ThroughputCalculator downloadSpeed;
private ThroughputCalculator uploadSpeed;
private ThroughputListener throughputListener;
private Gson gson;
public UeControlExecutor(){
behaviours = new ArrayList<UeBehaviour>();
downloadSpeed = new ThroughputCalculator();
uploadSpeed = new ThroughputCalculator();
deviceBasedThroughput = false;
gson = new GsonBuilder()
.registerTypeAdapter(UeBehaviour.class, new AbstractElementAdapter<UeBehaviour>())
@ -62,15 +64,10 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
FileReader in = new FileReader(file);
Type type = new TypeToken<ArrayList<UeBehaviour>>(){}.getType();
ArrayList<UeBehaviour> newBehaviours = gson.fromJson(in, type);
behaviours.addAll(newBehaviours);
in.close();
/*ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
int size = in.readInt();
for(int i=0; i<size; i++){
addBehaviour((UeBehaviour) in.readObject());
for(UeBehaviour behaviour : newBehaviours){
this.addBehaviour(behaviour);
}
in.close();*/
in.close();
}
public void save(String file) throws IOException {
FileWriter out = new FileWriter(file);
@ -78,13 +75,6 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
log.debug("Saving behaviours: "+gson.toJson(behaviours, type));
gson.toJson(behaviours, type, out);
out.close();
/*ObjectOutput out = new ObjectOutputStream(new FileOutputStream(file));
out.writeInt(behaviours.size());
for(UeBehaviour behaviour : behaviours){
out.writeObject(behaviour);
}
out.close();*/
}
@ -128,9 +118,6 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
public boolean isRunning(){
return thread != null && thread.isAlive();
//if(currentlyActive == null)
// return false;
//return currentlyActive.isRunning();
}
public void run(){
@ -187,10 +174,16 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
}
protected boolean setRealDataUsage(){
boolean ret = false;
long currentRxBytes = TrafficStats.getUidRxBytes(MainActivity.getUID());
long currentTxBytes = TrafficStats.getUidTxBytes(MainActivity.getUID());
//long currentRxBytes = TrafficStats.getTotalRxBytes();
//long currentTxBytes = TrafficStats.getTotalTxBytes();
long currentRxBytes = 0;
long currentTxBytes = 0;
if(deviceBasedThroughput) {
currentRxBytes = TrafficStats.getTotalRxBytes();
currentTxBytes = TrafficStats.getTotalTxBytes();
}
else{
currentRxBytes = TrafficStats.getUidRxBytes(MainActivity.getUID());
currentTxBytes = TrafficStats.getUidTxBytes(MainActivity.getUID());
}
if (currentRxBytes != TrafficStats.UNSUPPORTED && currentTxBytes != TrafficStats.UNSUPPORTED
&& previousRxBytes != TrafficStats.UNSUPPORTED && previousTxBytes != TrafficStats.UNSUPPORTED){
@ -206,6 +199,9 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
public void setThroughputListener(ThroughputListener listener){
throughputListener = listener;
}
public void setDeviceBasedThroughput(boolean enable){
deviceBasedThroughput = enable;
}
public List<UeBehaviour> getBehaviourList() {
return behaviours;

View file

@ -19,6 +19,7 @@ import com.ericsson.uecontrol.core.UeControlExecutor;
import com.ericsson.uecontrol.core.behaviour.UeBehaviourSleep;
import com.ericsson.uecontrol.core.behaviour.UeBehaviourSurfing;
import com.ericsson.uecontrol.gui.fragments.BehaviourListFragment;
import com.ericsson.uecontrol.gui.fragments.FileBrowserDialog;
import com.ericsson.uecontrol.gui.fragments.StatusFragment;
import com.ericsson.uecontrol.gui.util.CSVWriter;
@ -51,8 +52,12 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set static fields
// Set static fields and preferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
prefs.registerOnSharedPreferenceChangeListener(this);
uid = getApplicationInfo().uid;
context = this;
@ -66,10 +71,11 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
getFragmentManager().beginTransaction()
.replace(R.id.container, new BehaviourListFragment()).commit();
// Setup Executor
if(currentExecutor == null) {
log.info("Creating new instance of executor");
currentExecutor = new UeControlExecutor();
currentExecutor.setDeviceBasedThroughput(Boolean.parseBoolean(prefs.getString("throughput_type", "false")));
File input = new File(this.getFilesDir(), BEHAVIOUR_SAVE_FILE);
if (input.exists()) {
try {
@ -91,6 +97,9 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
}
currentExecutor.setThroughputListener(statusFragment.getThroughputListener());
updateExecutionState();
}
@ -109,8 +118,6 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
// Set log level of a specific logger
logConfigurator.setLevel("org.apache", Level.ERROR);
logConfigurator.configure();
prefs.registerOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
@ -124,6 +131,11 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
Logger.getRootLogger().setLevel(Level.WARN);
}
}
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")));
}
}
@ -190,6 +202,12 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
Toast.makeText(this, "Stop execution to edit behaviours", Toast.LENGTH_SHORT).show();
return true;
}
else if (id == R.id.action_import) {
//TODO:
}
else if (id == R.id.action_export) {
//TODO:
}
else if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
@ -197,6 +215,7 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
return super.onOptionsItemSelected(item);
}
private void updateExecutionState(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(action_execute != null) {
@ -253,5 +272,4 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
return uid;
}
public static Context getContext(){return context;}
}