package com.latinsud.wificonnect;


import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class WCActivity extends Activity {
	
	private static final String TAG="WifiConnect";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }   
    
    @Override
    public void onStart() {
    	super.onStart();
   new Thread(new Runnable() {
        public void run() {
        	try {
               	WifiManager wm;
               	wm=(WifiManager) getSystemService(Context.WIFI_SERVICE);
               	addText("");
               	
              	// Enable Wifi (if not enabled)
               	addText("- Enabling wifi...");
               	wm.setWifiEnabled(true);
               	addText("OK\n\n");
                    	
               	try {
               		Thread.sleep(500);
               	} catch (Exception e) { ; }	
               	
               	// Force disconnect of previously connected network (each 10 seconds)
               	addText("- Force disconnect...");
               	wm.disconnect();
               	addText("OK\n\n");
                    	
                // Wait until supplicant connects
               	addText("- Wait for supplicant connection...");
               	int i=0;
                while (wm.getConnectionInfo().getSupplicantState() != SupplicantState.COMPLETED) {
                	if ( i>100 ) {
                		addText("TIMEOUT! (please close and try again)");
                		return;
                	}
                		
                	
                	try {
                		Thread.sleep(100);
                		addText(".");
                	} catch (Exception e) { ; }
                	i++;
                }
                addText("OK\n\n");

                // Force dhcp relaunch
                addText("- Force DHCP restart...");
                ContentResolver cr = getContentResolver();
                int dhcpState=android.provider.Settings.System.getInt(cr, android.provider.Settings.System.WIFI_USE_STATIC_IP, 1);	           	 
                android.provider.Settings.System.putInt(cr, android.provider.Settings.System.WIFI_USE_STATIC_IP, 1-dhcpState);
                try {
                	Thread.sleep(100);
                } catch (Exception e) { ; }
                android.provider.Settings.System.putInt(cr, android.provider.Settings.System.WIFI_USE_STATIC_IP, dhcpState);
                addText("OK\n\n");
                addText("- FINISHED! (wait a few seconds for DHCP)");
        	} catch(Exception e) { ; }
        }
      }).start();
    }
   
    // Adds a string to the UI text.
   	public void addText(String aMsg) {
		runOnUiThread(new Runnable() {
			String msg;
			public void run() {
		        TextView t=(TextView)findViewById(R.id.Texto);
		        if (msg.equals(""))
		        	t.setText("");
		        else {
		        	t.append(msg);
		        	if (!msg.equals("."))
		        		Log.d(TAG,msg);
		        }
			}
			
			public Runnable setMsg(String aMsg2) {
				msg=aMsg2;
				return this;
			}
		}.setMsg(aMsg));   		
   	}

}