root / src / com / rackspacecloud / android / AddAccountActivity.java @ f3633a16
History | View | Annotate | Download (4.7 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import android.content.Intent; |
4 |
import android.os.Bundle; |
5 |
import android.text.method.PasswordTransformationMethod; |
6 |
import android.text.method.SingleLineTransformationMethod; |
7 |
import android.util.Log; |
8 |
import android.view.View; |
9 |
import android.view.View.OnClickListener; |
10 |
import android.widget.AdapterView; |
11 |
import android.widget.ArrayAdapter; |
12 |
import android.widget.Button; |
13 |
import android.widget.CheckBox; |
14 |
import android.widget.EditText; |
15 |
import android.widget.Spinner; |
16 |
|
17 |
public class AddAccountActivity extends CloudActivity implements OnClickListener{ |
18 |
|
19 |
private final String[] PROVIDERS = {"Rackspace Cloud (US)", "Rackspace Cloud (UK)", "Custom"}; |
20 |
private EditText usernameText;
|
21 |
private EditText passwordText;
|
22 |
private EditText customServer;
|
23 |
private Spinner providerSpinner;
|
24 |
private String authServer; |
25 |
boolean isHidden;
|
26 |
|
27 |
public void onCreate(Bundle savedInstanceState) { |
28 |
super.onCreate(savedInstanceState);
|
29 |
trackPageView(GoogleAnalytics.PAGE_PROVIDERS); |
30 |
setContentView(R.layout.createaccount); |
31 |
usernameText = (EditText) findViewById(R.id.username); |
32 |
passwordText = (EditText) findViewById(R.id.addaccount_apikey); |
33 |
customServer = (EditText) findViewById(R.id.custom_auth_server_edit); |
34 |
((Button) findViewById(R.id.submit_new_account)).setOnClickListener(this); |
35 |
isHidden = true;
|
36 |
customServer.setEnabled(false);
|
37 |
if(savedInstanceState != null) |
38 |
isHidden = savedInstanceState.containsKey("isHidden") && savedInstanceState.getBoolean("isHidden"); |
39 |
setUpApiText(savedInstanceState); |
40 |
setUpCheckBox(); |
41 |
loadProviderSpinner(); |
42 |
} |
43 |
|
44 |
protected void onSaveInstanceState(Bundle outState) { |
45 |
super.onSaveInstanceState(outState);
|
46 |
outState.putBoolean("isHidden", isHidden);
|
47 |
} |
48 |
|
49 |
//setup the API textedit to be password dots or regular text
|
50 |
private void setUpApiText(Bundle state){ |
51 |
isHidden = true;
|
52 |
if(state != null) |
53 |
isHidden = state.containsKey("isHidden") && state.getBoolean("isHidden"); |
54 |
if(isHidden){
|
55 |
passwordText.setTransformationMethod(new PasswordTransformationMethod());
|
56 |
} |
57 |
else{
|
58 |
passwordText.setTransformationMethod(new SingleLineTransformationMethod());
|
59 |
} |
60 |
} |
61 |
|
62 |
private void setUpCheckBox(){ |
63 |
final CheckBox show_clear = (CheckBox) findViewById(R.id.show_clear);
|
64 |
show_clear.setChecked(!isHidden); |
65 |
show_clear.setOnClickListener(new OnClickListener() {
|
66 |
@Override
|
67 |
public void onClick(View v) { |
68 |
if (((CheckBox) v).isChecked()) {
|
69 |
passwordText.setTransformationMethod(new SingleLineTransformationMethod());
|
70 |
isHidden = false;
|
71 |
} else {
|
72 |
passwordText.setTransformationMethod(new PasswordTransformationMethod());
|
73 |
isHidden = true;
|
74 |
} |
75 |
passwordText.requestFocus(); |
76 |
} |
77 |
}); |
78 |
} |
79 |
|
80 |
private void loadProviderSpinner(){ |
81 |
//set the auth server default to us
|
82 |
authServer = "https://auth.api.rackspacecloud.com/v2.0";
|
83 |
providerSpinner = (Spinner) findViewById(R.id.provider_spinner); |
84 |
ArrayAdapter<String> imageAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, PROVIDERS); |
85 |
imageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
86 |
providerSpinner.setAdapter(imageAdapter); |
87 |
providerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
|
88 |
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { |
89 |
if(pos == 0){ |
90 |
authServer = Preferences.COUNTRY_US_AUTH_SERVER_V2;
|
91 |
customServer.setEnabled(false);
|
92 |
} |
93 |
else if(pos == 1){ |
94 |
authServer = Preferences.COUNTRY_UK_AUTH_SERVER_V2;
|
95 |
customServer.setEnabled(false);
|
96 |
} |
97 |
else{
|
98 |
customServer.setEnabled(true);
|
99 |
} |
100 |
} |
101 |
public void onNothingSelected(AdapterView<?> parent) { |
102 |
} |
103 |
}); |
104 |
} |
105 |
|
106 |
public void onClick(View arg0) { |
107 |
|
108 |
if (hasValidInput()) {
|
109 |
//showActivityIndicators();
|
110 |
Intent result = new Intent();
|
111 |
Bundle b = new Bundle();
|
112 |
b.putString("username", usernameText.getText().toString());
|
113 |
b.putString("apiKey", passwordText.getText().toString());
|
114 |
b.putString("server", getAuthServer());
|
115 |
result.putExtra("accountInfo", b);
|
116 |
setResult(RESULT_OK, result); |
117 |
finish(); |
118 |
} else {
|
119 |
showAlert("Required Fields Missing", "Username and API Key are required."); |
120 |
} |
121 |
|
122 |
} |
123 |
|
124 |
private String getAuthServer(){ |
125 |
if(customServer.isEnabled()){
|
126 |
authServer = customServer.getText().toString(); |
127 |
} |
128 |
return authServer;
|
129 |
} |
130 |
|
131 |
private boolean hasValidInput() { |
132 |
String username = usernameText.getText().toString();
|
133 |
String apiKey = passwordText.getText().toString();
|
134 |
return !"".equals(username) && !"".equals(apiKey); |
135 |
} |
136 |
|
137 |
} |