package androidx.appcompat.widget;

import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.accessibility.AccessibilityManager;

/* JADX INFO: compiled from: TooltipCompatHandler.java */
/* JADX INFO: loaded from: classes.dex */
class g1 implements View.OnLongClickListener, View.OnHoverListener, View.OnAttachStateChangeListener {

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private static g1 f911o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    private static g1 f912p;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final View f913e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final CharSequence f914f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final int f915g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final Runnable f916h = new Runnable() { // from class: androidx.appcompat.widget.f1
        @Override // java.lang.Runnable
        public final void run() {
            this.f906e.e();
        }
    };

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final Runnable f917i = new Runnable() { // from class: androidx.appcompat.widget.e1
        @Override // java.lang.Runnable
        public final void run() {
            this.f899e.d();
        }
    };

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private int f918j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private int f919k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private h1 f920l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private boolean f921m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private boolean f922n;

    private g1(View view, CharSequence charSequence) {
        this.f913e = view;
        this.f914f = charSequence;
        this.f915g = androidx.core.view.a0.c(ViewConfiguration.get(view.getContext()));
        c();
        view.setOnLongClickListener(this);
        view.setOnHoverListener(this);
    }

    private void b() {
        this.f913e.removeCallbacks(this.f916h);
    }

    private void c() {
        this.f922n = true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void e() {
        i(false);
    }

    private void f() {
        this.f913e.postDelayed(this.f916h, ViewConfiguration.getLongPressTimeout());
    }

    private static void g(g1 g1Var) {
        g1 g1Var2 = f911o;
        if (g1Var2 != null) {
            g1Var2.b();
        }
        f911o = g1Var;
        if (g1Var != null) {
            g1Var.f();
        }
    }

    public static void h(View view, CharSequence charSequence) {
        g1 g1Var = f911o;
        if (g1Var != null && g1Var.f913e == view) {
            g(null);
        }
        if (!TextUtils.isEmpty(charSequence)) {
            new g1(view, charSequence);
            return;
        }
        g1 g1Var2 = f912p;
        if (g1Var2 != null && g1Var2.f913e == view) {
            g1Var2.d();
        }
        view.setOnLongClickListener(null);
        view.setLongClickable(false);
        view.setOnHoverListener(null);
    }

    private boolean j(MotionEvent motionEvent) {
        int x2 = (int) motionEvent.getX();
        int y2 = (int) motionEvent.getY();
        if (!this.f922n && Math.abs(x2 - this.f918j) <= this.f915g && Math.abs(y2 - this.f919k) <= this.f915g) {
            return false;
        }
        this.f918j = x2;
        this.f919k = y2;
        this.f922n = false;
        return true;
    }

    void d() {
        if (f912p == this) {
            f912p = null;
            h1 h1Var = this.f920l;
            if (h1Var != null) {
                h1Var.c();
                this.f920l = null;
                c();
                this.f913e.removeOnAttachStateChangeListener(this);
            } else {
                Log.e("TooltipCompatHandler", "sActiveHandler.mPopup == null");
            }
        }
        if (f911o == this) {
            g(null);
        }
        this.f913e.removeCallbacks(this.f917i);
    }

    void i(boolean z2) {
        long j3;
        int longPressTimeout;
        long j4;
        if (androidx.core.view.y.U(this.f913e)) {
            g(null);
            g1 g1Var = f912p;
            if (g1Var != null) {
                g1Var.d();
            }
            f912p = this;
            this.f921m = z2;
            h1 h1Var = new h1(this.f913e.getContext());
            this.f920l = h1Var;
            h1Var.e(this.f913e, this.f918j, this.f919k, this.f921m, this.f914f);
            this.f913e.addOnAttachStateChangeListener(this);
            if (this.f921m) {
                j4 = 2500;
            } else {
                if ((androidx.core.view.y.N(this.f913e) & 1) == 1) {
                    j3 = 3000;
                    longPressTimeout = ViewConfiguration.getLongPressTimeout();
                } else {
                    j3 = 15000;
                    longPressTimeout = ViewConfiguration.getLongPressTimeout();
                }
                j4 = j3 - ((long) longPressTimeout);
            }
            this.f913e.removeCallbacks(this.f917i);
            this.f913e.postDelayed(this.f917i, j4);
        }
    }

    @Override // android.view.View.OnHoverListener
    public boolean onHover(View view, MotionEvent motionEvent) {
        if (this.f920l != null && this.f921m) {
            return false;
        }
        AccessibilityManager accessibilityManager = (AccessibilityManager) this.f913e.getContext().getSystemService("accessibility");
        if (accessibilityManager.isEnabled() && accessibilityManager.isTouchExplorationEnabled()) {
            return false;
        }
        int action = motionEvent.getAction();
        if (action != 7) {
            if (action == 10) {
                c();
                d();
            }
        } else if (this.f913e.isEnabled() && this.f920l == null && j(motionEvent)) {
            g(this);
        }
        return false;
    }

    @Override // android.view.View.OnLongClickListener
    public boolean onLongClick(View view) {
        this.f918j = view.getWidth() / 2;
        this.f919k = view.getHeight() / 2;
        i(true);
        return true;
    }

    @Override // android.view.View.OnAttachStateChangeListener
    public void onViewAttachedToWindow(View view) {
    }

    @Override // android.view.View.OnAttachStateChangeListener
    public void onViewDetachedFromWindow(View view) {
        d();
    }
}
