package com.tencent.mm.plugin.exdevice.ui;

import android.content.Intent;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.Bundle;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.bp.d;
import com.tencent.mm.model.aw;
import com.tencent.mm.model.c;
import com.tencent.mm.network.n;
import com.tencent.mm.plugin.exdevice.d.b;
import com.tencent.mm.plugin.exdevice.jni.Java2CExDevice;
import com.tencent.mm.plugin.exdevice.model.j;
import com.tencent.mm.plugin.exdevice.model.j.a;
import com.tencent.mm.plugin.report.service.h;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.ah;
import com.tencent.mm.sdk.platformtools.al;
import com.tencent.mm.sdk.platformtools.at;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.mm.ui.MMActivity;
import com.tencent.mm.ui.base.p;
import com.tencent.mm.vfs.e;
import com.tencent.ytcommon.BuildConfig;
import java.io.IOException;

public class ExdeviceConnectWifiUI extends MMActivity {
    private TextWatcher arx;
    private EditText gBJ;
    private String kWl = BuildConfig.FLAVOR;
    private b lAa;
    private n lAb;
    private a lAc;
    private MulticastLock lAd;
    private Runnable lAe = new 1(this);
    private View lzG;
    private TextView lzH;
    private View lzI;
    private View lzJ;
    private p lzK;
    private String lzL;
    private com.tencent.mm.plugin.exdevice.d.a lzM;
    private boolean lzN;
    private boolean lzO;
    private boolean lzP;
    private int lzQ;
    private byte[] lzR;
    private int lzS;
    private int lzT;
    private int lzU;
    private String lzV = BuildConfig.FLAVOR;
    private int lzW;
    private long lzX;
    boolean lzY = false;
    private b lzZ;

    public void onWindowFocusChanged(boolean z) {
        super.onWindowFocusChanged(z);
        AppMethodBeat.at(this, z);
    }

    public ExdeviceConnectWifiUI() {
        AppMethodBeat.i(19896);
        AppMethodBeat.o(19896);
    }

    static /* synthetic */ void e(ExdeviceConnectWifiUI exdeviceConnectWifiUI) {
        AppMethodBeat.i(19912);
        exdeviceConnectWifiUI.gO(true);
        AppMethodBeat.o(19912);
    }

    static /* synthetic */ void p(ExdeviceConnectWifiUI exdeviceConnectWifiUI) {
        AppMethodBeat.i(19914);
        exdeviceConnectWifiUI.bpG();
        AppMethodBeat.o(19914);
    }

    public final int getLayoutId() {
        return 2130969416;
    }

    public void onCreate(Bundle bundle) {
        AppMethodBeat.i(19897);
        super.onCreate(bundle);
        this.lAb = new 8(this);
        this.lAa = b.lAo;
        if (aw.RK()) {
            this.lzZ = new b();
            this.lzM = new com.tencent.mm.plugin.exdevice.d.a();
            aw.ZK();
            this.lzQ = c.QF();
            String stringExtra = getIntent().getStringExtra("encryptKey");
            this.lzS = getIntent().getIntExtra("procInterval", 0);
            this.lzT = getIntent().getIntExtra("dataInterval", 0);
            ab.d("MicroMsg.exdevice.ExdeviceConnectWifiUI", "Process interval:" + this.lzS + " Data interval:" + this.lzT);
            if (!bo.isNullOrNil(stringExtra)) {
                this.lzR = Base64.decode(stringExtra, 0);
                this.lzW = 1;
            }
            this.lzY = false;
            this.lzU = getIntent().getExtras().getInt("exdevice_airkiss_open_type");
            if (this.lzU == 2) {
                this.lzV = getIntent().getStringExtra("device_brand_name");
                this.kWl = getIntent().getStringExtra("device_category_id");
            }
            this.lAc = new 9(this);
            j.boN().a(0, this.lAc);
            initView();
            aw.RS().aa(new 3(this));
            uP(1);
            this.lAd = ((WifiManager) getApplicationContext().getSystemService("wifi")).createMulticastLock("localWifi");
            AppMethodBeat.o(19897);
            return;
        }
        finish();
        AppMethodBeat.o(19897);
    }

    public void onResume() {
        AppMethodBeat.i(19898);
        super.onResume();
        gO(false);
        aw.a(this.lAb);
        if (this.lzO) {
            bpG();
            this.lzO = false;
        }
        AppMethodBeat.o(19898);
    }

    public void onDestroy() {
        AppMethodBeat.i(19899);
        super.onDestroy();
        if (!this.lzY) {
            uP(2);
        }
        j.boN().b(0, this.lAc);
        AppMethodBeat.o(19899);
    }

    public void onStop() {
        AppMethodBeat.i(19900);
        super.onStop();
        aw.b(this.lAb);
        AppMethodBeat.o(19900);
    }

    public final void initView() {
        AppMethodBeat.i(19901);
        setMMTitle(2131299297);
        setBackBtn(new 10(this));
        this.lzG = findViewById(2131823643);
        this.lzH = (TextView) findViewById(2131823625);
        this.lzI = findViewById(2131823645);
        this.gBJ = (EditText) findViewById(2131823644);
        this.lzJ = findViewById(2131823648);
        this.arx = new 11(this);
        this.lzG.setOnTouchListener(new 12(this));
        this.lzJ.setOnClickListener(new 13(this));
        this.gBJ.setTransformationMethod(new PasswordTransformationMethod());
        this.gBJ.addTextChangedListener(this.arx);
        this.gBJ.setOnEditorActionListener(new 14(this));
        this.gBJ.setOnKeyListener(new 15(this));
        this.gBJ.requestFocus();
        AppMethodBeat.o(19901);
    }

    private void uP(int i) {
        AppMethodBeat.i(19902);
        long j = 0;
        if (i == 4) {
            j = System.currentTimeMillis() - this.lzX;
        }
        h.pYm.e(13503, new Object[]{Integer.valueOf(i), Integer.valueOf(this.lzU), Long.valueOf(j), this.lzV, this.kWl, Integer.valueOf(this.lzW)});
        AppMethodBeat.o(19902);
    }

    private com.tencent.mm.plugin.exdevice.d.a au(int i, String str) {
        AppMethodBeat.i(19903);
        if (bo.isNullOrNil(str) || this.lzZ == null) {
            ab.i("MicroMsg.exdevice.ExdeviceConnectWifiUI", "ssid or wifiInfoList is null or nil.");
            AppMethodBeat.o(19903);
            return null;
        }
        int i2 = 0;
        while (true) {
            int i3 = i2;
            if (i3 < this.lzZ.luI.size()) {
                com.tencent.mm.plugin.exdevice.d.a aVar = (com.tencent.mm.plugin.exdevice.d.a) this.lzZ.luI.get(i3);
                if (aVar == null) {
                    i2 = i3 - 1;
                    this.lzZ.luI.remove(i3);
                    this.lzN = true;
                    i3 = i2;
                } else if (aVar.luF == i && str.equals(aVar.luG)) {
                    AppMethodBeat.o(19903);
                    return aVar;
                }
                i2 = i3 + 1;
            } else {
                AppMethodBeat.o(19903);
                return null;
            }
        }
    }

    private void bpG() {
        AppMethodBeat.i(19904);
        this.lzM.luH = BuildConfig.FLAVOR;
        this.lzM.luG = BuildConfig.FLAVOR;
        String str = this.lzL;
        com.tencent.mm.plugin.exdevice.d.a au = au(this.lzQ, str);
        if (au == null || bo.isNullOrNil(str)) {
            AppMethodBeat.o(19904);
        } else if (bo.isNullOrNil(au.luH)) {
            ab.i("MicroMsg.exdevice.ExdeviceConnectWifiUI", "Do not have a local password to fill for the current wifi.");
            this.lzZ.luI.remove(au);
            this.lzN = true;
            AppMethodBeat.o(19904);
        } else {
            this.lzM.luH = com.tencent.mm.plugin.base.model.b.da(au.luH, a.av(this.lzQ, au.luG));
            this.lzM.luG = au.luG;
            al.d(this.lAe);
            AppMethodBeat.o(19904);
        }
    }

    private String getPassword() {
        AppMethodBeat.i(19905);
        if (this.gBJ.getText() != null) {
            String obj = this.gBJ.getText().toString();
            AppMethodBeat.o(19905);
            return obj;
        }
        AppMethodBeat.o(19905);
        return null;
    }

    private void gO(boolean z) {
        AppMethodBeat.i(19906);
        b bVar = z ? b.lAo : this.lAa;
        if (at.getNetType(this) != 0) {
            bVar = b.lAp;
        } else {
            this.lzL = at.gE(ah.getContext());
            ab.i("MicroMsg.exdevice.ExdeviceConnectWifiUI", "Get SSID(%s)", new Object[]{this.lzL});
            if (bo.isNullOrNil(this.lzL)) {
                this.lzH.setText(BuildConfig.FLAVOR);
            } else {
                this.lzH.setText(this.lzL);
            }
        }
        a(bVar);
        AppMethodBeat.o(19906);
    }

    public final synchronized void a(b bVar) {
        AppMethodBeat.i(19907);
        if (bVar == null) {
            ab.e("MicroMsg.exdevice.ExdeviceConnectWifiUI", "Status is null.");
            AppMethodBeat.o(19907);
        } else {
            this.lAa = bVar;
            switch (7.lAk[bVar.ordinal()]) {
                case 1:
                    g(true, false, false);
                    break;
                case 2:
                    g(true, true, false);
                    break;
                case 3:
                    g(false, false, false);
                    break;
                case 4:
                    uP(5);
                    this.lzP = true;
                    setResult(1);
                    break;
                case 5:
                    uP(4);
                    g(true, false, true);
                    this.lzP = true;
                    setResult(-1);
                    aw.RS().aa(new 4(this));
                    if (getIntent().getBooleanExtra("jumpToBindDevice", false)) {
                        Intent intent = new Intent();
                        intent.putExtra("device_scan_mode", getIntent().getStringExtra("device_scan_mode"));
                        intent.putExtra("device_scan_conn_proto", getIntent().getStringExtra("device_scan_conn_proto"));
                        intent.putExtra("device_id", getIntent().getStringExtra("device_id"));
                        intent.putExtra("device_type", getIntent().getStringExtra("device_type"));
                        intent.putExtra("device_title", getIntent().getStringExtra("device_title"));
                        intent.putExtra("device_desc", getIntent().getStringExtra("device_desc"));
                        intent.putExtra("device_icon_url", getIntent().getStringExtra("device_icon_url"));
                        intent.putExtra("device_category_id", getIntent().getStringExtra("device_category_id"));
                        intent.putExtra("device_brand_name", getIntent().getStringExtra("device_brand_name"));
                        intent.putExtra("bind_ticket", getIntent().getStringExtra("bind_ticket"));
                        d.b(this.mController.ylL, "exdevice", ".ui.ExdeviceBindDeviceUI", intent);
                        break;
                    }
                    break;
            }
            finish();
            if (bVar != b.lAo) {
                this.gBJ.clearFocus();
                aqX();
            }
            AppMethodBeat.o(19907);
        }
    }

    private void bpH() {
        AppMethodBeat.i(19908);
        runOnUiThread(new 5(this));
        AppMethodBeat.o(19908);
    }

    private void bpD() {
        AppMethodBeat.i(19909);
        runOnUiThread(new 6(this));
        if (this.lAd.isHeld()) {
            this.lAd.release();
        }
        AppMethodBeat.o(19909);
    }

    private void g(boolean z, boolean z2, boolean z3) {
        AppMethodBeat.i(19910);
        this.lzG.setVisibility(z ? 0 : 8);
        if (z2) {
            bpH();
        } else {
            bpD();
        }
        if (z3) {
            Toast.makeText(this.mController.ylL, 2131299300, 0).show();
        }
        AppMethodBeat.o(19910);
    }

    static /* synthetic */ void k(ExdeviceConnectWifiUI exdeviceConnectWifiUI) {
        AppMethodBeat.i(19913);
        exdeviceConnectWifiUI.lzY = true;
        exdeviceConnectWifiUI.lzX = System.currentTimeMillis();
        exdeviceConnectWifiUI.uP(3);
        String password = exdeviceConnectWifiUI.getPassword();
        String str = exdeviceConnectWifiUI.lzL;
        ab.i("MicroMsg.exdevice.ExdeviceConnectWifiUI", "do start AirKiss, ssid(%s).", new Object[]{str});
        if (bo.isNullOrNil(str)) {
            ab.e("MicroMsg.exdevice.ExdeviceConnectWifiUI", "ssid is invalid");
            Toast.makeText(exdeviceConnectWifiUI.mController.ylL, "SSID is invalid", 0).show();
            AppMethodBeat.o(19913);
            return;
        }
        exdeviceConnectWifiUI.a(b.lAq);
        aw.RS().aa(new 2(exdeviceConnectWifiUI, password, str));
        AppMethodBeat.o(19913);
    }

    static /* synthetic */ void q(ExdeviceConnectWifiUI exdeviceConnectWifiUI) {
        AppMethodBeat.i(19915);
        String str = exdeviceConnectWifiUI.lzL;
        String nullAsNil = bo.nullAsNil(exdeviceConnectWifiUI.getPassword());
        if (bo.isNullOrNil(str)) {
            ab.i("MicroMsg.exdevice.ExdeviceConnectWifiUI", "ssid is null or nil.");
            AppMethodBeat.o(19915);
            return;
        }
        if (bo.isNullOrNil(exdeviceConnectWifiUI.lzM.luG) || (str.equals(exdeviceConnectWifiUI.lzM.luG) && !nullAsNil.equals(exdeviceConnectWifiUI.lzM.luH))) {
            com.tencent.mm.plugin.exdevice.d.a au = exdeviceConnectWifiUI.au(exdeviceConnectWifiUI.lzQ, str);
            if (au == null) {
                au = new com.tencent.mm.plugin.exdevice.d.a();
                au.luG = str;
                au.luF = exdeviceConnectWifiUI.lzQ;
                exdeviceConnectWifiUI.lzZ.luI.add(au);
            }
            au.luH = com.tencent.mm.plugin.base.model.b.da(nullAsNil, a.av(exdeviceConnectWifiUI.lzQ, str));
        } else if (!exdeviceConnectWifiUI.lzN) {
            ab.i("MicroMsg.exdevice.ExdeviceConnectWifiUI", "password hasn't changed, so do not need to save.");
            AppMethodBeat.o(19915);
            return;
        }
        StringBuilder stringBuilder = new StringBuilder();
        aw.ZK();
        str = stringBuilder.append(c.getAccPath()).append("exdevice_wifi_infos").toString();
        try {
            byte[] toByteArray = exdeviceConnectWifiUI.lzZ.toByteArray();
            e.b(str, toByteArray, toByteArray.length);
        } catch (IOException e) {
            ab.d("MicroMsg.exdevice.ExdeviceConnectWifiUI", e.getMessage());
            ab.printErrStackTrace("MicroMsg.exdevice.ExdeviceConnectWifiUI", e, BuildConfig.FLAVOR, new Object[0]);
        }
        ab.v("MicroMsg.exdevice.ExdeviceConnectWifiUI", "Current wifiInfo was save to file(%s).", new Object[]{str});
        AppMethodBeat.o(19915);
    }

    static /* synthetic */ void r(ExdeviceConnectWifiUI exdeviceConnectWifiUI) {
        AppMethodBeat.i(19916);
        Java2CExDevice.stopAirKiss();
        exdeviceConnectWifiUI.gO(true);
        if (exdeviceConnectWifiUI.lAd.isHeld()) {
            exdeviceConnectWifiUI.lAd.release();
        }
        AppMethodBeat.o(19916);
    }
}
