package cc.mvdan.libaccesspointexample;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.net.InetAddress;

import cc.mvdan.libaccesspoint.WifiApControl;

public class MainActivity extends Activity {

	private WifiApControl apControl;
	private WifiManager wifiManager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		apControl = WifiApControl.getApControl(wifiManager);

		new Thread() {
			@Override
			public void run() {
				try {
					while (!isInterrupted()) {
						Thread.sleep(1000);
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								refresh();
							}
						});
					}
				} catch (InterruptedException e) {
				}
			}
		}.start();
	}

	@Override
	public void onResume() {
		super.onResume();
		refresh();
	}

	private static String stateString(final int state) {
		switch (state) {
			case WifiApControl.STATE_FAILED:
				return "FAILED";
			case WifiApControl.STATE_DISABLED:
				return "DISABLED";
			case WifiApControl.STATE_DISABLING:
				return "DISABLING";
			case WifiApControl.STATE_ENABLED:
				return "ENABLED";
			case WifiApControl.STATE_ENABLING:
				return "ENABLING";
			default:
				return "UNKNOWN!";
		}
	}

	private void refresh() {
		final TextView tv = (TextView) findViewById(R.id.text1);
		final StringBuilder sb = new StringBuilder();
		if (!WifiApControl.isSupported()) {
			sb.append("Warning: Wifi AP mode not supported!\n");
			sb.append("You should get unknown or zero values below.\n");
			sb.append("If you don't, isSupported() is probably buggy!\n");
		}
		final int state = apControl.getState();
		sb.append("State: ").append(stateString(state)).append('\n');
		sb.append("Enabled: ");
		if (apControl.isEnabled()) {
			sb.append("YES\n");
		} else {
			sb.append("NO\n");
		}
		final WifiConfiguration config = apControl.getConfiguration();
		sb.append("WifiConfiguration:");
		if (config == null) {
			sb.append(" null\n");
		} else {
			sb.append("\n");
			sb.append("   SSID: \"").append(config.SSID).append("\"\n");
			sb.append("   preSharedKey: \"").append(config.preSharedKey).append("\"\n");
		}
		final InetAddress addr = WifiApControl.getInetAddress();
		sb.append("InetAddress: ");
		if (addr == null) {
			sb.append("null");
		} else {
			sb.append(addr.toString());
		}
		tv.setText(sb.toString());
	}

	public void refresh(final View view) {
		refresh();
	}

	public void enable() {
		wifiManager.setWifiEnabled(false);
		apControl.enable();
		refresh();
	}

	public void enable(final View view) {
		final Button button = (Button) view;
		button.setEnabled(false);
		new Thread() {
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							button.setEnabled(true);
						}
					});
				} catch (InterruptedException e) {
				}
			}
		}.start();
		enable();
	}

	public void disable() {
		apControl.disable();
		wifiManager.setWifiEnabled(true);
		refresh();
	}

	public void disable(final View view) {
		final Button button = (Button) view;
		button.setEnabled(false);
		new Thread() {
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							button.setEnabled(true);
						}
					});
				} catch (InterruptedException e) {
				}
			}
		}.start();
		disable();
	}
}

@@ -318,16 +318,39 @@
 Address;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -2610,16 +2610,18 @@
 nd(%22null
+%5Cn
 %22);%0A%09%09%7D 
@@ -2656,16 +2656,475 @@
 tring())
+.append('%5Cn');%0A%09%09%7D%0A%09%09final List%3CWifiApControl.Client%3E clients = apControl.getClients();%0A%09%09sb.append(%22Clients: %22);%0A%09%09if (clients == null) %7B%0A%09%09%09sb.append(%22null%5Cn%22);%0A%09%09%7D else if (clients.size() == 0) %7B%0A%09%09%09sb.append(%22none%5Cn%22);%0A%09%09%7D else %7B%0A%09%09%09sb.append('%5Cn');%0A%09%09%09for (final WifiApControl.Client c : clients) %7B%0A%09%09%09%09sb.append(%22   %22).append(c.IPAddr);%0A%09%09%09%09sb.append(%22 %22).append(c.HWAddr).append('%5Cn');%0A%09%09%09%7D%0A%09%09%7D%0A%09%09if (sb.length() %3E 0) %7B%0A%09%09%09sb.setLength(sb.length() - 1)
 ;%0A%09%09%7D%0A%09%09

Add list of connected devices to the example app