package com.github.leondevlifelog.gesturelockview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Region;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import androidx.core.content.ContextCompat;
import java.lang.reflect.Array;
import org.apache.http.HttpStatus;

/* loaded from: classes.dex */
public class GestureLockView extends View {
    private static final int DEFAULT_COL = 3;
    private static final int DEFAULT_ROW = 3;
    private static final int LANDSCAPE = 0;
    private static final int PORTRAIT = 1;
    private static final String TAG = "GestureLockView";
    private int ERROR;
    private int NORMAL;
    private int STATUS;
    private Runnable action;
    private int col;
    private int contentHeight;
    private int contentWidth;
    private Point currentPoint;
    private int dotColor;
    private Paint dotPaint;
    private int dotPressedColor;
    private int dotPressedRadius;
    private int dotRadius;
    private Point[][] dotsPos;
    private Region[][] dotsRegion;
    private int[][] dotsStatus;
    private Region globalRegion;
    private boolean isTouching;
    private Point lastPoint;
    private int lineColor;
    private Paint linePaint;
    private Path linePath;
    private int lineWidth;
    private Context mContext;
    private int minLength;
    private OnCheckPasswordListener onCheckPasswordListener;
    private int paddingLeft;
    private StringBuilder password;
    private int realDotPressedColor;
    private int realLineColor;
    private int row;
    private boolean securityMode;
    private Path tmpPath;
    private Vibrator vibrator;
    private int widthHeightOffset;

    /* loaded from: classes.dex */
    public interface OnCheckPasswordListener {
        boolean onCheckPassword(String str);

        void onError();

        void onSuccess();
    }

    public GestureLockView(Context context) {
        super(context);
        this.isTouching = false;
        this.ERROR = 1;
        this.NORMAL = 2;
        this.STATUS = 2;
        this.mContext = context;
        init(null, 0);
    }

    public GestureLockView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.isTouching = false;
        this.ERROR = 1;
        this.NORMAL = 2;
        this.STATUS = 2;
        this.mContext = context;
        init(attributeSet, 0);
    }

    public GestureLockView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.isTouching = false;
        this.ERROR = 1;
        this.NORMAL = 2;
        this.STATUS = 2;
        this.mContext = context;
        init(attributeSet, i);
    }

    private void init(AttributeSet attributeSet, int i) {
        TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attributeSet, R.styleable.GestureLockView, i, 0);
        this.col = obtainStyledAttributes.getInt(R.styleable.GestureLockView_row, 3);
        this.row = obtainStyledAttributes.getInt(R.styleable.GestureLockView_col, 3);
        this.dotColor = obtainStyledAttributes.getColor(R.styleable.GestureLockView_dot_color, ContextCompat.getColor(this.mContext, R.color.default_dot_color));
        this.dotRadius = obtainStyledAttributes.getDimensionPixelSize(R.styleable.GestureLockView_dot_radius, getResources().getDimensionPixelSize(R.dimen.default_dot_radius));
        this.dotPressedColor = obtainStyledAttributes.getColor(R.styleable.GestureLockView_dot_color_pressed, ContextCompat.getColor(this.mContext, R.color.default_dot_pressed_color));
        this.lineColor = obtainStyledAttributes.getColor(R.styleable.GestureLockView_line_color, ContextCompat.getColor(this.mContext, R.color.default_line_color));
        this.lineWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.GestureLockView_line_width, getResources().getDimensionPixelSize(R.dimen.default_line_width));
        this.securityMode = obtainStyledAttributes.getBoolean(R.styleable.GestureLockView_security_mode, false);
        this.dotPressedRadius = obtainStyledAttributes.getDimensionPixelSize(R.styleable.GestureLockView_dot_pressed_radius, getResources().getDimensionPixelSize(R.dimen.default_dot_pressed_radius));
        boolean z = obtainStyledAttributes.getBoolean(R.styleable.GestureLockView_vibrate, true);
        this.minLength = obtainStyledAttributes.getInt(R.styleable.GestureLockView_min_length, 4);
        obtainStyledAttributes.recycle();
        if (z) {
            this.vibrator = (Vibrator) getContext().getSystemService("vibrator");
        }
        Paint paint = new Paint();
        this.dotPaint = paint;
        paint.setFlags(1);
        this.dotPaint.setAntiAlias(true);
        this.dotPaint.setStyle(Paint.Style.FILL);
        Paint paint2 = new Paint();
        this.linePaint = paint2;
        paint2.setColor(this.lineColor);
        this.linePaint.setStyle(Paint.Style.STROKE);
        this.linePaint.setStrokeWidth(this.lineWidth);
        this.linePaint.setAntiAlias(true);
        this.linePaint.setStrokeJoin(Paint.Join.ROUND);
        this.linePaint.setStrokeCap(Paint.Cap.ROUND);
        this.dotsRegion = (Region[][]) Array.newInstance((Class<?>) Region.class, this.row, this.col);
        this.dotsPos = (Point[][]) Array.newInstance((Class<?>) Point.class, this.row, this.col);
        int i2 = 0;
        while (true) {
            int i3 = this.row;
            if (i2 < i3) {
                for (int i4 = 0; i4 < this.col; i4++) {
                    this.dotsRegion[i2][i4] = new Region();
                    this.dotsPos[i2][i4] = new Point(0, 0);
                }
                i2++;
            } else {
                this.dotsStatus = (int[][]) Array.newInstance((Class<?>) Integer.TYPE, i3, this.col);
                this.tmpPath = new Path();
                this.currentPoint = new Point();
                this.linePath = new Path();
                this.lastPoint = new Point(0, 0);
                this.password = new StringBuilder();
                return;
            }
        }
    }

    @Override // android.view.View
    protected void onSizeChanged(int i, int i2, int i3, int i4) {
        super.onSizeChanged(i, i2, i3, i4);
        this.paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();
        this.contentWidth = (getWidth() - this.paddingLeft) - paddingRight;
        int height = (getHeight() - paddingTop) - paddingBottom;
        this.contentHeight = height;
        this.widthHeightOffset = Math.abs(height - this.contentWidth);
        this.globalRegion = new Region(0, 0, getWidth(), getBottom());
    }

    @Override // android.view.View
    protected void onMeasure(int i, int i2) {
        int mode = View.MeasureSpec.getMode(i);
        int size = View.MeasureSpec.getSize(i);
        int mode2 = View.MeasureSpec.getMode(i2);
        int size2 = View.MeasureSpec.getSize(i2);
        if (mode != 1073741824) {
            size = mode == Integer.MIN_VALUE ? Math.min(HttpStatus.SC_BAD_REQUEST, size) : 400;
        }
        if (mode2 != 1073741824) {
            size2 = mode2 == Integer.MIN_VALUE ? Math.min(HttpStatus.SC_BAD_REQUEST, size2) : 400;
        }
        setMeasuredDimension(size, size2);
    }

    @Override // android.view.View
    protected void onDraw(Canvas canvas) {
        int i;
        super.onDraw(canvas);
        if ((this.lastPoint.x != 0 || this.lastPoint.y != 0) && !this.securityMode && this.password.length() > 0) {
            canvas.drawLine(this.lastPoint.x, this.lastPoint.y, this.currentPoint.x, this.currentPoint.y, this.linePaint);
        }
        if (!this.securityMode || this.STATUS == this.ERROR) {
            canvas.drawPath(this.linePath, this.linePaint);
        }
        for (int i2 = 1; i2 <= this.row; i2++) {
            for (int i3 = 1; i3 <= this.col; i3++) {
                float miniViewSize = (((getMiniViewSize() / this.row) * i2) - ((getMiniViewSize() / this.row) / 2.0f)) + this.paddingLeft;
                float miniViewSize2 = (((getMiniViewSize() / this.col) * i3) - ((getMiniViewSize() / this.col) / 2.0f)) + this.paddingLeft;
                if (getViewOrientation() == 1) {
                    miniViewSize2 += this.widthHeightOffset / 2.0f;
                } else {
                    miniViewSize += this.widthHeightOffset / 2.0f;
                }
                int i4 = i2 - 1;
                int i5 = i3 - 1;
                if (this.dotsStatus[i4][i5] == 1) {
                    if (!this.securityMode || this.STATUS == this.ERROR) {
                        this.dotPaint.setColor(this.realDotPressedColor);
                    }
                    i = this.dotPressedRadius > getTouchAreaMimiRadius() ? getTouchAreaMimiRadius() - 16 : this.dotPressedRadius;
                } else {
                    this.dotPaint.setColor(this.dotColor);
                    i = this.dotRadius;
                }
                canvas.drawCircle(miniViewSize, miniViewSize2, i, this.dotPaint);
                this.dotsPos[i4][i5].set((int) miniViewSize, (int) miniViewSize2);
                this.tmpPath.reset();
                this.tmpPath.addCircle(miniViewSize, miniViewSize2, getTouchAreaMimiRadius(), Path.Direction.CW);
                this.dotsRegion[i4][i5].setPath(this.tmpPath, this.globalRegion);
            }
        }
    }

    private int getTouchAreaMimiRadius() {
        return (getMiniViewSize() / Math.max(this.col, this.row)) / 2;
    }

    private int getMiniViewSize() {
        return Math.min(this.contentHeight, this.contentWidth);
    }

    private int getViewOrientation() {
        return this.contentWidth > this.contentHeight ? 0 : 1;
    }

    @Override // android.view.View
    public boolean onTouchEvent(MotionEvent motionEvent) {
        int action = motionEvent.getAction();
        if (action == 0) {
            dealDown(motionEvent);
            return true;
        }
        if (action == 1) {
            resetStatus();
            return true;
        }
        if (action != 2) {
            return true;
        }
        dealMove(motionEvent);
        return true;
    }

    private void dealDown(MotionEvent motionEvent) {
        Runnable runnable = this.action;
        if (runnable != null) {
            removeCallbacks(runnable);
        }
        reset();
        setStatus(this.NORMAL);
        for (int i = 0; i < this.row; i++) {
            for (int i2 = 0; i2 < this.col; i2++) {
                if (this.dotsRegion[i][i2].contains((int) motionEvent.getX(), (int) motionEvent.getY())) {
                    if (this.dotsStatus[i][i2] == 0) {
                        this.linePath.moveTo(this.dotsPos[i][i2].x, this.dotsPos[i][i2].y);
                        this.lastPoint.set(this.dotsPos[i][i2].x, this.dotsPos[i][i2].y);
                        vibrator();
                        String valueOf = String.valueOf((char) ((this.row * i2) + i + 97));
                        Log.d(TAG, "dealDown: posString" + valueOf);
                        if (this.password.indexOf(valueOf) == -1) {
                            this.password.append(valueOf);
                        }
                    }
                    this.dotsStatus[i][i2] = 1;
                }
            }
        }
    }

    private void dealMove(MotionEvent motionEvent) {
        this.currentPoint.set((int) motionEvent.getX(), (int) motionEvent.getY());
        for (int i = 0; i < this.row; i++) {
            for (int i2 = 0; i2 < this.col; i2++) {
                if (this.dotsRegion[i][i2].contains((int) motionEvent.getX(), (int) motionEvent.getY())) {
                    if (this.dotsStatus[i][i2] == 0) {
                        this.linePath.lineTo(this.dotsPos[i][i2].x, this.dotsPos[i][i2].y);
                        this.lastPoint.set(this.dotsPos[i][i2].x, this.dotsPos[i][i2].y);
                        vibrator();
                        this.isTouching = true;
                        String valueOf = String.valueOf((char) ((this.row * i2) + i + 97));
                        Log.d(TAG, "dealDown: posString" + valueOf);
                        if (this.password.indexOf(valueOf) == -1) {
                            this.password.append(valueOf);
                        }
                    }
                    this.dotsStatus[i][i2] = 1;
                } else {
                    this.isTouching = false;
                }
            }
        }
        invalidate();
    }

    private void resetStatus() {
        this.currentPoint.set(this.lastPoint.x, this.lastPoint.y);
        if (this.password.toString().length() < this.minLength) {
            setStatus(this.ERROR);
            OnCheckPasswordListener onCheckPasswordListener = this.onCheckPasswordListener;
            if (onCheckPasswordListener != null) {
                onCheckPasswordListener.onError();
            }
        } else {
            OnCheckPasswordListener onCheckPasswordListener2 = this.onCheckPasswordListener;
            if (onCheckPasswordListener2 != null && onCheckPasswordListener2.onCheckPassword(this.password.toString())) {
                OnCheckPasswordListener onCheckPasswordListener3 = this.onCheckPasswordListener;
                if (onCheckPasswordListener3 != null) {
                    onCheckPasswordListener3.onSuccess();
                }
            } else {
                setStatus(this.ERROR);
                OnCheckPasswordListener onCheckPasswordListener4 = this.onCheckPasswordListener;
                if (onCheckPasswordListener4 != null) {
                    onCheckPasswordListener4.onError();
                }
            }
        }
        StringBuilder sb = this.password;
        sb.delete(0, sb.length());
        invalidate();
        Runnable runnable = new Runnable() { // from class: com.github.leondevlifelog.gesturelockview.GestureLockView.1
            @Override // java.lang.Runnable
            public void run() {
                GestureLockView.this.reset();
                GestureLockView.this.invalidate();
            }
        };
        this.action = runnable;
        postDelayed(runnable, 1000L);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void reset() {
        for (int i = 0; i < this.row; i++) {
            for (int i2 = 0; i2 < this.col; i2++) {
                this.dotsStatus[i][i2] = 0;
            }
        }
        this.linePath.rewind();
        this.linePath.moveTo(this.currentPoint.x, this.currentPoint.y);
        StringBuilder sb = this.password;
        sb.delete(0, sb.length());
    }

    private void setStatus(int i) {
        if (this.ERROR == i) {
            this.realLineColor = Color.parseColor("#66FF0000");
            this.realDotPressedColor = Color.parseColor("#FF0000");
        } else if (this.NORMAL == i) {
            this.realLineColor = this.lineColor;
            this.realDotPressedColor = this.dotPressedColor;
        }
        this.STATUS = i;
        this.linePaint.setColor(this.realLineColor);
    }

    private void vibrator() {
        if (this.isTouching || this.vibrator == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= 26) {
            this.vibrator.vibrate(VibrationEffect.createOneShot(30L, 10));
        } else {
            this.vibrator.vibrate(30L);
        }
    }

    public int getRow() {
        return this.row;
    }

    public void setRow(int i) {
        this.row = i;
        invalidate();
    }

    public int getCol() {
        return this.col;
    }

    public void setCol(int i) {
        this.col = i;
        invalidate();
    }

    public int getDotColor() {
        return this.dotColor;
    }

    public void setDotColor(int i) {
        this.dotColor = i;
        invalidate();
    }

    public int getDotRadius() {
        return this.dotRadius;
    }

    public void setDotRadius(int i) {
        this.dotRadius = i;
        invalidate();
    }

    public int getDotPressedColor() {
        return this.dotPressedColor;
    }

    public void setDotPressedColor(int i) {
        this.dotPressedColor = i;
        invalidate();
    }

    public int getLineColor() {
        return this.lineColor;
    }

    public void setLineColor(int i) {
        this.lineColor = i;
        invalidate();
    }

    public int getLineWidth() {
        return this.lineWidth;
    }

    public void setLineWidth(int i) {
        this.lineWidth = i;
        invalidate();
    }

    public boolean isSecurityMode() {
        return this.securityMode;
    }

    public void setSecurityMode(boolean z) {
        this.securityMode = z;
        invalidate();
    }

    public int getDotPressedRadius() {
        return this.dotPressedRadius;
    }

    public void setDotPressedRadius(int i) {
        this.dotPressedRadius = i;
        invalidate();
    }

    public int getMinLength() {
        return this.minLength;
    }

    public void setMinLength(int i) {
        this.minLength = i;
        invalidate();
    }

    public OnCheckPasswordListener getOnCheckPasswordListener() {
        return this.onCheckPasswordListener;
    }

    public void setOnCheckPasswordListener(OnCheckPasswordListener onCheckPasswordListener) {
        this.onCheckPasswordListener = onCheckPasswordListener;
    }
}
