Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / AddAccountActivity.java @ fea03400

History | View | Annotate | Download (4.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.util.Log;
11
import android.view.View;
12
import android.view.View.OnClickListener;
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{
21
        
22
        EditText usernameText;
23
        EditText apiKeyText;
24
        Spinner providerSpinner;
25
        String authServer;
26
        boolean isHidden;
27
        
28
        public void onCreate(Bundle savedInstanceState) {
29
        super.onCreate(savedInstanceState);
30
        setContentView(R.layout.createaccount);
31
        usernameText = (EditText) findViewById(R.id.username);
32
        apiKeyText = (EditText) findViewById(R.id.addaccount_apikey);
33
        ((Button) findViewById(R.id.submit_new_account)).setOnClickListener(this);
34
        isHidden = true;
35
        if(savedInstanceState != null)
36
                isHidden = savedInstanceState.containsKey("isHidden") && savedInstanceState.getBoolean("isHidden");
37
        setUpApiText(savedInstanceState);
38
        setUpCheckBox();
39
        loadProviderSpinner();
40
    } 
41
        
42
        protected void onSaveInstanceState(Bundle outState) {
43
                super.onSaveInstanceState(outState);
44
                outState.putBoolean("isHidden", isHidden);
45
        }
46
        
47
        //setup the API textedit to be password dots or regular text
48
        private void setUpApiText(Bundle state){
49
        isHidden = true;
50
        if(state != null)
51
                isHidden = state.containsKey("isHidden") && state.getBoolean("isHidden");
52
                if(isHidden){
53
                apiKeyText.setTransformationMethod(new PasswordTransformationMethod());
54
                }
55
                else{
56
                apiKeyText.setTransformationMethod(new SingleLineTransformationMethod());
57
                }
58
        }
59
        
60
        private void setUpCheckBox(){
61
                final CheckBox show_clear = (CheckBox) findViewById(R.id.show_clear);
62
                show_clear.setChecked(!isHidden);
63
        show_clear.setOnClickListener(new OnClickListener() {
64
                @Override 
65
                        public void onClick(View v) {
66
                        if (((CheckBox) v).isChecked()) {
67
                                apiKeyText.setTransformationMethod(new SingleLineTransformationMethod());
68
                                isHidden = false;
69
                        } else {
70
                                apiKeyText.setTransformationMethod(new PasswordTransformationMethod());
71
                                isHidden = true;
72
                        }
73
                        apiKeyText.requestFocus();
74
                    }        
75
                });
76
        }
77
        
78
        private void loadProviderSpinner(){
79
                //set the auth server default to us
80
                authServer = "https://auth.api.rackspacecloud.com/v1.0";
81
                providerSpinner = (Spinner) findViewById(R.id.provider_spinner);
82
                String[] providers = {"Rackspace Cloud (US)", "Rackspace Cloud (UK)"};
83
                ArrayAdapter<String> imageAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, providers);
84
                imageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
85
                providerSpinner.setAdapter(imageAdapter);
86
                providerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
87
                    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
88
                            if(pos == 0){
89
                                        authServer = "https://auth.api.rackspacecloud.com/v1.0";
90
                                }
91
                                else if(pos == 1){
92
                                        authServer = "https://lon.auth.api.rackspacecloud.com/v1.0";
93
                                }
94
                    }
95
                    public void onNothingSelected(AdapterView<?> parent) {
96
                    }
97
                });
98
        }
99
        
100
        public void onClick(View arg0) {
101
                
102
                if (hasValidInput()) {
103
                        //showActivityIndicators();
104
                        Intent result = new Intent();
105
                        Bundle b = new Bundle();
106
                        b.putString("username", usernameText.getText().toString());
107
                        b.putString("apiKey", apiKeyText.getText().toString());
108
                        b.putString("server", authServer);
109
                        result.putExtra("accountInfo", b);
110
                        setResult(RESULT_OK, result);
111
                        finish();
112
                } else {
113
                        showAlert("Required Fields Missing", "Username and API Key are required.");
114
                }
115
                
116
        }
117
        
118
        private void showAlert(String title, String message) {
119
            try {
120
                AlertDialog alert = new AlertDialog.Builder(this).create();
121
                alert.setTitle(title);
122
                alert.setMessage(message);
123
                alert.setButton("OK", new DialogInterface.OnClickListener() {
124
              public void onClick(DialogInterface dialog, int which) {
125
                return;
126
            } }); 
127
                alert.show();
128
            } catch (Exception e) {
129
                    e.printStackTrace();
130
            }
131
    }
132
        
133
        private boolean hasValidInput() {
134
            String username = usernameText.getText().toString();
135
            String apiKey = apiKeyText.getText().toString();
136
            return !"".equals(username) && !"".equals(apiKey);
137
    }
138

    
139
}