blob: 2981c9e73c5d7239cc98968baac18f299d5e6631 [file] [log] [blame]
package com.projectara.araepm;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;
import android.os.Handler;
import android.util.Log;
public class MainActivity extends Activity
implements EpmController.EpmChangeCallback {
private static final String TAG = "araepm";
private static String EPM_ATTACHED;
private static String EPM_DETACHED;
private static String EPM_ATTACHING;
private static String EPM_DETACHING;
private static String EPM_STATUS_OK;
private static String EPM_STATUS_ERROR;
private static String EPM_STATUS_TIMEOUT;
private TextView statusText;
private EditText portNumber;
private Handler handler;
private Button[] buttons;
private EpmController epmController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
portNumber = (EditText)findViewById(R.id.portNumber);
statusText = (TextView)findViewById(R.id.statusText);
EPM_ATTACHED = getString(R.string.epm_attached);
EPM_DETACHED = getString(R.string.epm_detached);
EPM_ATTACHING = getString(R.string.epm_attaching);
EPM_DETACHING = getString(R.string.epm_detaching);
EPM_STATUS_OK = getString(R.string.epm_status_ok);
EPM_STATUS_ERROR = getString(R.string.epm_status_error);
EPM_STATUS_TIMEOUT = getString(R.string.epm_status_timeout);
buttons = new Button[2];
buttons[0] = (Button)findViewById(R.id.attachButton);
buttons[1] = (Button)findViewById(R.id.detachButton);
handler = new Handler();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onStart(){
Log.d(TAG, "onstart");
super.onStart();
epmController = new EpmController(this, this);
epmController.start();
}
@Override
protected void onStop() {
Log.d(TAG, "onstop");
super.onStop();
epmController.stop();
epmController = null;
}
private void disableStateChanges() {
for (int i = 0; i < buttons.length; i++) {
buttons[i].setClickable(false);
}
}
private void enableStateChanges() {
for (int i = 0; i < buttons.length; i++) {
buttons[i].setClickable(true);
}
}
private int getPortNumber() {
String s = portNumber.getText().toString();
try {
return Integer.parseInt(s);
} catch (NumberFormatException nfe) {
// Can't happen; portNumber only allows numeric input
Log.wtf(TAG, "non-numeric text " + s + " in portNumber");
return -1;
}
}
private void doAttachDetach(boolean attach) {
int port = getPortNumber();
if (port < 0) {
return;
}
Log.d(TAG,
(attach ? "Attaching" : "Detaching") + " EPM on port " + port);
disableStateChanges();
if (attach) {
statusText.setText(String.format(EPM_ATTACHING, port));
epmController.attachEpm(port);
} else {
statusText.setText(String.format(EPM_DETACHING, port));
epmController.detachEpm(port);
}
}
public void attachEpm(View view) {
doAttachDetach(true);
}
public void detachEpm(View view) {
doAttachDetach(false);
}
@Override
public void onEpmChangeFinished(EpmController.EpmState attemptedState,
EpmController.EpmChangeStatus changeStatus,
int changedPort) {
final EpmController.EpmState attempted = attemptedState;
final EpmController.EpmChangeStatus status = changeStatus;
final int port = changedPort;
handler.post(new Runnable() {
@Override
public void run() {
String newStatus;
String s;
enableStateChanges();
switch (attempted) {
case EPM_ATTACH:
s = EPM_ATTACHED;
break;
case EPM_DETACH:
s = EPM_DETACHED;
break;
default:
Log.wtf(TAG, "can't happen");
return;
}
String statusFormat;
switch (status) {
case EPM_OK:
statusFormat = EPM_STATUS_OK;
break;
case EPM_ERROR:
statusFormat = EPM_STATUS_ERROR;
break;
case EPM_TIMEOUT:
statusFormat = EPM_STATUS_TIMEOUT;
break;
default:
Log.wtf(TAG, "can't happen");
return;
}
statusText.setText(String.format(statusFormat, s, port));
}
});
}
}