package com.share.connect.wifip2p;

import a.b.a.k.d;
import a.b.a.k.f.a;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pGroup;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;
import com.share.connect.wifip2p.IWifiP2p;
import com.share.connect.wifip2p.WifiP2pService;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class WifiP2pService extends Service {
    private static final int A = 13;
    private static final int B = 14;
    private static final int C = 50;
    private static final int D = 3000;
    private static final String s = "WifiP2pService";
    private static final String t = "android.net.wifi.WIFI_AP_STATE_CHANGED";
    private static final String u = "wifi_state";
    private static final int v = 5000;
    private static final int w = 20000;
    private static final int x = 30000;
    private static final int y = 11;
    private static final int z = 12;

    /* renamed from: a  reason: collision with root package name */
    private final p f202a = new p(this, null);
    private WifiP2pManager b;
    private WifiP2pManager.Channel c;
    private WifiManager d;
    private Runnable e;
    private Handler f;
    private volatile boolean g = false;
    private boolean h = false;
    private o i;
    private Thread j;
    private ReentrantLock k = new ReentrantLock();
    private CountDownLatch l;
    private final Runnable m = new Runnable() {
        /* class com.share.connect.wifip2p.$$Lambda$WifiP2pService$OMjJ8vzW38S3Ctu6y156GDzup8 */

        public final void run() {
            WifiP2pService.m15lambda$OMjJ8vzW38S3Ctu6y156GDzup8(WifiP2pService.this);
        }
    };
    private final Runnable n = new Runnable() {
        /* class com.share.connect.wifip2p.$$Lambda$WifiP2pService$f3VdE_iXUMXRuVxsKtT2VT5SiU */

        public final void run() {
            WifiP2pService.m16lambda$f3VdE_iXUMXRuVxsKtT2VT5SiU(WifiP2pService.this);
        }
    };
    private final a.b.a.k.d o = new a.b.a.k.d();
    private BroadcastReceiver p;
    private String q;
    private final List<WifiP2pObserver> r = new ArrayList();

    public class a extends a.b.a.k.b {
        public a(String str, String str2) {
            super(str, str2);
        }

        @Override // a.b.a.k.b
        public void onFailure(int i) {
            super.onFailure(i);
            WifiP2pService.this.o();
            WifiP2pService.this.j();
        }
    }

    public class b implements Runnable {

        /* renamed from: a  reason: collision with root package name */
        public final /* synthetic */ Runnable f203a;

        public b(Runnable runnable) {
            this.f203a = runnable;
        }

        public void run() {
            if (WifiP2pService.this.c != null) {
                TimeUnit timeUnit = TimeUnit.SECONDS;
                try {
                    CountDownLatch countDownLatch = new CountDownLatch(1);
                    WifiP2pService.this.a((WifiP2pService) new a.b.a.k.a(WifiP2pService.s, "Stop peer discovery", countDownLatch));
                    if (!countDownLatch.await(5, timeUnit)) {
                        a.a.a.b.e(WifiP2pService.s, "Stop peer discovery timeout.");
                    }
                    a.a.a.b.a(WifiP2pService.s, "Removing group...");
                    CountDownLatch countDownLatch2 = new CountDownLatch(1);
                    WifiP2pService.this.b.removeGroup(WifiP2pService.this.c, new a.b.a.k.a(WifiP2pService.s, "Remove group", countDownLatch2));
                    if (!countDownLatch2.await(5, timeUnit)) {
                        a.a.a.b.e(WifiP2pService.s, "Remove group timeout.");
                    }
                    a.a.a.b.a(WifiP2pService.s, "Reset p2p channel...");
                    CountDownLatch countDownLatch3 = new CountDownLatch(1);
                    if (a.b.a.k.f.c.b(WifiP2pService.this.b, WifiP2pService.this.c, 0, new a.b.a.k.a(WifiP2pService.s, "Reset channel", countDownLatch3)) && !countDownLatch3.await(5, timeUnit)) {
                        a.a.a.b.e(WifiP2pService.s, "Reset p2p channel timeout.");
                    }
                    a.a.a.b.a(WifiP2pService.s, "Cancel invited requests...");
                    CountDownLatch countDownLatch4 = new CountDownLatch(1);
                    WifiP2pService.this.b.cancelConnect(WifiP2pService.this.c, new a.b.a.k.a(WifiP2pService.s, "Cancel invited requests", countDownLatch4));
                    if (!countDownLatch4.await(5, timeUnit)) {
                        a.a.a.b.e(WifiP2pService.s, "Cancel invited requests timeout.");
                    }
                    a.b.a.k.f.c.a(WifiP2pService.this.b, WifiP2pService.this.c);
                } catch (Exception e) {
                    a.a.a.b.b(WifiP2pService.s, "Latch await failed.", e);
                }
            } else {
                a.a.a.b.e(WifiP2pService.s, "Channel is null, resetP2pNetworks is not executable");
            }
            if (this.f203a != null) {
                WifiP2pService.this.f.post(this.f203a);
            }
        }
    }

    public class c implements d.a {

        /* renamed from: a  reason: collision with root package name */
        public final /* synthetic */ a.b.a.k.f.a f204a;

        public class a implements WifiP2pManager.GroupInfoListener {
            public a() {
            }

            /* access modifiers changed from: private */
            public /* synthetic */ void a(WifiP2pGroup wifiP2pGroup, a.b.a.k.f.a aVar) {
                WifiP2pService.this.a((WifiP2pService) wifiP2pGroup, (WifiP2pGroup) aVar.b());
            }

            public void onGroupInfoAvailable(WifiP2pGroup wifiP2pGroup) {
                a.a.a.b.a(WifiP2pService.s, "Actually group: " + wifiP2pGroup);
                if (wifiP2pGroup != null) {
                    a.b.a.i.e.a("P2p-createGroup");
                    c cVar = c.this;
                    WifiP2pService.this.e = new Runnable(wifiP2pGroup, cVar.f204a) {
                        /* class com.share.connect.wifip2p.$$Lambda$WifiP2pService$c$a$SK3_h0l19zzIhtSV1FufwLiBB7M */
                        public final /* synthetic */ WifiP2pGroup f$1;
                        public final /* synthetic */ a f$2;

                        {
                            this.f$1 = r2;
                            this.f$2 = r3;
                        }

                        public final void run() {
                            WifiP2pService.c.a.lambda$SK3_h0l19zzIhtSV1FufwLiBB7M(WifiP2pService.c.a.this, this.f$1, this.f$2);
                        }
                    };
                    WifiP2pService.this.e.run();
                    return;
                }
                a.a.a.b.e(WifiP2pService.s, "Group in broadcast isn't null but request return null.");
            }
        }

        public class b implements d.a {
            public b() {
            }

            @Override // a.b.a.k.d.a
            public void a(WifiP2pInfo wifiP2pInfo, WifiP2pGroup wifiP2pGroup, NetworkInfo networkInfo) {
                boolean z;
                if (wifiP2pGroup != null) {
                    z = WifiP2pService.this.a((WifiP2pService) wifiP2pGroup.getClientList());
                    a.a.a.b.a(WifiP2pService.s, "hasValidClient=" + z + ", clientConnected=" + WifiP2pService.this.g);
                } else {
                    z = false;
                }
                if (z) {
                    WifiP2pService.this.b((WifiP2pService) false);
                    if (!WifiP2pService.this.g && wifiP2pInfo != null) {
                        WifiP2pService.this.g = true;
                        a.b.a.i.e.a("P2p-clientJoin");
                        WifiP2pService.this.k();
                    }
                } else if (WifiP2pService.this.d()) {
                    a.a.a.b.a(WifiP2pService.s, "ignored acceptance timeout");
                } else if (WifiP2pService.this.g) {
                    WifiP2pService.this.g = false;
                    WifiP2pService.this.l();
                }
            }
        }

        public c(a.b.a.k.f.a aVar) {
            this.f204a = aVar;
        }

        /* access modifiers changed from: private */
        public /* synthetic */ void a(WifiP2pGroup wifiP2pGroup, a.b.a.k.f.a aVar) {
            WifiP2pService.this.a((WifiP2pService) wifiP2pGroup, (WifiP2pGroup) aVar.b());
        }

        @Override // a.b.a.k.d.a
        public void a(WifiP2pInfo wifiP2pInfo, WifiP2pGroup wifiP2pGroup, NetworkInfo networkInfo) {
            if (wifiP2pInfo != null && wifiP2pInfo.groupFormed && wifiP2pInfo.isGroupOwner && wifiP2pGroup != null) {
                if (WifiP2pService.this.e()) {
                    WifiP2pService.this.b.requestGroupInfo(WifiP2pService.this.c, new a());
                } else {
                    a.b.a.i.e.a("P2p-createGroup");
                    WifiP2pService.this.e = new Runnable(wifiP2pGroup, this.f204a) {
                        /* class com.share.connect.wifip2p.$$Lambda$WifiP2pService$c$fVQsG92mw1YzzAX09SNTA2SZ0oM */
                        public final /* synthetic */ WifiP2pGroup f$1;
                        public final /* synthetic */ a f$2;

                        {
                            this.f$1 = r2;
                            this.f$2 = r3;
                        }

                        public final void run() {
                            WifiP2pService.c.lambda$fVQsG92mw1YzzAX09SNTA2SZ0oM(WifiP2pService.c.this, this.f$1, this.f$2);
                        }
                    };
                    WifiP2pService.this.e.run();
                }
                a.b.a.i.e.b("P2p-clientJoin");
                WifiP2pService.this.o.a(new b());
            }
        }
    }

    public class d implements d.a {

        /* renamed from: a  reason: collision with root package name */
        private boolean f207a = false;

        public d() {
        }

        @Override // a.b.a.k.d.a
        public void a(WifiP2pInfo wifiP2pInfo, WifiP2pGroup wifiP2pGroup, NetworkInfo networkInfo) {
            String str;
            if (!this.f207a && wifiP2pInfo != null && wifiP2pInfo.groupFormed && !wifiP2pInfo.isGroupOwner && wifiP2pGroup != null && TextUtils.equals(wifiP2pGroup.getOwner().deviceAddress, WifiP2pService.this.q)) {
                this.f207a = true;
                WifiP2pService.this.r();
                WifiP2pService.this.c((WifiP2pService) false);
                WifiP2pService.this.a((WifiP2pService) wifiP2pInfo.groupOwnerAddress, (InetAddress) wifiP2pGroup.getInterface(), (String) wifiP2pInfo.isGroupOwner);
            }
            if (wifiP2pInfo != null && this.f207a && !wifiP2pInfo.groupFormed) {
                this.f207a = false;
                WifiP2pService.this.o();
                WifiP2pService.this.l();
            }
            if (networkInfo != null) {
                String typeName = networkInfo.getTypeName();
                if (!TextUtils.isEmpty(typeName) && typeName.toUpperCase().contains("P2P") && networkInfo.getState() == NetworkInfo.State.DISCONNECTED && networkInfo.getDetailedState() == NetworkInfo.DetailedState.FAILED) {
                    if (this.f207a) {
                        this.f207a = false;
                        WifiP2pService.this.o();
                        WifiP2pService.this.l();
                        return;
                    }
                    str = "NetworkInfo.getDetailedState(): FAILED";
                } else {
                    return;
                }
            } else {
                str = "NetworkInfo is null FAILED";
            }
            a.a.a.b.e(WifiP2pService.s, str);
            WifiP2pService.this.o();
            WifiP2pService.this.j();
        }
    }

    public class e implements Runnable {

        /* renamed from: a  reason: collision with root package name */
        public final /* synthetic */ InetAddress f208a;
        public final /* synthetic */ String b;

        public e(InetAddress inetAddress, String str) {
            this.f208a = inetAddress;
            this.b = str;
        }

        public void run() {
            try {
                a.a.a.b.a(WifiP2pService.s, "Group owner address: " + this.f208a.getHostAddress() + ", local address: " + a.b.a.k.c.b(NetworkInterface.getByName(this.b)));
                boolean z = false;
                int i = 0;
                while (true) {
                    if (i >= 10) {
                        break;
                    } else if (this.f208a.isReachable(200)) {
                        a.a.a.b.a(WifiP2pService.s, "isReachable=true, ready to notifyOnConnected.");
                        a.b.a.i.e.a("P2p-connectGroup");
                        WifiP2pService.this.k();
                        z = true;
                        break;
                    } else {
                        a.a.a.b.e(WifiP2pService.s, "isReachable=false, waiting...");
                        Thread.sleep(200);
                        i++;
                    }
                }
                if (!z) {
                    a.a.a.b.b(WifiP2pService.s, "Network can't reach.");
                    WifiP2pService.this.j();
                }
            } catch (IOException | InterruptedException e) {
                a.a.a.b.b(WifiP2pService.s, "Checking isReachable exception", e);
                WifiP2pService.this.j();
            }
        }
    }

    public class f extends BroadcastReceiver {
        public f() {
        }

        public void onReceive(Context context, Intent intent) {
            if (intent.getIntExtra(WifiP2pService.u, 4) == 3) {
                WifiP2pService.this.unregisterReceiver(this);
                WifiP2pService.this.c();
            }
        }
    }

    public class g extends BroadcastReceiver {
        public g() {
        }

        public void onReceive(Context context, Intent intent) {
            if (WifiP2pService.t.equals(intent.getAction()) && 11 == intent.getIntExtra(WifiP2pService.u, 4)) {
                a.a.a.b.c(WifiP2pService.s, "SoftAp has been disabled");
                WifiP2pService.this.unregisterReceiver(this);
                WifiP2pService.this.l.countDown();
            }
        }
    }

    public class h implements WifiP2pManager.DeviceInfoListener {
        public h() {
        }

        public void onDeviceInfoAvailable(WifiP2pDevice wifiP2pDevice) {
            if (wifiP2pDevice != null) {
                WifiP2pService wifiP2pService = WifiP2pService.this;
                wifiP2pService.a((WifiP2pService) wifiP2pDevice.deviceAddress, (String) (a.b.a.k.e.c(wifiP2pService.d) ? 1 : 0));
                return;
            }
            a.a.a.b.b(WifiP2pService.s, "Device info is null.");
            WifiP2pService.this.m();
        }
    }

    public class i implements d.c {

        public class a implements WifiP2pManager.DeviceInfoListener {
            public a() {
            }

            public void onDeviceInfoAvailable(WifiP2pDevice wifiP2pDevice) {
                if (wifiP2pDevice != null) {
                    a.a.a.b.c(WifiP2pService.s, "Device changed: " + wifiP2pDevice);
                    WifiP2pService.this.a((WifiP2pService) wifiP2pDevice.deviceAddress);
                    return;
                }
                a.a.a.b.b(WifiP2pService.s, "Device info is null.");
            }
        }

        public i() {
        }

        @Override // a.b.a.k.d.c
        public void a(WifiP2pDevice wifiP2pDevice) {
            WifiP2pService.this.k.lock();
            try {
                if (WifiP2pService.this.c == null) {
                    a.a.a.b.e(WifiP2pService.s, "onThisDeviceChanged: mChannel is null, return!");
                } else {
                    a.a.a.b.a(WifiP2pService.s, "Requesting device info...");
                    WifiP2pService.this.b.requestDeviceInfo(WifiP2pService.this.c, new a());
                }
            } finally {
                WifiP2pService.this.k.unlock();
            }
        }
    }

    public class j implements d.c {
        public j() {
        }

        @Override // a.b.a.k.d.c
        public void a(WifiP2pDevice wifiP2pDevice) {
            WifiP2pService.this.o.a((d.c) null);
            WifiP2pService wifiP2pService = WifiP2pService.this;
            wifiP2pService.a((WifiP2pService) wifiP2pDevice.deviceAddress, (String) (a.b.a.k.e.c(wifiP2pService.d) ? 1 : 0));
        }
    }

    public class k implements Runnable {

        /* renamed from: a  reason: collision with root package name */
        public final /* synthetic */ a.b.a.k.f.a f215a;
        public final /* synthetic */ WifiP2pConfig b;
        public final /* synthetic */ int c;

        public class a extends a.b.a.k.b {
            public a(String str, String str2) {
                super(str, str2);
            }

            @Override // a.b.a.k.b
            public void onFailure(int i) {
                super.onFailure(i);
                WifiP2pService.this.p();
                WifiP2pService.this.h();
            }
        }

        public class b extends a.b.a.k.b {
            public final /* synthetic */ WifiP2pManager.ActionListener c;

            /* JADX INFO: super call moved to the top of the method (can break code semantics) */
            public b(String str, String str2, WifiP2pManager.ActionListener actionListener) {
                super(str, str2);
                this.c = actionListener;
            }

            @Override // a.b.a.k.b
            public void onFailure(int i) {
                super.onFailure(i);
                k.this.f215a.b(0);
                WifiP2pService.this.b.createGroup(WifiP2pService.this.c, this.c);
            }

            @Override // a.b.a.k.b
            public void onSuccess() {
                super.onSuccess();
                WifiP2pService.this.b.createGroup(WifiP2pService.this.c, this.c);
            }
        }

        public k(a.b.a.k.f.a aVar, WifiP2pConfig wifiP2pConfig, int i) {
            this.f215a = aVar;
            this.b = wifiP2pConfig;
            this.c = i;
        }

        public void run() {
            a.b.a.i.e.b("P2p-createGroup");
            WifiP2pService.this.a((WifiP2pService) this.f215a);
            a aVar = new a(WifiP2pService.s, "Create group");
            if (!WifiP2pService.this.e() || this.b == null) {
                a.a.a.b.c(WifiP2pService.s, "Method selected: Classic");
                a.b.a.k.f.c.b(WifiP2pService.this.b, WifiP2pService.this.c, this.c, new b(WifiP2pService.s, "Set channel to " + this.c, aVar));
                return;
            }
            a.a.a.b.c(WifiP2pService.s, "Method selected: Q");
            WifiP2pService.this.b.createGroup(WifiP2pService.this.c, this.b, aVar);
        }
    }

    public class l implements Runnable {

        /* renamed from: a  reason: collision with root package name */
        public final /* synthetic */ WifiP2pConfig f216a;

        public class a extends a.b.a.k.b {
            public a(String str, String str2) {
                super(str, str2);
            }

            @Override // a.b.a.k.b
            public void onFailure(int i) {
                super.onFailure(i);
                WifiP2pService.this.o();
                WifiP2pService.this.j();
            }
        }

        public l(WifiP2pConfig wifiP2pConfig) {
            this.f216a = wifiP2pConfig;
        }

        public void run() {
            a.b.a.i.e.b("P2p-connectGroup");
            WifiP2pService.this.q();
            WifiP2pService.this.b.connect(WifiP2pService.this.c, this.f216a, new a(WifiP2pService.s, "Connect group on Q"));
        }
    }

    public class m implements Runnable {
        public m() {
        }

        public void run() {
            a.b.a.i.e.b("P2p-discoverPeer");
            WifiP2pService.this.a((WifiP2pService) false);
        }
    }

    public class n implements Runnable {

        public class a implements d.b {
            public a() {
            }

            @Override // a.b.a.k.d.b
            public void a(WifiP2pDeviceList wifiP2pDeviceList) {
                WifiP2pDevice wifiP2pDevice = wifiP2pDeviceList.get(WifiP2pService.this.q);
                if (wifiP2pDevice != null) {
                    WifiP2pService.this.f.removeCallbacks(WifiP2pService.this.i);
                    WifiP2pService.this.o.a((d.b) null);
                    a.b.a.i.e.a("P2p-discoverPeer");
                    WifiP2pService.this.a((WifiP2pService) wifiP2pDevice);
                }
            }
        }

        public class b extends a.b.a.k.b {
            public b(String str, String str2) {
                super(str, str2);
            }

            @Override // a.b.a.k.b
            public void onFailure(int i) {
                super.onFailure(i);
                WifiP2pService.this.f.removeCallbacks(WifiP2pService.this.i);
                WifiP2pService.this.o();
                WifiP2pService.this.j();
            }
        }

        public n() {
        }

        public void run() {
            WifiP2pService.this.o.a(new a());
            WifiP2pService.this.b.discoverPeers(WifiP2pService.this.c, new b(WifiP2pService.s, "Discover peers"));
        }
    }

    public class o implements Runnable {
        private o() {
        }

        public /* synthetic */ o(WifiP2pService wifiP2pService, f fVar) {
            this();
        }

        public void run() {
            a.a.a.b.e(WifiP2pService.s, "Haven't found p2p device in a single scan duration, rescanning.");
            WifiP2pService.this.a((WifiP2pService) true);
        }
    }

    public class p extends IWifiP2p.b {
        private p() {
        }

        public /* synthetic */ p(WifiP2pService wifiP2pService, f fVar) {
            this();
        }

        /* access modifiers changed from: private */
        public /* synthetic */ void a(int i, int i2) {
            WifiP2pService.this.a(i, i2);
        }

        @Override // com.share.connect.wifip2p.IWifiP2p
        public void cancelConnect() {
            WifiP2pService.this.c((WifiP2pService) false);
            WifiP2pService.this.b((WifiP2pService) false);
            WifiP2pService.this.p();
        }

        @Override // com.share.connect.wifip2p.IWifiP2p
        public void close() {
            WifiP2pService.this.a();
        }

        @Override // com.share.connect.wifip2p.IWifiP2p
        public void connectGroupOwner(String str, String str2, String str3, int i) {
            WifiP2pService.this.a(str, str2, str3, i);
        }

        @Override // com.share.connect.wifip2p.IWifiP2p
        public void createGroupForClient(int i, String str, int i2) {
            a.a.a.b.c(WifiP2pService.s, "Creating group for client: " + i + ", " + str);
            WifiP2pService.this.b((WifiP2pService) true);
            if (WifiP2pService.this.e != null) {
                a.a.a.b.c(WifiP2pService.s, "reuse existing Wifi p2p group");
                WifiP2pService.this.e.run();
            } else if (!WifiP2pService.this.h) {
                WifiP2pService.this.h = true;
                new Thread(new Runnable(i, i2) {
                    /* class com.share.connect.wifip2p.$$Lambda$WifiP2pService$p$taNhNSAXppnU1uwan5t8j4wmXdg */
                    public final /* synthetic */ int f$1;
                    public final /* synthetic */ int f$2;

                    {
                        this.f$1 = r2;
                        this.f$2 = r3;
                    }

                    public final void run() {
                        WifiP2pService.p.lambda$taNhNSAXppnU1uwan5t8j4wmXdg(WifiP2pService.p.this, this.f$1, this.f$2);
                    }
                }).start();
            } else {
                a.a.a.b.c(WifiP2pService.s, "createWifiP2pGroup func has been called, but Don't receive p2p broadcast!");
            }
        }

        @Override // com.share.connect.wifip2p.IWifiP2p
        public boolean isUserClose() {
            return !WifiP2pService.this.d.isWifiEnabled();
        }

        @Override // com.share.connect.wifip2p.IWifiP2p
        public void open() {
            WifiP2pService.this.n();
        }

        @Override // com.share.connect.wifip2p.IWifiP2p
        public void registerWifiP2pObserver(WifiP2pObserver wifiP2pObserver) {
            WifiP2pService.this.a(wifiP2pObserver);
        }

        @Override // com.share.connect.wifip2p.IWifiP2p
        public void unregisterWifiP2pObserver(WifiP2pObserver wifiP2pObserver) {
            WifiP2pService.this.b(wifiP2pObserver);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(a.b.a.k.f.a aVar) {
        this.g = false;
        this.o.a(new c(aVar));
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(WifiP2pDevice wifiP2pDevice) {
        a.a.a.b.c(s, "Found target device: " + wifiP2pDevice.deviceAddress);
        a.b.a.i.e.b("P2p-connectGroup");
        WifiP2pConfig wifiP2pConfig = new WifiP2pConfig();
        wifiP2pConfig.deviceAddress = wifiP2pDevice.deviceAddress;
        q();
        this.b.connect(this.c, wifiP2pConfig, new a(s, "Connect device: " + wifiP2pConfig.deviceAddress));
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void a(WifiP2pDeviceList wifiP2pDeviceList) {
        if (!a(wifiP2pDeviceList.getDeviceList())) {
            a.a.a.b.e(s, "No client join. notifyOnDisconnected");
            this.g = false;
            o();
            l();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(WifiP2pGroup wifiP2pGroup, int i2) {
        synchronized (this.r) {
            for (WifiP2pObserver wifiP2pObserver : this.r) {
                try {
                    wifiP2pObserver.onGroupCreated(wifiP2pGroup, i2);
                } catch (RemoteException e2) {
                    a.a.a.b.b(s, "Observer.onGroupCreated(...) failed.", e2);
                }
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(WifiP2pManager.ActionListener actionListener) {
        a.a.a.b.c(s, "stopPeerDiscovery...");
        this.o.a((d.b) null);
        if (this.c != null) {
            if (actionListener == null) {
                actionListener = new a.b.a.k.b(s, "Stop peer discovery");
            }
            this.b.stopPeerDiscovery(this.c, actionListener);
        }
    }

    private void a(Runnable runnable) {
        a.a.a.b.c(s, "resetP2pNetworks...");
        Thread thread = this.j;
        if (thread != null) {
            thread.interrupt();
            try {
                this.j.join(50);
            } catch (Exception e2) {
                a.a.a.b.b(s, "Wait last reset thread error.", e2);
            }
            this.j = null;
        }
        Thread thread2 = new Thread(new b(runnable), "Reset networks");
        this.j = thread2;
        thread2.start();
        this.e = null;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(String str) {
        synchronized (this.r) {
            for (WifiP2pObserver wifiP2pObserver : this.r) {
                try {
                    wifiP2pObserver.onDeviceChanged(str);
                } catch (RemoteException e2) {
                    a.a.a.b.b(s, "Observer.onDeviceChanged(" + str + ") failed.", e2);
                }
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(String str, int i2) {
        synchronized (this.r) {
            for (WifiP2pObserver wifiP2pObserver : this.r) {
                try {
                    wifiP2pObserver.onOpenSuccess(str, i2);
                } catch (RemoteException e2) {
                    a.a.a.b.b(s, "Observer.onOpenSuccess(" + str + ", " + i2 + ") failed.", e2);
                }
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(InetAddress inetAddress, String str, boolean z2) {
        a.b.a.i.k.a(new e(inetAddress, str), "Check address reachable");
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(boolean z2) {
        r();
        this.f.postDelayed(new n(), z2 ? 500 : 0);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private boolean a(Collection<WifiP2pDevice> collection) {
        return !collection.isEmpty();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void b(boolean z2) {
        a.a.a.b.c(s, "setAcceptTimeout: " + z2);
        if (d()) {
            this.f.removeCallbacks(this.n);
        }
        if (z2) {
            this.f.postDelayed(this.n, 30000);
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:26:0x00ec  */
    private boolean b() {
        String str;
        Exception e2;
        int intValue = ((Integer) WifiManager.class.getMethod("getWifiApState", new Class[0]).invoke(this.d, new Object[0])).intValue();
        boolean z2 = true;
        if (intValue == 13 || intValue == 12 || intValue == 14) {
            a.a.a.b.c(s, "wifi ap state = " + intValue + ", stopTethering");
            this.l = new CountDownLatch(1);
            this.p = new g();
            registerReceiver(this.p, new IntentFilter(t));
            if (Build.VERSION.SDK_INT >= 30) {
                try {
                    Class.forName("android.net.TetheringManager").getMethod("stopTethering", Integer.TYPE).invoke(getSystemService("tethering"), 0);
                } catch (Exception e3) {
                    e2 = e3;
                    str = "call stopTetheringMethod error";
                }
            } else {
                try {
                    Field declaredField = ConnectivityManager.class.getDeclaredField("mService");
                    declaredField.setAccessible(true);
                    Object obj = declaredField.get((ConnectivityManager) getSystemService("connectivity"));
                    Class.forName("android.net.IConnectivityManager").getDeclaredMethod("stopTethering", Integer.TYPE, String.class).invoke(obj, 0, getPackageName());
                } catch (Exception e4) {
                    e2 = e4;
                    str = "call old function for stopTethering error";
                }
            }
            try {
                z2 = this.l.await(3000, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e5) {
                a.a.a.b.b(s, "close SoftAp exception", e5);
            }
            if (!z2) {
                unregisterReceiver(this.p);
            }
        }
        return z2;
        a.a.a.b.b(s, str, e2);
        z2 = this.l.await(3000, TimeUnit.MILLISECONDS);
        if (!z2) {
        }
        return z2;
    }

    /* JADX INFO: finally extract failed */
    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void c() {
        this.k.lock();
        try {
            WifiP2pManager.Channel channel = this.c;
            if (channel != null) {
                try {
                    channel.close();
                    this.c = null;
                } catch (Exception e2) {
                    a.a.a.b.b(s, "Channel close failed.", e2);
                }
            }
            this.c = this.b.initialize(this, getMainLooper(), null);
            this.k.unlock();
            if (this.c == null) {
                a.a.a.b.b(s, "P2pChannel is null");
                m();
            } else if (e()) {
                this.b.requestDeviceInfo(this.c, new h());
                this.o.a(this, true);
                this.o.a(new i());
            } else {
                this.o.a(new j());
                this.o.a(this, true);
            }
        } catch (Throwable th) {
            this.k.unlock();
            throw th;
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void c(boolean z2) {
        a.a.a.b.c(s, "setJoinTimeout: " + z2);
        if (z2) {
            this.f.postDelayed(this.m, 20000);
        } else {
            this.f.removeCallbacks(this.m);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private boolean d() {
        return this.f.hasCallbacks(this.n);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private boolean e() {
        StringBuilder append = new StringBuilder().append("Build.VERSION.SDK_INT=");
        int i2 = Build.VERSION.SDK_INT;
        a.a.a.b.c(s, append.append(i2).toString());
        return i2 > 28;
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void f() {
        a.a.a.b.e(s, "Haven't found p2p device, cancel peer discovery");
        r();
        j();
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void g() {
        if (!this.g) {
            a.a.a.b.e(s, "No client join. notifyOnAcceptFailed");
            p();
            i();
            return;
        }
        this.b.requestPeers(this.c, new WifiP2pManager.PeerListListener() {
            /* class com.share.connect.wifip2p.$$Lambda$WifiP2pService$p2voSer8QBeWvrT7oX2FsvcRwM */

            public final void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
                WifiP2pService.m17lambda$p2voSer8QBeWvrT7oX2FsvcRwM(WifiP2pService.this, wifiP2pDeviceList);
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void h() {
        synchronized (this.r) {
            for (WifiP2pObserver wifiP2pObserver : this.r) {
                try {
                    wifiP2pObserver.onCreateGroupFailed();
                } catch (RemoteException e2) {
                    a.a.a.b.b(s, "Observer.onAcceptFailed() failed.", e2);
                }
            }
        }
    }

    private void i() {
        synchronized (this.r) {
            for (WifiP2pObserver wifiP2pObserver : this.r) {
                try {
                    wifiP2pObserver.onAcceptFailed();
                } catch (RemoteException e2) {
                    a.a.a.b.b(s, "Observer.onAcceptFailed() failed.", e2);
                }
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void j() {
        synchronized (this.r) {
            for (WifiP2pObserver wifiP2pObserver : this.r) {
                try {
                    wifiP2pObserver.onConnectFailed();
                } catch (RemoteException e2) {
                    a.a.a.b.b(s, "Observer.onConnectFailed() failed.", e2);
                }
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void l() {
        synchronized (this.r) {
            for (WifiP2pObserver wifiP2pObserver : this.r) {
                try {
                    if (this.d.isWifiEnabled()) {
                        wifiP2pObserver.onDisconnected(false);
                    } else {
                        wifiP2pObserver.onDisconnected(true);
                    }
                } catch (RemoteException e2) {
                    a.a.a.b.b(s, "Observer.onDisconnected() failed.", e2);
                }
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void m() {
        synchronized (this.r) {
            for (WifiP2pObserver wifiP2pObserver : this.r) {
                try {
                    wifiP2pObserver.onOpenFailed();
                } catch (RemoteException e2) {
                    a.a.a.b.b(s, "Observer.onOpenFailed() failed.", e2);
                }
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void o() {
        a.a.a.b.c(s, "reset...");
        this.o.a((d.b) null);
        this.o.a((d.a) null);
        if (!e()) {
            this.o.a((d.c) null);
        }
        this.q = null;
        c(false);
        b(false);
        p();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void q() {
        this.o.a(new d());
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void r() {
        a((WifiP2pManager.ActionListener) null);
    }

    public void a() {
        try {
            unregisterReceiver(this.p);
        } catch (Exception unused) {
        }
        this.o.a((d.c) null);
        this.o.a(this, false);
        o();
    }

    public void a(int i2, int i3) {
        if (!b()) {
            a.a.a.b.b(s, "close tethering failure");
            h();
            return;
        }
        int a2 = a.b.a.k.e.a(i2, i3, this.d, true);
        int b2 = a.b.a.k.e.b(a2);
        if (!e() && a2 == 0) {
            a2 = 36;
        }
        a.b.a.k.f.a aVar = new a.b.a.k.f.a();
        aVar.b("DIRECT-vs-" + UUID.randomUUID().toString().substring(0, 6).toUpperCase());
        aVar.c(UUID.randomUUID().toString().substring(0, 8));
        aVar.b(b2);
        aVar.a(true);
        if (b2 == 0) {
            aVar.a(2);
        }
        a(new k(aVar, a.b.a.k.f.b.a(aVar), a2));
    }

    public void a(WifiP2pObserver wifiP2pObserver) {
        synchronized (this.r) {
            if (!this.r.contains(wifiP2pObserver)) {
                this.r.add(wifiP2pObserver);
            }
        }
    }

    public void a(String str, String str2, String str3, int i2) {
        a.a.a.b.c(s, "Connecting GO: " + str + ", mac=" + str3 + ", frequency=" + i2);
        if (TextUtils.isEmpty(str3)) {
            a.a.a.b.b(s, "Empty GO mac address.");
            j();
            return;
        }
        boolean z2 = true;
        c(true);
        this.q = str3;
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2) || TextUtils.isEmpty(str3) || i2 == 0) {
            z2 = false;
        }
        a.b.a.k.f.a aVar = new a.b.a.k.f.a();
        aVar.b(str).c(str2).a(false).a(str3).b(i2);
        WifiP2pConfig a2 = a.b.a.k.f.b.a(aVar);
        if (!z2 || !e() || a2 == null) {
            a.a.a.b.c(s, "Method selected: Classic");
            o oVar = new o(this, null);
            this.i = oVar;
            this.f.postDelayed(oVar, 5000);
            a(new m());
            return;
        }
        a.a.a.b.c(s, "Method selected: Q");
        a(new l(a2));
    }

    public void b(WifiP2pObserver wifiP2pObserver) {
        synchronized (this.r) {
            this.r.remove(wifiP2pObserver);
        }
    }

    public void k() {
        synchronized (this.r) {
            for (WifiP2pObserver wifiP2pObserver : this.r) {
                try {
                    wifiP2pObserver.onConnected();
                } catch (RemoteException e2) {
                    a.a.a.b.b(s, "Observer.onConnect() failed.", e2);
                }
            }
        }
    }

    public void n() {
        if (this.d.isWifiEnabled()) {
            c();
            return;
        }
        this.p = new f();
        registerReceiver(this.p, new IntentFilter("android.net.wifi.WIFI_STATE_CHANGED"));
        if (!this.d.setWifiEnabled(true)) {
            a.a.a.b.b(s, "setWifiEnabled(true) failed, were hotspot or airplane mode activated?");
            m();
            a();
        }
    }

    public IBinder onBind(Intent intent) {
        return this.f202a;
    }

    public void onCreate() {
        super.onCreate();
        this.f = new Handler();
        this.d = (WifiManager) getSystemService(a.b.a.c.q);
        this.b = (WifiP2pManager) getSystemService("wifip2p");
    }

    public void onDestroy() {
        super.onDestroy();
        a();
    }

    public void p() {
        this.h = false;
        a((Runnable) null);
    }
}
