root / src / com / rackspacecloud / android / CreatePasswordActivity.java @ 6b8dad86
History | View | Annotate | Download (6.1 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import android.app.AlertDialog; |
4 |
import android.app.Dialog; |
5 |
import android.content.DialogInterface; |
6 |
import android.os.Bundle; |
7 |
import android.view.View; |
8 |
import android.view.View.OnClickListener; |
9 |
import android.widget.Button; |
10 |
import android.widget.CheckBox; |
11 |
import android.widget.EditText; |
12 |
|
13 |
public class CreatePasswordActivity extends CloudActivity { |
14 |
|
15 |
private PasswordManager pwManager;
|
16 |
private EditText passwordText;
|
17 |
private EditText confirmText;
|
18 |
private Button submitPassword; |
19 |
private CheckBox passwordCheckBox;
|
20 |
private boolean isChecked; |
21 |
|
22 |
public void onCreate(Bundle savedInstanceState) { |
23 |
super.onCreate(savedInstanceState);
|
24 |
setContentView(R.layout.password); |
25 |
pwManager = new PasswordManager(getSharedPreferences(
|
26 |
Preferences.SHARED_PREFERENCES_NAME, MODE_PRIVATE));
|
27 |
restoreState(savedInstanceState); |
28 |
} |
29 |
|
30 |
protected void restoreState(Bundle state) { |
31 |
super.restoreState(state);
|
32 |
if (state != null && state.containsKey("isChecked")) { |
33 |
isChecked = state.getBoolean("isChecked");
|
34 |
} |
35 |
else{
|
36 |
isChecked = hadPassword(); |
37 |
} |
38 |
setUpWidgets(); |
39 |
|
40 |
} |
41 |
|
42 |
protected void onSaveInstanceState(Bundle outState) { |
43 |
super.onSaveInstanceState(outState);
|
44 |
outState.putBoolean("isChecked", passwordCheckBox.isChecked());
|
45 |
} |
46 |
|
47 |
private void setUpWidgets() { |
48 |
setUpCheckBox(); |
49 |
passwordText = (EditText) findViewById(R.id.password_edittext); |
50 |
confirmText = (EditText) findViewById(R.id.confirm_edittext); |
51 |
if(!passwordCheckBox.isChecked()){
|
52 |
passwordText.setEnabled(false);
|
53 |
confirmText.setEnabled(false);
|
54 |
} |
55 |
setUpSubmit(); |
56 |
} |
57 |
|
58 |
private void setUpCheckBox() { |
59 |
passwordCheckBox = (CheckBox) findViewById(R.id.password_checkbox); |
60 |
passwordCheckBox.setChecked(isChecked); |
61 |
passwordCheckBox.setOnClickListener(new OnClickListener() {
|
62 |
public void onClick(View v) { |
63 |
if (((CheckBox) v).isChecked()) {
|
64 |
passwordText.setEnabled(true);
|
65 |
confirmText.setEnabled(true);
|
66 |
} else {
|
67 |
passwordText.setEnabled(false);
|
68 |
confirmText.setEnabled(false);
|
69 |
} |
70 |
} |
71 |
}); |
72 |
} |
73 |
|
74 |
|
75 |
/*
|
76 |
* sets up submit button to deal with each
|
77 |
* case of starting having/not having a password
|
78 |
* and ending with having/not having a password
|
79 |
*/
|
80 |
private void setUpSubmit() { |
81 |
submitPassword = (Button) findViewById(R.id.create_password_button);
|
82 |
submitPassword.setOnClickListener(new OnClickListener() {
|
83 |
public void onClick(View v) { |
84 |
// didn't have password before and still doesn't
|
85 |
if (!hadPassword() && !willHavePassword()) {
|
86 |
finish(); |
87 |
} |
88 |
// didnt have password before and does now
|
89 |
else if (!hadPassword() && willHavePassword()) { |
90 |
if (passwordsMatch()) {
|
91 |
if(validInputs()){
|
92 |
pwManager.changePassword(passwordText.getText().toString()); |
93 |
showToast("Password has been enabled.");
|
94 |
finish(); |
95 |
} |
96 |
else{
|
97 |
showAlert("Missing Field",
|
98 |
"Password and confirmation are required.");
|
99 |
} |
100 |
} else {
|
101 |
showAlert("Passwords must match",
|
102 |
"Password and confirmation did not match. Try again.");
|
103 |
} |
104 |
} else if (hadPassword()) { |
105 |
showDialog(R.id.create_password_button); |
106 |
|
107 |
} |
108 |
} |
109 |
}); |
110 |
} |
111 |
|
112 |
/*
|
113 |
* handles the case where the was a password before
|
114 |
* the user will need to enter the old password
|
115 |
* in order to make any changes
|
116 |
*/
|
117 |
@Override
|
118 |
protected Dialog onCreateDialog(int id) { |
119 |
switch (id) {
|
120 |
case R.id.create_password_button:
|
121 |
final EditText input = new EditText(this); |
122 |
input.setInputType(android.text.InputType.TYPE_CLASS_TEXT |
123 |
| android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD); |
124 |
input.setText("");
|
125 |
input.setMaxWidth(10);
|
126 |
return new AlertDialog.Builder(CreatePasswordActivity.this) |
127 |
.setIcon(R.drawable.alert_dialog_icon) |
128 |
.setView(input) |
129 |
.setTitle("Verification Required")
|
130 |
.setMessage("Enter your old password: ")
|
131 |
.setPositiveButton("Submit",
|
132 |
new DialogInterface.OnClickListener() {
|
133 |
public void onClick(DialogInterface dialog, |
134 |
int whichButton) {
|
135 |
// had password before and doesnt now
|
136 |
if (!willHavePassword()) {
|
137 |
if (pwManager.verifyEnteredPassword(input.getText().toString())) {
|
138 |
pwManager.turnOffPassword(); |
139 |
showToast("Password has been disabled.");
|
140 |
finish(); |
141 |
} else {
|
142 |
showAlert("Problem with password",
|
143 |
"The entered password was incorrect");
|
144 |
passwordCheckBox.setChecked(true);
|
145 |
passwordText.setEnabled(true);
|
146 |
confirmText.setEnabled(true);
|
147 |
} |
148 |
} |
149 |
// had a password and still has one
|
150 |
else {
|
151 |
if (pwManager.verifyEnteredPassword(input.getText().toString())) {
|
152 |
if (passwordsMatch()) {
|
153 |
if(validInputs()){
|
154 |
pwManager.changePassword(passwordText.getText().toString()); |
155 |
showToast("Password has been changed.");
|
156 |
finish(); |
157 |
} |
158 |
else{
|
159 |
showAlert("Missing Field",
|
160 |
"Password and confirmation are required.");
|
161 |
} |
162 |
} else {
|
163 |
showAlert("Passwords must match",
|
164 |
"Password and confirmation did not match. Try again.");
|
165 |
} |
166 |
|
167 |
} else {
|
168 |
showAlert("Problem with password",
|
169 |
"The entered password was incorrect");
|
170 |
passwordCheckBox.setChecked(true);
|
171 |
passwordText.setEnabled(true);
|
172 |
confirmText.setEnabled(true);
|
173 |
} |
174 |
} |
175 |
} |
176 |
}) |
177 |
.setNegativeButton("Cancel",
|
178 |
new DialogInterface.OnClickListener() {
|
179 |
public void onClick(DialogInterface dialog, |
180 |
int whichButton) {
|
181 |
removeDialog(R.id.create_password_button); |
182 |
//passwordCheckBox.setChecked(true);
|
183 |
} |
184 |
}).create(); |
185 |
} |
186 |
return null; |
187 |
} |
188 |
|
189 |
|
190 |
/*
|
191 |
* must ensure that the passwords are the same before you can submit
|
192 |
*/
|
193 |
private boolean passwordsMatch() { |
194 |
return passwordText.getText().toString()
|
195 |
.equals(confirmText.getText().toString()); |
196 |
} |
197 |
|
198 |
private boolean willHavePassword() { |
199 |
return passwordCheckBox.isChecked();
|
200 |
} |
201 |
|
202 |
/*
|
203 |
* returns true if application required a
|
204 |
* password before current edit
|
205 |
*/
|
206 |
private boolean hadPassword(){ |
207 |
return pwManager.hasPassword();
|
208 |
} |
209 |
|
210 |
/*
|
211 |
* checks that when the user submits
|
212 |
* the password fields are not empty
|
213 |
*/
|
214 |
private boolean validInputs(){ |
215 |
return !passwordText.getText().toString().equals("") |
216 |
|| !confirmText.getText().toString().equals("");
|
217 |
} |
218 |
} |