package com.samples.network.scanwifi;

import java.util.List;

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

public class WiFiScanActivity extends Activity
		implements OnCheckedChangeListener, OnClickListener {
	
	private TextView text;
	private CheckBox cbEnable;
	private Button bStart;
	
	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);
			
			switch(wifiState){
			case WifiManager.WIFI_STATE_ENABLING:
				text.setText("Wi-Fi state enabling");
				break;
			case WifiManager.WIFI_STATE_ENABLED:
				text.setText("Wi-Fi state enabled.");				
				break;
			case WifiManager.WIFI_STATE_DISABLING:
				text.setText("Wi-Fi state disabling");
				break;
			case WifiManager.WIFI_STATE_DISABLED:
				text.setText("Wi-Fi state disabled");
				break;
			case WifiManager.WIFI_STATE_UNKNOWN:
				text.setText("Wi-Fi state unknown");
				break;
			} 	
		}
	};
	
	private BroadcastReceiver scanReceiver = new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent) {
			List<ScanResult> results = manager.getScanResults(); 
			text.setText("Scan result: " + results.size() + " points");
			
			for (ScanResult result : results) {
				text.append("\nSS ID:\t" + result.SSID);
				text.append("\n\tLevel:\t" + result.level + " dBm");
				text.append("\n\tFrequency:\t" + result.frequency + " MHz");
				text.append("\n\tCapabilities:\t" + result.capabilities);
			}
		}
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        text = (TextView)findViewById(R.id.text);
        cbEnable = (CheckBox)findViewById(R.id.cbEnable);
        bStart = (Button)findViewById(R.id.bStart);
        
		manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
		
		this.registerReceiver(this.receiver,
				new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
		
		cbEnable.setChecked(manager.isWifiEnabled());	
        cbEnable.setOnCheckedChangeListener(this);
        bStart.setOnClickListener(this);
    }
    
	@Override
	public void onCheckedChanged(CompoundButton view, boolean isChecked) {
		manager.setWifiEnabled(isChecked);
	}  
	
	@Override
	public void onClick(View arg0) {
		this.registerReceiver(this.scanReceiver,
			new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        manager.startScan();
	}
}