Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / PasswordServerActivity.java @ 6b8dad86

History | View | Annotate | Download (3.1 kB)

1
package com.rackspacecloud.android;
2

    
3
import org.apache.http.HttpResponse;
4

    
5
import android.os.AsyncTask;
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.EditText;
11
import android.widget.Toast;
12

    
13
import com.rackspace.cloud.servers.api.client.CloudServersException;
14
import com.rackspace.cloud.servers.api.client.Server;
15
import com.rackspace.cloud.servers.api.client.ServerManager;
16
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
17

    
18
public class PasswordServerActivity extends CloudActivity implements OnClickListener{
19
        
20
        private Server server;
21
        private String modifiedPassword;
22
        
23
        public void onCreate(Bundle savedInstanceState) {
24
        super.onCreate(savedInstanceState);
25
        trackPageView(PAGE_PASSCODE);
26
        setContentView(R.layout.viewchangepassword); 
27
        server = (Server) this.getIntent().getExtras().get("server");
28
    }
29

    
30
        protected void restoreState(Bundle state){
31
                super.restoreState(state);
32
                setupButtons();  
33
        }
34
        
35
        private void setupButtons() {
36
                Button update = (Button) findViewById(R.id.password_change_button);
37
                update.setOnClickListener(this);
38
        }
39
        
40
        @Override
41
        public void onClick(View v) {
42
                String password = ((EditText)findViewById(R.id.password_edittext)).getText().toString();
43
                String confirm = ((EditText)findViewById(R.id.password_confirm_edittext)).getText().toString();
44
                if(password.equals(confirm)){
45
                        trackEvent(CATEGORY_SERVER, EVENT_PASSWORD, "", -1);
46
                        modifiedPassword = password;
47
                        new PasswordServerTask().execute((Void[]) null);        
48
                }
49
                else{
50
                        showToast("The password and confirmation do not match");
51
                }
52
        }
53
        
54
        private class PasswordServerTask extends AsyncTask<Void, Void, HttpBundle> {
55

    
56
                private CloudServersException exception;
57

    
58
                protected void onPreExecute(){
59
                        showToast("Change root password process has begun");
60
                }
61
                
62
                @Override
63
                protected HttpBundle doInBackground(Void... arg0) {
64
                        HttpBundle bundle = null;
65
                        try {
66
                                bundle = (new ServerManager()).changePassword(server, modifiedPassword, getApplicationContext());
67
                        } catch (CloudServersException e) {
68
                                exception = e;
69
                        }
70
                        return bundle;
71
                }
72

    
73
                @Override
74
                protected void onPostExecute(HttpBundle bundle) {
75
                        HttpResponse response = bundle.getResponse();
76
                        if (response != null) {
77
                                int statusCode = response.getStatusLine().getStatusCode();        
78
                                if(statusCode == 204){
79
                                        String mustMatch = "The server's root password has successfully been changed.";
80
                                        Toast passwordError = Toast.makeText(getApplicationContext(), mustMatch, Toast.LENGTH_SHORT);
81
                                        passwordError.show();
82
                                        finish();
83
                                }
84
                                if (statusCode != 204) {
85
                                        CloudServersException cse = parseCloudServersException(response);
86
                                        if ("".equals(cse.getMessage())) {
87
                                                showError("There was a problem changing your password.", bundle);
88
                                        } else {
89
                                                showError("There was a problem changing your password: " + cse.getMessage() + " " + statusCode, bundle);
90
                                        }
91
                                }
92
                        } else if (exception != null) {
93
                                showError("There was a problem changing your password: " + exception.getMessage(), bundle);
94
                                
95
                        }
96
                }
97

    
98

    
99
        }
100
}