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