package com.ucar.vehiclesdk.connect;

import a.b.a.c;
import a.b.a.i.h;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import com.google.common.io.BaseEncoding;
import com.share.connect.Device;
import com.share.connect.IShareLinkManager;
import com.share.connect.ShareLinkObserver;
import com.share.connect.ShareLinkService;
import com.share.connect.WifiOwnerConfig;
import com.ucar.vehiclesdk.MDevice;
import com.ucar.vehiclesdk.UCarCommon;
import com.ucar.vehiclesdk.UCarConfig;
import com.ucar.vehiclesdk.UCarConnectState;
import java.util.Timer;
import java.util.TimerTask;

public class UCarConnectProxy {
    private static final String s = "UCarConnectProxy";
    private static final int t = 6;
    public static final int u = 2;
    public static final int v = 6;
    public static final String w = "0000";
    public static final int x = 120000;

    /* renamed from: a  reason: collision with root package name */
    private Context f231a;
    private UCarConfig b;
    private boolean c = false;
    private IShareLinkManager d;
    private a.c.d.f.a e;
    private String f;
    private String g;
    private String h;
    private String i;
    private String j;
    private String k;
    private int l;
    private MDevice m;
    private Timer n;
    private int o = 0;
    private volatile boolean p = true;
    private ServiceConnection q = new a();
    private ShareLinkObserver.Stub r = new ShareLinkObserver.Stub() {
        /* class com.ucar.vehiclesdk.connect.UCarConnectProxy.AnonymousClass2 */

        @Override // com.share.connect.ShareLinkObserver
        public void onAuthenticationOk() throws RemoteException {
            if (UCarConnectProxy.this.e != null) {
                UCarConnectProxy.this.e.a(UCarConnectProxy.this.m.getId());
            }
        }

        @Override // com.share.connect.ShareLinkObserver
        public void onConnectFailed(int i) throws RemoteException {
            UCarConnectProxy.this.a((UCarConnectProxy) i);
        }

        @Override // com.share.connect.ShareLinkObserver
        public void onConnected() throws RemoteException {
            if (UCarConnectProxy.this.e != null) {
                UCarConnectProxy.this.e.onConnected();
            }
        }

        @Override // com.share.connect.ShareLinkObserver
        public void onDeviceDiscover(boolean z, Device device) throws RemoteException {
        }

        @Override // com.share.connect.ShareLinkObserver
        public void onDisconnected(boolean z) throws RemoteException {
            a.a.a.b.c(UCarConnectProxy.s, "onDisconnected");
            if (UCarConnectProxy.this.e != null) {
                UCarConnectProxy.this.e.a(UCarConnectProxy.this.m.getId(), z);
            }
        }

        @Override // com.share.connect.ShareLinkObserver
        public void onOpenResult(boolean z) throws RemoteException {
            if (!z) {
                UCarConnectProxy.this.a((UCarConnectProxy) 1);
            }
        }

        @Override // com.share.connect.ShareLinkObserver
        public void onProgress(int i) {
            UCarConnectProxy.this.c((UCarConnectProxy) i);
        }

        @Override // com.share.connect.ShareLinkObserver
        public void onReconfigureWifi(WifiOwnerConfig wifiOwnerConfig) throws RemoteException {
            if (UCarConnectProxy.this.e != null) {
                UCarConnectProxy.this.e.onReconfigureWifi(wifiOwnerConfig);
            }
        }

        @Override // com.share.connect.ShareLinkObserver
        public void onScanResult(boolean z) throws RemoteException {
        }

        @Override // com.share.connect.ShareLinkObserver
        public int onSelectWorkMode(int i) {
            return UCarConnectProxy.this.d((UCarConnectProxy) i);
        }

        @Override // com.share.connect.ShareLinkObserver
        public void onUserInterventionNeeded(boolean z) {
            UCarConnectProxy.this.b((UCarConnectProxy) z);
        }

        @Override // com.share.connect.ShareLinkObserver
        public void receivedClientAddress(String str, int i) throws RemoteException {
            UCarConnectProxy.this.m.setAddress(str);
            UCarConnectProxy.this.m.setSelectedWorkMode(i);
            a.a.a.b.a(UCarConnectProxy.s, "receivedClientAddress");
            if (UCarConnectProxy.this.e != null) {
                UCarConnectProxy.this.e.b(UCarConnectProxy.this.m.getId(), str);
            }
            if (UCarConnectProxy.this.d != null) {
                UCarConnectProxy.this.d.stopAdvertise();
            }
        }

        /* JADX WARNING: Removed duplicated region for block: B:12:0x0077  */
        /* JADX WARNING: Removed duplicated region for block: B:14:? A[RETURN, SYNTHETIC] */
        @Override // com.share.connect.ShareLinkObserver
        public void receivedClientHello(String str, String str2, String str3, String str4, int i) throws RemoteException {
            MDevice mDevice;
            int i2;
            UCarConnectProxy.this.m.setId(str);
            UCarConnectProxy.this.m.setProductName(str4);
            UCarConnectProxy.this.m.setModel(str2);
            UCarConnectProxy.this.m.setProtocolVersion(i);
            a.a.a.b.a(UCarConnectProxy.s, "receivedClientHello connectionType = " + str3);
            if (str3.equalsIgnoreCase(c.n)) {
                mDevice = UCarConnectProxy.this.m;
                i2 = 2;
            } else if (str3.equalsIgnoreCase(c.o)) {
                mDevice = UCarConnectProxy.this.m;
                i2 = 1;
            } else {
                if (str3.equalsIgnoreCase(c.p)) {
                    mDevice = UCarConnectProxy.this.m;
                    i2 = 3;
                }
                if (UCarConnectProxy.this.e == null) {
                    UCarConnectProxy.this.e.a(str, str4);
                    return;
                }
                return;
            }
            mDevice.setConnectType(i2);
            if (UCarConnectProxy.this.e == null) {
            }
        }

        @Override // com.share.connect.ShareLinkObserver
        public void receivedClientInfo(String str) throws RemoteException {
            a.a.a.b.a(UCarConnectProxy.s, "received client info from: " + str);
            UCarConnectProxy.this.m.setModel(str);
        }
    };

    public class a implements ServiceConnection {
        public a() {
        }

        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            a.a.a.b.a(UCarConnectProxy.s, "share link service connected.");
            UCarConnectProxy.this.d = IShareLinkManager.Stub.asInterface(iBinder);
            try {
                if (!UCarConnectProxy.this.c) {
                    UCarConnectProxy.this.d.registerLinkObserver(UCarConnectProxy.this.r);
                    UCarConnectProxy.this.c = true;
                }
                if (UCarConnectProxy.this.e != null) {
                    a.a.a.b.a(UCarConnectProxy.s, "notify share link service connected");
                    UCarConnectProxy.this.e.b();
                }
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        public void onServiceDisconnected(ComponentName componentName) {
            a.a.a.b.e(UCarConnectProxy.s, "share link service disconnected.");
            UCarConnectProxy.this.c = false;
            if (UCarConnectProxy.this.d != null) {
                if (UCarConnectProxy.this.e != null) {
                    a.a.a.b.a(UCarConnectProxy.s, "notify share link service disconnected");
                    UCarConnectProxy.this.e.a();
                }
                try {
                    UCarConnectProxy.this.d.unregisterLinkObserver(UCarConnectProxy.this.r);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public class b extends TimerTask {
        public b() {
        }

        public void run() {
            UCarConnectProxy.this.f = h.b(6);
            try {
                UCarConnectProxy.this.d.updatePinCode(UCarConnectProxy.this.f);
                if (UCarConnectProxy.this.e != null && UCarConnectProxy.this.i != null) {
                    a.a.a.b.c(UCarConnectProxy.s, "notify pin code to car app");
                    UCarConnectProxy.this.e.onPinCode(UCarConnectProxy.this.f, UCarConnectProxy.this.i);
                }
            } catch (RemoteException e) {
                UCarConnectProxy.this.a((UCarConnectProxy) 7);
                a.a.a.b.b(UCarConnectProxy.s, "updatePinCode failed", e);
            }
        }
    }

    public UCarConnectProxy(Context context, UCarConfig uCarConfig) {
        this.f231a = context;
        this.b = uCarConfig;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(int i2) {
        int i3;
        a.a.a.b.c(s, "handleConnectFailed, reason: " + i2);
        switch (i2) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 8:
                i3 = UCarConnectState.ErrorCode.ERROR_WIRELESS_CONNECT_FAILED;
                break;
            case 5:
                i3 = UCarConnectState.ErrorCode.ERROR_USB_CONNECT_FAILED;
                break;
            case 6:
            case 7:
                i3 = UCarConnectState.ErrorCode.ERROR_CONNECT_INNER_FAILED;
                break;
            default:
                return;
        }
        b(i3);
    }

    private void a(byte[] bArr) {
        this.h = BaseEncoding.base16().lowerCase().encode(bArr);
        a.a.a.b.c(s, "get car id: " + this.h);
    }

    private boolean a() {
        a.a.a.b.c(s, "bind connect service");
        if (this.c) {
            return true;
        }
        try {
            Intent intent = new Intent(this.f231a, ShareLinkService.class);
            intent.putExtra("car_id", this.h);
            intent.putExtra("mod_id", this.g);
            intent.putExtra("car_short_name", this.j);
            intent.putExtra("car_name", this.i);
            intent.putExtra("protocol_version", "1.5");
            intent.putExtra("car_vendor_custom_data", this.k);
            intent.putExtra("ble_rssi_threshold", this.l);
            this.f231a.bindService(intent, this.q, 1);
            return true;
        } catch (Exception e2) {
            a.a.a.b.b(s, "bind connect service error.", e2);
            return false;
        }
    }

    private boolean a(byte[] bArr, byte[] bArr2) {
        if (bArr == null || bArr.length != 6) {
            a.a.a.b.b(s, "Invalid car id");
            return false;
        }
        a(bArr);
        f();
        g();
        b(bArr2);
        this.m = new MDevice();
        return true;
    }

    private void b(int i2) {
        a.c.d.f.a aVar = this.e;
        if (aVar != null) {
            aVar.b(this.m.getId(), i2);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void b(boolean z) {
        a.c.d.f.a aVar = this.e;
        if (aVar != null) {
            aVar.onUserInterventionNeeded(z);
        }
    }

    private void b(byte[] bArr) {
        String str;
        if (bArr == null || bArr.length != 2) {
            a.a.a.b.e(s, "Invalid custom Data.");
            str = w;
        } else {
            str = BaseEncoding.base16().lowerCase().encode(bArr);
        }
        this.k = str;
        a.a.a.b.c(s, "get Custom Data: " + this.k);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void c(int i2) {
        a.c.d.f.a aVar = this.e;
        if (aVar != null) {
            aVar.a(this.m.getId(), i2);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private int d(int i2) {
        a.c.d.f.a aVar = this.e;
        return aVar != null ? aVar.onSelectWorkMode(i2) : UCarCommon.WorkMode.NORMAL_CAST.toInt();
    }

    private void f() {
        Device e2 = new a.b.a.i.c(this.f231a, this.b.getCcdFilePath()).e();
        if (e2 == null) {
            e2 = Device.UNKNOWN_CAR;
        }
        this.g = (e2.getVid() + e2.getPid()).toLowerCase();
    }

    private void g() {
        Device e2 = new a.b.a.i.c(this.f231a, this.b.getCcdFilePath()).e();
        if (e2 == null) {
            e2 = Device.UNKNOWN_CAR;
        }
        this.i = e2.getPName();
        this.j = e2.getShortName();
    }

    private void h() {
        j();
        Timer timer = new Timer();
        this.n = timer;
        timer.schedule(new b(), 0, 120000);
    }

    private void j() {
        Timer timer = this.n;
        if (timer != null) {
            timer.cancel();
            this.n = null;
        }
    }

    public MDevice a(String str) {
        a.a.a.b.a(s, "getMDevice, mMDevice: " + this.m);
        return this.m;
    }

    public void a(a.c.d.f.a aVar) {
        this.e = aVar;
    }

    public void a(boolean z) {
        IShareLinkManager iShareLinkManager = this.d;
        if (iShareLinkManager != null) {
            try {
                iShareLinkManager.enableUsbDeviceScanning(z);
            } catch (RemoteException e2) {
                a.a.a.b.b(s, "enableUsbDeviceScanning failed", e2);
            }
        }
    }

    public boolean a(boolean z, UCarConfig uCarConfig) {
        int i2;
        if (this.d == null) {
            a.a.a.b.b(s, "start advertise failed, mShareLinkManager is null");
            return false;
        }
        a.a.a.b.a(s, "start advertise is reconnect:" + z);
        if (!this.p) {
            a.a.a.b.e(s, "already in advertised, need to stop first");
            i();
        }
        a.a.a.b.a(s, "start advertise running. sdk version is v1.5.1-202307102121-6b313012");
        h();
        boolean isSupportSoftAP = uCarConfig.isSupportSoftAP();
        boolean isSupportP2P = uCarConfig.isSupportP2P();
        boolean isSupportRealWifiAddress = uCarConfig.isSupportRealWifiAddress();
        String str = uCarConfig.getVideoDisplayWidth() + "_" + uCarConfig.getVideoDisplayHeight() + "_" + uCarConfig.getRealDpi();
        if (isSupportP2P && isSupportSoftAP) {
            i2 = 1003;
        } else if (isSupportP2P) {
            i2 = 1001;
        } else {
            if (isSupportSoftAP) {
                i2 = 1002;
            }
            this.d.open(z, this.o, uCarConfig.getAllowed5gChannels(), isSupportRealWifiAddress, uCarConfig.getSupportWorkModesInt(), uCarConfig.getWifiSsid(), uCarConfig.getWifiPassword(), uCarConfig.isApStaCoexist(), uCarConfig.isReconfigureWifi(), str);
            this.p = false;
            return true;
        }
        this.o = i2;
        try {
            this.d.open(z, this.o, uCarConfig.getAllowed5gChannels(), isSupportRealWifiAddress, uCarConfig.getSupportWorkModesInt(), uCarConfig.getWifiSsid(), uCarConfig.getWifiPassword(), uCarConfig.isApStaCoexist(), uCarConfig.isReconfigureWifi(), str);
            this.p = false;
            return true;
        } catch (RemoteException e2) {
            a(7);
            a.a.a.b.b(s, "startAdvertise failed", e2);
            return false;
        }
    }

    public boolean a(byte[] bArr, byte[] bArr2, int i2) {
        if (!a(bArr, bArr2)) {
            return false;
        }
        this.l = i2;
        return a();
    }

    public void b() {
        IShareLinkManager iShareLinkManager = this.d;
        if (iShareLinkManager != null) {
            try {
                iShareLinkManager.close();
            } catch (RemoteException e2) {
                a.a.a.b.b(s, "close RemoteException", e2);
            }
        }
    }

    public void b(a.c.d.f.a aVar) {
        if (this.e == aVar) {
            this.e = null;
        }
    }

    public void c() {
        IShareLinkManager iShareLinkManager = this.d;
        if (iShareLinkManager != null) {
            try {
                iShareLinkManager.disconnect();
            } catch (RemoteException e2) {
                a.a.a.b.b(s, "disconnect Exception", e2);
            }
        }
    }

    public void d() {
        if (this.c) {
            try {
                this.f231a.unbindService(this.q);
                this.q = null;
            } catch (Exception e2) {
                a.a.a.b.b(s, "disconnectService Exception", e2);
            }
        }
    }

    public String e() {
        return this.h;
    }

    public void e(int i2) {
        IShareLinkManager iShareLinkManager = this.d;
        if (iShareLinkManager != null) {
            try {
                iShareLinkManager.updateDayOrNightMode(i2);
            } catch (RemoteException e2) {
                a.a.a.b.b(s, "updateDayOrNightMode RemoteException", e2);
            }
        }
    }

    public boolean i() {
        if (this.d == null) {
            a.a.a.b.b(s, "stopAdvertise failed, mShareLinkManager is null");
            return false;
        }
        a.a.a.b.a(s, "stopAdvertise");
        j();
        try {
            this.d.stopAdvertise();
            this.p = true;
            return true;
        } catch (RemoteException e2) {
            a.a.a.b.b(s, "stopAdvertise failed", e2);
            return false;
        }
    }
}
