package com.andrognito.pinlockview;

import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
/* loaded from: classes.dex */
public class PinLockAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int VIEW_TYPE_DELETE = 1;
    private static final int VIEW_TYPE_NUMBER = 0;
    private Context mContext;
    private CustomizationOptionsBundle mCustomizationOptionsBundle;
    private int[] mKeyValues = getAdjustKeyValues(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0});
    private OnDeleteClickListener mOnDeleteClickListener;
    private OnNumberClickListener mOnNumberClickListener;
    private int mPinLength;

    /* loaded from: classes.dex */
    public interface OnDeleteClickListener {
        void onDeleteClicked();

        void onDeleteLongClicked();
    }

    /* loaded from: classes.dex */
    public interface OnNumberClickListener {
        void onNumberClicked(int i);
    }

    public PinLockAdapter(Context context) {
        this.mContext = context;
    }

    @Override // android.support.v7.widget.RecyclerView.Adapter
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        if (viewType == 0) {
            View view = inflater.inflate(R.layout.layout_number_item, parent, false);
            RecyclerView.ViewHolder viewHolder = new NumberViewHolder(view);
            return viewHolder;
        }
        View view2 = inflater.inflate(R.layout.layout_delete_item, parent, false);
        RecyclerView.ViewHolder viewHolder2 = new DeleteViewHolder(view2);
        return viewHolder2;
    }

    @Override // android.support.v7.widget.RecyclerView.Adapter
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder.getItemViewType() == 0) {
            NumberViewHolder vh1 = (NumberViewHolder) holder;
            configureNumberButtonHolder(vh1, position);
        } else if (holder.getItemViewType() == 1) {
            DeleteViewHolder vh2 = (DeleteViewHolder) holder;
            configureDeleteButtonHolder(vh2);
        }
    }

    private void configureNumberButtonHolder(NumberViewHolder holder, int position) {
        if (holder != null) {
            if (position == 9) {
                holder.mNumberButton.setVisibility(8);
            } else {
                holder.mNumberButton.setText(String.valueOf(this.mKeyValues[position]));
                holder.mNumberButton.setVisibility(0);
                holder.mNumberButton.setTag(Integer.valueOf(this.mKeyValues[position]));
            }
            if (this.mCustomizationOptionsBundle != null) {
                holder.mNumberButton.setTextColor(this.mCustomizationOptionsBundle.getTextColor());
                if (this.mCustomizationOptionsBundle.getButtonBackgroundDrawable() != null) {
                    if (Build.VERSION.SDK_INT < 16) {
                        holder.mNumberButton.setBackgroundDrawable(this.mCustomizationOptionsBundle.getButtonBackgroundDrawable());
                    } else {
                        holder.mNumberButton.setBackground(this.mCustomizationOptionsBundle.getButtonBackgroundDrawable());
                    }
                }
                holder.mNumberButton.setTextSize(0, this.mCustomizationOptionsBundle.getTextSize());
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(this.mCustomizationOptionsBundle.getButtonSize(), this.mCustomizationOptionsBundle.getButtonSize());
                holder.mNumberButton.setLayoutParams(params);
            }
        }
    }

    private void configureDeleteButtonHolder(DeleteViewHolder holder) {
        if (holder != null) {
            if (this.mCustomizationOptionsBundle.isShowDeleteButton() && this.mPinLength > 0) {
                holder.mButtonImage.setVisibility(0);
                if (this.mCustomizationOptionsBundle.getDeleteButtonDrawable() != null) {
                    holder.mButtonImage.setImageDrawable(this.mCustomizationOptionsBundle.getDeleteButtonDrawable());
                }
                holder.mButtonImage.setColorFilter(this.mCustomizationOptionsBundle.getTextColor(), PorterDuff.Mode.SRC_ATOP);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(this.mCustomizationOptionsBundle.getDeleteButtonSize(), this.mCustomizationOptionsBundle.getDeleteButtonSize());
                holder.mButtonImage.setLayoutParams(params);
                return;
            }
            holder.mButtonImage.setVisibility(8);
        }
    }

    @Override // android.support.v7.widget.RecyclerView.Adapter
    public int getItemCount() {
        return 12;
    }

    @Override // android.support.v7.widget.RecyclerView.Adapter
    public int getItemViewType(int position) {
        return position == getItemCount() - 1 ? 1 : 0;
    }

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

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

    public int[] getKeyValues() {
        return this.mKeyValues;
    }

    public void setKeyValues(int[] keyValues) {
        this.mKeyValues = getAdjustKeyValues(keyValues);
        notifyDataSetChanged();
    }

    private int[] getAdjustKeyValues(int[] keyValues) {
        int[] adjustedKeyValues = new int[keyValues.length + 1];
        for (int i = 0; i < keyValues.length; i++) {
            if (i < 9) {
                adjustedKeyValues[i] = keyValues[i];
            } else {
                adjustedKeyValues[i] = -1;
                adjustedKeyValues[i + 1] = keyValues[i];
            }
        }
        return adjustedKeyValues;
    }

    public OnNumberClickListener getOnItemClickListener() {
        return this.mOnNumberClickListener;
    }

    public void setOnItemClickListener(OnNumberClickListener onNumberClickListener) {
        this.mOnNumberClickListener = onNumberClickListener;
    }

    public OnDeleteClickListener getOnDeleteClickListener() {
        return this.mOnDeleteClickListener;
    }

    public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        this.mOnDeleteClickListener = onDeleteClickListener;
    }

    public CustomizationOptionsBundle getCustomizationOptions() {
        return this.mCustomizationOptionsBundle;
    }

    public void setCustomizationOptions(CustomizationOptionsBundle customizationOptionsBundle) {
        this.mCustomizationOptionsBundle = customizationOptionsBundle;
    }

    /* loaded from: classes.dex */
    public class NumberViewHolder extends RecyclerView.ViewHolder {
        Button mNumberButton;

        public NumberViewHolder(View itemView) {
            super(itemView);
            Button button = (Button) itemView.findViewById(R.id.button);
            this.mNumberButton = button;
            button.setOnClickListener(new View.OnClickListener() { // from class: com.andrognito.pinlockview.PinLockAdapter.NumberViewHolder.1
                @Override // android.view.View.OnClickListener
                public void onClick(View v) {
                    if (PinLockAdapter.this.mOnNumberClickListener != null) {
                        PinLockAdapter.this.mOnNumberClickListener.onNumberClicked(((Integer) v.getTag()).intValue());
                    }
                }
            });
        }
    }

    /* loaded from: classes.dex */
    public class DeleteViewHolder extends RecyclerView.ViewHolder {
        ImageView mButtonImage;
        LinearLayout mDeleteButton;

        public DeleteViewHolder(View itemView) {
            super(itemView);
            this.mDeleteButton = (LinearLayout) itemView.findViewById(R.id.button);
            this.mButtonImage = (ImageView) itemView.findViewById(R.id.buttonImage);
            if (PinLockAdapter.this.mCustomizationOptionsBundle.isShowDeleteButton() && PinLockAdapter.this.mPinLength > 0) {
                this.mDeleteButton.setOnClickListener(new View.OnClickListener() { // from class: com.andrognito.pinlockview.PinLockAdapter.DeleteViewHolder.1
                    @Override // android.view.View.OnClickListener
                    public void onClick(View v) {
                        if (PinLockAdapter.this.mOnDeleteClickListener != null) {
                            PinLockAdapter.this.mOnDeleteClickListener.onDeleteClicked();
                        }
                    }
                });
                this.mDeleteButton.setOnLongClickListener(new View.OnLongClickListener() { // from class: com.andrognito.pinlockview.PinLockAdapter.DeleteViewHolder.2
                    @Override // android.view.View.OnLongClickListener
                    public boolean onLongClick(View v) {
                        if (PinLockAdapter.this.mOnDeleteClickListener != null) {
                            PinLockAdapter.this.mOnDeleteClickListener.onDeleteLongClicked();
                            return true;
                        }
                        return true;
                    }
                });
                this.mDeleteButton.setOnTouchListener(new View.OnTouchListener() { // from class: com.andrognito.pinlockview.PinLockAdapter.DeleteViewHolder.3
                    private Rect rect;

                    @Override // android.view.View.OnTouchListener
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == 0) {
                            DeleteViewHolder.this.mButtonImage.setColorFilter(PinLockAdapter.this.mCustomizationOptionsBundle.getDeleteButtonPressesColor());
                            this.rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
                        }
                        if (event.getAction() == 1) {
                            DeleteViewHolder.this.mButtonImage.clearColorFilter();
                        }
                        if (event.getAction() == 2 && !this.rect.contains(v.getLeft() + ((int) event.getX()), v.getTop() + ((int) event.getY()))) {
                            DeleteViewHolder.this.mButtonImage.clearColorFilter();
                            return false;
                        }
                        return false;
                    }
                });
            }
        }
    }
}
