package com.google.android.gms.internal.ads;

import android.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.webkit.ConsoleMessage;
import android.webkit.GeolocationPermissions;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.media3.datasource.cache.CacheDataSink;
import io.flutter.plugins.urllauncher.WebViewActivity;
import tv.danmaku.ijk.media.player.IjkMediaMeta;

/* JADX INFO: loaded from: classes2.dex */
public final class w50 extends WebChromeClient {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ int f17869a = 0;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Object f17870b;

    public w50(zzckh zzckhVar) {
        this.f17870b = zzckhVar;
    }

    /* JADX WARN: Multi-variable type inference failed */
    public static final Context b(WebView webView) {
        if (!(webView instanceof x50)) {
            return webView.getContext();
        }
        x50 x50Var = (x50) webView;
        Activity activityZzj = x50Var.zzj();
        return activityZzj != null ? activityZzj : x50Var.getContext();
    }

    public final boolean a(Context context, String str, String str2, String str3, String str4, JsResult jsResult, JsPromptResult jsPromptResult, boolean z10) {
        x50 x50Var;
        int i10;
        int i11;
        m7.b bVar;
        int i12 = 1;
        try {
            x50Var = (x50) this.f17870b;
            i10 = 2;
            i11 = 0;
        } catch (WindowManager.BadTokenException unused) {
        }
        if (x50Var != null && x50Var.zzP() != null && x50Var.zzP().X != null && (bVar = x50Var.zzP().X) != null && !bVar.a()) {
            StringBuilder sb = new StringBuilder(str.length() + 9 + String.valueOf(str3).length() + 2);
            sb.append("window.");
            sb.append(str);
            sb.append("('");
            sb.append(str3);
            sb.append("')");
            bVar.zzc(sb.toString());
            return false;
        }
        p7.r0 r0Var = m7.n.C.f30464c;
        AlertDialog.Builder builderJ = p7.r0.j(context);
        builderJ.setTitle(str2);
        if (z10) {
            LinearLayout linearLayout = new LinearLayout(context);
            linearLayout.setOrientation(1);
            TextView textView = new TextView(context);
            textView.setText(str3);
            EditText editText = new EditText(context);
            editText.setText(str4);
            linearLayout.addView(textView);
            linearLayout.addView(editText);
            builderJ.setView(linearLayout).setPositiveButton(R.string.ok, new ew0(jsPromptResult, editText, i10)).setNegativeButton(R.string.cancel, new vx(jsPromptResult)).setOnCancelListener(new t50(jsPromptResult, i12)).create().show();
        } else {
            builderJ.setMessage(str3).setPositiveButton(R.string.ok, new u50(jsResult, 1)).setNegativeButton(R.string.cancel, new u50(jsResult, 0)).setOnCancelListener(new t50(jsResult, i11)).create().show();
        }
        return true;
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // android.webkit.WebChromeClient
    public final void onCloseWindow(WebView webView) {
        switch (this.f17869a) {
            case 0:
                if (!(webView instanceof x50)) {
                    q7.j.d("Tried to close a WebView that wasn't an AdWebView.");
                } else {
                    com.google.android.gms.ads.internal.overlay.l lVarS = ((x50) webView).S();
                    if (lVarS != null) {
                        lVarS.zzb();
                    } else {
                        q7.j.d("Tried to close an AdWebView not associated with an overlay.");
                    }
                }
                break;
            default:
                super.onCloseWindow(webView);
                break;
        }
    }

    @Override // android.webkit.WebChromeClient
    public final boolean onConsoleMessage(ConsoleMessage consoleMessage) {
        switch (this.f17869a) {
            case 0:
                String strMessage = consoleMessage.message();
                String strSourceId = consoleMessage.sourceId();
                int iLineNumber = consoleMessage.lineNumber();
                int length = String.valueOf(strMessage).length();
                StringBuilder sb = new StringBuilder(length + 6 + String.valueOf(strSourceId).length() + 1 + String.valueOf(iLineNumber).length() + 1);
                u.a.h(sb, "JS: ", strMessage, " (", strSourceId);
                String strG = com.bytedance.sdk.component.adexpress.dynamic.dynamicview.b.g(sb, ":", iLineNumber, ")");
                if (!strG.contains("Application Cache")) {
                    int i10 = v50.f17517a[consoleMessage.messageLevel().ordinal()];
                    if (i10 == 1) {
                        q7.j.b(strG);
                    } else if (i10 == 2) {
                        q7.j.d(strG);
                    } else if (i10 == 3 || i10 == 4 || i10 != 5) {
                        q7.j.c(strG);
                    } else {
                        q7.j.a(strG);
                    }
                }
                break;
        }
        return super.onConsoleMessage(consoleMessage);
    }

    @Override // android.webkit.WebChromeClient
    public final boolean onCreateWindow(WebView webView, boolean z10, boolean z11, Message message) {
        int i10 = this.f17869a;
        Object obj = this.f17870b;
        switch (i10) {
            case 0:
                WebView.WebViewTransport webViewTransport = (WebView.WebViewTransport) message.obj;
                WebView webView2 = new WebView(webView.getContext());
                x50 x50Var = (x50) obj;
                if (x50Var.o0() != null) {
                    webView2.setWebViewClient(x50Var.o0());
                }
                webViewTransport.setWebView(webView2);
                message.sendToTarget();
                break;
            default:
                io.flutter.plugins.urllauncher.h hVar = new io.flutter.plugins.urllauncher.h(this);
                WebView webView3 = new WebView(((WebViewActivity) obj).f28321c.getContext());
                webView3.setWebViewClient(hVar);
                ((WebView.WebViewTransport) message.obj).setWebView(webView3);
                message.sendToTarget();
                break;
        }
        return true;
    }

    @Override // android.webkit.WebChromeClient
    public final void onExceededDatabaseQuota(String str, String str2, long j10, long j11, long j12, WebStorage.QuotaUpdater quotaUpdater) {
        switch (this.f17869a) {
            case 0:
                long j13 = CacheDataSink.DEFAULT_FRAGMENT_SIZE - j12;
                if (j13 > 0) {
                    if (j10 == 0) {
                        if (j11 > j13 || j11 > 1048576) {
                            j11 = 0;
                        }
                    } else if (j11 == 0) {
                        j11 = Math.min(Math.min(IjkMediaMeta.AV_CH_TOP_BACK_RIGHT, j13) + j10, 1048576L);
                    } else {
                        if (j11 <= Math.min(1048576 - j10, j13)) {
                            j10 += j11;
                        }
                        j11 = j10;
                    }
                    quotaUpdater.updateQuota(j11);
                } else {
                    quotaUpdater.updateQuota(j10);
                }
                break;
            default:
                super.onExceededDatabaseQuota(str, str2, j10, j11, j12, quotaUpdater);
                break;
        }
    }

    @Override // android.webkit.WebChromeClient
    public final void onGeolocationPermissionsShowPrompt(String str, GeolocationPermissions.Callback callback) {
        switch (this.f17869a) {
            case 0:
                if (callback != null) {
                    x50 x50Var = (x50) this.f17870b;
                    p7.r0 r0Var = m7.n.C.f30464c;
                    boolean z10 = p7.r0.b(x50Var.getContext(), "android.permission.ACCESS_FINE_LOCATION") || p7.r0.b(x50Var.getContext(), "android.permission.ACCESS_COARSE_LOCATION");
                    tn tnVar = ao.f9124ve;
                    n7.y yVar = n7.y.f31093e;
                    if (((Boolean) yVar.f31096c.b(tnVar)).booleanValue()) {
                        callback.invoke(str, false, true);
                    } else {
                        callback.invoke(str, z10, true);
                    }
                    if (((Boolean) yVar.f31096c.b(ao.f9141we)).booleanValue()) {
                        q7.j.a("AdWebChromeClient.onGeolocationPermissionsShowPrompt()");
                    }
                }
                break;
            default:
                super.onGeolocationPermissionsShowPrompt(str, callback);
                break;
        }
    }

    @Override // android.webkit.WebChromeClient
    public final void onHideCustomView() {
        switch (this.f17869a) {
            case 0:
                com.google.android.gms.ads.internal.overlay.l lVarS = ((x50) this.f17870b).S();
                if (lVarS != null) {
                    lVarS.zzc();
                } else {
                    q7.j.d("Could not get ad overlay when hiding custom view.");
                }
                break;
            default:
                super.onHideCustomView();
                break;
        }
    }

    @Override // android.webkit.WebChromeClient
    public final boolean onJsAlert(WebView webView, String str, String str2, JsResult jsResult) {
        switch (this.f17869a) {
            case 0:
                return a(b(webView), "alert", str, str2, null, jsResult, null, false);
            default:
                return super.onJsAlert(webView, str, str2, jsResult);
        }
    }

    @Override // android.webkit.WebChromeClient
    public final boolean onJsBeforeUnload(WebView webView, String str, String str2, JsResult jsResult) {
        switch (this.f17869a) {
            case 0:
                return a(b(webView), "onBeforeUnload", str, str2, null, jsResult, null, false);
            default:
                return super.onJsBeforeUnload(webView, str, str2, jsResult);
        }
    }

    @Override // android.webkit.WebChromeClient
    public final boolean onJsConfirm(WebView webView, String str, String str2, JsResult jsResult) {
        switch (this.f17869a) {
            case 0:
                return a(b(webView), "confirm", str, str2, null, jsResult, null, false);
            default:
                return super.onJsConfirm(webView, str, str2, jsResult);
        }
    }

    @Override // android.webkit.WebChromeClient
    public final boolean onJsPrompt(WebView webView, String str, String str2, String str3, JsPromptResult jsPromptResult) {
        switch (this.f17869a) {
            case 0:
                return a(b(webView), "prompt", str, str2, str3, null, jsPromptResult, true);
            default:
                return super.onJsPrompt(webView, str, str2, str3, jsPromptResult);
        }
    }

    @Override // android.webkit.WebChromeClient
    public final void onShowCustomView(View view, int i10, WebChromeClient.CustomViewCallback customViewCallback) {
        switch (this.f17869a) {
            case 0:
                com.google.android.gms.ads.internal.overlay.l lVarS = ((x50) this.f17870b).S();
                if (lVarS != null) {
                    Activity activity = lVarS.f7928b;
                    FrameLayout frameLayout = new FrameLayout(activity);
                    lVarS.f7933g = frameLayout;
                    frameLayout.setBackgroundColor(-16777216);
                    lVarS.f7933g.addView(view, -1, -1);
                    activity.setContentView(lVarS.f7933g);
                    lVarS.T = true;
                    lVarS.f7934h = customViewCallback;
                    lVarS.f7932f = true;
                    lVarS.Y2(i10);
                } else {
                    q7.j.d("Could not get ad overlay when showing custom view.");
                    customViewCallback.onCustomViewHidden();
                }
                break;
            default:
                super.onShowCustomView(view, i10, customViewCallback);
                break;
        }
    }

    public w50(WebViewActivity webViewActivity) {
        this.f17870b = webViewActivity;
    }

    @Override // android.webkit.WebChromeClient
    public final void onShowCustomView(View view, WebChromeClient.CustomViewCallback customViewCallback) {
        switch (this.f17869a) {
            case 0:
                onShowCustomView(view, -1, customViewCallback);
                break;
            default:
                super.onShowCustomView(view, customViewCallback);
                break;
        }
    }
}
