root / src / com / rackspacecloud / android / PasswordServerActivity.java @ 48601850
History | View | Annotate | Download (3.2 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.android.R; |
14 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
15 |
import com.rackspace.cloud.servers.api.client.Server; |
16 |
import com.rackspace.cloud.servers.api.client.ServerManager; |
17 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
18 |
|
19 |
public class PasswordServerActivity extends CloudActivity implements OnClickListener{ |
20 |
|
21 |
private Server server;
|
22 |
private String modifiedPassword; |
23 |
|
24 |
public void onCreate(Bundle savedInstanceState) { |
25 |
super.onCreate(savedInstanceState);
|
26 |
trackPageView(GoogleAnalytics.PAGE_PASSCODE); |
27 |
setContentView(R.layout.viewchangepassword); |
28 |
server = (Server) this.getIntent().getExtras().get("server"); |
29 |
} |
30 |
|
31 |
protected void restoreState(Bundle state){ |
32 |
super.restoreState(state);
|
33 |
setupButtons(); |
34 |
} |
35 |
|
36 |
private void setupButtons() { |
37 |
Button update = (Button) findViewById(R.id.password_change_button); |
38 |
update.setOnClickListener(this);
|
39 |
} |
40 |
|
41 |
@Override
|
42 |
public void onClick(View v) { |
43 |
String password = ((EditText)findViewById(R.id.password_edittext)).getText().toString();
|
44 |
String confirm = ((EditText)findViewById(R.id.password_confirm_edittext)).getText().toString();
|
45 |
if(password.equals(confirm)){
|
46 |
trackEvent(GoogleAnalytics.CATEGORY_SERVER, GoogleAnalytics.EVENT_PASSWORD, "", -1); |
47 |
modifiedPassword = password; |
48 |
new PasswordServerTask().execute((Void[]) null); |
49 |
} |
50 |
else{
|
51 |
showToast("The password and confirmation do not match");
|
52 |
} |
53 |
} |
54 |
|
55 |
private class PasswordServerTask extends AsyncTask<Void, Void, HttpBundle> { |
56 |
|
57 |
private CloudServersException exception;
|
58 |
|
59 |
protected void onPreExecute(){ |
60 |
showToast("Change root password process has begun");
|
61 |
} |
62 |
|
63 |
@Override
|
64 |
protected HttpBundle doInBackground(Void... arg0) { |
65 |
HttpBundle bundle = null;
|
66 |
try {
|
67 |
bundle = (new ServerManager()).changePassword(server, modifiedPassword, getApplicationContext());
|
68 |
} catch (CloudServersException e) {
|
69 |
exception = e; |
70 |
} |
71 |
return bundle;
|
72 |
} |
73 |
|
74 |
@Override
|
75 |
protected void onPostExecute(HttpBundle bundle) { |
76 |
HttpResponse response = bundle.getResponse(); |
77 |
if (response != null) { |
78 |
int statusCode = response.getStatusLine().getStatusCode();
|
79 |
if(statusCode == 204){ |
80 |
String mustMatch = "The server's root password has successfully been changed."; |
81 |
Toast passwordError = Toast.makeText(getApplicationContext(), mustMatch, Toast.LENGTH_SHORT); |
82 |
passwordError.show(); |
83 |
finish(); |
84 |
} |
85 |
if (statusCode != 204) { |
86 |
CloudServersException cse = parseCloudServersException(response); |
87 |
if ("".equals(cse.getMessage())) { |
88 |
showError("There was a problem changing your password.", bundle);
|
89 |
} else {
|
90 |
showError("There was a problem changing your password: " + cse.getMessage() + " " + statusCode, bundle); |
91 |
} |
92 |
} |
93 |
} else if (exception != null) { |
94 |
showError("There was a problem changing your password: " + exception.getMessage(), bundle);
|
95 |
|
96 |
} |
97 |
} |
98 |
|
99 |
|
100 |
} |
101 |
} |