Add custom PasswordPreference

Fixes #26
This commit is contained in:
Jakob Nixdorf 2017-08-14 12:36:01 +02:00
parent e6514bf646
commit fc02a3cbd2
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
3 changed files with 74 additions and 3 deletions

View file

@ -0,0 +1,55 @@
package org.shadowice.flocke.andotp.Preferences;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.support.design.widget.TextInputEditText;
import android.util.AttributeSet;
import android.view.View;
import org.shadowice.flocke.andotp.R;
public class PasswordPreference extends DialogPreference {
private static final String DEFAULT_VALUE = "";
private TextInputEditText passwordInput;
private String value = DEFAULT_VALUE;
public PasswordPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.component_password);
}
@Override
protected void onBindDialogView(View view) {
passwordInput = (TextInputEditText) view.findViewById(R.id.passwordEdit);
passwordInput.setText(value);
super.onBindDialogView(view);
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getString(index);
}
@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
if (restorePersistedValue) {
value = getPersistedString(DEFAULT_VALUE);
} else {
value = (String) defaultValue;
persistString(value);
}
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
value = passwordInput.getText().toString();
persistString(value);
}
}
}

View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TextInputLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="@dimen/activity_margin"
android:paddingEnd="@dimen/activity_margin"
app:passwordToggleEnabled="true" >
<android.support.design.widget.TextInputEditText
android:id="@+id/passwordEdit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>

View file

@ -18,11 +18,10 @@
android:summary="@string/settings_desc_auth_device"
android:defaultValue="false" />
<EditTextPreference
<org.shadowice.flocke.andotp.Preferences.PasswordPreference
android:key="@string/settings_key_backup_password"
android:title="@string/settings_title_backup_password"
android:summary="@string/settings_desc_backup_password"
android:inputType="textPassword" />
android:summary="@string/settings_desc_backup_password" />
</PreferenceCategory>