Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (5 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;
13
import android.widget.ArrayAdapter;
14
import android.widget.Button;
15
import android.widget.CheckBox;
16
import android.widget.EditText;
17
import android.widget.Spinner;
18

    
19
public class AddAccountActivity extends Activity implements OnClickListener{
20
        
21
        EditText usernameText;
22
        EditText apiKeyText;
23
        EditText customServer;
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
        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
                apiKeyText.setTransformationMethod(new PasswordTransformationMethod());
56
                }
57
                else{
58
                apiKeyText.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
                                apiKeyText.setTransformationMethod(new SingleLineTransformationMethod());
70
                                isHidden = false;
71
                        } else {
72
                                apiKeyText.setTransformationMethod(new PasswordTransformationMethod());
73
                                isHidden = true;
74
                        }
75
                        apiKeyText.requestFocus();
76
                    }        
77
                });
78
        }
79
        
80
        private void loadProviderSpinner(){
81
                //set the auth server default to us
82
                authServer = "https://auth.api.rackspacecloud.com/v1.0";
83
                providerSpinner = (Spinner) findViewById(R.id.provider_spinner);
84
                String[] providers = {"Rackspace Cloud (US)", "Rackspace Cloud (UK)", "Custom"};
85
                ArrayAdapter<String> imageAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, providers);
86
                imageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
87
                providerSpinner.setAdapter(imageAdapter);
88
                providerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
89
                    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
90
                            if(pos == 0){
91
                                        authServer = Preferences.COUNTRY_US_AUTH_SERVER;
92
                                customServer.setEnabled(false);
93
                                }
94
                                else if(pos == 1){
95
                                        authServer = Preferences.COUNTRY_UK_AUTH_SERVER;
96
                                customServer.setEnabled(false);
97
                                }
98
                                else{
99
                                customServer.setEnabled(true);
100
                                }
101
                    }
102
                    public void onNothingSelected(AdapterView<?> parent) {
103
                    }
104
                });
105
        }
106
        
107
        public void onClick(View arg0) {
108

    
109
                if (hasValidInput()) {
110
                        //showActivityIndicators();
111
                        Intent result = new Intent();
112
                        Bundle b = new Bundle();
113
                        b.putString("username", usernameText.getText().toString());
114
                        b.putString("apiKey", apiKeyText.getText().toString());
115
                        b.putString("server", getAuthServer());
116
                        result.putExtra("accountInfo", b);
117
                        setResult(RESULT_OK, result);
118
                        finish();
119
                } else {
120
                        showAlert("Required Fields Missing", "Username and API Key are required.");
121
                }
122
                
123
        }
124
        
125
        private String getAuthServer(){
126
                if(customServer.isEnabled()){
127
                        authServer = customServer.getText().toString();
128
                }
129
                return authServer;
130
        }
131
        
132
        private void showAlert(String title, String message) {
133
            try {
134
                AlertDialog alert = new AlertDialog.Builder(this).create();
135
                alert.setTitle(title);
136
                alert.setMessage(message);
137
                alert.setButton("OK", new DialogInterface.OnClickListener() {
138
              public void onClick(DialogInterface dialog, int which) {
139
                return;
140
            } }); 
141
                alert.show();
142
            } catch (Exception e) {
143
                    e.printStackTrace();
144
            }
145
    }
146
        
147
        private boolean hasValidInput() {
148
            String username = usernameText.getText().toString();
149
            String apiKey = apiKeyText.getText().toString();
150
            return !"".equals(username) && !"".equals(apiKey);
151
    }
152

    
153
}