package com.mogo.cloud.wifi;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import com.elegant.analytics.utils.Logger;


public class WifiBroadCastReceiver extends BroadcastReceiver {

    public WifiBroadCastReceiver() {
        Logger.d(TAG, "WifiBroadCastReceiver init");
    }

    private static final String TAG = "WifiBroadCastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Logger.d(TAG, "onReceive 接收intent action : " + intent.getAction());
        int wifiState = intent.getIntExtra("wifi_state", 0);
        WifiManager wifiManager = ((WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE));
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if (wifiInfo != null) {
            Logger.d(TAG, "onReceive wifiInfo, update wifiState");
            WifiStateManager.getInstance().updateWifiState(wifiInfo);

            int linkSpeed = wifiInfo.getLinkSpeed();
            Logger.d(TAG, "wifi linkSpeed : " + linkSpeed); //wifi连接速度
            //0到-50表示信号最好，-50到-70表示信号偏差，小于-70表示最差，有可能连接不上或者掉线
            int level = Math.abs(wifiInfo.getRssi());
            Logger.d(TAG, "wifi level : " + level); //wifi信号强度

            switch (wifiState) {
                case WifiManager.WIFI_STATE_DISABLING:
                    Logger.d(TAG, "WIFI State : DISABLING");
                    break;
                case WifiManager.WIFI_STATE_DISABLED:
                    Logger.d(TAG, "WIFI State : DISABLED");
                    break;
                case WifiManager.WIFI_STATE_ENABLING:
                    Logger.d(TAG, "WIFI State : ENABLING");
                    break;
                case WifiManager.WIFI_STATE_ENABLED:
                    Logger.d(TAG, "WIFI State : ENABLED");
                    break;
                case WifiManager.WIFI_STATE_UNKNOWN:
                    Logger.d(TAG, "WIFI State : UNKNOWN");
                    break;
            }
        }
    }
}
