package z3;

import a4.g0;
import a4.k0;
import a4.m0;
import a4.n0;
import a4.r0;
import a4.s0;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Looper;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebMessage;
import android.webkit.WebView;
import android.webkit.WebViewRenderProcess;
import android.webkit.WebViewRenderProcessClient;
import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.UiThread;
import com.pichillilorenzo.flutter_inappwebview_android.webview.in_app_webview.InAppWebView;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.chromium.support_lib_boundary.ScriptHandlerBoundaryInterface;

/* JADX INFO: loaded from: classes.dex */
public abstract class b0 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final Uri f37032a = Uri.parse("*");

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final Uri f37033b = Uri.parse("");

    public static a4.x a(WebView webView, String str, Set set) {
        if (!k0.M.b()) {
            throw k0.a();
        }
        n0 n0VarE = e(webView);
        return new a4.x((ScriptHandlerBoundaryInterface) ee.b.castToSuppLibClass(ScriptHandlerBoundaryInterface.class, n0VarE.f346a.addDocumentStartJavaScript(str, (String[]) set.toArray(new String[0]))));
    }

    public static void b(WebView webView, String str, Set set, a0 a0Var) {
        if (!k0.L.b()) {
            throw k0.a();
        }
        n0 n0VarE = e(webView);
        n0VarE.f346a.addWebMessageListener(str, (String[]) set.toArray(new String[0]), ee.b.createInvocationHandlerFor(new xyz.stream.api.a0(a0Var, 10)));
    }

    public static void c(WebView webView) {
        if (Build.VERSION.SDK_INT < 28) {
            try {
                Method declaredMethod = WebView.class.getDeclaredMethod("checkThread", new Class[0]);
                declaredMethod.setAccessible(true);
                declaredMethod.invoke(webView, new Object[0]);
                return;
            } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e10) {
                throw new RuntimeException(e10);
            }
        }
        Looper webViewLooper = webView.getWebViewLooper();
        if (webViewLooper == Looper.myLooper()) {
            return;
        }
        throw new RuntimeException("A WebView method was called on thread '" + Thread.currentThread().getName() + "'. All WebView methods must be called on the same thread. (Expected Looper " + webViewLooper + " called on " + Looper.myLooper() + ", FYI main Looper is " + Looper.getMainLooper() + ")");
    }

    public static PackageInfo d() {
        return (PackageInfo) Class.forName("android.webkit.WebViewFactory").getMethod("getLoadedPackageInfo", new Class[0]).invoke(null, new Object[0]);
    }

    public static n0 e(WebView webView) {
        return new n0(m0.f345a.createWebView(webView));
    }

    public static void f(InAppWebView inAppWebView, p pVar, Uri uri) {
        if (f37032a.equals(uri)) {
            uri = f37033b;
        }
        a4.b bVar = k0.f341x;
        boolean zA = bVar.a();
        int i10 = pVar.f37045a;
        if (zA && i10 == 0) {
            inAppWebView.postWebMessage(new WebMessage(pVar.getData(), g0.compatToPorts(pVar.getPorts())), uri);
            return;
        }
        if (!bVar.b() || (i10 != 0 && (i10 != 1 || !k0.f338u.b()))) {
            throw k0.a();
        }
        c(inAppWebView);
        n0 n0VarE = e(inAppWebView);
        n0VarE.f346a.postMessageToMainFrame(ee.b.createInvocationHandlerFor(new a4.e0(pVar)), uri);
    }

    @Nullable
    @AnyThread
    @RestrictTo({RestrictTo.Scope.f555a})
    public static PackageInfo getCurrentLoadedWebViewPackage() {
        if (Build.VERSION.SDK_INT >= 26) {
            return WebView.getCurrentWebViewPackage();
        }
        try {
            return d();
        } catch (ClassNotFoundException | IllegalAccessException | NoSuchMethodException | InvocationTargetException unused) {
            return null;
        }
    }

    @Nullable
    @AnyThread
    public static PackageInfo getCurrentWebViewPackage(@NonNull Context context) {
        PackageInfo currentLoadedWebViewPackage = getCurrentLoadedWebViewPackage();
        if (currentLoadedWebViewPackage != null) {
            return currentLoadedWebViewPackage;
        }
        try {
            String str = (String) Class.forName("android.webkit.WebViewUpdateService").getMethod("getCurrentWebViewPackageName", new Class[0]).invoke(null, new Object[0]);
            if (str == null) {
                return null;
            }
            return context.getPackageManager().getPackageInfo(str, 0);
        } catch (PackageManager.NameNotFoundException | ClassNotFoundException | IllegalAccessException | NoSuchMethodException | InvocationTargetException unused) {
            return null;
        }
    }

    @Nullable
    @UiThread
    public static WebChromeClient getWebChromeClient(@NonNull WebView webView) {
        a4.b bVar = k0.f343z;
        if (bVar.a()) {
            return a4.h.getWebChromeClient(webView);
        }
        if (!bVar.b()) {
            throw k0.a();
        }
        c(webView);
        return e(webView).getWebChromeClient();
    }

    @Nullable
    @UiThread
    public static d0 getWebViewRenderProcess(@NonNull WebView webView) {
        a4.b bVar = k0.A;
        if (bVar.a()) {
            WebViewRenderProcess webViewRenderProcess = a4.m.getWebViewRenderProcess(webView);
            if (webViewRenderProcess != null) {
                return s0.b(webViewRenderProcess);
            }
            return null;
        }
        if (!bVar.b()) {
            throw k0.a();
        }
        c(webView);
        return e(webView).getWebViewRenderProcess();
    }

    @Nullable
    @UiThread
    public static e0 getWebViewRenderProcessClient(@NonNull WebView webView) {
        a4.b bVar = k0.F;
        if (!bVar.a()) {
            if (!bVar.b()) {
                throw k0.a();
            }
            c(webView);
            return e(webView).getWebViewRenderProcessClient();
        }
        WebViewRenderProcessClient webViewRenderProcessClient = a4.m.getWebViewRenderProcessClient(webView);
        if (webViewRenderProcessClient == null || !(webViewRenderProcessClient instanceof r0)) {
            return null;
        }
        return ((r0) webViewRenderProcessClient).getFrameworkRenderProcessClient();
    }

    @AnyThread
    public static void setSafeBrowsingAllowlist(@NonNull Set<String> set, @Nullable ValueCallback<Boolean> valueCallback) {
        a4.b bVar = k0.f323f;
        a4.b bVar2 = k0.f322e;
        if (bVar.b()) {
            m0.f345a.getStatics().setSafeBrowsingAllowlist(set, valueCallback);
            return;
        }
        ArrayList arrayList = new ArrayList(set);
        if (bVar2.a()) {
            a4.j.setSafeBrowsingWhitelist(arrayList, valueCallback);
        } else {
            if (!bVar2.b()) {
                throw k0.a();
            }
            m0.f345a.getStatics().setSafeBrowsingWhitelist(arrayList, valueCallback);
        }
    }

    @AnyThread
    @Deprecated
    public static void setSafeBrowsingWhitelist(@NonNull List<String> list, @Nullable ValueCallback<Boolean> valueCallback) {
        setSafeBrowsingAllowlist(new HashSet(list), valueCallback);
    }

    @UiThread
    public static void setWebViewRenderProcessClient(@NonNull WebView webView, @Nullable e0 e0Var) {
        a4.b bVar = k0.F;
        if (bVar.a()) {
            a4.m.setWebViewRenderProcessClient(webView, e0Var);
        } else {
            if (!bVar.b()) {
                throw k0.a();
            }
            c(webView);
            e(webView).setWebViewRenderProcessClient(null, e0Var);
        }
    }

    @AnyThread
    public static void startSafeBrowsing(@NonNull Context context, @Nullable ValueCallback<Boolean> valueCallback) {
        a4.b bVar = k0.f321d;
        if (bVar.a()) {
            a4.j.startSafeBrowsing(context, valueCallback);
        } else {
            if (!bVar.b()) {
                throw k0.a();
            }
            m0.f345a.getStatics().initSafeBrowsing(context, valueCallback);
        }
    }
}
