package com.samples.network.monitoringrssi;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MonitoringRssiActivity extends Activity
		implements OnCheckedChangeListener {
	
	private TextView text;
	private CheckBox cbEnable;
	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");
				startMonitoringRssi();
				break;
			case WifiManager.WIFI_STATE_DISABLING:
				text.setText("Wi-Fi state disabling");
				break;
			case WifiManager.WIFI_STATE_DISABLED:
				text.setText("Wi-Fi state disabled");
				stopMonitoringRssi();
				break;
			case WifiManager.WIFI_STATE_UNKNOWN:
				text.setText("Wi-Fi state unknown");
				break;
			} 	
		}
	};
	
	private BroadcastReceiver rssiReceiver = new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent) {
			WifiInfo info = manager.getConnectionInfo();

			text.append("\nChange signal in " + info.getSSID());
			text.append("\n\tSignal level:\t" + 
					WifiManager.calculateSignalLevel(info.getRssi(), 5));
			text.append("\n\tLink speed:\t" + info.getLinkSpeed() + 
					" " + WifiInfo.LINK_SPEED_UNITS);
		}
	};
	
    @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);
        
		manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
		
		
		this.registerReceiver(this.receiver,
				new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
		
		cbEnable.setChecked(manager.isWifiEnabled());	
        cbEnable.setOnCheckedChangeListener(this);
    }
    
	@Override
	public void onCheckedChanged(CompoundButton view, boolean isChecked) {
		manager.setWifiEnabled(isChecked);
	}  
	
	private void startMonitoringRssi() {
		this.registerReceiver(rssiReceiver,
				new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
	}
	
	private void stopMonitoringRssi() {
		if (this.rssiReceiver.isInitialStickyBroadcast())
			this.unregisterReceiver(rssiReceiver);
	}
}