package com.samples.network.wifiinfo;

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

public class WiFiChangeActivity extends Activity 
	implements View.OnClickListener {
 
	private TextView text;
	private WifiManager manager;
	
	private BroadcastReceiver receiver = new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent) {
			int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
					WifiManager.WIFI_STATE_UNKNOWN);
			text.append("\n\t");
			
			switch(wifiState){
			case WifiManager.WIFI_STATE_ENABLING:
				text.append("Wi-Fi state enabling");
				break;
			case WifiManager.WIFI_STATE_ENABLED:
				text.append("Wi-Fi state enabled");
				break;
			case WifiManager.WIFI_STATE_DISABLING:
				text.append("Wi-Fi state disabling");
				break;
			case WifiManager.WIFI_STATE_DISABLED:
				text.append("Wi-Fi state disabled");
				break;
			case WifiManager.WIFI_STATE_UNKNOWN:
				text.append("Wi-Fi state unknown");
				break;
			default:
				text.append("Not defined");
				break;
			}  
		}
	};
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		text = (TextView)findViewById(R.id.text);
		
		Button bEnable = (Button)findViewById(R.id.bEnable);
		Button bDisable = (Button)findViewById(R.id.bDisable);
      
		text.append("Current Wi-Fi state:");
		bEnable.setOnClickListener(this);
		bDisable.setOnClickListener(this);
		
		manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
		
		this.registerReceiver(this.receiver,
				new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
      
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bEnable:
			manager.setWifiEnabled(true);	
			text.append("\nStart enable Wi-Fi...");
			break;
		case R.id.bDisable:
			manager.setWifiEnabled(false);
			text.append("\nStart disable Wi-Fi...");
			break;
		}	
	}
}