root / src / com / rackspacecloud / android / AddAccountActivity.java @ 441d5327
History | View | Annotate | Download (3.6 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import android.app.Activity; |
4 |
import android.app.AlertDialog; |
5 |
import android.content.DialogInterface; |
6 |
import android.content.Intent; |
7 |
import android.os.Bundle; |
8 |
import android.text.method.PasswordTransformationMethod; |
9 |
import android.text.method.SingleLineTransformationMethod; |
10 |
import android.view.View; |
11 |
import android.view.View.OnClickListener; |
12 |
import android.widget.AdapterView.OnItemSelectedListener; |
13 |
import android.widget.AdapterView; |
14 |
import android.widget.ArrayAdapter; |
15 |
import android.widget.Button; |
16 |
import android.widget.CheckBox; |
17 |
import android.widget.EditText; |
18 |
import android.widget.Spinner; |
19 |
|
20 |
public class AddAccountActivity extends Activity implements OnClickListener, OnItemSelectedListener{ |
21 |
|
22 |
EditText usernameText; |
23 |
EditText apiKeyText; |
24 |
Spinner providerSpinner; |
25 |
String authServer;
|
26 |
|
27 |
public void onCreate(Bundle savedInstanceState) { |
28 |
super.onCreate(savedInstanceState);
|
29 |
setContentView(R.layout.createaccount); |
30 |
usernameText = (EditText) findViewById(R.id.username); |
31 |
apiKeyText = (EditText) findViewById(R.id.apikey); |
32 |
((Button) findViewById(R.id.submit_new_account)).setOnClickListener(this); |
33 |
|
34 |
final CheckBox show_clear = (CheckBox) findViewById(R.id.show_clear);
|
35 |
show_clear.setOnClickListener(new OnClickListener() {
|
36 |
@Override
|
37 |
public void onClick(View v) { |
38 |
if (((CheckBox) v).isChecked()) {
|
39 |
apiKeyText.setTransformationMethod(new SingleLineTransformationMethod());
|
40 |
} else {
|
41 |
apiKeyText.setTransformationMethod(new PasswordTransformationMethod());
|
42 |
} |
43 |
apiKeyText.requestFocus(); |
44 |
} |
45 |
}); |
46 |
|
47 |
loadProviderSpinner(); |
48 |
} |
49 |
|
50 |
private void loadProviderSpinner(){ |
51 |
//set the auth server default to us
|
52 |
authServer = "https://auth.api.rackspacecloud.com/v1.0";
|
53 |
providerSpinner = (Spinner) findViewById(R.id.provider_spinner); |
54 |
String[] providers = {"Rackspace Cloud (US)", "Rackspace Cloud (UK)"}; |
55 |
ArrayAdapter<String> imageAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, providers); |
56 |
imageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
57 |
providerSpinner.setAdapter(imageAdapter); |
58 |
} |
59 |
|
60 |
public void onClick(View arg0) { |
61 |
|
62 |
if (hasValidInput()) {
|
63 |
//showActivityIndicators();
|
64 |
Intent result = new Intent();
|
65 |
Bundle b = new Bundle();
|
66 |
b.putString("username", usernameText.getText().toString());
|
67 |
b.putString("apiKey", apiKeyText.getText().toString());
|
68 |
b.putString("server", authServer);
|
69 |
result.putExtra("accountInfo", b);
|
70 |
setResult(RESULT_OK, result); |
71 |
finish(); |
72 |
} else {
|
73 |
showAlert("Required Fields Missing", "Username and API Key are required."); |
74 |
} |
75 |
|
76 |
} |
77 |
|
78 |
private void showAlert(String title, String message) { |
79 |
try {
|
80 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
81 |
alert.setTitle(title); |
82 |
alert.setMessage(message); |
83 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
84 |
public void onClick(DialogInterface dialog, int which) { |
85 |
return;
|
86 |
} }); |
87 |
alert.show(); |
88 |
} catch (Exception e) { |
89 |
e.printStackTrace(); |
90 |
} |
91 |
} |
92 |
|
93 |
private boolean hasValidInput() { |
94 |
String username = usernameText.getText().toString();
|
95 |
String apiKey = apiKeyText.getText().toString();
|
96 |
return !"".equals(username) && !"".equals(apiKey); |
97 |
} |
98 |
|
99 |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
100 |
if(position == 0){ |
101 |
authServer = "https://auth.api.rackspacecloud.com/v1.0";
|
102 |
} |
103 |
else{
|
104 |
authServer = "https://lon.auth.api.rackspacecloud.com/v1.0";
|
105 |
} |
106 |
} |
107 |
|
108 |
public void onNothingSelected(AdapterView<?> parent) { |
109 |
} |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
} |