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

simplified the UI for http server activity

parent f05a06ba
...@@ -8,28 +8,8 @@ ...@@ -8,28 +8,8 @@
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" > android:layout_height="wrap_content"
android:onClick="showNetworkInterfaceDialog" >
<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" >
<TextView <TextView
android:id="@+id/textView3" android:id="@+id/textView3"
...@@ -72,15 +52,27 @@ ...@@ -72,15 +52,27 @@
</LinearLayout> </LinearLayout>
<ToggleButton <LinearLayout
android:id="@+id/toggle_server_status" android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:gravity="center_horizontal" >
android:text="ToggleButton"
android:textOff="@string/http_server_activity_stopped" <Button
android:textOn="@string/http_server_activity_running" android:id="@+id/button_start_http"
android:onClick="toggleServerStatus" /> android:layout_width="wrap_content"
android:layout_height="wrap_content"
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 <TextView
android:id="@+id/text_server_log" android:id="@+id/text_server_log"
......
...@@ -41,8 +41,7 @@ ...@@ -41,8 +41,7 @@
<string name="http_server_activity_server_status">Server status</string> <string name="http_server_activity_server_status">Server status</string>
<string name="http_server_activity_host">Host</string> <string name="http_server_activity_host">Host</string>
<string name="http_server_activity_port">Port</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_server_activity_ip">IP address</string>
<string name="http_ui_start">START</string>
<string name="http_ui_stop">STOP</string>
</resources> </resources>
...@@ -8,7 +8,7 @@ import org.lp20.aikuma.http.NanoHTTPD.IHTTPSession; ...@@ -8,7 +8,7 @@ import org.lp20.aikuma.http.NanoHTTPD.IHTTPSession;
import org.lp20.aikuma.http.NanoHTTPD.Response; import org.lp20.aikuma.http.NanoHTTPD.Response;
/** /**
* A chain of HTTP request processors. * Allows to create a chain of HTTP request processors.
* *
* @author haejoong * @author haejoong
*/ */
......
package org.lp20.aikuma.ui; package org.lp20.aikuma.ui;
import java.util.Map; import java.util.Map;
import org.lp20.aikuma.R; import org.lp20.aikuma.R;
import org.lp20.aikuma.http.Server; import org.lp20.aikuma.http.Server;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.View; 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.TextView;
import android.widget.ToggleButton;
public class HttpServerActivity extends AikumaActivity { public class HttpServerActivity extends AikumaActivity {
private String hostname; private String hostname;
private int port; private int port;
private String selected_device;
private TextView log; private TextView log;
@Override @Override
...@@ -35,25 +30,12 @@ public class HttpServerActivity extends AikumaActivity { ...@@ -35,25 +30,12 @@ public class HttpServerActivity extends AikumaActivity {
Server.setAssetManager(getAssets()); Server.setAssetManager(getAssets());
Spinner hosts = (Spinner) findViewById(R.id.spinner_ifs); // initially set the ip address field
hosts.removeAllViewsInLayout(); Map<String,String> addrs = Server.getIpAddresses();
final Map<String,String> addrs = Server.getIpAddresses(); if (addrs.size() > 0) {
String[] ifs = addrs.keySet().toArray(new String[0]); selected_device = (String) addrs.keySet().toArray()[0];
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.simple_spinner_item, ifs); ((TextView) findViewById(R.id.ip_address)).setText(addrs.get(selected_device));
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);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
TextView t = (TextView) findViewById(R.id.text_port); TextView t = (TextView) findViewById(R.id.text_port);
t.addTextChangedListener(new TextWatcher() { t.addTextChangedListener(new TextWatcher() {
...@@ -69,31 +51,65 @@ public class HttpServerActivity extends AikumaActivity { ...@@ -69,31 +51,65 @@ public class HttpServerActivity extends AikumaActivity {
} }
}); });
} }
public void toggleServerStatus(View view) { public void showNetworkInterfaceDialog(View view) {
if (((ToggleButton) view).isChecked()) { Map<String,String> addrs = Server.getIpAddresses();
String msg; CharSequence[] menuitems = new CharSequence[addrs.size()];
Resources res = this.getResources(); final String[] devices = new String[addrs.size()];
try { final CharSequence[] ipaddrs = new CharSequence[addrs.size()];
Server.setHost(hostname); int count = 0;
Server.setPort(port); int checked = 0;
Server.getServer().start(); for (String dev : addrs.keySet()) {
msg = String.format(res.getString(R.string.http_dialog_success, port)); devices[count] = dev;
} ipaddrs[count] = addrs.get(dev);
catch (java.io.IOException e) { menuitems[count] = addrs.get(dev) + " (" + dev + ")";
((ToggleButton) view).setChecked(false); if (dev.equals(selected_device))
msg = res.getString(R.string.http_dialog_failure); checked = count;
log.append("error: "); count++;
log.append(e.getMessage()); }
log.append("\n");
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();
} }
AlertDialog.Builder builder = new AlertDialog.Builder(this); });
builder.setTitle(R.string.http_dialog_title).setMessage(msg); builder.show();
AlertDialog dialog = builder.create(); }
dialog.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;
} }
else { catch (java.io.IOException e) {
Server.getServer().stop(); 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();
}
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