2014-07-31 15:01:44 +02:00
|
|
|
package com.ericsson.uecontrol.gui;
|
|
|
|
|
|
|
|
|
|
import android.app.ActionBar;
|
2014-08-06 13:18:27 +02:00
|
|
|
import android.content.Context;
|
2014-07-31 15:01:44 +02:00
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.SharedPreferences;
|
2014-08-01 16:07:48 +02:00
|
|
|
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
2014-08-05 12:55:51 +02:00
|
|
|
import android.content.pm.ApplicationInfo;
|
|
|
|
|
import android.content.pm.PackageManager;
|
2014-07-31 15:01:44 +02:00
|
|
|
import android.os.Bundle;
|
2014-08-01 10:58:55 +02:00
|
|
|
import android.os.Handler;
|
2014-07-31 15:01:44 +02:00
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
|
import android.support.v4.app.FragmentActivity;
|
|
|
|
|
import android.view.Menu;
|
|
|
|
|
import android.view.MenuItem;
|
2014-08-01 10:58:55 +02:00
|
|
|
import android.widget.Toast;
|
2014-07-31 15:01:44 +02:00
|
|
|
|
|
|
|
|
import com.ericsson.uecontrol.R;
|
|
|
|
|
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.NavigationDrawerFragment;
|
|
|
|
|
import com.ericsson.uecontrol.gui.fragments.StatusFragment;
|
|
|
|
|
import com.ericsson.uecontrol.gui.util.CSVWriter;
|
|
|
|
|
|
2014-08-01 16:07:48 +02:00
|
|
|
import org.apache.log4j.Level;
|
|
|
|
|
import org.apache.log4j.Logger;
|
2014-07-31 15:01:44 +02:00
|
|
|
|
2014-08-01 16:07:48 +02:00
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
|
|
import de.mindpipe.android.logging.log4j.LogConfigurator;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class MainActivity extends FragmentActivity implements OnSharedPreferenceChangeListener{
|
|
|
|
|
private static final Logger log = Logger.getLogger(MainActivity.class);
|
2014-08-04 14:21:27 +02:00
|
|
|
public static final String DEFAULT_LOG_PATH = "/sdcard/uecontrol/";
|
2014-08-04 15:36:45 +02:00
|
|
|
public static final String BEHAVIOUR_SAVE_FILE = "behaviour_list.dat";
|
|
|
|
|
|
2014-07-31 15:01:44 +02:00
|
|
|
|
2014-08-04 14:21:27 +02:00
|
|
|
/** Fragments **/
|
2014-07-31 15:01:44 +02:00
|
|
|
private StatusFragment statusFragment;
|
2014-08-06 14:21:22 +02:00
|
|
|
private MenuItem action_execute;
|
2014-08-01 10:58:55 +02:00
|
|
|
private boolean backButtonPressed = false;
|
2014-07-31 15:01:44 +02:00
|
|
|
|
|
|
|
|
private static UeControlExecutor currentExecutor;
|
2014-08-01 16:07:48 +02:00
|
|
|
private static CSVWriter csvLogger;
|
2014-08-05 12:55:51 +02:00
|
|
|
private static int uid;
|
2014-08-06 13:18:27 +02:00
|
|
|
private static Context context;
|
2014-07-31 15:01:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
2014-08-06 13:18:27 +02:00
|
|
|
// Set static fields
|
2014-08-05 12:55:51 +02:00
|
|
|
uid = getApplicationInfo().uid;
|
2014-08-06 13:18:27 +02:00
|
|
|
context = this;
|
2014-07-31 15:01:44 +02:00
|
|
|
|
2014-08-01 16:07:48 +02:00
|
|
|
// Setup Debugging
|
|
|
|
|
setupDebugLogging();
|
|
|
|
|
|
|
|
|
|
// Setup Main GUI
|
|
|
|
|
setContentView(R.layout.activity_main);
|
2014-07-31 15:01:44 +02:00
|
|
|
statusFragment = (StatusFragment)
|
|
|
|
|
getFragmentManager().findFragmentById(R.id.status_fragment);
|
|
|
|
|
getFragmentManager().beginTransaction()
|
2014-08-06 16:35:03 +02:00
|
|
|
.replace(R.id.container, new BehaviourListFragment()).commit();
|
|
|
|
|
|
2014-08-06 14:21:22 +02:00
|
|
|
|
|
|
|
|
if(currentExecutor == null) {
|
2014-08-06 16:35:03 +02:00
|
|
|
log.info("Creating new instance of executor");
|
2014-08-06 14:21:22 +02:00
|
|
|
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));
|
2014-08-06 13:18:27 +02:00
|
|
|
}
|
2014-08-04 15:36:45 +02:00
|
|
|
}
|
2014-08-06 16:35:03 +02:00
|
|
|
else
|
|
|
|
|
log.info("Using existing executor");
|
2014-08-06 14:21:22 +02:00
|
|
|
updateExecutionState();
|
2014-07-31 15:01:44 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-04 15:36:45 +02:00
|
|
|
|
2014-08-01 16:07:48 +02:00
|
|
|
public void setupDebugLogging(){
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2014-08-04 14:21:27 +02:00
|
|
|
String path = prefs.getString("logging_path", DEFAULT_LOG_PATH);
|
2014-08-01 16:07:48 +02:00
|
|
|
if(!path.endsWith("/")) path += File.separator;
|
|
|
|
|
|
|
|
|
|
LogConfigurator logConfigurator = new LogConfigurator();
|
|
|
|
|
logConfigurator.setFileName(path + "uecontrol.log");
|
|
|
|
|
if(prefs.getBoolean("debug", false))
|
|
|
|
|
logConfigurator.setRootLevel(Level.DEBUG);
|
|
|
|
|
else
|
|
|
|
|
logConfigurator.setRootLevel(Level.WARN);
|
|
|
|
|
// 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) {
|
|
|
|
|
log.debug("Preference changed: "+key);
|
|
|
|
|
if(key.equals("debug")) {
|
|
|
|
|
if (sharedPreferences.getBoolean("debug", false)) {
|
|
|
|
|
Logger.getRootLogger().setLevel(Level.DEBUG);
|
|
|
|
|
log.info("Enabling debug logging.");
|
|
|
|
|
} else {
|
|
|
|
|
log.info("Disabling debug logging.");
|
|
|
|
|
Logger.getRootLogger().setLevel(Level.WARN);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 15:01:44 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
2014-08-04 14:21:27 +02:00
|
|
|
// Only show items in the action bar relevant to this screen
|
|
|
|
|
// if the drawer is not showing. Otherwise, let the drawer
|
|
|
|
|
// decide what to show in the action bar.
|
|
|
|
|
getMenuInflater().inflate(R.menu.main, menu);
|
2014-07-31 15:01:44 +02:00
|
|
|
|
2014-08-04 14:21:27 +02:00
|
|
|
ActionBar actionBar = getActionBar();
|
|
|
|
|
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
|
|
|
|
|
actionBar.setDisplayShowTitleEnabled(true);
|
2014-08-06 16:35:03 +02:00
|
|
|
action_execute = (MenuItem) menu.findItem(R.id.action_execute);
|
|
|
|
|
updateExecutionState();
|
2014-07-31 15:01:44 +02:00
|
|
|
|
2014-08-04 14:21:27 +02:00
|
|
|
return true;
|
2014-07-31 15:01:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
|
// Handle action bar item clicks here. The action bar will
|
|
|
|
|
// automatically handle clicks on the Home/Up button, so long
|
|
|
|
|
// as you specify a parent activity in AndroidManifest.xml.
|
|
|
|
|
int id = item.getItemId();
|
|
|
|
|
if (id == R.id.action_execute) {
|
2014-08-06 16:35:03 +02:00
|
|
|
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();
|
|
|
|
|
}
|
2014-08-06 14:21:22 +02:00
|
|
|
updateExecutionState();
|
2014-07-31 15:01:44 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
2014-08-04 15:36:45 +02:00
|
|
|
else if (id == R.id.action_edit) {
|
2014-08-04 16:47:04 +02:00
|
|
|
if(!currentExecutor.isRunning()) {
|
|
|
|
|
Intent intent = new Intent(this, EditActivity.class);
|
|
|
|
|
startActivity(intent);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Toast.makeText(this, "Stop execution to edit behaviours", Toast.LENGTH_SHORT).show();
|
2014-07-31 15:01:44 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
2014-08-04 15:36:45 +02:00
|
|
|
else if (id == R.id.action_settings) {
|
2014-07-31 15:01:44 +02:00
|
|
|
startActivity(new Intent(this, SettingsActivity.class));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-06 14:21:22 +02:00
|
|
|
private void updateExecutionState(){
|
2014-08-06 16:35:03 +02:00
|
|
|
if(action_execute != null) {
|
|
|
|
|
if (currentExecutor.isRunning())
|
|
|
|
|
action_execute.setTitle(R.string.action_stop);
|
2014-08-06 14:21:22 +02:00
|
|
|
else
|
2014-08-06 16:35:03 +02:00
|
|
|
action_execute.setTitle(R.string.action_run);
|
2014-08-06 14:21:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-01 16:07:48 +02:00
|
|
|
|
2014-08-01 10:58:55 +02:00
|
|
|
@Override
|
|
|
|
|
public void onBackPressed() {
|
|
|
|
|
if (backButtonPressed) {
|
2014-08-06 16:35:03 +02:00
|
|
|
if(currentExecutor != null){
|
|
|
|
|
log.info("Terminating executor");
|
|
|
|
|
currentExecutor.terminate();
|
|
|
|
|
}
|
2014-08-01 10:58:55 +02:00
|
|
|
super.onBackPressed();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.backButtonPressed = true;
|
|
|
|
|
Toast.makeText(this, "Press BACK again to Exit", Toast.LENGTH_SHORT).show();
|
|
|
|
|
|
|
|
|
|
new Handler().postDelayed(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
backButtonPressed = false;
|
|
|
|
|
}
|
|
|
|
|
}, 2000);
|
|
|
|
|
}
|
2014-08-06 13:18:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
public static int getUID(){
|
|
|
|
|
return uid;
|
|
|
|
|
}
|
|
|
|
|
public static Context getContext(){return context;}
|
|
|
|
|
|
2014-07-31 15:01:44 +02:00
|
|
|
}
|