ue-control/app/src/main/java/com/ericsson/uecontrol/gui/MainActivity.java
2014-07-25 13:42:36 +02:00

147 lines
5.1 KiB
Java
Executable file

package com.ericsson.uecontrol.gui;
import android.app.ActionBar;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
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 java.util.ArrayList;
public class MainActivity extends FragmentActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment navigationDrawerFragment;
private StatusFragment statusFragment;
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence title;
private int currentExecutor;
private static ArrayList<UeControlExecutor> executors;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
statusFragment = (StatusFragment)
getFragmentManager().findFragmentById(R.id.status_fragment);
title = getTitle();
// Set up the drawer.
navigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
executors = new ArrayList<UeControlExecutor>();
currentExecutor = 0;
UeControlExecutor exec = new UeControlExecutor();
exec.addBehaviour(new UeBehaviourSleep(1000));
exec.addBehaviour(new UeBehaviourSurfing("http://google.com"));
exec.addBehaviour(new UeBehaviourSleep(4000));
exec.setThroughputListener(statusFragment.getThroughputListener());
executors.add(exec);
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, BehaviourListFragment.newInstance(position)).commit();
}
public void onSectionAttached(int number) {
currentExecutor = number;
switch (number) {
case 1:
title = getString(R.string.title_section1);
break;
case 2:
title = getString(R.string.title_section2);
break;
case 3:
title = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(title);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!navigationDrawerFragment.isDrawerOpen()) {
// 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);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@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) {
UeControlExecutor exec = executors.get(currentExecutor);
if(exec.isRunning()){
exec.terminate();
item.setTitle(R.string.action_run);
}
else {
exec.execute();
item.setTitle(R.string.action_stop);
}
return true;
}
if (id == R.id.action_edit) {
Intent intent = new Intent(this, EditActivity.class);
intent.putExtra("executor", currentExecutor);
startActivity(intent);
return true;
}
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
public static UeControlExecutor getExecutor(int index) {
if(index >= executors.size())
return null;
return executors.get(index);
}
}