package com.share.connect.wifiap;

import a.b.a.i.k;
import a.b.a.j.a;
import a.b.a.k.e;
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.wifi.SoftApConfiguration;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.os.ResultReceiver;
import android.text.TextUtils;
import com.google.common.primitives.UnsignedBytes;
import com.share.connect.wifiap.IWifiAp;
import com.share.connect.wifiap.WifiApService;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class WifiApService extends Service {
    private static final String A = "wifi_state";
    private static final String B = "tetherArray";
    private static final String C = "tethering";
    private static final int D = 0;
    private static final int E = 10;
    private static final int F = 11;
    private static final int G = 12;
    private static final int H = 13;
    private static final int I = 14;
    private static final int J = 2;
    private static final int K = 1;
    private static final int L = 30000;
    private static final int M = 2000;
    private static final int N = 32;
    private static final int O = 9;
    private static final String v = "WifiApService";
    private static final String w = "DIRECT-ICCOA-";
    private static final String x = "CN";
    private static final String y = "android.net.wifi.WIFI_AP_STATE_CHANGED";
    private static final String z = "android.net.conn.TETHER_STATE_CHANGED";

    /* renamed from: a  reason: collision with root package name */
    private final c f194a = new c(this, null);
    private WifiManager b;
    private ConnectivityManager c;
    private Handler d = null;
    private Runnable e;
    private Object f;
    private String g = null;
    private String h = null;
    private String i = null;
    private String j = null;
    private int k = 36;
    private boolean l = false;
    private boolean m = false;
    private boolean n = false;
    private boolean o = false;
    private boolean p = false;
    private CountDownLatch q = null;
    private CountDownLatch r = null;
    private final List<WifiApObserver> s = new ArrayList();
    private final BroadcastReceiver t = new a();
    private final Runnable u = new Runnable() {
        /* class com.share.connect.wifiap.$$Lambda$WifiApService$Hh6i1SWxzQJqIW4tk2BqgxH4dM */

        public final void run() {
            WifiApService.m14lambda$Hh6i1SWxzQJqIW4tk2BqgxH4dM(WifiApService.this);
        }
    };

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

        /* access modifiers changed from: private */
        public /* synthetic */ void a() {
            WifiApService wifiApService = WifiApService.this;
            wifiApService.a(wifiApService.g, WifiApService.this.h, WifiApService.this.j, WifiApService.this.i, e.b(WifiApService.this.k));
            WifiApService.this.l = true;
        }

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (WifiApService.y.equals(action)) {
                int intExtra = intent.getIntExtra(WifiApService.A, 14);
                a.a.a.b.a(WifiApService.v, "receiver WIFI_AP_STATE_CHANGED_ACTION,  state = " + intExtra + " connect state = " + a.b.a.c.b().a());
                if (intExtra == 11) {
                    WifiApService.this.l = false;
                    WifiApService.this.m = false;
                    WifiApService.this.o = false;
                    WifiApService.this.e = null;
                    WifiApService.this.q = null;
                    WifiApService.this.h = null;
                    WifiApService.this.f = null;
                    WifiApService.this.g = null;
                    if (WifiApService.this.r != null) {
                        a.a.a.b.a(WifiApService.v, "tethering has been disabled !");
                        WifiApService.this.r.countDown();
                    }
                }
            } else if (WifiApService.z.equals(action)) {
                a.a.a.b.a(WifiApService.v, "ACTION_TETHER_STATE_CHANGED !");
                ArrayList<String> stringArrayListExtra = intent.getStringArrayListExtra(WifiApService.B);
                if (stringArrayListExtra != null && !stringArrayListExtra.isEmpty()) {
                    WifiApService.this.c((WifiApService) stringArrayListExtra.get(0));
                    try {
                        if (!WifiApService.this.l) {
                            if (WifiApService.this.g == null) {
                                a.b.a.j.a a2 = new a.b(WifiApService.this.b).a();
                                if (WifiApService.this.b((WifiApService) a2.e())) {
                                    a.a.a.b.a(WifiApService.v, "the existing soft ap config ---- " + a2);
                                    WifiApService.this.l();
                                    WifiApService.this.g = a2.e();
                                    WifiApService.this.k = a2.b();
                                    WifiApService.this.h = a2.d();
                                    WifiApService.this.o = true;
                                } else {
                                    a.a.a.b.a(WifiApService.v, "the existing soft ap ssid is invalid");
                                    return;
                                }
                            }
                            int i = Build.VERSION.SDK_INT;
                            if (i >= 30) {
                                if (WifiApService.this.q != null) {
                                    WifiApService.this.q.countDown();
                                }
                            } else if (i >= 28) {
                                WifiApService.this.e = new Runnable() {
                                    /* class com.share.connect.wifiap.$$Lambda$WifiApService$a$nnrv17eLqX8W_Sodrl92xsfJU7k */

                                    public final void run() {
                                        WifiApService.a.lambda$nnrv17eLqX8W_Sodrl92xsfJU7k(WifiApService.a.this);
                                    }
                                };
                                WifiApService.this.e.run();
                            } else {
                                a.a.a.b.c(WifiApService.v, "the version less than Android P don't support");
                            }
                        }
                    } catch (Exception e) {
                        a.a.a.b.b(WifiApService.v, "onReceive ACTION_TETHER_STATE_CHANGED error: ", e);
                    }
                }
            }
        }
    }

    public final class b implements InvocationHandler {
        private static final int b = 2;
        private static final int c = 3;

        private b() {
        }

        public /* synthetic */ b(WifiApService wifiApService, a aVar) {
            this();
        }

        /* access modifiers changed from: private */
        public /* synthetic */ void a(SoftApConfiguration softApConfiguration, int i) {
            WifiApService.this.a(softApConfiguration.getSsid(), softApConfiguration.getPassphrase(), WifiApService.this.j, WifiApService.this.i, i);
            WifiApService.this.l = true;
        }

        @Override // java.lang.reflect.InvocationHandler
        public Object invoke(Object obj, Method method, Object[] objArr) throws Throwable {
            Object invoke;
            if (Build.VERSION.SDK_INT >= 30) {
                if (TextUtils.equals("onConnectedClientsChanged", method.getName())) {
                    a.a.a.b.a(WifiApService.v, "onConnectedClientsChanged");
                    List list = (List) (objArr.length == 2 ? objArr[1] : objArr[0]);
                    if (list.size() > 0) {
                        WifiApService.this.a((WifiApService) false);
                        if (list.get(0) != null && !WifiApService.this.m) {
                            a.a.a.b.a(WifiApService.v, "onConnectedClientsChanged WifiClient = " + list.get(0));
                            WifiApService.this.g();
                            WifiApService.this.m = true;
                        }
                    } else if (WifiApService.this.m && !WifiApService.this.b()) {
                        WifiApService.this.m = false;
                        WifiApService.this.h();
                    }
                    return null;
                } else if (TextUtils.equals("onInfoChanged", method.getName())) {
                    try {
                        Method method2 = Class.forName("android.net.wifi.SoftApInfo").getMethod("getFrequency", new Class[0]);
                        if (!(objArr[0] instanceof List)) {
                            invoke = method2.invoke(objArr[0], new Object[0]);
                        } else if (((List) objArr[0]).size() > 0) {
                            invoke = method2.invoke(((List) objArr[0]).get(0), new Object[0]);
                        } else {
                            a.a.a.b.b(WifiApService.v, "onInfoChanged SoftApInfo list size is 0");
                            return null;
                        }
                        int intValue = ((Integer) invoke).intValue();
                        SoftApConfiguration softApConfiguration = (SoftApConfiguration) WifiManager.class.getMethod("getSoftApConfiguration", new Class[0]).invoke(WifiApService.this.b, new Object[0]);
                        a.a.a.b.a(WifiApService.v, "SoftApInfo freq = " + intValue + " ssid = " + softApConfiguration.getSsid());
                        if (!WifiApService.this.l && intValue != 0) {
                            if (WifiApService.this.q == null || !WifiApService.this.q.await(3, TimeUnit.SECONDS)) {
                                a.a.a.b.b(WifiApService.v, "soft Ap create fail !");
                                WifiApService.this.f();
                            } else {
                                WifiApService.this.e = new Runnable(softApConfiguration, intValue) {
                                    /* class com.share.connect.wifiap.$$Lambda$WifiApService$b$ku1T4HtUKlKXqYUsT5KafsDXWsw */
                                    public final /* synthetic */ SoftApConfiguration f$1;
                                    public final /* synthetic */ int f$2;

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

                                    public final void run() {
                                        WifiApService.b.lambda$ku1T4HtUKlKXqYUsT5KafsDXWsw(WifiApService.b.this, this.f$1, this.f$2);
                                    }
                                };
                                if (!WifiApService.this.o) {
                                    WifiApService.this.e.run();
                                }
                            }
                        }
                        return null;
                    } catch (Exception e) {
                        a.a.a.b.b(WifiApService.v, "onInfoChanged reflect error: ", e);
                        return null;
                    }
                }
            } else if (TextUtils.equals("onStateChanged", method.getName())) {
                a.a.a.b.a(WifiApService.v, "onStateChanged state = " + objArr[0]);
                return null;
            } else if (TextUtils.equals("onNumClientsChanged", method.getName())) {
                a.a.a.b.a(WifiApService.v, "onNumClientsChanged WifiClient size = " + objArr[0]);
                if (WifiApService.this.a() == 13 && ((Integer) objArr[0]).intValue() > 0) {
                    WifiApService.this.a((WifiApService) false);
                    if (!WifiApService.this.m) {
                        WifiApService.this.g();
                        WifiApService.this.m = true;
                    }
                } else if (WifiApService.this.m && ((Integer) objArr[0]).intValue() == 0 && !WifiApService.this.b()) {
                    WifiApService.this.m = false;
                    WifiApService.this.h();
                }
                return null;
            }
            if (String.class == method.getReturnType()) {
                return "";
            }
            if (Integer.class == method.getReturnType() || Integer.TYPE == method.getReturnType()) {
                return 0;
            }
            if (Boolean.class == method.getReturnType()) {
                return Boolean.FALSE;
            }
            if (Boolean.TYPE == method.getReturnType()) {
                return Boolean.FALSE;
            }
            return null;
        }
    }

    public class c extends IWifiAp.b {
        private c() {
        }

        public /* synthetic */ c(WifiApService wifiApService, a aVar) {
            this();
        }

        @Override // com.share.connect.wifiap.IWifiAp
        public void cancelConnect() {
            WifiApService.this.m();
        }

        @Override // com.share.connect.wifiap.IWifiAp
        public void close() {
            WifiApService.this.m();
        }

        @Override // com.share.connect.wifiap.IWifiAp
        public void createSoftAp(int i2, String str, String str2, boolean z, boolean z2) {
            WifiApService.this.p = z2;
            a.b.a.j.a a2 = new a.b(WifiApService.this.b).a();
            if (a2 == null || !WifiApService.this.b((WifiApService) a2.e())) {
                if (str == null) {
                    str = WifiApService.w + UUID.randomUUID().toString().substring(0, 3).toUpperCase();
                }
                if (str2 == null) {
                    str2 = UUID.randomUUID().toString().substring(0, 8);
                }
            } else {
                str = a2.e();
                str2 = a2.d();
            }
            if (WifiApService.this.g != null && WifiApService.this.g.equals(str) && WifiApService.this.a() == 13) {
                a.a.a.b.c(WifiApService.v, "reuse existing Wifi softAp");
                if (WifiApService.this.e != null) {
                    WifiApService.this.e.run();
                    return;
                } else if (a.b.a.c.b().j()) {
                    a.a.a.b.c(WifiApService.v, "Wireless is connecting, but the softAp is not ready");
                } else {
                    return;
                }
            }
            WifiApService.this.a(i2, str, str2, z);
        }

        @Override // com.share.connect.wifiap.IWifiAp
        public boolean isUserClose() {
            return WifiApService.this.a() == 10 || WifiApService.this.a() == 11;
        }

        @Override // com.share.connect.wifiap.IWifiAp
        public void open() {
            WifiApService.this.k();
        }

        @Override // com.share.connect.wifiap.IWifiAp
        public void registerWifiApObserver(WifiApObserver wifiApObserver) {
            WifiApService.this.a(wifiApObserver);
        }

        @Override // com.share.connect.wifiap.IWifiAp
        public void unregisterWifiApObserver(WifiApObserver wifiApObserver) {
            WifiApService.this.b(wifiApObserver);
        }
    }

    public final class d implements InvocationHandler {
        private d() {
        }

        public /* synthetic */ d(WifiApService wifiApService, a aVar) {
            this();
        }

        @Override // java.lang.reflect.InvocationHandler
        public Object invoke(Object obj, Method method, Object[] objArr) throws Throwable {
            if (TextUtils.equals("onTetheringStarted", method.getName())) {
                a.a.a.b.a(WifiApService.v, "WifiApService startTethering Success");
                return null;
            } else if (!TextUtils.equals("onTetheringFailed", method.getName())) {
                return null;
            } else {
                a.a.a.b.a(WifiApService.v, "WifiApService startTethering failed, error num: " + objArr[0]);
                WifiApService.this.f();
                return null;
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private int a() {
        return ((Integer) WifiManager.class.getMethod("getWifiApState", new Class[0]).invoke(this.b, new Object[0])).intValue();
    }

    private String a(byte[] bArr) {
        if (bArr == null) {
            a.a.a.b.b(v, "MacAddress params is null");
            return null;
        }
        StringBuffer stringBuffer = new StringBuffer(bArr.length);
        int length = bArr.length;
        for (int i2 = 0; i2 < length; i2++) {
            String hexString = Integer.toHexString(bArr[i2] & UnsignedBytes.MAX_VALUE);
            if (hexString.length() == 1) {
                stringBuffer = stringBuffer.append("0");
            }
            stringBuffer = stringBuffer.append(hexString);
            if (i2 < length - 1) {
                stringBuffer.append(":");
            }
        }
        return String.valueOf(stringBuffer);
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void a(String str, String str2) {
        this.o = false;
        m();
        CountDownLatch countDownLatch = new CountDownLatch(1);
        this.r = countDownLatch;
        try {
            if (countDownLatch.await(2000, TimeUnit.MILLISECONDS)) {
                c(str, str2);
            } else {
                f();
            }
        } catch (InterruptedException e2) {
            a.a.a.b.b(v, "tetherReset thread error: ", e2);
            f();
        }
        this.r = null;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(String str, String str2, String str3, String str4, int i2) {
        a(true);
        synchronized (this.s) {
            for (WifiApObserver wifiApObserver : this.s) {
                try {
                    wifiApObserver.onApCreated(str3, str, str2, str4, i2);
                } catch (RemoteException e2) {
                    a.a.a.b.b(v, "Observer.notifyOnApCreated(...) failed.", e2);
                }
            }
        }
    }

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

    private boolean a(String str) {
        try {
            return InetAddress.getByName(str) instanceof Inet4Address;
        } catch (UnknownHostException unused) {
            return false;
        }
    }

    private void b(String str, String str2) {
        try {
            k.a(new Runnable(str, str2) {
                /* class com.share.connect.wifiap.$$Lambda$WifiApService$jeOiYe8lnEpgQRKzKbQczxv73UM */
                public final /* synthetic */ String f$1;
                public final /* synthetic */ String f$2;

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

                public final void run() {
                    WifiApService.lambda$jeOiYe8lnEpgQRKzKbQczxv73UM(WifiApService.this, this.f$1, this.f$2);
                }
            }, "reCreate soft ap");
        } catch (Exception e2) {
            a.a.a.b.b(v, "useExistSoftAp error: ", e2);
        }
    }

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

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private boolean b(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
        if (bytes.length < 9 || bytes.length > 32) {
            return false;
        }
        return !this.p || str.matches("^DIRECT-[a-zA-Z0-9]{2}.*");
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void c() {
        if (!this.m) {
            a.a.a.b.e(v, "No client join. notifyOnAcceptFailed");
            m();
            e();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void c(String str) {
        try {
            NetworkInterface byName = NetworkInterface.getByName(str);
            if (byName != null) {
                this.i = a(byName.getHardwareAddress());
                Enumeration<InetAddress> inetAddresses = byName.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress nextElement = inetAddresses.nextElement();
                    if (!nextElement.isLoopbackAddress() && a(nextElement.getHostAddress())) {
                        this.j = nextElement.getHostAddress();
                    }
                }
            }
            a.a.a.b.a(v, "IpAddress = " + this.j + " mac = " + this.i);
        } catch (SocketException e2) {
            a.a.a.b.b(v, "setApAddress error: ", e2);
        }
    }

    private void c(String str, String str2) {
        this.l = false;
        this.g = str;
        this.h = str2;
        WifiManager wifiManager = this.b;
        if (wifiManager != null && !this.n && (wifiManager.getWifiState() == 3 || this.b.getWifiState() == 2)) {
            a.a.a.b.a(v, "disable wifi, before start startTethering");
            this.b.setWifiEnabled(false);
        }
        if (Build.VERSION.SDK_INT >= 30) {
            a.a.a.b.a(v, "call new function for startTethering");
            d();
            return;
        }
        a.a.a.b.a(v, "call old function for startTethering");
        j();
    }

    private void d() {
        if (this.c != null) {
            try {
                a.b a2 = new a.b().b(this.g).a(this.h);
                int i2 = this.k;
                if (i2 != 0) {
                    a2.b(i2);
                } else {
                    a2.a(2);
                }
                a.b.a.j.a a3 = a2.a();
                WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(a.b.a.c.q);
                wifiManager.getClass().getMethod("setSoftApConfiguration", SoftApConfiguration.class).invoke(wifiManager, a3.c());
                l();
                Class<?> cls = Class.forName("android.net.TetheringManager$TetheringRequest$Builder");
                Object newInstance = cls.getConstructor(Integer.TYPE).newInstance(0);
                Method method = cls.getMethod("setShouldShowEntitlementUi", Boolean.TYPE);
                Method method2 = cls.getMethod("build", new Class[0]);
                method.invoke(newInstance, Boolean.TRUE);
                Object invoke = method2.invoke(newInstance, new Object[0]);
                Class<?> cls2 = Class.forName("android.net.TetheringManager$StartTetheringCallback");
                Object newProxyInstance = Proxy.newProxyInstance(cls2.getClassLoader(), new Class[]{cls2}, new d(this, null));
                Class.forName("android.net.TetheringManager").getMethod("startTethering", Class.forName("android.net.TetheringManager$TetheringRequest"), Executor.class, cls2).invoke(getSystemService(C), invoke, Executors.newSingleThreadExecutor(), newProxyInstance);
            } catch (Exception e2) {
                a.a.a.b.b(v, "start new startTethering method error: ", e2);
            }
        }
    }

    private void e() {
        synchronized (this.s) {
            for (WifiApObserver wifiApObserver : this.s) {
                try {
                    wifiApObserver.onAcceptFailed();
                } catch (RemoteException e2) {
                    a.a.a.b.b(v, "Observer.onAcceptFailed() failed.", e2);
                }
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void f() {
        synchronized (this.s) {
            for (WifiApObserver wifiApObserver : this.s) {
                try {
                    wifiApObserver.onApCreateFailed();
                } catch (RemoteException e2) {
                    a.a.a.b.b(v, "Observer.notifyOnApCreateFailed() failed.", e2);
                }
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void g() {
        synchronized (this.s) {
            for (WifiApObserver wifiApObserver : this.s) {
                try {
                    wifiApObserver.onConnected();
                } catch (RemoteException e2) {
                    a.a.a.b.b(v, "Observer.notifyOnConnected() failed.", e2);
                }
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void h() {
        synchronized (this.s) {
            for (WifiApObserver wifiApObserver : this.s) {
                try {
                    if (a() != 10) {
                        if (a() != 11) {
                            wifiApObserver.onDisconnected(false);
                        }
                    }
                    wifiApObserver.onDisconnected(true);
                } catch (RemoteException e2) {
                    a.a.a.b.b(v, "Observer.onDisconnected() failed.", e2);
                }
            }
        }
    }

    private void i() {
        synchronized (this.s) {
            for (WifiApObserver wifiApObserver : this.s) {
                try {
                    wifiApObserver.onOpenSuccess();
                } catch (RemoteException e2) {
                    a.a.a.b.b(v, "Observer.onOpenSuccess error: ", e2);
                }
            }
        }
    }

    private void j() {
        if (this.c != null) {
            try {
                a.b.a.j.a a2 = new a.b().b(this.g).a(this.h).b(this.k).a(1).a();
                Method method = WifiManager.class.getMethod("setCountryCode", String.class);
                if (((String) WifiManager.class.getMethod("getCountryCode", new Class[0]).invoke(this.b, new Object[0])) == null) {
                    a.a.a.b.b(v, "getCountryCode is null, reset it");
                    method.invoke(this.b, x);
                }
                WifiManager.class.getMethod("setWifiApConfiguration", WifiConfiguration.class).invoke(this.b, a2.c());
                l();
                Field declaredField = ConnectivityManager.class.getDeclaredField("mService");
                declaredField.setAccessible(true);
                Object obj = declaredField.get(this.c);
                Class.forName("android.net.IConnectivityManager").getDeclaredMethod("startTethering", Integer.TYPE, ResultReceiver.class, Boolean.TYPE, String.class).invoke(obj, 0, new ResultReceiver(null) {
                    /* class com.share.connect.wifiap.WifiApService.AnonymousClass2 */

                    public void onReceiveResult(int i, Bundle bundle) {
                        super.onReceiveResult(i, bundle);
                    }
                }, Boolean.TRUE, getPackageName());
            } catch (Exception e2) {
                a.a.a.b.b(v, "start old startTethering method error: ", e2);
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void l() {
        if (this.f == null) {
            a.a.a.b.a(v, "register SoftAp Callback");
            try {
                Class<?> cls = Class.forName("android.net.wifi.WifiManager$SoftApCallback");
                this.f = Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{cls}, new b(this, null));
                if (Build.VERSION.SDK_INT >= 30) {
                    this.q = new CountDownLatch(1);
                    WifiManager.class.getMethod("registerSoftApCallback", Executor.class, cls).invoke(this.b, Executors.newSingleThreadExecutor(), this.f);
                    return;
                }
                WifiManager.class.getMethod("registerSoftApCallback", cls, Handler.class).invoke(this.b, this.f, new Handler(Looper.getMainLooper()));
            } catch (Exception e2) {
                this.f = null;
                a.a.a.b.b(v, "registerSoftApCallback reflect error!", e2);
            }
        }
    }

    public void a(int i2, String str, String str2, boolean z2) {
        this.n = z2;
        this.k = e.a(1, i2, this.b, z2);
        if (a() == 12 || a() == 13) {
            a.a.a.b.c(v, "the current soft ap can't be used, we need to recreate it");
            b(str, str2);
            return;
        }
        c(str, str2);
    }

    public void a(WifiApObserver wifiApObserver) {
        synchronized (this.s) {
            if (!this.s.contains(wifiApObserver)) {
                this.s.add(wifiApObserver);
            }
        }
    }

    public void b(WifiApObserver wifiApObserver) {
        synchronized (this.s) {
            this.s.remove(wifiApObserver);
        }
    }

    public void k() {
        a.a.a.b.c(v, "WifiApService open");
        i();
    }

    public void m() {
        String str;
        Exception e2;
        if (this.o) {
            a.a.a.b.a(v, "we should keep system softAp alive");
            return;
        } else if (this.c != null) {
            if (this.f != null) {
                try {
                    WifiManager.class.getMethod("unregisterSoftApCallback", Class.forName("android.net.wifi.WifiManager$SoftApCallback")).invoke(this.b, this.f);
                    this.f = null;
                } catch (Exception e3) {
                    a.a.a.b.b(v, "call unregisterSoftApCallback error", e3);
                }
            }
            if (a() == 13 || a() == 12 || a() == 14) {
                a.a.a.b.c(v, "wifi ap state = " + a() + ", stopTethering");
                if (Build.VERSION.SDK_INT >= 30) {
                    try {
                        Class.forName("android.net.TetheringManager").getMethod("stopTethering", Integer.TYPE).invoke(getSystemService(C), 0);
                        return;
                    } catch (Exception e4) {
                        e2 = e4;
                        str = "call stopTetheringMethod error";
                    }
                } else {
                    try {
                        Field declaredField = ConnectivityManager.class.getDeclaredField("mService");
                        declaredField.setAccessible(true);
                        Object obj = declaredField.get(this.c);
                        Class.forName("android.net.IConnectivityManager").getDeclaredMethod("stopTethering", Integer.TYPE, String.class).invoke(obj, 0, getPackageName());
                        return;
                    } catch (Exception e5) {
                        e2 = e5;
                        str = "call old function for stopTethering error";
                    }
                }
            } else {
                return;
            }
        } else {
            return;
        }
        a.a.a.b.b(v, str, e2);
    }

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

    public void onCreate() {
        super.onCreate();
        this.b = (WifiManager) getSystemService(a.b.a.c.q);
        this.c = (ConnectivityManager) getSystemService("connectivity");
        this.d = new Handler(Looper.getMainLooper());
        IntentFilter intentFilter = new IntentFilter(y);
        intentFilter.addAction(z);
        registerReceiver(this.t, intentFilter);
    }

    public void onDestroy() {
        super.onDestroy();
        m();
        unregisterReceiver(this.t);
    }
}
