Commit 964828a0 authored by Haejoong Lee's avatar Haejoong Lee

simplified the UI for http server activity

parent f05a06ba
......@@ -8,28 +8,8 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/http_server_activity_interface"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner_ifs"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" >
</Spinner>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
android:onClick="showNetworkInterfaceDialog" >
<TextView
android:id="@+id/textView3"
......@@ -72,15 +52,27 @@
</LinearLayout>
<ToggleButton
android:id="@+id/toggle_server_status"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="@+id/button_start_http"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="ToggleButton"
android:textOff="@string/http_server_activity_stopped"
android:textOn="@string/http_server_activity_running"
android:onClick="toggleServerStatus" />
android:text="@string/http_ui_start"
android:onClick="startServer" />
<Button
android:id="@+id/button_stop_http"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:onClick="stopServer"
android:text="@string/http_ui_stop" />
</LinearLayout>
<TextView
android:id="@+id/text_server_log"
......
......@@ -41,8 +41,7 @@
<string name="http_server_activity_server_status">Server status</string>
<string name="http_server_activity_host">Host</string>
<string name="http_server_activity_port">Port</string>
<string name="http_server_activity_running">Running</string>
<string name="http_server_activity_stopped">Stopped</string>
<string name="http_server_activity_interface">Network interface</string>
<string name="http_server_activity_ip">IP address</string>
<string name="http_ui_start">START</string>
<string name="http_ui_stop">STOP</string>
</resources>
......@@ -8,7 +8,7 @@ import org.lp20.aikuma.http.NanoHTTPD.IHTTPSession;
import org.lp20.aikuma.http.NanoHTTPD.Response;
/**
* A chain of HTTP request processors.
* Allows to create a chain of HTTP request processors.
*
* @author haejoong
*/
......
package org.lp20.aikuma.ui;
import java.util.Map;
import org.lp20.aikuma.R;
import org.lp20.aikuma.http.Server;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;
public class HttpServerActivity extends AikumaActivity {
private String hostname;
private int port;
private String selected_device;
private TextView log;
@Override
......@@ -35,25 +30,12 @@ public class HttpServerActivity extends AikumaActivity {
Server.setAssetManager(getAssets());
Spinner hosts = (Spinner) findViewById(R.id.spinner_ifs);
hosts.removeAllViewsInLayout();
final Map<String,String> addrs = Server.getIpAddresses();
String[] ifs = addrs.keySet().toArray(new String[0]);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.simple_spinner_item, ifs);
hosts.setAdapter(aa);
hosts.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View view, int pos, long id) {
TextView t = (TextView) view;
hostname = addrs.get(t.getText());
TextView ip = (TextView) findViewById(R.id.ip_address);
ip.setText(hostname);
// initially set the ip address field
Map<String,String> addrs = Server.getIpAddresses();
if (addrs.size() > 0) {
selected_device = (String) addrs.keySet().toArray()[0];
((TextView) findViewById(R.id.ip_address)).setText(addrs.get(selected_device));
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
TextView t = (TextView) findViewById(R.id.text_port);
t.addTextChangedListener(new TextWatcher() {
......@@ -70,30 +52,64 @@ public class HttpServerActivity extends AikumaActivity {
});
}
public void toggleServerStatus(View view) {
if (((ToggleButton) view).isChecked()) {
public void showNetworkInterfaceDialog(View view) {
Map<String,String> addrs = Server.getIpAddresses();
CharSequence[] menuitems = new CharSequence[addrs.size()];
final String[] devices = new String[addrs.size()];
final CharSequence[] ipaddrs = new CharSequence[addrs.size()];
int count = 0;
int checked = 0;
for (String dev : addrs.keySet()) {
devices[count] = dev;
ipaddrs[count] = addrs.get(dev);
menuitems[count] = addrs.get(dev) + " (" + dev + ")";
if (dev.equals(selected_device))
checked = count;
count++;
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select a network interface");
builder.setSingleChoiceItems(menuitems, checked, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selected_device = devices[which];
((TextView) findViewById(R.id.ip_address)).setText(ipaddrs[which]);
dialog.dismiss();
}
});
builder.show();
}
public void startServer(View view) {
String msg;
Boolean success;
Resources res = this.getResources();
try {
Server.setHost(hostname);
Server.setPort(port);
Server.getServer().start();
msg = String.format(res.getString(R.string.http_dialog_success, port));
success = true;
}
catch (java.io.IOException e) {
((ToggleButton) view).setChecked(false);
success = false;
msg = res.getString(R.string.http_dialog_failure);
log.append("error: ");
log.append(e.getMessage());
log.append("\n");
}
view.setEnabled(!success);
findViewById(R.id.button_stop_http).setEnabled(success);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.http_dialog_title).setMessage(msg);
AlertDialog dialog = builder.create();
dialog.show();
}
else {
public void stopServer(View view) {
Server.getServer().stop();
}
findViewById(R.id.button_start_http).setEnabled(true);
view.setEnabled(false);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment