Format code of merged password strength widget

This commit is contained in:
Dominik Schürmann 2015-03-05 23:39:41 +01:00
parent a321131ed5
commit 0bb3b9c6cc
2 changed files with 49 additions and 40 deletions

View File

@ -34,7 +34,6 @@ import android.util.AttributeSet;
/** /**
* Created by matt on 04/07/2014. * Created by matt on 04/07/2014.
* https://github.com/matt-allen/android-password-strength-indicator * https://github.com/matt-allen/android-password-strength-indicator
*
*/ */
public class PasswordStrengthBarView extends PasswordStrengthView { public class PasswordStrengthBarView extends PasswordStrengthView {
@ -65,12 +64,12 @@ public class PasswordStrengthBarView extends PasswordStrengthView {
if (mShowGuides) { if (mShowGuides) {
// TODO: Try and do this with a loop, for efficiency // TODO: Try and do this with a loop, for efficiency
// Draw bottom guide border // Draw bottom guide border
float positionY = getHeight()-getPaddingBottom()-getPaddingTop(); float positionY = getHeight() - getPaddingBottom() - getPaddingTop();
float notchHeight = (float)(positionY * 0.8); float notchHeight = (float) (positionY * 0.8);
canvas.drawLine( canvas.drawLine(
getPaddingLeft(), getPaddingLeft(),
positionY, positionY,
getWidth()-getPaddingRight(), getWidth() - getPaddingRight(),
positionY, positionY,
mGuidePaint); mGuidePaint);
// Show left-most notch // Show left-most notch
@ -83,33 +82,33 @@ public class PasswordStrengthBarView extends PasswordStrengthView {
); );
// Show middle-left notch // Show middle-left notch
canvas.drawLine( canvas.drawLine(
(float)(mIndicatorWidth*0.25)+getPaddingLeft(), (float) (mIndicatorWidth * 0.25) + getPaddingLeft(),
positionY, positionY,
(float)(mIndicatorWidth*0.25)+getPaddingLeft(), (float) (mIndicatorWidth * 0.25) + getPaddingLeft(),
notchHeight, notchHeight,
mGuidePaint mGuidePaint
); );
// Show the middle notch // Show the middle notch
canvas.drawLine( canvas.drawLine(
(float)(mIndicatorWidth*0.5)+getPaddingLeft(), (float) (mIndicatorWidth * 0.5) + getPaddingLeft(),
positionY, positionY,
(float)(mIndicatorWidth*0.5)+getPaddingLeft(), (float) (mIndicatorWidth * 0.5) + getPaddingLeft(),
notchHeight, notchHeight,
mGuidePaint mGuidePaint
); );
// Show the middle-right notch // Show the middle-right notch
canvas.drawLine( canvas.drawLine(
(float)(mIndicatorWidth*0.75)+getPaddingLeft(), (float) (mIndicatorWidth * 0.75) + getPaddingLeft(),
positionY, positionY,
(float)(mIndicatorWidth*0.75)+getPaddingLeft(), (float) (mIndicatorWidth * 0.75) + getPaddingLeft(),
notchHeight, notchHeight,
mGuidePaint mGuidePaint
); );
// Show the right-most notch // Show the right-most notch
canvas.drawLine( canvas.drawLine(
mIndicatorWidth+getPaddingLeft(), mIndicatorWidth + getPaddingLeft(),
positionY, positionY,
mIndicatorWidth+getPaddingLeft(), mIndicatorWidth + getPaddingLeft(),
notchHeight, notchHeight,
mGuidePaint mGuidePaint
); );

View File

@ -38,15 +38,15 @@ import org.sufficientlysecure.keychain.R;
* 01/07/14 * 01/07/14
* http://www.mattallensoftware.co.uk * http://www.mattallensoftware.co.uk
* mattallen092@gmail.com * mattallen092@gmail.com
* * <p/>
* https://github.com/matt-allen/android-password-strength-indicator * https://github.com/matt-allen/android-password-strength-indicator
* * <p/>
* <p> * <p>
* This View is designed to indicate how secure a user-entered password is in a visual way to * This View is designed to indicate how secure a user-entered password is in a visual way to
* relay to the user if they need to make it stronger. The strength of the password can be set * relay to the user if they need to make it stronger. The strength of the password can be set
* at creation (or after) which will decide whether their password is strong enough. * at creation (or after) which will decide whether their password is strong enough.
* </p> * </p>
* * <p/>
* <p> * <p>
* The password strength is decided by an index of 20. The minimum score needed to pass is 10 * The password strength is decided by an index of 20. The minimum score needed to pass is 10
* which means the String has met the conditions imposed by the strength test, but can be improved. * which means the String has met the conditions imposed by the strength test, but can be improved.
@ -113,7 +113,7 @@ public class PasswordStrengthView extends View {
mColorWeak = style.getColor(R.styleable.PasswordStrengthView_color_weak, COLOR_WEAK); mColorWeak = style.getColor(R.styleable.PasswordStrengthView_color_weak, COLOR_WEAK);
mColorStrong = style.getColor(R.styleable.PasswordStrengthView_color_strong, mColorStrong = style.getColor(R.styleable.PasswordStrengthView_color_strong,
COLOR_STRONG); COLOR_STRONG);
} catch (Exception e){ } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
// Create and style the paint used for drawing the guide on the indicator // Create and style the paint used for drawing the guide on the indicator
@ -128,6 +128,7 @@ public class PasswordStrengthView extends View {
/** /**
* This view can determine if the password entered by the user is acceptable for * This view can determine if the password entered by the user is acceptable for
* use by your use case. This is based on the strength requirement you have set. * use by your use case. This is based on the strength requirement you have set.
*
* @return True if requirement has been met * @return True if requirement has been met
*/ */
public boolean isStrengthRequirementMet() { public boolean isStrengthRequirementMet() {
@ -137,10 +138,11 @@ public class PasswordStrengthView extends View {
/** /**
* Change the strength requirement of the password entered by the user. This will also * Change the strength requirement of the password entered by the user. This will also
* re-check the password already entered against these new requirements. * re-check the password already entered against these new requirements.
*
* @param requiredStrength Use the public constants of this class to set * @param requiredStrength Use the public constants of this class to set
*/ */
public void setStrengthRequirement(int requiredStrength) { public void setStrengthRequirement(int requiredStrength) {
if(requiredStrength >= 0 && requiredStrength <= 2){ if (requiredStrength >= 0 && requiredStrength <= 2) {
mStrengthRequirement = requiredStrength; mStrengthRequirement = requiredStrength;
if (mPassword != null && mPassword.length() > 0) { if (mPassword != null && mPassword.length() > 0) {
generatePasswordScore(); generatePasswordScore();
@ -155,10 +157,11 @@ public class PasswordStrengthView extends View {
/** /**
* Update the password string to check strength of * Update the password string to check strength of
*
* @param passwordString String representation of user-input * @param passwordString String representation of user-input
*/ */
public void setPassword(String passwordString) { public void setPassword(String passwordString) {
if(passwordString != null && passwordString.length() > 0) { if (passwordString != null && passwordString.length() > 0) {
mPassword = passwordString; mPassword = passwordString;
generatePasswordScore(); generatePasswordScore();
} else { } else {
@ -173,13 +176,14 @@ public class PasswordStrengthView extends View {
/** /**
* Private convenience method for adding to the password score * Private convenience method for adding to the password score
*
* @param score Amount to be added to current score * @param score Amount to be added to current score
*/ */
protected void addToPasswordScore(int score) { protected void addToPasswordScore(int score) {
int newScore = mCurrentScore + score; int newScore = mCurrentScore + score;
// Limit max score // Limit max score
if (newScore > 20){ if (newScore > 20) {
mCurrentScore = 20; mCurrentScore = 20;
} else { } else {
mCurrentScore = newScore; mCurrentScore = newScore;
@ -188,6 +192,7 @@ public class PasswordStrengthView extends View {
/** /**
* Call this to determine the current strength requirement set on the algorithm * Call this to determine the current strength requirement set on the algorithm
*
* @return Int representation of the current strength set for the indicator * @return Int representation of the current strength set for the indicator
*/ */
public int getStrengthRequirement() { public int getStrengthRequirement() {
@ -203,23 +208,23 @@ public class PasswordStrengthView extends View {
int upperCase = getUppercaseCount(mPassword); int upperCase = getUppercaseCount(mPassword);
int nonAlpha = getNonAlphanumericCount(mPassword); int nonAlpha = getNonAlphanumericCount(mPassword);
int numbers = getNumberCount(mPassword); int numbers = getNumberCount(mPassword);
switch (mStrengthRequirement){ switch (mStrengthRequirement) {
case STRENGTH_WEAK: case STRENGTH_WEAK:
addToPasswordScore(mPassword.length()*2); addToPasswordScore(mPassword.length() * 2);
addToPasswordScore(upperCase*2); addToPasswordScore(upperCase * 2);
addToPasswordScore(nonAlpha*2); addToPasswordScore(nonAlpha * 2);
addToPasswordScore(numbers*2); addToPasswordScore(numbers * 2);
break; break;
case STRENGTH_MEDIUM: case STRENGTH_MEDIUM:
addToPasswordScore(mPassword.length()); addToPasswordScore(mPassword.length());
addToPasswordScore(upperCase); addToPasswordScore(upperCase);
addToPasswordScore(nonAlpha*2); addToPasswordScore(nonAlpha * 2);
addToPasswordScore(numbers); addToPasswordScore(numbers);
break; break;
case STRENGTH_STRONG: case STRENGTH_STRONG:
addToPasswordScore(mPassword.length()/2); addToPasswordScore(mPassword.length() / 2);
// Cut the score in half to make this a very high requirement // Cut the score in half to make this a very high requirement
addToPasswordScore(upperCase); addToPasswordScore(upperCase);
addToPasswordScore(nonAlpha); addToPasswordScore(nonAlpha);
@ -241,7 +246,8 @@ public class PasswordStrengthView extends View {
* The standard parts of the onMeasure needed to create the password strength * The standard parts of the onMeasure needed to create the password strength
* indicator. Subclasses should call super.onMeasure, but also need to set * indicator. Subclasses should call super.onMeasure, but also need to set
* the minimum height and width in the constructor. * the minimum height and width in the constructor.
* @param widthMeasureSpec The measurement given by the system *
* @param widthMeasureSpec The measurement given by the system
* @param heightMeasureSpec The measurement given by the system * @param heightMeasureSpec The measurement given by the system
*/ */
@Override @Override
@ -264,8 +270,7 @@ public class PasswordStrengthView extends View {
int color = mColorFail; int color = mColorFail;
if (mCurrentScore >= 18) { if (mCurrentScore >= 18) {
color = mColorStrong; color = mColorStrong;
} } else if (mCurrentScore >= 10) {
else if (mCurrentScore >= 10) {
color = mColorWeak; color = mColorWeak;
} }
mIndicatorPaint.setColor(color); mIndicatorPaint.setColor(color);
@ -273,14 +278,15 @@ public class PasswordStrengthView extends View {
/** /**
* Quick method to determine how many of the characters in a given string are upper case * Quick method to determine how many of the characters in a given string are upper case
*
* @param stringToCheck The string to examine * @param stringToCheck The string to examine
* @return Number of upper case characters * @return Number of upper case characters
*/ */
protected int getUppercaseCount(String stringToCheck) { protected int getUppercaseCount(String stringToCheck) {
int score = 0; int score = 0;
int loops = stringToCheck.length()-1; int loops = stringToCheck.length() - 1;
for (int i=0;i<=loops;i++){ for (int i = 0; i <= loops; i++) {
if(Character.isUpperCase(stringToCheck.charAt(i))) { if (Character.isUpperCase(stringToCheck.charAt(i))) {
score++; score++;
} }
} }
@ -290,15 +296,16 @@ public class PasswordStrengthView extends View {
/** /**
* A convenience method to determine how many characters in the given String aren't * A convenience method to determine how many characters in the given String aren't
* letters or numbers. * letters or numbers.
*
* @param stringToCheck * @param stringToCheck
* @return Number of characters that aren't numbers or letters * @return Number of characters that aren't numbers or letters
*/ */
protected int getNonAlphanumericCount(String stringToCheck) { protected int getNonAlphanumericCount(String stringToCheck) {
int score = 0; int score = 0;
int loops = stringToCheck.length()-1; int loops = stringToCheck.length() - 1;
for (int i=0;i<=loops;i++) { for (int i = 0; i <= loops; i++) {
if(!Character.isLetter(stringToCheck.charAt(i)) && if (!Character.isLetter(stringToCheck.charAt(i)) &&
!Character.isDigit(stringToCheck.charAt(i))){ !Character.isDigit(stringToCheck.charAt(i))) {
score++; score++;
} }
} }
@ -307,14 +314,15 @@ public class PasswordStrengthView extends View {
/** /**
* A convenience method for returning the count of numbers in a given String. * A convenience method for returning the count of numbers in a given String.
*
* @param stringToCheck * @param stringToCheck
* @return The numbers of digits in the String * @return The numbers of digits in the String
*/ */
protected int getNumberCount(String stringToCheck) { protected int getNumberCount(String stringToCheck) {
int score = 0; int score = 0;
int loops = stringToCheck.length()-1; int loops = stringToCheck.length() - 1;
for (int i=0;i<=loops;i++) { for (int i = 0; i <= loops; i++) {
if(Character.isDigit(stringToCheck.charAt(i))) { if (Character.isDigit(stringToCheck.charAt(i))) {
score++; score++;
} }
} }
@ -326,6 +334,7 @@ public class PasswordStrengthView extends View {
* On the line style, the guides will show underneath<br /> * On the line style, the guides will show underneath<br />
* On the rounded style, the guides will be shown on the outer edges.<br /> * On the rounded style, the guides will be shown on the outer edges.<br />
* The view will be redrawn after the method is called. * The view will be redrawn after the method is called.
*
* @param showGuides True if you want the guides to be shown * @param showGuides True if you want the guides to be shown
*/ */
public void setShowGuides(boolean showGuides) { public void setShowGuides(boolean showGuides) {
@ -342,6 +351,7 @@ public class PasswordStrengthView extends View {
/** /**
* Determine whether the view is showing the guides for the password score * Determine whether the view is showing the guides for the password score
*
* @return True if the guides are being shown * @return True if the guides are being shown
*/ */
public boolean isShowingGuides() { public boolean isShowingGuides() {