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;
import io.netty.handler.traffic.AbstractTrafficShapingHandler;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private static l0 f625a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static l0 f626b;

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final Runnable f630f = new a();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final Runnable f631g = new b();
    private int h;
    private int i;
    private m0 j;
    private boolean k;

    /* JADX INFO: compiled from: TooltipCompatHandler.java */
    class a implements Runnable {
        a() {
        }

        @Override // java.lang.Runnable
        public void run() {
            l0.this.g(false);
        }
    }

    /* JADX INFO: compiled from: TooltipCompatHandler.java */
    class b implements Runnable {
        b() {
        }

        @Override // java.lang.Runnable
        public void run() {
            l0.this.c();
        }
    }

    private l0(View view, CharSequence charSequence) {
        this.f627c = view;
        this.f628d = charSequence;
        this.f629e = a.c.i.m.a(ViewConfiguration.get(view.getContext()));
        b();
        view.setOnLongClickListener(this);
        view.setOnHoverListener(this);
    }

    private void a() {
        this.f627c.removeCallbacks(this.f630f);
    }

    private void b() {
        this.h = Integer.MAX_VALUE;
        this.i = Integer.MAX_VALUE;
    }

    private void d() {
        this.f627c.postDelayed(this.f630f, ViewConfiguration.getLongPressTimeout());
    }

    private static void e(l0 l0Var) {
        l0 l0Var2 = f625a;
        if (l0Var2 != null) {
            l0Var2.a();
        }
        f625a = l0Var;
        if (l0Var != null) {
            l0Var.d();
        }
    }

    public static void f(View view, CharSequence charSequence) {
        l0 l0Var = f625a;
        if (l0Var != null && l0Var.f627c == view) {
            e(null);
        }
        if (!TextUtils.isEmpty(charSequence)) {
            new l0(view, charSequence);
            return;
        }
        l0 l0Var2 = f626b;
        if (l0Var2 != null && l0Var2.f627c == view) {
            l0Var2.c();
        }
        view.setOnLongClickListener(null);
        view.setLongClickable(false);
        view.setOnHoverListener(null);
    }

    private boolean h(MotionEvent motionEvent) {
        int x = (int) motionEvent.getX();
        int y = (int) motionEvent.getY();
        if (Math.abs(x - this.h) <= this.f629e && Math.abs(y - this.i) <= this.f629e) {
            return false;
        }
        this.h = x;
        this.i = y;
        return true;
    }

    void c() {
        if (f626b == this) {
            f626b = null;
            m0 m0Var = this.j;
            if (m0Var != null) {
                m0Var.c();
                this.j = null;
                b();
                this.f627c.removeOnAttachStateChangeListener(this);
            } else {
                Log.e("TooltipCompatHandler", "sActiveHandler.mPopup == null");
            }
        }
        if (f625a == this) {
            e(null);
        }
        this.f627c.removeCallbacks(this.f631g);
    }

    void g(boolean z) {
        long j;
        int longPressTimeout;
        long j2;
        if (a.c.i.l.f(this.f627c)) {
            e(null);
            l0 l0Var = f626b;
            if (l0Var != null) {
                l0Var.c();
            }
            f626b = this;
            this.k = z;
            m0 m0Var = new m0(this.f627c.getContext());
            this.j = m0Var;
            m0Var.e(this.f627c, this.h, this.i, this.k, this.f628d);
            this.f627c.addOnAttachStateChangeListener(this);
            if (this.k) {
                j2 = 2500;
            } else {
                if ((a.c.i.l.e(this.f627c) & 1) == 1) {
                    j = 3000;
                    longPressTimeout = ViewConfiguration.getLongPressTimeout();
                } else {
                    j = AbstractTrafficShapingHandler.DEFAULT_MAX_TIME;
                    longPressTimeout = ViewConfiguration.getLongPressTimeout();
                }
                j2 = j - ((long) longPressTimeout);
            }
            this.f627c.removeCallbacks(this.f631g);
            this.f627c.postDelayed(this.f631g, j2);
        }
    }

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

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

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

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