Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / AddAccountActivity.java @ 4f5e4ef0

History | View | Annotate | Download (5.1 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 GaActivity 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
        trackPageView(PAGE_PROVIDERS);
31
        setContentView(R.layout.createaccount);
32
        usernameText = (EditText) findViewById(R.id.username);
33
        apiKeyText = (EditText) findViewById(R.id.addaccount_apikey);
34
        customServer = (EditText) findViewById(R.id.custom_auth_server_edit);
35
        ((Button) findViewById(R.id.submit_new_account)).setOnClickListener(this);
36
        isHidden = true;
37
        customServer.setEnabled(false);
38
        if(savedInstanceState != null)
39
                isHidden = savedInstanceState.containsKey("isHidden") && savedInstanceState.getBoolean("isHidden");
40
        setUpApiText(savedInstanceState);
41
        setUpCheckBox();
42
        loadProviderSpinner();
43
    } 
44
        
45
        protected void onSaveInstanceState(Bundle outState) {
46
                super.onSaveInstanceState(outState);
47
                outState.putBoolean("isHidden", isHidden);
48
        }
49
        
50
        //setup the API textedit to be password dots or regular text
51
        private void setUpApiText(Bundle state){
52
        isHidden = true;
53
        if(state != null)
54
                isHidden = state.containsKey("isHidden") && state.getBoolean("isHidden");
55
                if(isHidden){
56
                apiKeyText.setTransformationMethod(new PasswordTransformationMethod());
57
                }
58
                else{
59
                apiKeyText.setTransformationMethod(new SingleLineTransformationMethod());
60
                }
61
        }
62
        
63
        private void setUpCheckBox(){
64
                final CheckBox show_clear = (CheckBox) findViewById(R.id.show_clear);
65
                show_clear.setChecked(!isHidden);
66
        show_clear.setOnClickListener(new OnClickListener() {
67
                @Override 
68
                        public void onClick(View v) {
69
                        if (((CheckBox) v).isChecked()) {
70
                                apiKeyText.setTransformationMethod(new SingleLineTransformationMethod());
71
                                isHidden = false;
72
                        } else {
73
                                apiKeyText.setTransformationMethod(new PasswordTransformationMethod());
74
                                isHidden = true;
75
                        }
76
                        apiKeyText.requestFocus();
77
                    }        
78
                });
79
        }
80
        
81
        private void loadProviderSpinner(){
82
                //set the auth server default to us
83
                authServer = "https://auth.api.rackspacecloud.com/v1.0";
84
                providerSpinner = (Spinner) findViewById(R.id.provider_spinner);
85
                String[] providers = {"Rackspace Cloud (US)", "Rackspace Cloud (UK)", "Custom"};
86
                ArrayAdapter<String> imageAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, providers);
87
                imageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
88
                providerSpinner.setAdapter(imageAdapter);
89
                providerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
90
                    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
91
                            if(pos == 0){
92
                                        authServer = Preferences.COUNTRY_US_AUTH_SERVER;
93
                                customServer.setEnabled(false);
94
                                }
95
                                else if(pos == 1){
96
                                        authServer = Preferences.COUNTRY_UK_AUTH_SERVER;
97
                                customServer.setEnabled(false);
98
                                }
99
                                else{
100
                                customServer.setEnabled(true);
101
                                }
102
                    }
103
                    public void onNothingSelected(AdapterView<?> parent) {
104
                    }
105
                });
106
        }
107
        
108
        public void onClick(View arg0) {
109

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

    
154
}