Merge pull request #1111 from adithyaphilip/passphrase-strength-indicator

Passphrase strength indicator
This commit is contained in:
Dominik Schürmann 2015-03-09 12:51:00 +01:00
commit 7ac4f63ea3
6 changed files with 143 additions and 93 deletions

View File

@ -31,23 +31,19 @@ import android.widget.EditText;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.ui.CreateKeyActivity.FragAction;
import org.sufficientlysecure.keychain.ui.widget.EmailEditText;
import org.sufficientlysecure.keychain.ui.widget.PasswordEditText;
import org.sufficientlysecure.keychain.ui.widget.passwordstrengthindicator.PasswordStrengthView;
import org.sufficientlysecure.keychain.ui.widget.PassphraseEditText;
import org.sufficientlysecure.keychain.util.ContactHelper;
public class CreateKeyInputFragment extends Fragment {
CreateKeyActivity mCreateKeyActivity;
PasswordStrengthView mPassphraseStrengthView;
AutoCompleteTextView mNameEdit;
EmailEditText mEmailEdit;
PasswordEditText mPassphraseEdit;
EditText mPassphraseEditAgain;
View mCreateButton;
public static final String ARG_NAME = "name";
public static final String ARG_EMAIL = "email";
CreateKeyActivity mCreateKeyActivity;
AutoCompleteTextView mNameEdit;
EmailEditText mEmailEdit;
PassphraseEditText mPassphraseEdit;
EditText mPassphraseEditAgain;
View mCreateButton;
/**
* Creates new instance of this fragment
@ -64,15 +60,47 @@ public class CreateKeyInputFragment extends Fragment {
return frag;
}
/**
* Checks if text of given EditText is not empty. If it is empty an error is
* set and the EditText gets the focus.
*
* @param context
* @param editText
* @return true if EditText is not empty
*/
private static boolean isEditTextNotEmpty(Context context, EditText editText) {
boolean output = true;
if (editText.getText().toString().length() == 0) {
editText.setError(context.getString(R.string.create_key_empty));
editText.requestFocus();
output = false;
} else {
editText.setError(null);
}
return output;
}
private static boolean areEditTextsEqual(Context context, EditText editText1, EditText editText2) {
boolean output = true;
if (!editText1.getText().toString().equals(editText2.getText().toString())) {
editText2.setError(context.getString(R.string.create_key_passphrases_not_equal));
editText2.requestFocus();
output = false;
} else {
editText2.setError(null);
}
return output;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.create_key_input_fragment, container, false);
mPassphraseStrengthView = (PasswordStrengthView) view.findViewById(R.id
.create_key_passphrase_strength);
mNameEdit = (AutoCompleteTextView) view.findViewById(R.id.create_key_name);
mPassphraseEdit = (PassphraseEditText) view.findViewById(R.id.create_key_passphrase);
mEmailEdit = (EmailEditText) view.findViewById(R.id.create_key_email);
mPassphraseEdit = (PasswordEditText) view.findViewById(R.id.create_key_passphrase);
mPassphraseEditAgain = (EditText) view.findViewById(R.id.create_key_passphrase_again);
mCreateButton = view.findViewById(R.id.create_key_button);
@ -106,15 +134,6 @@ public class CreateKeyInputFragment extends Fragment {
)
);
// Edit text padding doesn't work via xml (http://code.google.com/p/android/issues/detail?id=77982)
// so we set the right padding programmatically.
mPassphraseEdit.setPadding(mPassphraseEdit.getPaddingLeft(),
mPassphraseEdit.getPaddingTop(),
(int) (56 * getResources().getDisplayMetrics().density),
mPassphraseEdit.getPaddingBottom());
mPassphraseEdit.setPasswordStrengthView(mPassphraseStrengthView);
mCreateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -165,38 +184,4 @@ public class CreateKeyInputFragment extends Fragment {
inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
/**
* Checks if text of given EditText is not empty. If it is empty an error is
* set and the EditText gets the focus.
*
* @param context
* @param editText
* @return true if EditText is not empty
*/
private static boolean isEditTextNotEmpty(Context context, EditText editText) {
boolean output = true;
if (editText.getText().toString().length() == 0) {
editText.setError(context.getString(R.string.create_key_empty));
editText.requestFocus();
output = false;
} else {
editText.setError(null);
}
return output;
}
private static boolean areEditTextsEqual(Context context, EditText editText1, EditText editText2) {
boolean output = true;
if (!editText1.getText().toString().equals(editText2.getText().toString())) {
editText2.setError(context.getString(R.string.create_key_passphrases_not_equal));
editText2.requestFocus();
output = false;
} else {
editText2.setError(null);
}
return output;
}
}

View File

@ -43,8 +43,7 @@ import android.widget.Toast;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.ui.widget.PasswordEditText;
import org.sufficientlysecure.keychain.ui.widget.passwordstrengthindicator.PasswordStrengthView;
import org.sufficientlysecure.keychain.ui.widget.PassphraseEditText;
import org.sufficientlysecure.keychain.util.Log;
public class SetPassphraseDialogFragment extends DialogFragment implements OnEditorActionListener {
@ -57,10 +56,9 @@ public class SetPassphraseDialogFragment extends DialogFragment implements OnEdi
public static final String MESSAGE_NEW_PASSPHRASE = "new_passphrase";
private Messenger mMessenger;
private PasswordEditText mPassphraseEditText;
private PassphraseEditText mPassphraseEditText;
private EditText mPassphraseAgainEditText;
private CheckBox mNoPassphraseCheckBox;
private PasswordStrengthView mPassphraseStrengthView;
/**
* Creates new instance of this dialog fragment
@ -100,11 +98,9 @@ public class SetPassphraseDialogFragment extends DialogFragment implements OnEdi
View view = inflater.inflate(R.layout.passphrase_repeat_dialog, null);
alert.setView(view);
mPassphraseEditText = (PasswordEditText) view.findViewById(R.id.passphrase_passphrase);
mPassphraseEditText = (PassphraseEditText) view.findViewById(R.id.passphrase_passphrase);
mPassphraseAgainEditText = (EditText) view.findViewById(R.id.passphrase_passphrase_again);
mNoPassphraseCheckBox = (CheckBox) view.findViewById(R.id.passphrase_no_passphrase);
mPassphraseStrengthView = (PasswordStrengthView) view.findViewById(R.id.passphrase_repeat_passphrase_strength);
mPassphraseEditText.setPasswordStrengthView(mPassphraseStrengthView);
if (TextUtils.isEmpty(oldPassphrase)) {

View File

@ -0,0 +1,84 @@
/*
* Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.ui.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.EditText;
import org.sufficientlysecure.keychain.ui.widget.passwordstrengthindicator.PasswordStrengthBarView;
public class PassphraseEditText extends EditText {
PasswordStrengthBarView mPasswordStrengthBarView;
int mPasswordBarWidth;
int mPasswordBarHeight;
float barGap;
public PassphraseEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mPasswordBarHeight = (int) (8 * getResources().getDisplayMetrics().density);
mPasswordBarWidth = (int) (50 * getResources().getDisplayMetrics().density);
barGap = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,
getContext().getResources().getDisplayMetrics());
this.setPadding(getPaddingLeft(), getPaddingTop(),
getPaddingRight() + (int) barGap + mPasswordBarWidth, getPaddingBottom());
mPasswordStrengthBarView = new PasswordStrengthBarView(context, attrs);
mPasswordStrengthBarView.setShowGuides(false);
this.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mPasswordStrengthBarView.setPassword(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
mPasswordStrengthBarView.layout(0, 0, mPasswordBarWidth, mPasswordBarHeight);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float translateX = getScrollX() + canvas.getWidth() - mPasswordBarWidth;
float translateY = (canvas.getHeight() - mPasswordBarHeight) / 2;
canvas.translate(translateX, translateY);
mPasswordStrengthBarView.draw(canvas);
canvas.translate(-translateX, -translateY);
}
}

View File

@ -56,9 +56,6 @@ import org.sufficientlysecure.keychain.R;
*/
public class PasswordStrengthView extends View {
protected static final int COLOR_FAIL = Color.parseColor("#e74c3c");
protected static final int COLOR_WEAK = Color.parseColor("#e67e22");
protected static final int COLOR_STRONG = Color.parseColor("#2ecc71");
protected int mMinWidth;
protected int mMinHeight;
@ -100,6 +97,11 @@ public class PasswordStrengthView extends View {
public PasswordStrengthView(Context context, AttributeSet attrs) {
super(context, attrs);
int COLOR_FAIL = context.getResources().getColor(R.color.android_red_light);
int COLOR_WEAK = context.getResources().getColor(R.color.android_orange_light);
int COLOR_STRONG = context.getResources().getColor(R.color.android_green_light);
TypedArray style = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.PasswordStrengthView,

View File

@ -58,34 +58,16 @@
android:layout_height="wrap_content"
android:text="@string/label_passphrase" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<org.sufficientlysecure.keychain.ui.widget.PasswordEditText
<org.sufficientlysecure.keychain.ui.widget.PassphraseEditText
android:id="@+id/create_key_passphrase"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:inputType="textPassword"
android:hint="@string/label_passphrase"
android:ems="10"
android:layout_gravity="center_horizontal" />
<org.sufficientlysecure.keychain.ui.widget.passwordstrengthindicator.PasswordStrengthBarView
android:id="@+id/create_key_passphrase_strength"
android:layout_width="48dp"
android:layout_height="8dp"
android:layout_gravity="end|center_vertical"
custom:strength="medium"
custom:showGuides="false"
custom:color_fail="@color/android_red_light"
custom:color_weak="@color/android_orange_light"
custom:color_strong="@color/android_green_light" />
</FrameLayout>
<EditText
android:id="@+id/create_key_passphrase_again"
android:layout_width="match_parent"

View File

@ -21,7 +21,7 @@
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<org.sufficientlysecure.keychain.ui.widget.PasswordEditText
<org.sufficientlysecure.keychain.ui.widget.PassphraseEditText
android:id="@+id/passphrase_passphrase"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -29,6 +29,7 @@
android:layout_marginBottom="8dp"
android:imeOptions="actionNext"
android:hint="@string/label_passphrase"
android:inputType="textPassword"
android:ems="10"
android:layout_gravity="center_horizontal" />