1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
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", true));
TextView textView = (TextView) view.findViewById(R.id.lengthNumber);
textView.setText(Integer.toString(prefs.getInt("length", 20)));
builder.setPositiveButton(getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
TextView edit = (TextView) getActivity().findViewById(R.id.crypto_password_edit);
TextView generate = (TextView) getDialog().findViewById(R.id.passwordText);
edit.append(generate.getText());
}
});
builder.setNegativeButton(getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNeutralButton(getResources().getString(R.string.pwgen_generate), null);
final AlertDialog ad = builder.setTitle("Generate Password").create();
ad.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
setPreferences();
TextView textView = (TextView) view.findViewById(R.id.passwordText);
textView.setText(pwgen.generate(getActivity().getApplicationContext()).get(0));
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);
}
}
}
|