package com.google.android.material.timepicker;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Pair;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import androidx.core.view.y;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import l0.j;
import l0.k;

/* JADX INFO: loaded from: classes.dex */
class ClockHandView extends View {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private ValueAnimator f4357e;

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private float f4359g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private float f4360h;

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private final List<d> f4363k;

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private final float f4365m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private final Paint f4366n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private final RectF f4367o;

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

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    private float f4369q;

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

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    private c f4371s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    private double f4372t;

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

    class a implements ValueAnimator.AnimatorUpdateListener {
        a() {
        }

        @Override // android.animation.ValueAnimator.AnimatorUpdateListener
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            ClockHandView.this.m(((Float) valueAnimator.getAnimatedValue()).floatValue(), true);
        }
    }

    class b extends AnimatorListenerAdapter {
        b(ClockHandView clockHandView) {
        }

        @Override // android.animation.AnimatorListenerAdapter, android.animation.Animator.AnimatorListener
        public void onAnimationCancel(Animator animator) {
            animator.end();
        }
    }

    public interface c {
        void a(float f3, boolean z2);
    }

    public interface d {
        void a(float f3, boolean z2);
    }

    public ClockHandView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, l0.b.f5337t);
    }

    private void c(Canvas canvas) {
        int height = getHeight() / 2;
        float width = getWidth() / 2;
        float fCos = (this.f4373u * ((float) Math.cos(this.f4372t))) + width;
        float f3 = height;
        float fSin = (this.f4373u * ((float) Math.sin(this.f4372t))) + f3;
        this.f4366n.setStrokeWidth(0.0f);
        canvas.drawCircle(fCos, fSin, this.f4364l, this.f4366n);
        double dSin = Math.sin(this.f4372t);
        double dCos = Math.cos(this.f4372t);
        this.f4366n.setStrokeWidth(this.f4368p);
        canvas.drawLine(width, f3, r1 + ((int) (dCos * d)), height + ((int) (d * dSin)), this.f4366n);
        canvas.drawCircle(width, f3, this.f4365m, this.f4366n);
    }

    private int e(float f3, float f4) {
        int degrees = ((int) Math.toDegrees(Math.atan2(f4 - (getHeight() / 2), f3 - (getWidth() / 2)))) + 90;
        return degrees < 0 ? degrees + 360 : degrees;
    }

    private Pair<Float, Float> h(float f3) {
        float f4 = f();
        if (Math.abs(f4 - f3) > 180.0f) {
            if (f4 > 180.0f && f3 < 180.0f) {
                f3 += 360.0f;
            }
            if (f4 < 180.0f && f3 > 180.0f) {
                f4 += 360.0f;
            }
        }
        return new Pair<>(Float.valueOf(f4), Float.valueOf(f3));
    }

    private boolean i(float f3, float f4, boolean z2, boolean z3, boolean z4) {
        float fE = e(f3, f4);
        boolean z5 = false;
        boolean z6 = f() != fE;
        if (z3 && z6) {
            return true;
        }
        if (!z6 && !z2) {
            return false;
        }
        if (z4 && this.f4358f) {
            z5 = true;
        }
        l(fE, z5);
        return true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void m(float f3, boolean z2) {
        float f4 = f3 % 360.0f;
        this.f4369q = f4;
        this.f4372t = Math.toRadians(f4 - 90.0f);
        int height = getHeight() / 2;
        float width = (getWidth() / 2) + (this.f4373u * ((float) Math.cos(this.f4372t)));
        float fSin = height + (this.f4373u * ((float) Math.sin(this.f4372t)));
        RectF rectF = this.f4367o;
        int i3 = this.f4364l;
        rectF.set(width - i3, fSin - i3, width + i3, fSin + i3);
        Iterator<d> it = this.f4363k.iterator();
        while (it.hasNext()) {
            it.next().a(f4, z2);
        }
        invalidate();
    }

    public void b(d dVar) {
        this.f4363k.add(dVar);
    }

    public RectF d() {
        return this.f4367o;
    }

    public float f() {
        return this.f4369q;
    }

    public int g() {
        return this.f4364l;
    }

    public void j(int i3) {
        this.f4373u = i3;
        invalidate();
    }

    public void k(float f3) {
        l(f3, false);
    }

    public void l(float f3, boolean z2) {
        ValueAnimator valueAnimator = this.f4357e;
        if (valueAnimator != null) {
            valueAnimator.cancel();
        }
        if (!z2) {
            m(f3, false);
            return;
        }
        Pair<Float, Float> pairH = h(f3);
        ValueAnimator valueAnimatorOfFloat = ValueAnimator.ofFloat(((Float) pairH.first).floatValue(), ((Float) pairH.second).floatValue());
        this.f4357e = valueAnimatorOfFloat;
        valueAnimatorOfFloat.setDuration(200L);
        this.f4357e.addUpdateListener(new a());
        this.f4357e.addListener(new b(this));
        this.f4357e.start();
    }

    @Override // android.view.View
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        c(canvas);
    }

    @Override // android.view.View
    protected void onLayout(boolean z2, int i3, int i4, int i5, int i6) {
        super.onLayout(z2, i3, i4, i5, i6);
        k(f());
    }

    @Override // android.view.View
    @SuppressLint({"ClickableViewAccessibility"})
    public boolean onTouchEvent(MotionEvent motionEvent) {
        boolean z2;
        boolean z3;
        boolean z4;
        c cVar;
        int actionMasked = motionEvent.getActionMasked();
        float x2 = motionEvent.getX();
        float y2 = motionEvent.getY();
        if (actionMasked == 0) {
            this.f4359g = x2;
            this.f4360h = y2;
            this.f4361i = true;
            this.f4370r = false;
            z2 = false;
            z3 = false;
            z4 = true;
        } else if (actionMasked == 1 || actionMasked == 2) {
            int i3 = (int) (x2 - this.f4359g);
            int i4 = (int) (y2 - this.f4360h);
            this.f4361i = (i3 * i3) + (i4 * i4) > this.f4362j;
            boolean z5 = this.f4370r;
            z2 = actionMasked == 1;
            z4 = false;
            z3 = z5;
        } else {
            z2 = false;
            z3 = false;
            z4 = false;
        }
        boolean zI = i(x2, y2, z3, z4, z2) | this.f4370r;
        this.f4370r = zI;
        if (zI && z2 && (cVar = this.f4371s) != null) {
            cVar.a(e(x2, y2), this.f4361i);
        }
        return true;
    }

    public ClockHandView(Context context, AttributeSet attributeSet, int i3) {
        super(context, attributeSet, i3);
        this.f4363k = new ArrayList();
        Paint paint = new Paint();
        this.f4366n = paint;
        this.f4367o = new RectF();
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, k.B0, i3, j.f5458m);
        this.f4373u = typedArrayObtainStyledAttributes.getDimensionPixelSize(k.D0, 0);
        this.f4364l = typedArrayObtainStyledAttributes.getDimensionPixelSize(k.E0, 0);
        this.f4368p = getResources().getDimensionPixelSize(l0.d.f5360k);
        this.f4365m = r6.getDimensionPixelSize(l0.d.f5358i);
        int color = typedArrayObtainStyledAttributes.getColor(k.C0, 0);
        paint.setAntiAlias(true);
        paint.setColor(color);
        k(0.0f);
        this.f4362j = ViewConfiguration.get(context).getScaledTouchSlop();
        y.B0(this, 2);
        typedArrayObtainStyledAttributes.recycle();
    }
}
