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