package com.share.connect;

import a.b.a.h.d;
import a.b.a.i.k;
import a.c.a.b.b;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.wifi.p2p.WifiP2pGroup;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.os.SystemClock;
import com.google.common.io.BaseEncoding;
import com.share.connect.IShareLinkManager;
import com.share.connect.ble.BluetoothLeObserver;
import com.share.connect.ble.BluetoothLeService;
import com.share.connect.ble.IBluetoothLe;
import com.share.connect.wifiap.IWifiAp;
import com.share.connect.wifiap.WifiApObserver;
import com.share.connect.wifiap.WifiApService;
import com.share.connect.wifip2p.IWifiP2p;
import com.share.connect.wifip2p.WifiP2pObserver;
import com.share.connect.wifip2p.WifiP2pService;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class ShareLinkService extends Service {
    private static final String T;
    private static final int U;
    private static final int V;
    private static final int W;
    private static final int X;
    private static final String Y;
    private static final int Z;
    private static final String a0;
    private int A;
    private String B;
    private String C;
    private String D;
    private final a.b.a.b E = new a.b.a.b();
    private final a.b.a.c F = a.b.a.c.b();
    private final CountDownLatch G = new CountDownLatch(3);
    private final a.b.a.e H = new a.b.a.e();
    private final ShareLinkStub I = new ShareLinkStub(this, null);
    private final Handler J = new Handler(Looper.getMainLooper());
    private final Runnable K = new Runnable() {
        /* class com.share.connect.$$Lambda$ShareLinkService$P6D7zjoTH7Bzevt7vhzhHWv7z8 */

        public final void run() {
            ShareLinkService.m13lambda$P6D7zjoTH7Bzevt7vhzhHWv7z8(ShareLinkService.this);
        }
    };
    private final Runnable L = new Runnable() {
        /* class com.share.connect.$$Lambda$ShareLinkService$6WafY4iLXfM_0i9ejDZl3ivKHM */

        public final void run() {
            ShareLinkService.m12lambda$6WafY4iLXfM_0i9ejDZl3ivKHM(ShareLinkService.this);
        }
    };
    private final Runnable M = new Runnable() {
        /* class com.share.connect.$$Lambda$ShareLinkService$tTGsSwUSogYNBwEb3ZbPTq_L5Lw */

        public final void run() {
            ShareLinkService.lambda$tTGsSwUSogYNBwEb3ZbPTq_L5Lw(ShareLinkService.this);
        }
    };
    private final ServiceConnection N = new a();
    private final ServiceConnection O = new b();
    private final ServiceConnection P = new c();
    private BluetoothLeObserver Q = new g();
    private WifiP2pObserver R = new h();
    private WifiApObserver S = new i();

    /* renamed from: a */
    private String f165a;
    private String b;
    private String c;
    private String d;
    private String e;
    private String f;
    private String g;
    private String h;
    private int i;
    private int j;
    private IBluetoothLe k;
    private IWifiP2p l;
    private IWifiAp m;
    private WifiOwnerConfig n;
    private a.b.a.h.d o;
    private boolean p;
    private boolean q = false;
    private boolean r = false;
    private boolean s = false;
    private boolean t = false;
    private boolean u = false;
    private boolean v = false;
    private boolean w = false;
    private int x;
    private int y;
    private int z;

    public class ShareLinkStub extends IShareLinkManager.Stub {
        private ShareLinkStub() {
            ShareLinkService.this = r1;
        }

        public /* synthetic */ ShareLinkStub(ShareLinkService shareLinkService, a aVar) {
            this();
        }

        @Override // com.share.connect.IShareLinkManager
        public void close() {
            ShareLinkService.this.b();
        }

        @Override // com.share.connect.IShareLinkManager
        public void connect(String str) {
            a.a.a.b.e(ShareLinkService.T, "unSupport method call");
        }

        @Override // com.share.connect.IShareLinkManager
        public void disconnect() {
            ShareLinkService.this.d();
        }

        @Override // com.share.connect.IShareLinkManager
        public void disconnectBle() {
            ShareLinkService.this.k.disconnectWithoutState();
        }

        @Override // com.share.connect.IShareLinkManager
        public void enableUsbDeviceScanning(boolean z) {
            ShareLinkService.this.t = z;
            ShareLinkService.this.c((ShareLinkService) z);
        }

        @Override // com.share.connect.IShareLinkManager
        public Map getDevicesSignal(int i, int i2) {
            if (ShareLinkService.this.k != null) {
                return ShareLinkService.this.k.getDevicesSignal(i, i2);
            }
            a.a.a.b.e(ShareLinkService.T, "getDevicesSignal when bluetooth haven't initialized");
            return Collections.emptyMap();
        }

        @Override // com.share.connect.IShareLinkManager
        public boolean isDeviceInMatch(String str) {
            if (ShareLinkService.this.k != null) {
                return ShareLinkService.this.k.isDeviceInMatch(str);
            }
            a.a.a.b.e(ShareLinkService.T, "Bluetooth not initialize");
            return false;
        }

        @Override // com.share.connect.IShareLinkManager
        public void open(boolean z, int i, int[] iArr, boolean z2, int i2, String str, String str2, boolean z3, boolean z4, String str3) {
            ShareLinkService.this.q = z;
            ShareLinkService.this.s = z2;
            ShareLinkService.this.u = z3;
            ShareLinkService.this.v = z4;
            ShareLinkService.this.D = str3;
            a.b.a.k.e.a(iArr);
            ShareLinkService.this.a(i, i2, str, str2);
        }

        @Override // com.share.connect.IShareLinkManager
        public void registerLinkObserver(ShareLinkObserver shareLinkObserver) {
            ShareLinkService.this.H.a(shareLinkObserver);
        }

        @Override // com.share.connect.IShareLinkManager
        public void startScan() {
            ShareLinkService.this.o();
        }

        @Override // com.share.connect.IShareLinkManager
        public void stopAdvertise() {
            ShareLinkService.this.b((ShareLinkService) false);
            ShareLinkService.this.c();
            if (ShareLinkService.this.F.j()) {
                ShareLinkService.this.F.a(a.b.a.c.c);
                ShareLinkService.this.a((ShareLinkService) false);
            }
        }

        @Override // com.share.connect.IShareLinkManager
        public void stopScan() {
            ShareLinkService.this.r();
        }

        @Override // com.share.connect.IShareLinkManager
        public void unregisterLinkObserver(ShareLinkObserver shareLinkObserver) {
            ShareLinkService.this.H.b(shareLinkObserver);
        }

        @Override // com.share.connect.IShareLinkManager
        public void updateDayOrNightMode(int i) {
            ShareLinkService.this.j = i;
        }

        @Override // com.share.connect.IShareLinkManager
        public void updatePinCode(String str) {
            ShareLinkService.this.c = str;
        }
    }

    public class a implements ServiceConnection {
        public a() {
            ShareLinkService.this = r1;
        }

        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            a.a.a.b.c(ShareLinkService.T, "Bluetooth service connected");
            ShareLinkService.this.k = IBluetoothLe.b.a(iBinder);
            ShareLinkService.this.G.countDown();
        }

        public void onServiceDisconnected(ComponentName componentName) {
            a.a.a.b.b(ShareLinkService.T, "Bluetooth service disconnected");
            ShareLinkService.this.k = null;
        }
    }

    public class b implements ServiceConnection {
        public b() {
            ShareLinkService.this = r1;
        }

        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            a.a.a.b.c(ShareLinkService.T, "WifiP2p service connected");
            ShareLinkService.this.l = IWifiP2p.b.a(iBinder);
            ShareLinkService.this.G.countDown();
        }

        public void onServiceDisconnected(ComponentName componentName) {
            a.a.a.b.b(ShareLinkService.T, "WifiP2p service disconnected");
            ShareLinkService.this.l = null;
        }
    }

    public class c implements ServiceConnection {
        public c() {
            ShareLinkService.this = r1;
        }

        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            a.a.a.b.c(ShareLinkService.T, "WifiAp service connected");
            ShareLinkService.this.m = IWifiAp.b.a(iBinder);
            ShareLinkService.this.G.countDown();
        }

        public void onServiceDisconnected(ComponentName componentName) {
            a.a.a.b.b(ShareLinkService.T, "WifiAp service disconnected");
            ShareLinkService.this.m = null;
        }
    }

    public class d implements d.c {

        /* renamed from: a */
        private String f169a = a.b.a.c.c;

        public d() {
            ShareLinkService.this = r1;
        }

        @Override // a.b.a.h.d.c
        public void a() {
            ShareLinkService.this.H.b();
            ShareLinkService.this.H.b(8);
        }

        @Override // a.b.a.h.d.c
        public void a(String str, int i) {
            ShareLinkService.this.o.a();
            ShareLinkService.this.o = null;
            ShareLinkService.this.a((ShareLinkService) false);
            ShareLinkService.this.F.a(this.f169a);
            ShareLinkService.this.H.b(9);
            ShareLinkService.this.H.a(str, i);
        }

        @Override // a.b.a.h.d.c
        public void a(String str, String str2, String str3, int i) {
            a.b.a.h.d dVar;
            String str4;
            if (ShareLinkService.this.F.g()) {
                ShareLinkService.this.H.a(str, str2, a.b.a.c.n, str3, i);
                dVar = ShareLinkService.this.o;
                str4 = ShareLinkService.this.d;
            } else {
                int b2 = ShareLinkService.this.E.b();
                a.a.a.b.a(ShareLinkService.T, "onClientKeyNegotiationReceived connectType = " + b2);
                ShareLinkService.this.H.a(str, str2, 1002 == b2 ? a.b.a.c.p : a.b.a.c.o, str3, i);
                dVar = ShareLinkService.this.o;
                str4 = ShareLinkService.this.c;
            }
            dVar.a(str4);
            ShareLinkService.this.a((ShareLinkService) true);
            ShareLinkService.this.H.b(7);
            this.f169a = ShareLinkService.this.F.a();
        }

        @Override // a.b.a.h.d.c
        public void b() {
            ShareLinkService.this.a((ShareLinkService) false);
            ShareLinkService.this.H.b(false);
            if (this.f169a.contains(a.b.a.c.q)) {
                ShareLinkService.this.F.a(a.b.a.c.c);
                if (ShareLinkService.this.k != null) {
                    try {
                        ShareLinkService.this.k.allowProcessNewConnection();
                    } catch (RemoteException e) {
                        a.a.a.b.b(ShareLinkService.T, "call failed.", e);
                    }
                }
                ShareLinkService shareLinkService = ShareLinkService.this;
                shareLinkService.c((ShareLinkService) shareLinkService.t);
            }
        }

        @Override // a.b.a.h.d.c
        public int onSelectWorkMode(int i) {
            return ShareLinkService.this.H.c(i);
        }
    }

    public class e implements Runnable {
        public e() {
            ShareLinkService.this = r1;
        }

        public void run() {
            ShareLinkService shareLinkService;
            try {
                a.a.a.b.c(ShareLinkService.T, "Open: Waiting for services bound, start at: " + SystemClock.elapsedRealtime());
                ShareLinkService.this.G.await(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                a.a.a.b.b(ShareLinkService.T, "READY.await() failed", e);
            }
            a.a.a.b.c(ShareLinkService.T, "Open: services bound at: " + SystemClock.elapsedRealtime());
            try {
                if (ShareLinkService.this.x == 1001 && ShareLinkService.this.l != null) {
                    ShareLinkService.this.b((ShareLinkService) true);
                    ShareLinkService.this.E.b(1001);
                    ShareLinkService.this.l.registerWifiP2pObserver(ShareLinkService.this.R);
                    shareLinkService = ShareLinkService.this;
                } else if (ShareLinkService.this.x == 1002 && ShareLinkService.this.m != null) {
                    ShareLinkService.this.b((ShareLinkService) true);
                    ShareLinkService.this.E.b(1002);
                    ShareLinkService.this.m.registerWifiApObserver(ShareLinkService.this.S);
                    ShareLinkService.this.m.open();
                    return;
                } else if (ShareLinkService.this.x != 1003 || ShareLinkService.this.m == null || ShareLinkService.this.l == null) {
                    a.a.a.b.b(ShareLinkService.T, "Open failed: wifi Services binds failed");
                    ShareLinkService.this.b((ShareLinkService) false);
                    ShareLinkService.this.H.c(false);
                    return;
                } else {
                    a.a.a.b.a(ShareLinkService.T, "support wifi p2p & ap connect type, default open p2p");
                    ShareLinkService.this.b((ShareLinkService) true);
                    ShareLinkService.this.E.b(1001);
                    ShareLinkService.this.l.registerWifiP2pObserver(ShareLinkService.this.R);
                    shareLinkService = ShareLinkService.this;
                }
                shareLinkService.l.open();
            } catch (Exception e2) {
                a.a.a.b.b(ShareLinkService.T, "Open failed.", e2);
                ShareLinkService.this.b((ShareLinkService) false);
                ShareLinkService.this.H.c(false);
            }
        }
    }

    public class f implements b.AbstractC0005b {
        public f() {
            ShareLinkService.this = r1;
        }

        @Override // a.c.a.b.b.AbstractC0005b
        public void a() {
            a.a.a.b.a(ShareLinkService.T, "aoa disconnected: ");
            ShareLinkService.this.d((ShareLinkService) false);
            if (ShareLinkService.this.F.g()) {
                ShareLinkService.this.a();
                ShareLinkService.this.a((ShareLinkService) false);
                ShareLinkService.this.F.a(a.b.a.c.g);
                ShareLinkService.this.H.a(false);
            } else if (ShareLinkService.this.F.h()) {
                ShareLinkService.this.H.a(5);
            } else {
                return;
            }
            ShareLinkService.this.F.a(a.b.a.c.c);
        }

        @Override // a.c.a.b.b.AbstractC0005b
        public void a(String str) {
            a.a.a.b.a(ShareLinkService.T, "usbPlugged: ");
            ShareLinkService.this.F.a(a.b.a.c.d);
            ShareLinkService.this.H.a(str);
        }

        @Override // a.c.a.b.b.AbstractC0005b
        public void b() {
            a.a.a.b.a(ShareLinkService.T, "usbConnected: ");
            ShareLinkService.this.F.a(a.b.a.c.e);
            ShareLinkService.this.H.b(3);
            ShareLinkService.this.d((ShareLinkService) true);
        }

        @Override // a.c.a.b.b.AbstractC0005b
        public void c() {
            a.a.a.b.a(ShareLinkService.T, "aoa NetworkReady: ");
            ShareLinkService.this.F.a(a.b.a.c.f);
            ShareLinkService.this.d((ShareLinkService) false);
            ShareLinkService.this.a((ShareLinkService) true);
            ShareLinkService.this.H.c();
            ShareLinkService.this.H.b(6);
            ShareLinkService.this.a((ShareLinkService) "127.0.0.1");
        }
    }

    public class g extends BluetoothLeObserver.b {
        private static final String k;

        public g() {
            ShareLinkService.this = r1;
        }

        private void b() {
            if (a.b.a.c.b().j()) {
                ShareLinkService.this.f();
                ShareLinkService.this.H.a(4);
            }
        }

        @Override // com.share.connect.ble.BluetoothLeObserver
        public void onClientInfoReceived(String str, String str2, String str3, int i, String str4, int i2, String str5, int i3) {
            boolean z = false;
            ShareLinkService.this.d((ShareLinkService) false);
            if (!ShareLinkService.this.F.c() || (ShareLinkService.this.E.c().equals(str) && !ShareLinkService.this.F.g())) {
                ShareLinkService.this.E.f();
                ShareLinkService.this.E.a(str).b(str2).a(i).c(str4);
                ShareLinkService.this.a((ShareLinkService) true);
                ShareLinkService.this.e();
                ShareLinkService.this.c((ShareLinkService) false);
                ShareLinkService.this.F.a(a.b.a.c.h);
                ShareLinkService.this.p = false;
                ShareLinkService shareLinkService = ShareLinkService.this;
                if (str3.startsWith(ShareLinkService.a0) && str5 == null) {
                    z = true;
                }
                shareLinkService.w = z;
                ShareLinkService.this.z = i3;
                ShareLinkService.this.H.a(str3);
                ShareLinkService.this.H.b(1);
                ShareLinkService.this.a((ShareLinkService) i2);
                return;
            }
            a.a.a.b.a(k, "new other client info received, need skip, because current state is " + ShareLinkService.this.F.a());
        }

        @Override // com.share.connect.ble.BluetoothLeObserver
        public void onDeviceLost(Device device) {
            ShareLinkService.this.H.a(device);
        }

        @Override // com.share.connect.ble.BluetoothLeObserver
        public void onDeviceMatch(Device device) {
            ShareLinkService.this.H.b(device);
        }

        @Override // com.share.connect.ble.BluetoothLeObserver
        public void onFailure(int i, int i2) {
            a.a.a.b.a(k, "onFailure: " + i + ", " + i2);
            if (i == 1) {
                ShareLinkService.this.b();
                ShareLinkService.this.b((ShareLinkService) false);
                ShareLinkService.this.H.c(false);
            } else if (i == 2) {
                ShareLinkService.this.r();
            } else {
                if (i == 3 || i == 5 || i == 6) {
                    if (i2 == 6) {
                        return;
                    }
                } else if (i != 4) {
                    return;
                }
                b();
            }
        }

        @Override // com.share.connect.ble.BluetoothLeObserver
        public void onPinAvailable(String str) {
        }

        @Override // com.share.connect.ble.BluetoothLeObserver
        public void onServerAddressSent() {
            ShareLinkService.this.H.b(4);
        }

        @Override // com.share.connect.ble.BluetoothLeObserver
        public void onServerInfoReceived(String str, String str2, String str3, int i) {
            try {
                a.a.a.b.a(k, "onServerInfoReceived");
                ShareLinkService.this.l.connectGroupOwner(str, str2, str3, i);
            } catch (RemoteException e) {
                a.a.a.b.b(k, "Connect group owner failed.", e);
                ShareLinkService.this.H.a(7);
            }
        }

        @Override // com.share.connect.ble.BluetoothLeObserver
        public void onSuccess(int i) {
            a.a.a.b.a(k, "onSuccess: " + i);
            if (i == 1) {
                ShareLinkService.this.b((ShareLinkService) false);
                ShareLinkService.this.H.c(true);
            }
            ShareLinkService.this.r = true;
        }
    }

    public class h extends WifiP2pObserver.b {
        private static final String l;

        public h() {
            ShareLinkService.this = r1;
        }

        @Override // com.share.connect.wifip2p.WifiP2pObserver
        public void onAcceptFailed() {
            a.a.a.b.e(l, "wifi onAcceptFailed, state=" + ShareLinkService.this.F.a());
            ShareLinkService.this.b((ShareLinkService) 3);
        }

        @Override // com.share.connect.wifip2p.WifiP2pObserver
        public void onConnectFailed() {
            a.a.a.b.e(l, "wifi onConnectFailed, state=" + ShareLinkService.this.F.a());
            ShareLinkService.this.b((ShareLinkService) 3);
        }

        @Override // com.share.connect.wifip2p.WifiP2pObserver
        public void onConnected() {
            ShareLinkService.this.k();
        }

        @Override // com.share.connect.wifip2p.WifiP2pObserver
        public void onCreateGroupFailed() {
            a.a.a.b.e(l, "wifi onCreateGroupFailed, state=" + ShareLinkService.this.F.a());
            ShareLinkService.this.b((ShareLinkService) 2);
        }

        @Override // com.share.connect.wifip2p.WifiP2pObserver
        public void onDeviceChanged(String str) {
            a.a.a.b.a(l, "onDeviceChanged: " + str);
            ShareLinkService.this.k.setP2pDeviceMac(str);
        }

        @Override // com.share.connect.wifip2p.WifiP2pObserver
        public void onDisconnected(boolean z) {
            ShareLinkService.this.e((ShareLinkService) z);
        }

        @Override // com.share.connect.wifip2p.WifiP2pObserver
        public void onGroupCreated(WifiP2pGroup wifiP2pGroup, int i) {
            String str;
            a.a.a.b.a(l, "onGroupCreated: Group owner mac: " + wifiP2pGroup.getOwner().deviceAddress + ", frequency: " + i);
            if ((ShareLinkService.this.getApplicationInfo().flags & 2) != 0) {
                a.a.a.b.c(l, "ssid:" + wifiP2pGroup.getNetworkName() + ", pw:" + wifiP2pGroup.getPassphrase());
            }
            if (ShareLinkService.this.F.j() && !ShareLinkService.this.p) {
                ShareLinkService.this.p = true;
                try {
                    a.a.a.b.a(l, "start UCarAuthService...");
                    ShareLinkService.this.H.b(2);
                    NetworkInterface byName = NetworkInterface.getByName(wifiP2pGroup.getInterface());
                    if (byName != null) {
                        Iterator it = Collections.list(byName.getInetAddresses()).iterator();
                        while (true) {
                            if (!it.hasNext()) {
                                break;
                            }
                            InetAddress inetAddress = (InetAddress) it.next();
                            if (inetAddress instanceof Inet4Address) {
                                str = inetAddress.getHostAddress();
                                break;
                            }
                        }
                    }
                    str = "0.0.0.0";
                    ShareLinkService.this.a((ShareLinkService) str);
                } catch (IOException e) {
                    a.a.a.b.b(l, "NetworkInterface.getByName cause exception" + e.toString());
                }
                try {
                    boolean z = ShareLinkService.this.s;
                    String str2 = ShareLinkService.Y;
                    String str3 = z ? wifiP2pGroup.getOwner().deviceAddress : str2;
                    if (ShareLinkService.this.v) {
                        ShareLinkService.this.n = new WifiOwnerConfig();
                        ShareLinkService.this.n.setSsid(wifiP2pGroup.getNetworkName());
                        ShareLinkService.this.n.setPassphrase(wifiP2pGroup.getPassphrase());
                        WifiOwnerConfig wifiOwnerConfig = ShareLinkService.this.n;
                        if (ShareLinkService.this.s) {
                            str2 = wifiP2pGroup.getOwner().deviceAddress;
                        }
                        wifiOwnerConfig.setMac(str2);
                        ShareLinkService.this.n.setFrequency(i);
                        ShareLinkService.this.n.setType(1001);
                        ShareLinkService.this.H.a(ShareLinkService.this.n);
                        ShareLinkService.this.k.notifyServerInfo(ShareLinkService.this.n.getSsid(), ShareLinkService.this.n.getPassphrase(), ShareLinkService.this.n.getMac(), ShareLinkService.this.n.getFrequency(), ShareLinkService.this.n.getType());
                        return;
                    }
                    ShareLinkService.this.k.notifyServerInfo(wifiP2pGroup.getNetworkName(), wifiP2pGroup.getPassphrase(), str3, i, 1001);
                } catch (RemoteException e2) {
                    a.a.a.b.b(l, "notifyServerInfo failed.", e2);
                    ShareLinkService.this.H.a(7);
                }
            }
        }

        @Override // com.share.connect.wifip2p.WifiP2pObserver
        public void onOpenFailed() {
            a.a.a.b.a(l, "onOpenFailed");
            ShareLinkService.this.b();
            ShareLinkService.this.b((ShareLinkService) false);
            ShareLinkService.this.H.c(false);
        }

        @Override // com.share.connect.wifip2p.WifiP2pObserver
        public void onOpenSuccess(String str, int i) {
            a.a.a.b.a(l, "onOpenSuccess: mac=" + str + ", bandSupported=" + i);
            if (ShareLinkService.this.l != null && a.b.a.c.b().j() && ShareLinkService.this.E.b() == 1001) {
                ShareLinkService.this.l.createGroupForClient(ShareLinkService.this.E.a(), ShareLinkService.this.E.e(), ShareLinkService.this.z);
            }
            ShareLinkService.this.y = 1001;
            if (ShareLinkService.this.k != null && !ShareLinkService.this.r) {
                ShareLinkService.this.k.registerBluetoothLeObserver(ShareLinkService.this.Q);
                ShareLinkService.this.k.setBandSupported(i);
                ShareLinkService.this.k.setP2pDeviceMac(str);
                ShareLinkService.this.k.open(ShareLinkService.this.f165a, ShareLinkService.this.b, ShareLinkService.this.e, ShareLinkService.this.f, ShareLinkService.this.g, ShareLinkService.this.h, ShareLinkService.this.q, ShareLinkService.this.i);
            }
        }
    }

    public class i extends WifiApObserver.b {
        private static final String j;

        public i() {
            ShareLinkService.this = r1;
        }

        @Override // com.share.connect.wifiap.WifiApObserver
        public void onAcceptFailed() {
            a.a.a.b.e(j, "wifi onAcceptFailed, state=" + ShareLinkService.this.F.a());
            ShareLinkService.this.b((ShareLinkService) 3);
        }

        @Override // com.share.connect.wifiap.WifiApObserver
        public void onApCreateFailed() {
            a.a.a.b.e(j, "softAp is created failed, state=" + ShareLinkService.this.F.a());
            ShareLinkService.this.b((ShareLinkService) 8);
        }

        @Override // com.share.connect.wifiap.WifiApObserver
        public void onApCreated(String str, String str2, String str3, String str4, int i2) {
            if (!ShareLinkService.this.F.j() || ShareLinkService.this.p) {
                a.a.a.b.a(j, "soft ap is created, but the ble is not coming");
                return;
            }
            a.a.a.b.a(j, "onApCreated start, ssid:" + str2 + " password:" + str3 + " macAddress:" + str4 + " frequency:" + i2 + " localAddress:" + str);
            ShareLinkService.this.p = true;
            ShareLinkService.this.H.b(2);
            ShareLinkService.this.a((ShareLinkService) str);
            try {
                if (!ShareLinkService.this.s) {
                    str4 = ShareLinkService.Y;
                }
                if (ShareLinkService.this.v) {
                    ShareLinkService.this.n = new WifiOwnerConfig();
                    ShareLinkService.this.n.setSsid(str2);
                    ShareLinkService.this.n.setPassphrase(str3);
                    ShareLinkService.this.n.setMac(str4);
                    ShareLinkService.this.n.setFrequency(i2);
                    ShareLinkService.this.n.setType(1002);
                    ShareLinkService.this.H.a(ShareLinkService.this.n);
                    ShareLinkService.this.k.notifyServerInfo(ShareLinkService.this.n.getSsid(), ShareLinkService.this.n.getPassphrase(), ShareLinkService.this.n.getMac(), ShareLinkService.this.n.getFrequency(), ShareLinkService.this.n.getType());
                    return;
                }
                ShareLinkService.this.k.notifyServerInfo(str2, str3, str4, i2, 1002);
            } catch (Exception e) {
                a.a.a.b.b(j, "notifyServerInfo failed.", e);
                ShareLinkService.this.H.a(7);
            }
        }

        @Override // com.share.connect.wifiap.WifiApObserver
        public void onConnected() {
            ShareLinkService.this.k();
        }

        @Override // com.share.connect.wifiap.WifiApObserver
        public void onDisconnected(boolean z) {
            ShareLinkService.this.e((ShareLinkService) z);
        }

        @Override // com.share.connect.wifiap.WifiApObserver
        public void onOpenFailed() {
            a.a.a.b.a(j, "onOpenFailed");
            ShareLinkService.this.b();
            ShareLinkService.this.b((ShareLinkService) false);
            ShareLinkService.this.H.c(false);
        }

        @Override // com.share.connect.wifiap.WifiApObserver
        public void onOpenSuccess() {
            a.a.a.b.a(j, "WifiApService onOpenSuccess");
            if (ShareLinkService.this.m != null && a.b.a.c.b().j() && ShareLinkService.this.E.b() == 1002) {
                ShareLinkService.this.m.createSoftAp(ShareLinkService.this.z, ShareLinkService.this.B, ShareLinkService.this.C, ShareLinkService.this.u, ShareLinkService.this.w);
            }
            ShareLinkService.this.y = 1002;
            if (ShareLinkService.this.k != null && !ShareLinkService.this.r) {
                ShareLinkService.this.k.registerBluetoothLeObserver(ShareLinkService.this.Q);
                ShareLinkService.this.k.open(ShareLinkService.this.f165a, ShareLinkService.this.b, ShareLinkService.this.e, ShareLinkService.this.f, ShareLinkService.this.g, ShareLinkService.this.h, ShareLinkService.this.q, ShareLinkService.this.i);
            }
        }
    }

    private void a() {
        a.c.b.z.g.b.b().a();
    }

    private void a(int i2) {
        IWifiP2p iWifiP2p;
        int a2;
        String e2;
        try {
            a.a.a.b.c(T, "expectConnectType = " + i2);
            if (i2 == 1002) {
                int i3 = this.x;
                if (i3 == 1001 && this.l != null) {
                    a.a.a.b.c(T, "phone expect ap connection, but car only supports p2p connection,use p2p instead, createGroupForClient.");
                    iWifiP2p = this.l;
                    a2 = this.E.a();
                    e2 = this.E.e();
                } else if (i3 == 1002 && this.m != null) {
                    a.a.a.b.c(T, "phone expect ap connection, car only supports ap connection,direct createSoftAp.");
                    this.m.createSoftAp(this.z, this.B, this.C, this.u, this.w);
                    return;
                } else if (i3 == 1003 && this.m != null) {
                    a.a.a.b.c(T, "phone expect ap connection, car supports ap&P2P connection, need close possible p2p connection and open ap");
                    this.E.b(1002);
                    IWifiP2p iWifiP2p2 = this.l;
                    if (iWifiP2p2 != null) {
                        iWifiP2p2.unregisterWifiP2pObserver(this.R);
                        this.l.close();
                    }
                    IWifiAp iWifiAp = this.m;
                    if (iWifiAp != null) {
                        iWifiAp.registerWifiApObserver(this.S);
                        this.m.open();
                        return;
                    }
                    return;
                } else {
                    return;
                }
            } else {
                int i4 = this.x;
                if (i4 == 1001 && this.l != null) {
                    a.a.a.b.a(T, "phone expect p2p connection, car only supports p2p connection, direct createGroupForClient.");
                    iWifiP2p = this.l;
                    a2 = this.E.a();
                    e2 = this.E.e();
                } else if (i4 == 1003) {
                    a.a.a.b.c(T, "phone expect p2p connection, car supports ap&P2P connection,need close possible ap connection and open p2p");
                    this.E.b(1001);
                    IWifiAp iWifiAp2 = this.m;
                    if (iWifiAp2 != null) {
                        iWifiAp2.unregisterWifiApObserver(this.S);
                        this.m.close();
                    }
                    IWifiP2p iWifiP2p3 = this.l;
                    if (iWifiP2p3 != null) {
                        iWifiP2p3.registerWifiP2pObserver(this.R);
                        this.l.open();
                        return;
                    }
                    return;
                } else {
                    a.a.a.b.c(T, "the phone don't support wireless connection. please plugin USB to connect");
                    f();
                    this.H.a(3);
                    return;
                }
            }
            iWifiP2p.createGroupForClient(a2, e2, this.z);
        } catch (Exception e3) {
            a.a.a.b.b(T, "create wifi connection failed." + e3.toString());
            f();
        }
    }

    private void a(String str) {
        if (this.o != null) {
            a.a.a.b.a(T, "stop existing auth service");
            this.o.a();
        }
        a.b.a.h.d dVar = new a.b.a.h.d(this, new d(), this.A, this.j, this.D);
        this.o = dVar;
        dVar.b(str);
    }

    private void a(boolean z2) {
        a.a.a.b.c(T, "enableAuthTimeout: " + z2);
        this.J.removeCallbacks(this.L);
        if (z2) {
            this.J.postDelayed(this.L, 20000);
        }
    }

    private void b(int i2) {
        if (this.F.j() || this.F.i()) {
            f();
            this.H.a(i2);
            return;
        }
        a.a.a.b.a(T, "ignored wifi connect event");
    }

    private void b(boolean z2) {
        a.a.a.b.c(T, "enableOpenTimeout: " + z2);
        if (z2) {
            this.J.postDelayed(this.K, 10000);
        } else {
            this.J.removeCallbacks(this.K);
        }
    }

    private void c() {
        try {
            IBluetoothLe iBluetoothLe = this.k;
            if (iBluetoothLe != null) {
                iBluetoothLe.unregisterBluetoothLeObserver(this.Q);
                this.k.close();
            }
        } catch (Exception e2) {
            a.a.a.b.b(T, "closeBluetooth failed", e2);
        }
    }

    private void c(boolean z2) {
        a.a.a.b.a(T, "set usb device scanning: " + z2);
        if (!z2) {
            a.c.a.b.b.d().s();
        } else if (this.F.f()) {
            a.c.a.b.b.d().o();
        } else {
            a.a.a.b.c(T, "Can't enable usb device scanning, current state is " + a.b.a.c.b().a());
        }
    }

    private void d(boolean z2) {
        a.a.a.b.c(T, "enableUserConfirmTimeout: " + z2);
        this.J.removeCallbacks(this.M);
        if (z2) {
            this.J.postDelayed(this.M, 15000);
        }
    }

    private void e() {
        a.c.a.b.b.d().c();
    }

    private void e(boolean z2) {
        a.a.a.b.e(T, "wifi onDisconnected, state=" + this.F.a());
        if (this.F.j() || this.F.i()) {
            a.b.a.i.g.a();
            a(false);
            f();
            if (this.o != null) {
                a.a.a.b.e(T, "key negotiation ongoing");
                this.o.a();
                this.H.a(6);
            } else {
                this.H.a(z2);
            }
            this.F.a(a.b.a.c.c);
            return;
        }
        a.a.a.b.a(T, "ignored wifi event");
    }

    private void f() {
        try {
            IBluetoothLe iBluetoothLe = this.k;
            if (iBluetoothLe != null) {
                iBluetoothLe.disconnect();
            }
            IWifiP2p iWifiP2p = this.l;
            if (iWifiP2p != null && this.y == 1001) {
                iWifiP2p.cancelConnect();
            }
            IWifiAp iWifiAp = this.m;
            if (iWifiAp != null && this.y == 1002) {
                iWifiAp.cancelConnect();
            }
        } catch (Exception e2) {
            a.a.a.b.e(T, "Disconnect failed.", e2);
        }
    }

    private boolean g() {
        try {
            IWifiP2p iWifiP2p = this.l;
            if (iWifiP2p != null && this.y == 1001) {
                return iWifiP2p.isUserClose();
            }
            IWifiAp iWifiAp = this.m;
            if (iWifiAp == null || this.y != 1002) {
                return false;
            }
            return iWifiAp.isUserClose();
        } catch (Exception e2) {
            a.a.a.b.e(T, "isUserClose failed.", e2);
            return false;
        }
    }

    /* access modifiers changed from: public */
    private /* synthetic */ void h() {
        a.a.a.b.e(T, "No response in 10000current state:" + this.F.a());
        if (this.F.f()) {
            a.a.a.b.e(T, "take this as open failure.");
            b();
            this.H.c(false);
        }
    }

    /* access modifiers changed from: public */
    private /* synthetic */ void i() {
        a.a.a.b.e(T, "key negotiation not finished in 20000");
        l();
        this.H.a(6);
    }

    /* access modifiers changed from: public */
    private /* synthetic */ void j() {
        a.a.a.b.e(T, "user confirm not finished in 15000");
        this.H.b(true);
    }

    private void k() {
        a.a.a.b.e(T, "wifi onConnected, state = " + this.F.a());
        a.b.a.i.g.a(this);
        if (this.F.j()) {
            this.H.c();
            this.H.b(5);
            this.F.a(a.b.a.c.i);
            return;
        }
        a.a.a.b.a(T, "ignored wifi event");
    }

    private void l() {
        f();
        e();
        this.F.a(a.b.a.c.c);
        this.E.f();
    }

    private void m() {
        if (this.f165a != null) {
            this.d = BaseEncoding.base64().encode(Arrays.copyOfRange(BaseEncoding.base16().lowerCase().decode(this.f165a.toLowerCase()), 0, 6));
        }
        a.c.a.b.b.d().a(getApplicationContext(), this.b, this.f165a, this.f, this.d, this.g, this.h);
        a.c.a.b.b.d().a(new f());
    }

    private void n() {
        a.a.a.b.c(T, "start BluetoothLe service");
        Intent intent = new Intent();
        intent.setClass(this, BluetoothLeService.class);
        bindService(intent, this.N, 1);
    }

    private void p() {
        n();
        q();
        m();
    }

    private void q() {
        a.a.a.b.c(T, "start Wifi service");
        bindService(new Intent().setClass(this, WifiP2pService.class), this.O, 1);
        bindService(new Intent().setClass(this, WifiApService.class), this.P, 1);
    }

    public void a(int i2, int i3, String str, String str2) {
        this.x = i2;
        this.A = i3;
        this.B = str;
        this.C = str2;
        this.r = false;
        if (i2 == 1001 || i2 == 1002 || i2 == 1003) {
            this.F.a(a.b.a.c.c);
            k.a(new e(), "Open() waiting services");
            return;
        }
        a.a.a.b.b(T, "wifi connectType error !!!");
    }

    public void b() {
        a.a.a.b.c(T, "Closing...");
        a.b.a.i.g.a();
        try {
            this.t = false;
            IWifiP2p iWifiP2p = this.l;
            if (iWifiP2p != null && this.y == 1001) {
                iWifiP2p.unregisterWifiP2pObserver(this.R);
                this.l.close();
            }
            IWifiAp iWifiAp = this.m;
            if (iWifiAp != null && this.y == 1002) {
                iWifiAp.unregisterWifiApObserver(this.S);
                this.m.close();
            }
            c();
            a.b.a.h.d dVar = this.o;
            if (dVar != null) {
                dVar.a();
            }
            a.c.a.b.b.d().c();
            this.F.a(a.b.a.c.c);
            this.E.f();
        } catch (Exception e2) {
            a.a.a.b.b(T, "close failed", e2);
        }
    }

    public void d() {
        a.a.a.b.a(T, "Disconnect.");
        a();
        b(false);
        l();
        this.H.a(g());
    }

    public void o() {
        IBluetoothLe iBluetoothLe = this.k;
        if (iBluetoothLe != null) {
            try {
                iBluetoothLe.startScan();
            } catch (Exception e2) {
                a.a.a.b.b(T, "Scan devices failed.", e2);
            }
        }
    }

    public IBinder onBind(Intent intent) {
        this.f165a = intent.getStringExtra("car_id");
        this.b = intent.getStringExtra("mod_id");
        this.e = intent.getStringExtra("car_name");
        this.f = intent.getStringExtra("car_short_name");
        this.g = intent.getStringExtra("protocol_version");
        this.h = intent.getStringExtra("car_vendor_custom_data");
        this.i = intent.getIntExtra("ble_rssi_threshold", Z);
        boolean z2 = true;
        StringBuilder append = new StringBuilder().append("onBind called id = ").append(this.f165a != null).append(" mod = ").append(this.b != null).append(" name = ").append(this.f != null).append(" custom data = ");
        if (this.h == null) {
            z2 = false;
        }
        a.a.a.b.c(T, append.append(z2).toString());
        p();
        return this.I;
    }

    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:3:0x0010 */
    public void onDestroy() {
        super.onDestroy();
        b();
        this.H.a();
        unbindService(this.N);
        try {
            unbindService(this.O);
        } catch (Exception unused) {
        }
    }

    public void r() {
        IBluetoothLe iBluetoothLe = this.k;
        if (iBluetoothLe != null) {
            try {
                iBluetoothLe.stopScan();
            } catch (Exception e2) {
                a.a.a.b.b(T, "Stop scan failed.", e2);
            }
        }
    }
}
