Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / CreatePasswordActivity.java @ 48601850

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
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
                }
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
}