Skip to content

Commit

Permalink
Adding separate authenticator that handles authentication process for…
Browse files Browse the repository at this point in the history
… the pin.
  • Loading branch information
kevalpatel2106 committed Apr 14, 2018
1 parent f7fa607 commit ddda493
Show file tree
Hide file tree
Showing 24 changed files with 505 additions and 408 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
/*
* Copyright 2017 Keval Patel.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright (c) 2018. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/

package com.securelockview.sample;
Expand All @@ -21,6 +13,7 @@
import android.support.v7.app.AppCompatActivity;

import com.kevalpatel.passcodeview.PatternView;
import com.kevalpatel.passcodeview.authenticator.PasscodeViewPatternAuthenticator;
import com.kevalpatel.passcodeview.interfaces.AuthenticationListener;
import com.kevalpatel.passcodeview.patternCells.CirclePatternCell;
import com.kevalpatel.passcodeview.patternCells.PatternPoint;
Expand All @@ -47,12 +40,13 @@ protected void onCreate(Bundle savedInstanceState) {
//Set the correct pin code.
//Display row and column number of the pattern point sequence.
//REQUIRED
patternView.setCorrectPattern(new PatternPoint[]{
final PatternPoint[] correctPattern = new PatternPoint[]{
new PatternPoint(0, 0),
new PatternPoint(1, 0),
new PatternPoint(2, 0),
new PatternPoint(2, 1)
});
};
patternView.setAuthenticator(new PasscodeViewPatternAuthenticator(correctPattern));

//Build the desired indicator shape and pass the theme attributes.
//REQUIRED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.support.v7.app.AppCompatActivity;

import com.kevalpatel.passcodeview.PinView;
import com.kevalpatel.passcodeview.authenticator.PasscodeViewPinAuthenticator;
import com.kevalpatel.passcodeview.box.KeyNamesBuilder;
import com.kevalpatel.passcodeview.indicators.CircleIndicator;
import com.kevalpatel.passcodeview.interfaces.AuthenticationListener;
Expand All @@ -37,7 +38,8 @@ protected void onCreate(Bundle savedInstanceState) {

//Set the correct pin code.
//REQUIRED
mPinView.setCorrectPin(new int[]{1, 2, 3, 4});
final int[] correctPattern = new int[]{1, 2, 3, 4};
mPinView.setAuthenticator(new PasscodeViewPinAuthenticator(correctPattern));

//Build the desired key shape and pass the theme parameters.
//REQUIRED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.kevalpatel.passcodeview.authenticator.PatternAuthenticator;
import com.kevalpatel.passcodeview.box.BoxPattern;
import com.kevalpatel.passcodeview.box.BoxTitle;
import com.kevalpatel.passcodeview.internal.BasePasscodeView;
import com.kevalpatel.passcodeview.patternCells.PatternCell;
import com.kevalpatel.passcodeview.patternCells.PatternPoint;

Expand All @@ -37,8 +39,12 @@
*/

public final class PatternView extends BasePasscodeView {
private PatternPoint[] mCorrectPattern; //Current PIN with witch entered PIN will check.
private ArrayList<PatternCell> mPatternTyped; //PIN typed.

/**
* {@link ArrayList} that holds the list of all the {@link PatternCell} touched by the user
* while drawing the pattern.
*/
private ArrayList<PatternCell> mPatternTyped;

private float mPatternPathEndX;
private float mPatternPathEndY;
Expand All @@ -52,11 +58,15 @@ public final class PatternView extends BasePasscodeView {
/**
* {@link BoxPattern} to display the {@link PatternPoint}. User can enter the correct pattern
* in this box.
*
* @see BoxPattern
*/
private BoxPattern mBoxPattern;

/**
* {@link BoxTitle} to display the title message.
*
* @see BoxTitle
*/
private BoxTitle mBoxTitle;

Expand All @@ -79,6 +89,16 @@ public final class PatternView extends BasePasscodeView {
*/
private boolean isErrorShowing = false;

/**
* {@link PatternAuthenticator} to perform authentication on the pattern entered by the user.
* This field is required to set.
*
* @see #setAuthenticator(PatternAuthenticator)
* @see #getAuthenticator()
*/
private PatternAuthenticator mAuthenticator;


///////////////////////////////////////////////////////////////
// CONSTRUCTORS
///////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -124,7 +144,7 @@ public void setDefaults() {
mBoxTitle.setDefaults();
mBoxPattern.setDefaults();

mPatternPathColor = mContext.getResources().getColor(android.R.color.holo_green_dark);
mPatternPathColor = getResources().getColor(android.R.color.holo_green_dark);
}

/**
Expand Down Expand Up @@ -153,12 +173,12 @@ public void preparePaint() {
@SuppressWarnings("deprecation")
@Override
public void parseTypeArr(@NonNull AttributeSet typedArray) {
TypedArray a = mContext.getTheme()
final TypedArray a = getContext().getTheme()
.obtainStyledAttributes(typedArray, R.styleable.PatternView, 0, 0);

try { //Parse title params
mPatternPathColor = a.getColor(R.styleable.PatternView_patternLineColor,
mContext.getResources().getColor(android.R.color.holo_green_dark));
getResources().getColor(android.R.color.holo_green_dark));
} finally {
a.recycle();
}
Expand Down Expand Up @@ -243,10 +263,12 @@ public boolean onTouchEvent(MotionEvent event) {
case MotionEvent.ACTION_DOWN:
reset();
case MotionEvent.ACTION_MOVE:
PatternCell cellNumber = mBoxPattern.findCell(touchX, touchY);

final PatternCell cellNumber = mBoxPattern.findCell(touchX, touchY);

if (cellNumber != null && !mPatternTyped.contains(cellNumber)) {
mPatternTyped.add(cellNumber);
if (isTactileFeedbackEnable()) Utils.giveTactileFeedbackForKeyPress(mContext);
giveTactileFeedbackForKeyPress();
}

mPatternPathEndX = touchX;
Expand All @@ -255,9 +277,24 @@ public boolean onTouchEvent(MotionEvent event) {
invalidate();
break;
case MotionEvent.ACTION_UP:
//Validate the current state
if (mPatternTyped.size() == 0) return true;
if (mAuthenticator == null) {
throw new IllegalStateException("Set authenticator first.");
}

validatePattern();
//Prepare the pattern points
final ArrayList<PatternPoint> patternPoints = new ArrayList<>(mPatternTyped.size());
for (PatternCell cell : mPatternTyped) {
patternPoints.add(cell.getPoint());
}

//Validate using authenticator,
if (mAuthenticator.isValidPattern(patternPoints)) {
onAuthenticationSuccess();
} else {
onAuthenticationFail();
}

//Reset the view.
new android.os.Handler().postDelayed(new Runnable() {
Expand All @@ -273,31 +310,21 @@ public void run() {
return true;
}

private void validatePattern() {
if (mCorrectPattern.length == mPatternTyped.size() && Utils.isPatternMatched(mCorrectPattern, mPatternTyped)) {
onAuthenticationSuccess();
} else {
onAuthenticationFail();
}
invalidate();
}

///////////////////////////////////////////////////////////////
// GETTERS/SETTERS
///////////////////////////////////////////////////////////////

public void setCorrectPattern(@NonNull PatternPoint[] correctPattern) {
mCorrectPattern = correctPattern;

mPatternTyped.clear();
invalidate();
public PatternAuthenticator getAuthenticator() {
return mAuthenticator;
}

@Nullable
public PatternCell.Builder getPatternCellBuilder() {
return mBoxPattern.getCellBuilder();
public void setAuthenticator(final PatternAuthenticator authenticator) {
mAuthenticator = authenticator;
}

//********************** For pattern box

public boolean isOneHandOperationEnabled() {
return mBoxPattern.isOneHandOperation();
}
Expand All @@ -308,22 +335,17 @@ public void enableOneHandOperation(boolean isEnable) {
invalidate();
}

public int getTitleColor() {
return mBoxTitle.getTitleColor();
}

public void setTitleColor(@ColorInt int titleColor) {
mBoxTitle.setTitleColor(titleColor);
public void setPatternCell(@NonNull PatternCell.Builder indicatorBuilder) {
mBoxPattern.setCellBuilder(indicatorBuilder);
requestLayout();
invalidate();
}

@SuppressWarnings("deprecation")
public void setTitleColorRes(@ColorRes int titleColor) {
mBoxTitle.setTitleColor(Utils.getColorCompat(getContext(), titleColor));
invalidate();
@Nullable
public PatternCell.Builder getPatternCellBuilder() {
return mBoxPattern.getCellBuilder();
}


public int getPatternPathColor() {
return mPatternPathColor;
}
Expand All @@ -339,6 +361,43 @@ public void setPatternPathColorRes(@ColorRes int pathColor) {
invalidate();
}

public int getNoOfColumn() {
return mBoxPattern.getNoOfColumn();
}

public void setNoOfColumn(int noOfColumn) {
mBoxPattern.setNoOfColumn(noOfColumn);
requestLayout();
invalidate();
}

public int getNoOfRows() {
return mBoxPattern.getNoOfRows();
}

public void setNoOfRows(int noOfRows) {
mBoxPattern.setNoOfRows(noOfRows);
requestLayout();
invalidate();
}

//********************** For title box

public int getTitleColor() {
return mBoxTitle.getTitleColor();
}

public void setTitleColor(@ColorInt int titleColor) {
mBoxTitle.setTitleColor(titleColor);
invalidate();
}

@SuppressWarnings("deprecation")
public void setTitleColorRes(@ColorRes int titleColor) {
mBoxTitle.setTitleColor(Utils.getColorCompat(getContext(), titleColor));
invalidate();
}

/**
* @return Current title of the view.
*/
Expand All @@ -365,30 +424,4 @@ public void setTitle(@NonNull String title) {
mBoxTitle.setTitle(title);
invalidate();
}

public void setPatternCell(@NonNull PatternCell.Builder indicatorBuilder) {
mBoxPattern.setCellBuilder(indicatorBuilder);
requestLayout();
invalidate();
}

public int getNoOfColumn() {
return mBoxPattern.getNoOfColumn();
}

public void setNoOfColumn(int noOfColumn) {
mBoxPattern.setNoOfColumn(noOfColumn);
requestLayout();
invalidate();
}

public int getNoOfRows() {
return mBoxPattern.getNoOfRows();
}

public void setNoOfRows(int noOfRows) {
mBoxPattern.setNoOfRows(noOfRows);
requestLayout();
invalidate();
}
}
Loading

0 comments on commit ddda493

Please sign in to comment.