package com.epson.mobilephone.common.wifidirect;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;

import android.support.annotation.RequiresApi;

@RequiresApi(api = 21)
public class WiFiNetworkManager {
    private static final String TAG = "WiFiNetworkManager";
    private static WiFiNetworkManager instance;
    ConnectivityManager connectivityManager;
    Context context;

    private WiFiNetworkManager(@NonNull Context context2) {
        context = context2;
        connectivityManager = (ConnectivityManager) context2.getSystemService("connectivity");
    }

    public static WiFiNetworkManager getInsetance(@NonNull Context context2) {
        if (instance == null) {
            instance = new WiFiNetworkManager(context2);
        }
        return instance;
    }

    protected Network getWifiNetwork() {
        for (Network network : connectivityManager.getAllNetworks()) {
            if (this.connectivityManager.getNetworkCapabilities(network).hasTransport(1)) {
                return network;
            }
        }
        return null;
    }

    @RequiresApi(api = 23)
    public boolean isWiFiValidated() {
        Network wifiNetwork = getWifiNetwork();
        if (wifiNetwork != null) {
            NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(wifiNetwork);
            if (networkCapabilities.hasCapability(16)) {
                EPLog.m86d(TAG, "capabilities :" + networkCapabilities.toString());
                EPLog.m86d(TAG, "isWiFiValidated = OK : " + WiFiUtils.getInstance(this.context).getCurSSID());
                return true;
            }
        }
        EPLog.m86d(TAG, "isWiFiValidated = NG :" + WiFiUtils.getInstance(this.context).getCurSSID());
        return false;
    }
}
