Create password generation dialog

This commit is contained in:
Matthew Wong 2015-07-10 19:45:32 -04:00
parent d2a252a06b
commit fccefadd32
2 changed files with 231 additions and 0 deletions

View File

@ -0,0 +1,123 @@
package com.zeapo.pwdstore;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import com.zeapo.pwdstore.pwgen.pwgen;
import java.util.ArrayList;
/**
* A placeholder fragment containing a simple view.
*/
public class pwgenDialogFragment extends DialogFragment {
public pwgenDialogFragment() {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.fragment_pwgen, null);
builder.setView(view);
SharedPreferences prefs
= getActivity().getApplicationContext().getSharedPreferences("pwgen", Context.MODE_PRIVATE);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.numerals);
checkBox.setChecked(!prefs.getBoolean("0", false));
checkBox = (CheckBox) view.findViewById(R.id.symbols);
checkBox.setChecked(prefs.getBoolean("y", false));
checkBox = (CheckBox) view.findViewById(R.id.uppercase);
checkBox.setChecked(!prefs.getBoolean("A", false));
checkBox = (CheckBox) view.findViewById(R.id.ambiguous);
checkBox.setChecked(!prefs.getBoolean("B", false));
checkBox = (CheckBox) view.findViewById(R.id.pronounceable);
checkBox.setChecked(!prefs.getBoolean("s", false));
TextView textView = (TextView) view.findViewById(R.id.lengthNumber);
textView.setText(Integer.toString(prefs.getInt("length", 8)));
textView = (TextView) view.findViewById(R.id.passwordText);
textView.setText(pwgen.generate(getActivity().getApplicationContext()).get(0));
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setPreferences();
TextView textView = (TextView) getActivity().findViewById(R.id.crypto_password_edit);
textView.append(pwgen.generate(getActivity().getApplicationContext()).get(0));
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNeutralButton("Generate", null);
final AlertDialog ad = builder.setTitle("Generate Password").create();
ad.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button b = ad.getButton(AlertDialog.BUTTON_NEUTRAL);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setPreferences();
TextView textView = (TextView) getDialog().findViewById(R.id.passwordText);
textView.setText(pwgen.generate(getActivity().getApplicationContext()).get(0));
}
});
}
});
return ad;
}
private boolean setPreferences () {
ArrayList<String> preferences = new ArrayList<>();
if (!((CheckBox)getDialog().findViewById(R.id.numerals)).isChecked()) {
preferences.add("0");
}
if (((CheckBox) getDialog().findViewById(R.id.symbols)).isChecked()) {
preferences.add("y");
}
if (!((CheckBox) getDialog().findViewById(R.id.uppercase)).isChecked()) {
preferences.add("A");
}
if (!((CheckBox) getDialog().findViewById(R.id.ambiguous)).isChecked()) {
preferences.add("B");
}
if (!((CheckBox) getDialog().findViewById(R.id.pronounceable)).isChecked()) {
preferences.add("s");
}
TextView textView = (TextView) getDialog().findViewById(R.id.lengthNumber);
try {
int length = Integer.valueOf(textView.getText().toString());
return pwgen.setPrefs(getActivity().getApplicationContext(), preferences, length);
} catch(NumberFormatException e) {
return pwgen.setPrefs(getActivity().getApplicationContext(), preferences);
}
}
}

View File

@ -0,0 +1,108 @@
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivityFragment">
<EditText
android:id="@+id/passwordText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:editable="false"
android:textIsSelectable="true"
android:inputType="textVisiblePassword"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/include"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Include"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginBottom="8dp"/>
<CheckBox
android:id="@+id/numerals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Numerals"/>
<CheckBox
android:id="@+id/symbols"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Symbols"/>
<CheckBox
android:id="@+id/uppercase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Uppercase"/>
<CheckBox
android:id="@+id/ambiguous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ambiguous"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/length"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Length"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginBottom="8dp"
/>
<EditText
android:id="@+id/lengthNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:ems="10"
android:inputType="number"
/>
<CheckBox
android:id="@+id/pronounceable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pronounceable"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>