Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / CreatePasswordActivity.java @ d81b2ba1

History | View | Annotate | Download (6.4 kB)

1
package com.rackspace.cloud.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
import com.rackspace.cloud.android.R;
14

    
15
public class CreatePasswordActivity extends CloudActivity {
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
        protected void restoreState(Bundle state) {
33
                super.restoreState(state);
34
                if (state != null && state.containsKey("isChecked")) {
35
                        isChecked = state.getBoolean("isChecked");
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
         * sets up submit button to deal with each case of starting having/not
77
         * having a password and ending with having/not having a password
78
         */
79
        private void setUpSubmit() {
80
                submitPassword = (Button) findViewById(R.id.create_password_button);
81
                submitPassword.setOnClickListener(new OnClickListener() {
82
                        public void onClick(View v) {
83
                                // didn't have password before and still doesn't
84
                                if (!hadPassword() && !willHavePassword()) {
85
                                        finish();
86
                                }
87
                                // didnt have password before and does now
88
                                else if (!hadPassword() && willHavePassword()) {
89
                                        if (passwordsMatch()) {
90
                                                if (validInputs()) {
91
                                                        pwManager.changePassword(passwordText.getText()
92
                                                                        .toString());
93
                                                        showToast("Password has been enabled.");
94
                                                        finish();
95
                                                } else {
96
                                                        showAlert("Missing Field",
97
                                                                        "Password and confirmation are required.");
98
                                                }
99
                                        } else {
100
                                                showAlert("Passwords must match",
101
                                                                "Password and confirmation did not match. Try again.");
102
                                        }
103
                                } else if (hadPassword()) {
104
                                        showDialog(R.id.create_password_button);
105

    
106
                                }
107
                        }
108
                });
109
        }
110

    
111
        /*
112
         * handles the case where the was a password before the user will need to
113
         * enter the old password in order to make any changes
114
         */
115
        @Override
116
        protected Dialog onCreateDialog(int id) {
117
                switch (id) {
118
                case R.id.create_password_button:
119
                        final EditText input = new EditText(this);
120
                        input.setInputType(android.text.InputType.TYPE_CLASS_TEXT
121
                                        | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
122
                        input.setText("");
123
                        input.setMaxWidth(10);
124
                        return new AlertDialog.Builder(CreatePasswordActivity.this)
125
                                        .setIcon(R.drawable.alert_dialog_icon)
126
                                        .setView(input)
127
                                        .setTitle("Verification Required")
128
                                        .setMessage("Enter your old password: ")
129
                                        .setPositiveButton("Submit",
130
                                                        new DialogInterface.OnClickListener() {
131
                                                                public void onClick(DialogInterface dialog,
132
                                                                                int whichButton) {
133
                                                                        // had password before and doesnt now
134
                                                                        if (!willHavePassword()) {
135
                                                                                if (pwManager
136
                                                                                                .verifyEnteredPassword(input
137
                                                                                                                .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
152
                                                                                                .verifyEnteredPassword(input
153
                                                                                                                .getText().toString())) {
154
                                                                                        if (passwordsMatch()) {
155
                                                                                                if (validInputs()) {
156
                                                                                                        pwManager
157
                                                                                                                        .changePassword(passwordText
158
                                                                                                                                        .getText()
159
                                                                                                                                        .toString());
160
                                                                                                        showToast("Password has been changed.");
161
                                                                                                        finish();
162
                                                                                                } else {
163
                                                                                                        showAlert("Missing Field",
164
                                                                                                                        "Password and confirmation are required.");
165
                                                                                                }
166
                                                                                        } else {
167
                                                                                                showAlert(
168
                                                                                                                "Passwords must match",
169
                                                                                                                "Password and confirmation did not match. Try again.");
170
                                                                                        }
171

    
172
                                                                                } else {
173
                                                                                        showAlert("Problem with password",
174
                                                                                                        "The entered password was incorrect");
175
                                                                                        passwordCheckBox.setChecked(true);
176
                                                                                        passwordText.setEnabled(true);
177
                                                                                        confirmText.setEnabled(true);
178
                                                                                }
179
                                                                        }
180
                                                                }
181
                                                        })
182
                                        .setNegativeButton("Cancel",
183
                                                        new DialogInterface.OnClickListener() {
184
                                                                public void onClick(DialogInterface dialog,
185
                                                                                int whichButton) {
186
                                                                        removeDialog(R.id.create_password_button);
187
                                                                        // passwordCheckBox.setChecked(true);
188
                                                                }
189
                                                        }).create();
190
                }
191
                return null;
192
        }
193

    
194
        /*
195
         * must ensure that the passwords are the same before you can submit
196
         */
197
        private boolean passwordsMatch() {
198
                return passwordText.getText().toString()
199
                                .equals(confirmText.getText().toString());
200
        }
201

    
202
        private boolean willHavePassword() {
203
                return passwordCheckBox.isChecked();
204
        }
205

    
206
        /*
207
         * returns true if application required a password before current edit
208
         */
209
        private boolean hadPassword() {
210
                return pwManager.hasPassword();
211
        }
212

    
213
        /*
214
         * checks that when the user submits the password fields are not empty
215
         */
216
        private boolean validInputs() {
217
                return !passwordText.getText().toString().equals("")
218
                                || !confirmText.getText().toString().equals("");
219
        }
220
}