package com.ifpdos.factory;

import a.a.a.a.c.c0;
import a.a.a.a.c.e1;
import a.a.a.a.c.m1;
import a.a.a.a.c.u1.i;
import a.a.a.a.c.u1.m;
import a.a.a.a.g.v;
import a.a.b.f0;
import a.a.b.s0;
import a.f.a.f;
import a.f.a.g;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.activity.ComponentActivity;
import com.cvte.vman.instance.InvokeCmd;
import com.ifpdos.factory.FactoryService;
import com.ifpdos.factory.HttpProxyActivity;
import e.b;
import e.s.c.k;
import e.s.c.l;
import e.s.c.r;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public final class HttpProxyActivity extends ComponentActivity {

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final /* synthetic */ int f5515i = 0;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public TextView f5516j;
    public final b k = d.a.u.a.s(a.f5517b);

    public static final class a extends l implements e.s.b.a<f0> {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public static final a f5517b = new a();

        public a() {
            super(0);
        }

        @Override // e.s.b.a
        public f0 a() {
            return new f0(null, null, null, null, 15);
        }
    }

    public final f0 h() {
        return (f0) this.k.getValue();
    }

    public final void i() {
        new AlertDialog.Builder(this).setTitle(getString(R.string.exit)).setMessage(getString(R.string.exit_tips)).setPositiveButton(getString(R.string.exit), new DialogInterface.OnClickListener() { // from class: a.a.b.c
            @Override // android.content.DialogInterface.OnClickListener
            public final void onClick(DialogInterface dialogInterface, int i2) {
                HttpProxyActivity httpProxyActivity = this.f3704a;
                int i3 = HttpProxyActivity.f5515i;
                e.s.c.k.e(httpProxyActivity, "this$0");
                httpProxyActivity.finish();
            }
        }).setNegativeButton(getString(R.string.cancel), (DialogInterface.OnClickListener) null).show();
    }

    @Override // androidx.activity.ComponentActivity, android.app.Activity
    public void onBackPressed() {
        i();
    }

    @Override // androidx.activity.ComponentActivity, c.e.b.b, android.app.Activity
    public void onCreate(Bundle bundle) throws IllegalAccessException, IOException, InvocationTargetException {
        String str;
        String strB;
        String strA;
        String strC;
        super.onCreate(bundle);
        setContentView(R.layout.activity_http);
        ((Button) findViewById(R.id.exitButton)).setOnClickListener(new View.OnClickListener() { // from class: a.a.b.d
            @Override // android.view.View.OnClickListener
            public final void onClick(View view) {
                HttpProxyActivity httpProxyActivity = this.f3707b;
                int i2 = HttpProxyActivity.f5515i;
                e.s.c.k.e(httpProxyActivity, "this$0");
                httpProxyActivity.i();
            }
        });
        View viewFindViewById = findViewById(R.id.monitor);
        k.d(viewFindViewById, "findViewById(R.id.monitor)");
        this.f5516j = (TextView) viewFindViewById;
        File file = new File("/sdcard/Download/factory_udi_config/http_config.json");
        if (!file.exists()) {
            Log.e("HttpProxyActivity", "http_config.json not exist");
            return;
        }
        m.f1845a.h0("persist.sys.udi.http.static.ip", true);
        g gVar = f.f4517a;
        gVar.f("PropertiesUtils");
        gVar.e(3, null, "markStaticIpByFactoryService", new Object[0]);
        e1 e1Var = e1.f1583a;
        final c0 c0Var = (c0) e1.a(r.a(c0.class));
        Type type = new s0().getType();
        c0.a aVarI = c0Var.i("/v1/network/ethernet/mode", (2 & 2) != 0 ? e.o.g.f5966b : null);
        String str2 = "";
        if (aVarI == null || (str = aVarI.f1564b) == null) {
            str = "";
        }
        f0 f0VarH = h();
        i iVar = i.f1833a;
        k.d(type, "typeToken");
        m1 m1Var = (m1) i.f(str, type);
        v vVar = m1Var == null ? null : (v) m1Var.a();
        if (vVar == null) {
            vVar = v.DHCP;
        }
        Objects.requireNonNull(f0VarH);
        k.e(vVar, "<set-?>");
        f0VarH.f3726a = vVar;
        c0.a aVarI2 = c0Var.i("/v1/network/ethernet/config", (2 & 2) != 0 ? e.o.g.f5966b : null);
        String str3 = aVarI2 == null ? null : aVarI2.f1564b;
        if (str3 == null) {
            return;
        }
        FactoryService.a aVar = (FactoryService.a) i.e(str3, FactoryService.a.class);
        f0 f0VarH2 = h();
        if (aVar == null || (strB = aVar.b()) == null) {
            strB = "";
        }
        Objects.requireNonNull(f0VarH2);
        k.e(strB, "<set-?>");
        f0VarH2.f3727b = strB;
        f0 f0VarH3 = h();
        if (aVar == null || (strA = aVar.a()) == null) {
            strA = "";
        }
        Objects.requireNonNull(f0VarH3);
        k.e(strA, "<set-?>");
        f0VarH3.f3728c = strA;
        f0 f0VarH4 = h();
        if (aVar != null && (strC = aVar.c()) != null) {
            str2 = strC;
        }
        Objects.requireNonNull(f0VarH4);
        k.e(str2, "<set-?>");
        f0VarH4.f3729d = str2;
        Charset charset = e.w.a.f6045a;
        k.e(file, "$this$readText");
        k.e(charset, "charset");
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), charset);
        try {
            k.e(inputStreamReader, "$this$readText");
            StringWriter stringWriter = new StringWriter();
            k.e(inputStreamReader, "$this$copyTo");
            k.e(stringWriter, "out");
            char[] cArr = new char[InvokeCmd.CMD_TOUCH_CTRL_START];
            while (true) {
                int i2 = inputStreamReader.read(cArr);
                if (i2 < 0) {
                    break;
                } else {
                    stringWriter.write(cArr, 0, i2);
                }
            }
            final String string = stringWriter.toString();
            k.d(string, "buffer.toString()");
            d.a.u.a.g(inputStreamReader, null);
            g gVar2 = f.f4517a;
            gVar2.f("HttpProxyActivity");
            gVar2.b(k.j("http_config.json content: ", string), new Object[0]);
            i iVar2 = i.f1833a;
            final FactoryService.a aVar2 = (FactoryService.a) i.e(string, FactoryService.a.class);
            if (aVar2 == null) {
                g gVar3 = f.f4517a;
                gVar3.f("HttpProxyActivity");
                gVar3.e(3, null, "parser config to json failed,use default config:192.168.153.253", new Object[0]);
                aVar2 = new FactoryService.a("192.168.153.253", "192.168.1.1", "255.255.0.0", null, null);
            }
            c0.a aVarF = c0Var.f("/v1/network/ethernet/config", i.m(aVar2), (4 & 4) != 0 ? e.o.g.f5966b : null);
            Integer numValueOf = aVarF == null ? null : Integer.valueOf(aVarF.f1563a);
            if (numValueOf != null && numValueOf.intValue() == 200) {
                new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { // from class: a.a.b.e
                    @Override // java.lang.Runnable
                    public final void run() {
                        a.a.a.a.c.c0 c0Var2 = c0Var;
                        int i3 = HttpProxyActivity.f5515i;
                        e.s.c.k.e(c0Var2, "$server");
                        c0Var2.f("/v1/network/ethernet/enable", "{\"enable\": false}", (4 & 4) != 0 ? e.o.g.f5966b : null);
                    }
                }, 500L);
                new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { // from class: a.a.b.f
                    @Override // java.lang.Runnable
                    public final void run() {
                        a.a.a.a.c.c0 c0Var2 = c0Var;
                        HttpProxyActivity httpProxyActivity = this;
                        FactoryService.a aVar3 = aVar2;
                        String str4 = string;
                        int i3 = HttpProxyActivity.f5515i;
                        e.s.c.k.e(c0Var2, "$server");
                        e.s.c.k.e(httpProxyActivity, "this$0");
                        e.s.c.k.e(aVar3, "$httpConfig");
                        e.s.c.k.e(str4, "$fileContent");
                        c0Var2.f("/v1/network/ethernet/enable", "{\"enable\": true}", (4 & 4) != 0 ? e.o.g.f5966b : null);
                        TextView textView = httpProxyActivity.f5516j;
                        if (textView == null) {
                            e.s.c.k.l("textView");
                            throw null;
                        }
                        textView.setTextSize(40.0f);
                        TextView textView2 = httpProxyActivity.f5516j;
                        if (textView2 == null) {
                            e.s.c.k.l("textView");
                            throw null;
                        }
                        StringBuilder sbC = a.b.c.a.a.C("ADDRESS: ");
                        String strB2 = aVar3.b();
                        if (strB2 == null) {
                            strB2 = "?";
                        }
                        sbC.append(strB2);
                        sbC.append(":10110\nhttp_config.json content:\n");
                        sbC.append(str4);
                        textView2.setText(sbC.toString());
                    }
                }, 2500L);
            } else {
                g gVar4 = f.f4517a;
                gVar4.f("HttpProxyActivity");
                gVar4.e(3, null, "Set static ip by /v1/network/ethernet/config failed", new Object[0]);
            }
        } catch (Throwable th) {
            try {
                throw th;
            } catch (Throwable th2) {
                d.a.u.a.g(inputStreamReader, th);
                throw th2;
            }
        }
    }

    @Override // android.app.Activity
    public void onDestroy() {
        String strM;
        String str;
        e1 e1Var = e1.f1583a;
        c0 c0Var = (c0) e1.a(r.a(c0.class));
        v vVar = h().f3726a;
        v vVar2 = v.DHCP;
        if (vVar == vVar2) {
            i iVar = i.f1833a;
            strM = i.m(new m1(vVar2));
            str = "/v1/network/ethernet/mode";
        } else {
            FactoryService.a aVar = new FactoryService.a(h().f3727b, h().f3728c, h().f3729d, null, null);
            i iVar2 = i.f1833a;
            strM = i.m(aVar);
            str = "/v1/network/ethernet/config";
        }
        c0Var.f(str, strM, (4 & 4) != 0 ? e.o.g.f5966b : null);
        super.onDestroy();
    }
}
