Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added enter button and attribute to swap enter and delete buttons #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/src/main/res/layout/activity_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
app:keypadButtonSize="72dp"
app:keypadShowDeleteButton="true"
app:keypadTextColor="@color/white"
app:keypadTextSize="18dp" />
app:keypadTextSize="18dp"
app:keypadShowEnterButton="true"
app:keypadSwapEnterDeleteButtons="true"/>

</RelativeLayout>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Mar 10 00:08:20 IST 2017
#Tue Dec 12 09:32:28 EET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* The customization options for the buttons in {@link PinLockView}
* passed to the {@link PinLockAdapter} to decorate the individual views
*
* <p>
* Created by aritraroy on 01/06/16.
*/
public class CustomizationOptionsBundle {
Expand All @@ -19,6 +19,10 @@ public class CustomizationOptionsBundle {
private boolean showDeleteButton;
private int deleteButtonPressesColor;

private boolean showEnterButton;
private int pinLength;
private boolean swapEnterDeleteButtons;

public CustomizationOptionsBundle() {
}

Expand Down Expand Up @@ -85,4 +89,28 @@ public int getDeleteButtonPressesColor() {
public void setDeleteButtonPressesColor(int deleteButtonPressesColor) {
this.deleteButtonPressesColor = deleteButtonPressesColor;
}

public boolean isShowEnterButton() {
return showEnterButton;
}

public void setShowEnterButton(boolean showEnterButton) {
this.showEnterButton = showEnterButton;
}

public int getPinLength() {
return pinLength;
}

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

public boolean isSwapEnterDeleteButtons() {
return swapEnterDeleteButtons;
}

public void setSwapEnterDeleteButtons(boolean swapEnterDeleteButtons) {
this.swapEnterDeleteButtons = swapEnterDeleteButtons;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ public class PinLockAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder

private static final int VIEW_TYPE_NUMBER = 0;
private static final int VIEW_TYPE_DELETE = 1;
private static final int VIEW_TYPE_ENTER = 2;

private Context mContext;
private CustomizationOptionsBundle mCustomizationOptionsBundle;
private OnNumberClickListener mOnNumberClickListener;
private OnDeleteClickListener mOnDeleteClickListener;

private OnEnterClickListener mOnEnterClickListener;

private int enterButtonPosition = 9;
private int deleteButtonPosition = 11;

private int mPinLength;

private int[] mKeyValues;
Expand All @@ -43,9 +50,12 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType
if (viewType == VIEW_TYPE_NUMBER) {
View view = inflater.inflate(R.layout.layout_number_item, parent, false);
viewHolder = new NumberViewHolder(view);
} else {
} else if (viewType == VIEW_TYPE_DELETE) {
View view = inflater.inflate(R.layout.layout_delete_item, parent, false);
viewHolder = new DeleteViewHolder(view);
} else {
View view = inflater.inflate(R.layout.layout_enter_item, parent, false);
viewHolder = new EnterViewHolder(view);
}
return viewHolder;
}
Expand All @@ -58,6 +68,9 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
} else if (holder.getItemViewType() == VIEW_TYPE_DELETE) {
DeleteViewHolder vh2 = (DeleteViewHolder) holder;
configureDeleteButtonHolder(vh2);
} else if (holder.getItemViewType() == VIEW_TYPE_ENTER) {
EnterViewHolder vh3 = (EnterViewHolder) holder;
configureEnterButtonHolder(vh3);
}
}

Expand Down Expand Up @@ -108,6 +121,39 @@ private void configureDeleteButtonHolder(DeleteViewHolder holder) {
} else {
holder.mButtonImage.setVisibility(View.GONE);
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
mCustomizationOptionsBundle.getButtonSize(),
mCustomizationOptionsBundle.getButtonSize());
holder.mDeleteButton.setLayoutParams(params);
}
}

private void configureEnterButtonHolder(EnterViewHolder holder) {
if (holder != null) {
if (mCustomizationOptionsBundle.isShowEnterButton() && mPinLength >= mCustomizationOptionsBundle.getPinLength()) {
holder.mEnterButton.setVisibility(View.VISIBLE);

if (mCustomizationOptionsBundle != null) {
holder.mEnterButton.setTextColor(mCustomizationOptionsBundle.getTextColor());
if (mCustomizationOptionsBundle.getButtonBackgroundDrawable() != null) {
if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
holder.mEnterButton.setBackgroundDrawable(
mCustomizationOptionsBundle.getButtonBackgroundDrawable());
} else {
holder.mEnterButton.setBackground(
mCustomizationOptionsBundle.getButtonBackgroundDrawable());
}
}
holder.mEnterButton.setTextSize(TypedValue.COMPLEX_UNIT_PX,
mCustomizationOptionsBundle.getTextSize());
}
} else {
holder.mEnterButton.setVisibility(View.GONE);
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
mCustomizationOptionsBundle.getButtonSize(),
mCustomizationOptionsBundle.getButtonSize());
holder.mEnterButton.setLayoutParams(params);
}
}

Expand All @@ -118,7 +164,10 @@ public int getItemCount() {

@Override
public int getItemViewType(int position) {
if (position == getItemCount() - 1) {
if (position == getEnterButtonPosition()) {
return VIEW_TYPE_ENTER;
}
if (position == getDeleteButtonPosition()) {
return VIEW_TYPE_DELETE;
}
return VIEW_TYPE_NUMBER;
Expand Down Expand Up @@ -170,12 +219,34 @@ public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener
this.mOnDeleteClickListener = onDeleteClickListener;
}

public void setOnEnterClickListener(OnEnterClickListener onEnterClickListener) {
this.mOnEnterClickListener = onEnterClickListener;
}

public CustomizationOptionsBundle getCustomizationOptions() {
return mCustomizationOptionsBundle;
}

public void setCustomizationOptions(CustomizationOptionsBundle customizationOptionsBundle) {
this.mCustomizationOptionsBundle = customizationOptionsBundle;
setEnterButtonPosition(mCustomizationOptionsBundle.isSwapEnterDeleteButtons() ? 11 : 9);
setDeleteButtonPosition(mCustomizationOptionsBundle.isSwapEnterDeleteButtons() ? 9 : 11);
}

public int getEnterButtonPosition() {
return enterButtonPosition;
}

public void setEnterButtonPosition(int enterButtonPosition) {
this.enterButtonPosition = enterButtonPosition;
}

public int getDeleteButtonPosition() {
return deleteButtonPosition;
}

public void setDeleteButtonPosition(int deleteButtonPosition) {
this.deleteButtonPosition = deleteButtonPosition;
}

public class NumberViewHolder extends RecyclerView.ViewHolder {
Expand Down Expand Up @@ -250,6 +321,23 @@ public boolean onTouch(View v, MotionEvent event) {
}
}

public class EnterViewHolder extends RecyclerView.ViewHolder {
Button mEnterButton;

public EnterViewHolder(final View itemView) {
super(itemView);
mEnterButton = (Button) itemView.findViewById(R.id.button);
mEnterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnEnterClickListener != null) {
mOnEnterClickListener.onEnterClicked();
}
}
});
}
}

public interface OnNumberClickListener {
void onNumberClicked(int keyValue);
}
Expand All @@ -259,4 +347,8 @@ public interface OnDeleteClickListener {

void onDeleteLongClicked();
}

public interface OnEnterClickListener {
void onEnterClicked();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class PinLockView extends RecyclerView {
private Drawable mDeleteButtonDrawable;
private boolean mShowDeleteButton;

private boolean mShowEnterButton;
private boolean mSwapEnterDeleteButtons;

private IndicatorDots mIndicatorDots;
private PinLockAdapter mAdapter;
private PinLockListener mPinLockListener;
Expand All @@ -39,7 +42,7 @@ public class PinLockView extends RecyclerView {
= new PinLockAdapter.OnNumberClickListener() {
@Override
public void onNumberClicked(int keyValue) {
if (mPin.length() < getPinLength()) {
if (mPin.length() < getPinLength() || mShowEnterButton) {
mPin = mPin.concat(String.valueOf(keyValue));

if (isIndicatorDotsAttached()) {
Expand All @@ -48,11 +51,16 @@ public void onNumberClicked(int keyValue) {

if (mPin.length() == 1) {
mAdapter.setPinLength(mPin.length());
mAdapter.notifyItemChanged(mAdapter.getItemCount() - 1);
mAdapter.notifyItemChanged(mAdapter.getDeleteButtonPosition());
}

if (mPin.length() == mPinLength) {
mAdapter.setPinLength(mPin.length());
mAdapter.notifyItemChanged(mAdapter.getEnterButtonPosition());
}

if (mPinLockListener != null) {
if (mPin.length() == mPinLength) {
if (mPin.length() == mPinLength && !mShowEnterButton) {
mPinLockListener.onComplete(mPin);
} else {
mPinLockListener.onPinChange(mPin.length(), mPin);
Expand All @@ -73,7 +81,9 @@ public void onNumberClicked(int keyValue) {

} else {
if (mPinLockListener != null) {
mPinLockListener.onComplete(mPin);
if (!mShowEnterButton) {
mPinLockListener.onComplete(mPin);
}
}
}
}
Expand All @@ -93,7 +103,12 @@ public void onDeleteClicked() {

if (mPin.length() == 0) {
mAdapter.setPinLength(mPin.length());
mAdapter.notifyItemChanged(mAdapter.getItemCount() - 1);
mAdapter.notifyItemChanged(mAdapter.getDeleteButtonPosition());
}

if (mPin.length() == mPinLength - 1) {
mAdapter.setPinLength(mPin.length());
mAdapter.notifyItemChanged(mAdapter.getEnterButtonPosition());
}

if (mPinLockListener != null) {
Expand All @@ -120,6 +135,16 @@ public void onDeleteLongClicked() {
}
};

private PinLockAdapter.OnEnterClickListener mOnEnterClickListener
= new PinLockAdapter.OnEnterClickListener() {
@Override
public void onEnterClicked() {
if (mPin.length() >= mPinLength) {
mPinLockListener.onComplete(mPin);
}
}
};

public PinLockView(Context context) {
super(context);
init(null, 0);
Expand Down Expand Up @@ -151,6 +176,12 @@ private void init(AttributeSet attributeSet, int defStyle) {
mDeleteButtonDrawable = typedArray.getDrawable(R.styleable.PinLockView_keypadDeleteButtonDrawable);
mShowDeleteButton = typedArray.getBoolean(R.styleable.PinLockView_keypadShowDeleteButton, true);
mDeleteButtonPressedColor = typedArray.getColor(R.styleable.PinLockView_keypadDeleteButtonPressedColor, ResourceUtils.getColor(getContext(), R.color.greyish));

mShowEnterButton = typedArray.getBoolean(R.styleable.PinLockView_keypadShowEnterButton, false);
if (mShowEnterButton) {
mShowDeleteButton = true;
}
mSwapEnterDeleteButtons = typedArray.getBoolean(R.styleable.PinLockView_keypadSwapEnterDeleteButtons, false);
} finally {
typedArray.recycle();
}
Expand All @@ -165,6 +196,9 @@ private void init(AttributeSet attributeSet, int defStyle) {
mCustomizationOptionsBundle.setShowDeleteButton(mShowDeleteButton);
mCustomizationOptionsBundle.setDeleteButtonPressesColor(mDeleteButtonPressedColor);

mCustomizationOptionsBundle.setShowEnterButton(mShowEnterButton);
mCustomizationOptionsBundle.setPinLength(mPinLength);
mCustomizationOptionsBundle.setSwapEnterDeleteButtons(mSwapEnterDeleteButtons);
initView();
}

Expand All @@ -174,6 +208,9 @@ private void initView() {
mAdapter = new PinLockAdapter(getContext());
mAdapter.setOnItemClickListener(mOnNumberClickListener);
mAdapter.setOnDeleteClickListener(mOnDeleteClickListener);

mAdapter.setOnEnterClickListener(mOnEnterClickListener);

mAdapter.setCustomizationOptions(mCustomizationOptionsBundle);
setAdapter(mAdapter);

Expand Down Expand Up @@ -206,6 +243,7 @@ public int getPinLength() {
*/
public void setPinLength(int pinLength) {
this.mPinLength = pinLength;
mCustomizationOptionsBundle.setPinLength(mPinLength);

if (isIndicatorDotsAttached()) {
mIndicatorDots.setPinLength(pinLength);
Expand Down Expand Up @@ -405,7 +443,8 @@ public void resetPinLockView() {
clearInternalPin();

mAdapter.setPinLength(mPin.length());
mAdapter.notifyItemChanged(mAdapter.getItemCount() - 1);
mAdapter.notifyItemChanged(mAdapter.getEnterButtonPosition());
mAdapter.notifyItemChanged(mAdapter.getDeleteButtonPosition());

if (mIndicatorDots != null) {
mIndicatorDots.updateDot(mPin.length());
Expand Down
16 changes: 16 additions & 0 deletions pinlockview/src/main/res/layout/layout_enter_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical">

<Button
android:id="@+id/button"
android:layout_width="64dp"
android:layout_height="64dp"
android:background="@android:color/transparent"
android:text="OK" />

</LinearLayout>
3 changes: 3 additions & 0 deletions pinlockview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<attr name="keypadShowDeleteButton" format="boolean" />
<attr name="keypadDeleteButtonPressedColor" format="color" />

<attr name="keypadShowEnterButton" format="boolean" />
<attr name="keypadSwapEnterDeleteButtons" format="boolean" />

<attr name="dotEmptyBackground" format="reference" />
<attr name="dotFilledBackground" format="reference" />
<attr name="dotDiameter" format="dimension" />
Expand Down