/**
 * Copyright 2015 Daniel Martí
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cc.mvdan.libaccesspoint;

import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

import java.lang.reflect.Method;

public class WifiControl {

	private static Method getWifiApConfiguration;
	private static Method getWifiApState;
	private static Method isWifiApEnabled;
	private static Method setWifiApEnabled;

	static {
		for (Method method : WifiManager.class.getDeclaredMethods()) {
			String name = method.getName();
			if (name.equals("getWifiApConfiguration")) {
				getWifiApConfiguration = method;
			} else if (name.equals("getWifiApState")) {
				getWifiApState = method;
			} else if (name.equals("isWifiApEnabled")) {
				isWifiApEnabled = method;
			} else if (name.equals("setWifiApEnabled")) {
				setWifiApEnabled = method;
			}
		}
	}

	public static boolean isSoftwareSupported() {
		return (getWifiApState != null
				&& isWifiApEnabled != null
				&& setWifiApEnabled != null
				&& getWifiApConfiguration != null);
	}

	public static boolean isHardwareSupported() {
		// TODO: implement via native code
		return true;
	}

	public static boolean isSupported() {
		return isSoftwareSupported() && isHardwareSupported();
	}

	private final WifiManager wm;

	private WifiControl(WifiManager wm) {
		this.wm = wm;
	}

	public static WifiControl getApControl(WifiManager wm) {
		if (!isSupported()) {
			return null;
		}
		return new WifiControl(wm);
	}

	public boolean isWifiApEnabled() {
		try {
			return (Boolean) isWifiApEnabled.invoke(wm);
		} catch (Exception e) {
			// Ignored, we just return a zero value
		}
		return false;
	}

	public static int newStateNumber(int state) {
		// WifiManager's state constants were changed around Android 4.0
		if (state < 10) {
			return state + 10;
		}
		return state;
	}

	public int getWifiApState() {
		try {
			return newStateNumber((Integer) getWifiApState.invoke(wm));
		} catch (Exception e) {
			// Ignored, we just return a zero value
		}
		return -1;
	}

	public WifiConfiguration getWifiApConfiguration() {
		try {
			return (WifiConfiguration) getWifiApConfiguration.invoke(wm);
		} catch (Exception e) {
			// Ignored, we just return a zero value
		}
		return null;
	}

	public boolean setWifiApEnabled(WifiConfiguration config, boolean enabled) {
		try {
			return (Boolean) setWifiApEnabled.invoke(wm, config, enabled);
		} catch (Exception e) {
			// Ignored, we just return a zero value
		}
		return false;
	}

	public boolean enable() {
		return setWifiApEnabled(getWifiApConfiguration(), true);
	}

	public boolean disable() {
		return setWifiApEnabled(null, false);
	}
}

@@ -741,16 +741,142 @@
 .Method;
+%0Aimport java.net.InetAddress;%0Aimport java.net.NetworkInterface;%0Aimport java.net.SocketException;%0Aimport java.util.Enumeration;
 %0A%0Apublic
@@ -3304,11 +3304,719 @@
 lse);%0A%09%7D
+%0A%0A%09public static InetAddress getInetAddress() %7B%0A%09%09InetAddress resAddr = null;%0A%0A%09%09try %7B%0A%09%09%09Enumeration%3CNetworkInterface%3E ifaces = NetworkInterface.getNetworkInterfaces();%0A%09%09%09while (ifaces.hasMoreElements()) %7B%0A%09%09%09%09NetworkInterface iface = ifaces.nextElement();%0A%0A%09%09%09%09Enumeration%3CInetAddress%3E addrs = iface.getInetAddresses();%0A%09%09%09%09while (addrs.hasMoreElements()) %7B%0A%09%09%09%09%09InetAddress addr = addrs.nextElement();%0A%0A%09%09%09%09%09if (addr.isLoopbackAddress()) %7B%0A%09%09%09%09%09       continue;%0A%09%09%09%09%09%7D%0A%0A%09%09%09%09%09final String ifaceName = iface.getDisplayName();%0A%09%09%09%09%09if (ifaceName.contains(%22wlan0%22)) %7B%0A%09%09%09%09%09%09resAddr = addr;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%0A%09%09%7D catch (SocketException e) %7B%0A%09%09%09// Ignored, we just return a zero value%0A%09%09%7D%0A%09%09return resAddr;%0A%09%7D
 %0A%7D%0A

Add getInetAddress. Fixes #1