package com.andrognito.pinlockview;

import android.animation.LayoutTransition;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/* loaded from: classes.dex */
public class IndicatorDots extends LinearLayout {
    private static final int DEFAULT_PIN_LENGTH = 4;
    private int mDotDiameter;
    private int mDotSpacing;
    private int mEmptyDrawable;
    private int mFillDrawable;
    private int mIndicatorType;
    private int mPinLength;
    private int mPreviousLength;

    @Retention(RetentionPolicy.SOURCE)
    /* loaded from: classes.dex */
    public @interface IndicatorType {
        public static final int FILL = 1;
        public static final int FILL_WITH_ANIMATION = 2;
        public static final int FIXED = 0;
    }

    public IndicatorDots(Context context) {
        this(context, null);
    }

    public IndicatorDots(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndicatorDots(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PinLockView);
        try {
            this.mDotDiameter = (int) typedArray.getDimension(R.styleable.PinLockView_dotDiameter, ResourceUtils.getDimensionInPx(getContext(), R.dimen.default_dot_diameter));
            this.mDotSpacing = (int) typedArray.getDimension(R.styleable.PinLockView_dotSpacing, ResourceUtils.getDimensionInPx(getContext(), R.dimen.default_dot_spacing));
            this.mFillDrawable = typedArray.getResourceId(R.styleable.PinLockView_dotFilledBackground, R.drawable.dot_filled);
            this.mEmptyDrawable = typedArray.getResourceId(R.styleable.PinLockView_dotEmptyBackground, R.drawable.dot_empty);
            this.mPinLength = typedArray.getInt(R.styleable.PinLockView_pinLength, 4);
            this.mIndicatorType = typedArray.getInt(R.styleable.PinLockView_indicatorType, 0);
            typedArray.recycle();
            initView(context);
        } catch (Throwable th) {
            typedArray.recycle();
            throw th;
        }
    }

    private void initView(Context context) {
        ViewCompat.setLayoutDirection(this, 0);
        int i = this.mIndicatorType;
        if (i == 0) {
            for (int i2 = 0; i2 < this.mPinLength; i2++) {
                View dot = new View(context);
                emptyDot(dot);
                int i3 = this.mDotDiameter;
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(i3, i3);
                int i4 = this.mDotSpacing;
                params.setMargins(i4, 0, i4, 0);
                dot.setLayoutParams(params);
                addView(dot);
            }
        } else if (i == 2) {
            setLayoutTransition(new LayoutTransition());
        }
    }

    @Override // android.view.ViewGroup, android.view.View
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (this.mIndicatorType != 0) {
            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = this.mDotDiameter;
            requestLayout();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void updateDot(int length) {
        if (this.mIndicatorType == 0) {
            if (length > 0) {
                if (length > this.mPreviousLength) {
                    fillDot(getChildAt(length - 1));
                } else {
                    emptyDot(getChildAt(length));
                }
                this.mPreviousLength = length;
                return;
            }
            for (int i = 0; i < getChildCount(); i++) {
                View v = getChildAt(i);
                emptyDot(v);
            }
            this.mPreviousLength = 0;
        } else if (length > 0) {
            if (length > this.mPreviousLength) {
                View dot = new View(getContext());
                fillDot(dot);
                int i2 = this.mDotDiameter;
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(i2, i2);
                int i3 = this.mDotSpacing;
                params.setMargins(i3, 0, i3, 0);
                dot.setLayoutParams(params);
                addView(dot, length - 1);
            } else {
                removeViewAt(length);
            }
            this.mPreviousLength = length;
        } else {
            removeAllViews();
            this.mPreviousLength = 0;
        }
    }

    private void emptyDot(View dot) {
        dot.setBackgroundResource(this.mEmptyDrawable);
    }

    private void fillDot(View dot) {
        dot.setBackgroundResource(this.mFillDrawable);
    }

    public int getPinLength() {
        return this.mPinLength;
    }

    public void setPinLength(int pinLength) {
        this.mPinLength = pinLength;
        removeAllViews();
        initView(getContext());
    }

    public int getIndicatorType() {
        return this.mIndicatorType;
    }

    public void setIndicatorType(int type) {
        this.mIndicatorType = type;
        removeAllViews();
        initView(getContext());
    }
}
