package com.google.android.material.timepicker;

import android.R;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.TextView;
import androidx.core.view.s0;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.material.timepicker.ClockHandView;
import java.util.Arrays;
import t3.h;
import vi.d;
import vi.f;
import vi.k;
import vi.l;

/* loaded from: classes3.dex */
class ClockFaceView extends com.google.android.material.timepicker.b implements ClockHandView.c {
    private final ClockHandView Q;
    private final Rect R;
    private final RectF S;
    private final SparseArray T;
    private final androidx.core.view.a U;
    private final int[] V;
    private final float[] W;

    /* renamed from: a0, reason: collision with root package name */
    private final int f13748a0;

    /* renamed from: b0, reason: collision with root package name */
    private final int f13749b0;

    /* renamed from: c0, reason: collision with root package name */
    private final int f13750c0;

    /* renamed from: d0, reason: collision with root package name */
    private final int f13751d0;

    /* renamed from: e0, reason: collision with root package name */
    private String[] f13752e0;

    /* renamed from: f0, reason: collision with root package name */
    private float f13753f0;

    /* renamed from: g0, reason: collision with root package name */
    private final ColorStateList f13754g0;

    /* loaded from: classes3.dex */
    class a implements ViewTreeObserver.OnPreDrawListener {
        a() {
        }

        @Override // android.view.ViewTreeObserver.OnPreDrawListener
        public boolean onPreDraw() {
            if (!ClockFaceView.this.isShown()) {
                return true;
            }
            ClockFaceView.this.getViewTreeObserver().removeOnPreDrawListener(this);
            ClockFaceView.this.D(((ClockFaceView.this.getHeight() / 2) - ClockFaceView.this.Q.g()) - ClockFaceView.this.f13748a0);
            return true;
        }
    }

    /* loaded from: classes3.dex */
    class b extends androidx.core.view.a {
        b() {
        }

        @Override // androidx.core.view.a
        public void g(View view, h hVar) {
            super.g(view, hVar);
            int intValue = ((Integer) view.getTag(f.f39819p)).intValue();
            if (intValue > 0) {
                hVar.O0((View) ClockFaceView.this.T.get(intValue - 1));
            }
            hVar.j0(h.f.a(0, 1, intValue, 1, false, view.isSelected()));
            hVar.h0(true);
            hVar.b(h.a.f36134i);
        }

        @Override // androidx.core.view.a
        public boolean j(View view, int i10, Bundle bundle) {
            if (i10 != 16) {
                return super.j(view, i10, bundle);
            }
            long uptimeMillis = SystemClock.uptimeMillis();
            float x10 = view.getX() + (view.getWidth() / 2.0f);
            float height = (view.getHeight() / 2.0f) + view.getY();
            ClockFaceView.this.Q.onTouchEvent(MotionEvent.obtain(uptimeMillis, uptimeMillis, 0, x10, height, 0));
            ClockFaceView.this.Q.onTouchEvent(MotionEvent.obtain(uptimeMillis, uptimeMillis, 1, x10, height, 0));
            return true;
        }
    }

    public ClockFaceView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, vi.b.D);
    }

    private void K() {
        RectF d10 = this.Q.d();
        for (int i10 = 0; i10 < this.T.size(); i10++) {
            TextView textView = (TextView) this.T.get(i10);
            if (textView != null) {
                textView.getDrawingRect(this.R);
                offsetDescendantRectToMyCoords(textView, this.R);
                textView.setSelected(d10.contains(this.R.centerX(), this.R.centerY()));
                textView.getPaint().setShader(L(d10, this.R, textView));
                textView.invalidate();
            }
        }
    }

    private RadialGradient L(RectF rectF, Rect rect, TextView textView) {
        this.S.set(rect);
        this.S.offset(textView.getPaddingLeft(), textView.getPaddingTop());
        if (RectF.intersects(rectF, this.S)) {
            return new RadialGradient(rectF.centerX() - this.S.left, rectF.centerY() - this.S.top, rectF.width() * 0.5f, this.V, this.W, Shader.TileMode.CLAMP);
        }
        return null;
    }

    private static float M(float f10, float f11, float f12) {
        return Math.max(Math.max(f10, f11), f12);
    }

    private void O(int i10) {
        LayoutInflater from = LayoutInflater.from(getContext());
        int size = this.T.size();
        for (int i11 = 0; i11 < Math.max(this.f13752e0.length, size); i11++) {
            TextView textView = (TextView) this.T.get(i11);
            if (i11 >= this.f13752e0.length) {
                removeView(textView);
                this.T.remove(i11);
            } else {
                if (textView == null) {
                    textView = (TextView) from.inflate(vi.h.f39842j, (ViewGroup) this, false);
                    this.T.put(i11, textView);
                    addView(textView);
                }
                textView.setVisibility(0);
                textView.setText(this.f13752e0[i11]);
                textView.setTag(f.f39819p, Integer.valueOf(i11));
                s0.m0(textView, this.U);
                textView.setTextColor(this.f13754g0);
                if (i10 != 0) {
                    textView.setContentDescription(getResources().getString(i10, this.f13752e0[i11]));
                }
            }
        }
    }

    @Override // com.google.android.material.timepicker.b
    public void D(int i10) {
        if (i10 != C()) {
            super.D(i10);
            this.Q.j(C());
        }
    }

    public void N(String[] strArr, int i10) {
        this.f13752e0 = strArr;
        O(i10);
    }

    @Override // com.google.android.material.timepicker.ClockHandView.c
    public void a(float f10, boolean z10) {
        if (Math.abs(this.f13753f0 - f10) > 0.001f) {
            this.f13753f0 = f10;
            K();
        }
    }

    @Override // android.view.View
    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo accessibilityNodeInfo) {
        super.onInitializeAccessibilityNodeInfo(accessibilityNodeInfo);
        h.V0(accessibilityNodeInfo).i0(h.e.a(1, this.f13752e0.length, false, 1));
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // androidx.constraintlayout.widget.ConstraintLayout, android.view.ViewGroup, android.view.View
    public void onLayout(boolean z10, int i10, int i11, int i12, int i13) {
        super.onLayout(z10, i10, i11, i12, i13);
        K();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // androidx.constraintlayout.widget.ConstraintLayout, android.view.View
    public void onMeasure(int i10, int i11) {
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        int M = (int) (this.f13751d0 / M(this.f13749b0 / displayMetrics.heightPixels, this.f13750c0 / displayMetrics.widthPixels, 1.0f));
        int makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(M, 1073741824);
        setMeasuredDimension(M, M);
        super.onMeasure(makeMeasureSpec, makeMeasureSpec);
    }

    public ClockFaceView(Context context, AttributeSet attributeSet, int i10) {
        super(context, attributeSet, i10);
        this.R = new Rect();
        this.S = new RectF();
        this.T = new SparseArray();
        this.W = new float[]{BitmapDescriptorFactory.HUE_RED, 0.9f, 1.0f};
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, l.f40084r1, i10, k.A);
        Resources resources = getResources();
        ColorStateList a10 = ij.c.a(context, obtainStyledAttributes, l.f40104t1);
        this.f13754g0 = a10;
        LayoutInflater.from(context).inflate(vi.h.f39843k, (ViewGroup) this, true);
        ClockHandView clockHandView = (ClockHandView) findViewById(f.f39814k);
        this.Q = clockHandView;
        this.f13748a0 = resources.getDimensionPixelSize(d.f39786r);
        int colorForState = a10.getColorForState(new int[]{R.attr.state_selected}, a10.getDefaultColor());
        this.V = new int[]{colorForState, colorForState, a10.getDefaultColor()};
        clockHandView.b(this);
        int defaultColor = j.a.a(context, vi.c.f39751g).getDefaultColor();
        ColorStateList a11 = ij.c.a(context, obtainStyledAttributes, l.f40094s1);
        setBackgroundColor(a11 != null ? a11.getDefaultColor() : defaultColor);
        getViewTreeObserver().addOnPreDrawListener(new a());
        setFocusable(true);
        obtainStyledAttributes.recycle();
        this.U = new b();
        String[] strArr = new String[12];
        Arrays.fill(strArr, "");
        N(strArr, 0);
        this.f13749b0 = resources.getDimensionPixelSize(d.F);
        this.f13750c0 = resources.getDimensionPixelSize(d.G);
        this.f13751d0 = resources.getDimensionPixelSize(d.f39788t);
    }
}
