package com.pichillilorenzo.flutter_inappwebview_android.webview.in_app_webview;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.ListPopupWindow;
import androidx.annotation.Nullable;

/* JADX INFO: loaded from: classes.dex */
public class InputAwareWebView extends WebView {
    private static final String LOG_TAG = "InputAwareWebView";

    @Nullable
    public View containerView;
    private ThreadedInputConnectionProxyAdapterView proxyAdapterView;
    private View threadedInputConnectionProxyView;
    private boolean useHybridComposition;

    public InputAwareWebView(Context context, @Nullable View view, Boolean bool) {
        super(context);
        this.useHybridComposition = false;
        this.containerView = view;
        this.useHybridComposition = bool != null ? bool.booleanValue() : false;
    }

    private boolean isCalledFromListPopupWindowShow() {
        for (StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {
            if (stackTraceElement.getClassName().equals(ListPopupWindow.class.getCanonicalName()) && stackTraceElement.getMethodName().equals("show")) {
                return true;
            }
        }
        return false;
    }

    private void resetInputConnection() {
        View view;
        if (this.proxyAdapterView == null || (view = this.containerView) == null) {
            return;
        }
        setInputConnectionTarget(view);
    }

    private void setInputConnectionTarget(final View view) {
        if (this.containerView == null) {
            return;
        }
        view.requestFocus();
        this.containerView.post(new Runnable() { // from class: com.pichillilorenzo.flutter_inappwebview_android.webview.in_app_webview.InputAwareWebView.1
            @Override // java.lang.Runnable
            public void run() {
                InputAwareWebView inputAwareWebView = InputAwareWebView.this;
                if (inputAwareWebView.containerView == null) {
                    return;
                }
                InputMethodManager inputMethodManager = (InputMethodManager) inputAwareWebView.getContext().getSystemService("input_method");
                view.onWindowFocusChanged(true);
                if (Build.VERSION.SDK_INT < 29) {
                    inputMethodManager.isActive(InputAwareWebView.this.containerView);
                }
            }
        });
    }

    @Override // android.view.View
    public boolean checkInputConnectionProxy(View view) {
        if (this.useHybridComposition) {
            return super.checkInputConnectionProxy(view);
        }
        View view2 = this.threadedInputConnectionProxyView;
        this.threadedInputConnectionProxyView = view;
        if (view2 == view) {
            return super.checkInputConnectionProxy(view);
        }
        View view3 = this.containerView;
        if (view3 == null) {
            return super.checkInputConnectionProxy(view);
        }
        ThreadedInputConnectionProxyAdapterView threadedInputConnectionProxyAdapterView = new ThreadedInputConnectionProxyAdapterView(view3, view, view.getHandler());
        this.proxyAdapterView = threadedInputConnectionProxyAdapterView;
        setInputConnectionTarget(threadedInputConnectionProxyAdapterView);
        return super.checkInputConnectionProxy(view);
    }

    @Override // android.view.ViewGroup, android.view.View
    public void clearFocus() {
        super.clearFocus();
        if (this.useHybridComposition) {
            return;
        }
        resetInputConnection();
    }

    public void dispose() {
        if (this.useHybridComposition) {
            return;
        }
        resetInputConnection();
    }

    public void lockInputConnection() {
        ThreadedInputConnectionProxyAdapterView threadedInputConnectionProxyAdapterView = this.proxyAdapterView;
        if (threadedInputConnectionProxyAdapterView == null) {
            return;
        }
        threadedInputConnectionProxyAdapterView.setLocked(true);
    }

    @Override // android.webkit.WebView, android.view.View
    public void onFocusChanged(boolean z10, int i10, Rect rect) {
        if (this.useHybridComposition) {
            super.onFocusChanged(z10, i10, rect);
        } else if (Build.VERSION.SDK_INT >= 28 || !isCalledFromListPopupWindowShow() || z10) {
            super.onFocusChanged(z10, i10, rect);
        }
    }

    public void setContainerView(View view) {
        this.containerView = view;
        ThreadedInputConnectionProxyAdapterView threadedInputConnectionProxyAdapterView = this.proxyAdapterView;
        if (threadedInputConnectionProxyAdapterView == null || view == null) {
            return;
        }
        setInputConnectionTarget(threadedInputConnectionProxyAdapterView);
    }

    public void unlockInputConnection() {
        ThreadedInputConnectionProxyAdapterView threadedInputConnectionProxyAdapterView = this.proxyAdapterView;
        if (threadedInputConnectionProxyAdapterView == null) {
            return;
        }
        threadedInputConnectionProxyAdapterView.setLocked(false);
    }

    public InputAwareWebView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.useHybridComposition = false;
        this.containerView = null;
    }

    public InputAwareWebView(Context context) {
        super(context);
        this.useHybridComposition = false;
        this.containerView = null;
    }

    public InputAwareWebView(Context context, AttributeSet attributeSet, int i10) {
        super(context, attributeSet, i10);
        this.useHybridComposition = false;
        this.containerView = null;
    }
}
