package com.epson.mobilephone.common.wifidirect;

import android.annotation.TargetApi;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;

import com.google.api.client.http.HttpMethods;
import com.google.common.net.HttpHeaders;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.opencv.videoio.Videoio;

@TargetApi(21)
public class ManageDefaultNetwork {
    private static final String CHECKURL = "http://epson.com";
    private static final String TAG = "ManageDefaultNetwork";
    private static final int TIMEOUT_CHECKURL = 5000;
    private static ManageDefaultNetwork manageDefaultNetwork;
    volatile boolean canConnected = false;
    private String checkedSSID = null;
    private ConnectivityManager connectivityManager = null;
    private boolean isOnline = false;
    private volatile Object lock = new Object();
    private Context mContext = null;
    private ManageDefaultNetworkCallback manageDefaultNetworkCallback = null;
    private WifiManager wifiManager = null;

    private ManageDefaultNetwork(Context context) {
        mContext = context.getApplicationContext();
        connectivityManager = (ConnectivityManager) mContext.getSystemService(Service.CONNECTIVITY_SERVICE);
        wifiManager = (WifiManager) mContext.getSystemService(Service.WIFI_SERVICE);
    }

    public static ManageDefaultNetwork getInstance(Context context) {
        ManageDefaultNetwork manageDefaultNetwork2 = manageDefaultNetwork;
        if (manageDefaultNetwork2 != null) {
            return manageDefaultNetwork2;
        }
        manageDefaultNetwork = new ManageDefaultNetwork(context);
        return manageDefaultNetwork;
    }

    private synchronized boolean checkCurSSIDisOnline(String str) {
        boolean z;
        Network activeNetwork;
        NetworkCapabilities networkCapabilities;
        canConnected = false;
        if (WiFiControl.isSimpleAP(str)) {
            return false;
        }
        if (Build.VERSION.SDK_INT < 23 || (activeNetwork = connectivityManager.getActiveNetwork()) == null || (networkCapabilities = connectivityManager.getNetworkCapabilities(activeNetwork)) == null || !networkCapabilities.hasTransport(0)) {
            synchronized (this.lock) {
                EPLog.d(TAG, "Checking whether isOnline : " + str);
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(ManageDefaultNetwork.CHECKURL).openConnection();
                            httpURLConnection.setRequestMethod(HttpMethods.HEAD);
                            httpURLConnection.setUseCaches(false);
                            httpURLConnection.setRequestProperty(HttpHeaders.CONNECTION, "close");
                            httpURLConnection.setConnectTimeout(5000);
                            httpURLConnection.connect();
                            int responseCode = httpURLConnection.getResponseCode();
                            EPLog.d(ManageDefaultNetwork.TAG, "getResponseCode() : " + responseCode);
                            httpURLConnection.disconnect();
                            if (responseCode > 0) {
                                ManageDefaultNetwork.this.canConnected = true;
                            }
                        } catch (IOException unused) {
                            EPLog.e(ManageDefaultNetwork.TAG, "HttpURLConnection failed");
                            ManageDefaultNetwork.this.canConnected = false;
                        }
                        synchronized (ManageDefaultNetwork.this.lock) {
                            try {
                                ManageDefaultNetwork.this.lock.notifyAll();
                            } catch (IllegalMonitorStateException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
                try {
                    lock.wait(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                z = canConnected;
            }
            return z;
        }
        EPLog.d(TAG, "WiFi connected, But default network is cellular. " + str + "is offline.");
        return false;
    }

    public synchronized void setDefaultNetworkSimpleAp() {
        if (Build.VERSION.SDK_INT >= 21) {
            String curSSID = WiFiUtils.getInstance(mContext).getCurSSID();
            if (curSSID == null) {
                EPLog.d(TAG, "Not connected");
                return;
            }
            if (!curSSID.equals(this.checkedSSID)) {
                checkedSSID = curSSID;
                isOnline = checkCurSSIDisOnline(curSSID);
                if (this.isOnline) {
                    EPLog.d(TAG, curSSID + "is online.");
                    return;
                }
            } else if (this.isOnline) {
                EPLog.d(TAG, curSSID + "is online.");
                return;
            }
            if (Build.VERSION.SDK_INT >= 23) {
                if (this.connectivityManager.getBoundNetworkForProcess() != null) {
                    EPLog.d(TAG, "Already bindProcessToNetwork called");
                    return;
                }
            } else if (Build.VERSION.SDK_INT >= 21) {
                ConnectivityManager connectivityManager2 = connectivityManager;
                if (ConnectivityManager.getProcessDefaultNetwork() != null) {
                    EPLog.d(TAG, "Already setProcessDefaultNetwork called");
                    return;
                }
            } else {
                return;
            }
            NetworkRequest.Builder builder = new NetworkRequest.Builder();
            builder.removeCapability(12);
            builder.addTransportType(1);
            try {
                if (manageDefaultNetworkCallback == null) {
                    manageDefaultNetworkCallback = new ManageDefaultNetworkCallback();
                }
                connectivityManager.requestNetwork(builder.build(), manageDefaultNetworkCallback);
            } catch (SecurityException e) {
                e.printStackTrace();
                if (Build.VERSION.SDK_INT >= 23) {
                    try {
                        if (!Settings.System.canWrite(mContext)) {
                            Intent intent = new Intent("android.settings.action.MANAGE_WRITE_SETTINGS");
                            intent.setFlags(Videoio.CAP_INTELPERC_IMAGE_GENERATOR);
                            intent.setData(Uri.parse("package:" + mContext.getPackageName()));
                            mContext.startActivity(intent);
                            return;
                        }
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                }
            } catch (Exception e3) {
                e3.printStackTrace();
            }
        } else {
            return;
        }
        return;
    }

    public synchronized void resetDefaultNetwork() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (this.connectivityManager.getBoundNetworkForProcess() != null) {
                EPLog.d(TAG, "resetProcessDefaultNetwork");
                connectivityManager.bindProcessToNetwork((Network) null);
            }
        } else if (Build.VERSION.SDK_INT >= 21) {
            ConnectivityManager connectivityManager2 = connectivityManager;
            if (ConnectivityManager.getProcessDefaultNetwork() != null) {
                EPLog.d(TAG, "resetProcessDefaultNetwork");
                ConnectivityManager connectivityManager3 = connectivityManager;
                ConnectivityManager.setProcessDefaultNetwork((Network) null);
            }
        } else {
            return;
        }
        try {
            if (manageDefaultNetworkCallback != null) {
                connectivityManager.unregisterNetworkCallback(manageDefaultNetworkCallback);
                manageDefaultNetworkCallback = null;
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

    class ManageDefaultNetworkCallback extends ConnectivityManager.NetworkCallback {
        ManageDefaultNetworkCallback() {
        }

        public void onAvailable(Network network) {
            EPLog.d(ManageDefaultNetwork.TAG, "Call onAvailable");
            super.onAvailable(network);
            if (Build.VERSION.SDK_INT < 23) {
                ConnectivityManager unused = ManageDefaultNetwork.this.connectivityManager;
                if (ConnectivityManager.getProcessDefaultNetwork() == null) {
                    ConnectivityManager unused2 = ManageDefaultNetwork.this.connectivityManager;
                    if (ConnectivityManager.setProcessDefaultNetwork(network)) {
                        EPLog.d(ManageDefaultNetwork.TAG, "setProcessDefaultNetwork");
                    }
                }
            } else if (ManageDefaultNetwork.this.connectivityManager.getBoundNetworkForProcess() == null && ManageDefaultNetwork.this.connectivityManager.bindProcessToNetwork(network)) {
                EPLog.d(ManageDefaultNetwork.TAG, "bindProcessToNetwork");
            }
        }

        public void onLost(Network network) {
            EPLog.d(ManageDefaultNetwork.TAG, "Call onLost");
            super.onLost(network);
            ManageDefaultNetwork.this.resetDefaultNetwork();
        }
    }
}
