package anywheresoftware.b4a.jalle007.wificonnect;

package anywheresoftware.b4a.jalle007.wificonnect;

import java.util.List;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

@Author("jalle007")
@Version(1.2f)
@ShortName("WiFiConnect")

public class WiFiConnect
{
	//static int netId;
	
	// 1.0 initial release
	// 1.1 added test for no networks found and added IsEnabled 
   // 1.2 22/03/2015 edited from maxer73 added javadocs and these methods: 
	               // DisconnectFromSSID
	               // getWifiState
	               // getConfiguredNetworks
	               // setWifiEnabled
	               // getWifiEnabled
	
	private final static double version = 1.2;	
	
	/**
	 *Returns the version number of the library.
	 */
	public double getVersion()
	{
		return version;
	}
	
	//WiFi Encrypt
	public static int WIFI_OPEN = 0;
	public static int WIFI_WEP = 1;
	public static int WIFI_WPA = 2;

	//WiFi State
	public static int WIFI_STATE_UNKNOWN = -1;
	public static int WIFI_STATE_DISABLING = 0;
	public static int WIFI_STATE_DISABLED = 1;
	public static int WIFI_STATE_ENABLING = 2;
	public static int WIFI_STATE_ENABLED = 3;
	public static int WIFI_STATE_FAILED = 4;
	
	/**
	 * Connect to a specific SSID.
	 * 
	 * wifi_security: Securyty type (use costants)
	 * networkSSID: The SSID of the network to which we want to connect
	 * networkPass: The password of the network to which we want to connect
	 */
	public void ConnectToSSID(BA ba, int wifi_security, String networkSSID, String networkPass) throws Exception
	{	
		WifiConfiguration conf = new WifiConfiguration();
		conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String should contain ssid in quotes
                                          
		//tip = Integer.parseInt(txtTip.toString());

		switch (wifi_security) {
		case 0: // OPEN
			conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

		case 1: // WEP
			boolean isHex = networkPass.matches("^[0-9a-fA-F]+$");
			if (isHex == true) conf.wepKeys[0] = networkPass;    //In case of WEP, if your password is in hex, you do not need to surround it with quotes.
			else conf.wepKeys[0] = "\"" + networkPass + "\"";    
			conf.wepTxKeyIndex = 0;
			conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
			conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
			break;

		case 2: // WPA
			conf.preSharedKey = "\"" + networkPass + "\"";   

		default:
			break;
		}

		WifiManager wifiManager = (WifiManager) ba.context.getSystemService(Context.WIFI_SERVICE);
		wifiManager.addNetwork(conf);
		//add Network and remember id that we use for disconnect
		//netId = wifiManager.addNetwork(conf);
		
		// CONNECT
		List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
		if (list == null)
		{
			throw new Exception("No configured networks found!");
		}
		for (WifiConfiguration i : list)
		{
			if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\""))
			{
				wifiManager.disconnect();
				wifiManager.enableNetwork(i.networkId, true);
				wifiManager.reconnect();

				break;
			}
		}
	}
	
	/**
	 * Disconnect from a specific SSID.
	 * 
	 * networkSSID: The SSID of the network to which we want to disconnect
	 */
	public void DisconnectFromSSID(BA ba, String networkSSID) throws Exception
	{	
		WifiConfiguration conf = new WifiConfiguration();    //// see this line
		
		WifiManager wifiManager = (WifiManager) ba.context.getSystemService(Context.WIFI_SERVICE);
		conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String should contain ssid in quotes
	   wifiManager.removeNetwork(conf.networkId);
	   wifiManager.disconnect();
	   wifiManager.disableNetwork(conf.networkId);
	}
	
	/** 
	 * Gets a list with configured networks.
	 */
	public List<WifiConfiguration> getConfiguredNetworks(BA ba)
	{
		WifiManager wifiManager = (WifiManager) ba.context.getSystemService(Context.WIFI_SERVICE);
		 List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
		 return list;
	}
	
   /**
    * Sets the WiFi Enabled state.
    * 
    * enabled: set WiFi Enabled (set True to enable or False do disable)
    */
	public void setWifiEnabled(BA ba, boolean enabled)
		{		
			
			WifiManager wifiManager = (WifiManager) ba.context.getSystemService(Context.WIFI_SERVICE);
			wifiManager.setWifiEnabled(enabled);
		}
	
   /**
    * Gets the WiFi Enabled state.
    * 
    * return True if enabled or False if disabled
    */
	public boolean getWifiEnabled(BA ba)
		{		
			WifiManager wifiManager = (WifiManager) ba.context.getSystemService(Context.WIFI_SERVICE);
			return wifiManager.isWifiEnabled();
		}
	
   /**
    * Gets the WiFi State (see costants)
    * 
    * Return the WiFi state
    */
	public int getWifiState(BA ba)
		{		
			WifiManager wifiManager = (WifiManager) ba.context.getSystemService(Context.WIFI_SERVICE);
			return wifiManager.getWifiState();
		}
	
}

