package com.tencent.mm.plugin.freewifi;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Looper;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.plugin.freewifi.model.d;
import com.tencent.mm.sdk.platformtools.ab;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public final class b {
    Lock aCZ;
    private WifiManager bJj;
    private BroadcastReceiver broadcastReceiver;
    Condition cbS;
    private Context context;
    private long ftP = 15000;
    boolean mtC = false;
    boolean mtD = false;
    String ssid;

    public b(String str, Context context) {
        AppMethodBeat.i(20561);
        this.ssid = str;
        this.aCZ = new ReentrantLock();
        this.cbS = this.aCZ.newCondition();
        this.bJj = (WifiManager) context.getSystemService("wifi");
        this.context = context;
        AppMethodBeat.o(20561);
    }

    public final int byd() {
        AppMethodBeat.i(20562);
        if (((ConnectivityManager) this.context.getSystemService("connectivity")).getNetworkInfo(1).isConnected() && this.ssid.equals(d.byO())) {
            AppMethodBeat.o(20562);
            return 0;
        } else if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            RuntimeException runtimeException = new RuntimeException("ConnectNetworkHelper组件不能在主线程中运行。");
            AppMethodBeat.o(20562);
            throw runtimeException;
        } else {
            this.broadcastReceiver = new 1(this);
            try {
                int bye;
                this.aCZ.lock();
                IntentFilter intentFilter = new IntentFilter();
                intentFilter.addAction("android.net.wifi.STATE_CHANGE");
                intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
                this.context.registerReceiver(this.broadcastReceiver, intentFilter);
                if (!this.bJj.isWifiEnabled()) {
                    bye = new f(this.context).bye();
                    ab.i("MicroMsg.FreeWifi.ConnectNetworkHelper", "enable ret = ".concat(String.valueOf(bye)));
                    if (bye != 0) {
                        byc();
                        this.aCZ.unlock();
                        AppMethodBeat.o(20562);
                        return bye;
                    }
                }
                bye = d.ME(this.ssid);
                if (bye != 0) {
                    byc();
                    byc();
                    this.aCZ.unlock();
                    AppMethodBeat.o(20562);
                    return bye;
                }
                boolean z = false;
                while (true) {
                    if (!this.mtC || !this.mtD) {
                        long currentTimeMillis = System.currentTimeMillis();
                        z = this.cbS.await(this.ftP, TimeUnit.MILLISECONDS);
                        if (!z) {
                            break;
                        }
                        currentTimeMillis = System.currentTimeMillis() - currentTimeMillis;
                        this.ftP -= currentTimeMillis;
                        ab.i("MicroMsg.FreeWifi.ConnectNetworkHelper", "costMillis=" + currentTimeMillis + "; left timeout=" + this.ftP);
                        this.ftP = this.ftP > 0 ? this.ftP : 3000;
                    } else {
                        break;
                    }
                }
                if (z) {
                    byc();
                    this.aCZ.unlock();
                    AppMethodBeat.o(20562);
                    return 0;
                }
                byc();
                this.aCZ.unlock();
                AppMethodBeat.o(20562);
                return -16;
            } catch (InterruptedException e) {
                ab.i("MicroMsg.FreeWifi.ConnectNetworkHelper", "desc=ConnectNetworkHelper encounter interrupted exception. msg=%s", new Object[]{e.getMessage()});
                byc();
                this.aCZ.unlock();
                AppMethodBeat.o(20562);
                return -17;
            } catch (Throwable th) {
                byc();
                this.aCZ.unlock();
                AppMethodBeat.o(20562);
            }
        }
    }

    private void byc() {
        AppMethodBeat.i(20563);
        try {
            this.context.unregisterReceiver(this.broadcastReceiver);
            AppMethodBeat.o(20563);
        } catch (IllegalArgumentException e) {
            AppMethodBeat.o(20563);
        }
    }
}
