Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / RackspaceCloudActivity.java @ b347d5e3

History | View | Annotate | Download (9.4 kB)

1
package com.rackspacecloud.android;
2

    
3
import java.util.ArrayList;
4
import java.util.TreeMap;
5

    
6
import android.app.Activity;
7
import android.app.AlertDialog;
8
import android.content.Context;
9
import android.content.DialogInterface;
10
import android.content.Intent;
11
import android.content.SharedPreferences;
12
import android.content.SharedPreferences.Editor;
13
import android.os.AsyncTask;
14
import android.os.Bundle;
15
import android.text.method.PasswordTransformationMethod;
16
import android.text.method.SingleLineTransformationMethod;
17
import android.util.Log;
18
import android.view.KeyEvent;
19
import android.view.Menu;
20
import android.view.MenuItem;
21
import android.view.View;
22
import android.view.View.OnClickListener;
23
import android.widget.Button;
24
import android.widget.CheckBox;
25
import android.widget.EditText;
26
import android.widget.ProgressBar;
27
import android.widget.TextView;
28
import android.widget.TextView.OnEditorActionListener;
29

    
30
import com.rackspace.cloud.servers.api.client.Account;
31
import com.rackspace.cloud.servers.api.client.Flavor;
32
import com.rackspace.cloud.servers.api.client.FlavorManager;
33
import com.rackspace.cloud.servers.api.client.Image;
34
import com.rackspace.cloud.servers.api.client.ImageManager;
35
import com.rackspace.cloud.servers.api.client.http.Authentication;
36

    
37
public class RackspaceCloudActivity extends Activity implements View.OnClickListener, OnEditorActionListener {
38
        
39
        private static final String OPT_USERNAME = "username";
40
        private static final String OPT_USERNAME_DEF = "";
41
        private static final String OPT_API_KEY = "apiKey";
42
        private static final String OPT_API_KEY_DEF = "";
43

    
44
        private static final int SHOW_PREFERENCES = 1;
45

    
46
        private Intent tabViewIntent;
47
        private boolean authenticating;
48
                
49
    /** Called when the activity is first created. */
50
    @Override
51
    public void onCreate(Bundle savedInstanceState) {
52
        super.onCreate(savedInstanceState);
53
        setContentView(R.layout.main);
54
        
55
        final CheckBox show_clear = (CheckBox) findViewById(R.id.show_clear);
56
        final EditText loginApiKey = (EditText) findViewById(R.id.login_apikey);
57

    
58
        show_clear.setOnClickListener(new OnClickListener() {
59
                        @Override
60
                        public void onClick(View v) {
61
                        if (((CheckBox) v).isChecked()) {
62
                                loginApiKey.setTransformationMethod(new SingleLineTransformationMethod());
63
                        } else {
64
                                loginApiKey.setTransformationMethod(new PasswordTransformationMethod());        
65
                        }
66
                        loginApiKey.requestFocus();
67
                    }        
68
                });
69
        
70
        ((Button) findViewById(R.id.button)).setOnClickListener(this);
71
        
72
                loginApiKey.setOnEditorActionListener(this);
73
        loadLoginPreferences();
74
        restoreState(savedInstanceState);
75
        
76
        // use the TabViewActivity when Cloud Files is added
77
        // tabViewIntent = new Intent(this, TabViewActivity.class);
78
        
79
        tabViewIntent = new Intent(this, TabViewActivity.class);
80
    }
81

    
82
        @Override
83
        protected void onSaveInstanceState(Bundle outState) {
84
                super.onSaveInstanceState(outState);
85
                outState.putBoolean("authenticating", authenticating);
86
        }
87

    
88
    public boolean onCreateOptionsMenu(Menu menu) {
89
            MenuItem settings = menu.add(0, SHOW_PREFERENCES, 0, R.string.preference_name);
90
            settings.setIcon(android.R.drawable.ic_menu_preferences);
91
        return true;
92
    }
93
        
94
    public boolean onOptionsItemSelected(MenuItem item) {
95
        
96
            switch (item.getItemId()) {
97
                    case SHOW_PREFERENCES:
98
                            showPreferences();
99
                            break;
100
                        }        
101
            return true;
102
    }
103

    
104
    public void showPreferences() {
105
        Intent settingsActivity = new Intent(getBaseContext(),
106
                Preferences.class);
107
        startActivity(settingsActivity);
108
    }
109
    
110
    private void restoreState(Bundle state) {
111
            if (state != null && state.containsKey("authenticating") && state.getBoolean("authenticating")) {
112
                    showActivityIndicators();
113
            } else {
114
                    hideActivityIndicators();
115
            }
116
    }
117
    
118
    public void login() {
119
            if (hasValidInput()) {
120
                showActivityIndicators();
121
                setLoginPreferences();
122
                new AuthenticateTask().execute((Void[]) null);
123
            } else {
124
                    showAlert("Fields Missing", "User Name and API Key are required.");
125
            }
126
    }
127
    
128
    public void onClick(View view) {
129
            login();
130
    }
131
    
132
        public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
133
                login();
134
                return false;
135
        }    
136

    
137
        private void loadLoginPreferences() {
138
            SharedPreferences sp = this.getPreferences(Context.MODE_PRIVATE);
139
            String username = sp.getString(OPT_USERNAME, OPT_USERNAME_DEF);            
140
            String apiKey = sp.getString(OPT_API_KEY, OPT_API_KEY_DEF);
141
            EditText usernameText = (EditText) findViewById(R.id.login_username);
142
            usernameText.setText(username);
143
            EditText apiKeyText = (EditText) findViewById(R.id.login_apikey);
144
            apiKeyText.setText(apiKey);
145
    }
146
    
147
    private void setLoginPreferences() {
148
        SharedPreferences prefs = getSharedPreferences(
149
                Preferences.SHARED_PREFERENCES_NAME,
150
                Context.MODE_PRIVATE);
151
        String resultType = prefs.getString(
152
                Preferences.PREF_KEY_RESULTS_TYPE,
153
                String.valueOf(Preferences.COUNTRY_US));
154
        int resultTypeInt = Integer.parseInt(resultType);
155
        
156
        
157
        //Default Auth Server
158
        String authServer = Preferences.COUNTRY_US_AUTH_SERVER; 
159
        if (resultTypeInt == Preferences.COUNTRY_UK)
160
                authServer = Preferences.COUNTRY_UK_AUTH_SERVER;
161
        
162
        String customAuthServer = prefs.getString(Preferences.PREF_KEY_AUTH_SERVER, "http://");
163
        if (!customAuthServer.equals("http://"))
164
                authServer = customAuthServer;
165
        
166
        Log.d("RackSpace-Cloud", "Using AuthServer: " + authServer);
167
        
168
            String username = ((EditText) findViewById(R.id.login_username)).getText().toString();
169
            String apiKey = ((EditText) findViewById(R.id.login_apikey)).getText().toString();
170
            Account.setUsername(username);
171
            Account.setApiKey(apiKey);
172
            Account.setAuthServer(authServer);
173
            
174
            Editor e = this.getPreferences(Context.MODE_PRIVATE).edit();
175
            e.putString(OPT_USERNAME, username);
176
            e.putString(OPT_API_KEY, apiKey);
177
            e.commit();                
178
    }
179
    
180
    private void showAlert(String title, String message) {
181
                AlertDialog alert = new AlertDialog.Builder(this).create();
182
                alert.setTitle(title);
183
                alert.setMessage(message);
184
                alert.setButton("OK", new DialogInterface.OnClickListener() {
185
              public void onClick(DialogInterface dialog, int which) {
186
                return;
187
            } }); 
188
                alert.show();
189
                hideActivityIndicators();
190
    }
191
    
192
    private boolean hasValidInput() {
193
            String username = ((EditText) findViewById(R.id.login_username)).getText().toString();
194
            String apiKey = ((EditText) findViewById(R.id.login_apikey)).getText().toString();
195
            return !"".equals(username) && !"".equals(apiKey);
196
    }
197

    
198
    private void setActivityIndicatorsVisibility(int visibility) {
199
        ProgressBar pb = (ProgressBar) findViewById(R.id.login_progress_bar);
200
            TextView tv = (TextView) findViewById(R.id.login_authenticating_label);
201
        pb.setVisibility(visibility);
202
        tv.setVisibility(visibility);
203
    }
204

    
205
    private void showActivityIndicators() {
206
            setActivityIndicatorsVisibility(View.VISIBLE);
207
    }
208
    
209
    private void hideActivityIndicators() {
210
            setActivityIndicatorsVisibility(View.INVISIBLE);
211
    }
212
    
213
    private class AuthenticateTask extends AsyncTask<Void, Void, Boolean> {
214
            
215
                @Override
216
                protected Boolean doInBackground(Void... arg0) {
217
                        authenticating = true;
218
                        return new Boolean(Authentication.authenticate());
219
                }
220
            
221
                @Override
222
                protected void onPostExecute(Boolean result) {
223
                        authenticating = false;
224
                        if (result.booleanValue()) {
225
                                //startActivity(tabViewIntent);
226
                        new LoadImagesTask().execute((Void[]) null);                                
227
                        } else {
228
                                showAlert("Login Failure", "Authentication failed.  Please check your User Name and API Key.");
229
                        }
230
                }
231
    }
232

    
233
    private class LoadFlavorsTask extends AsyncTask<Void, Void, ArrayList<Flavor>> {
234
            
235
                @Override
236
                protected ArrayList<Flavor> doInBackground(Void... arg0) {
237
                        return (new FlavorManager()).createList(true);
238
                }
239
            
240
                @Override
241
                protected void onPostExecute(ArrayList<Flavor> result) {
242
                        if (result != null && result.size() > 0) {
243
                                TreeMap<String, Flavor> flavorMap = new TreeMap<String, Flavor>();
244
                                for (int i = 0; i < result.size(); i++) {
245
                                        Flavor flavor = result.get(i);
246
                                        flavorMap.put(flavor.getId(), flavor);
247
                                }
248
                                Flavor.setFlavors(flavorMap);
249
                                startActivity(tabViewIntent);
250
                        } else {
251
                                showAlert("Login Failure", "There was a problem loading server flavors.  Please try again.");
252
                        }
253
                        hideActivityIndicators();
254
                }
255
    }
256

    
257
    private class LoadImagesTask extends AsyncTask<Void, Void, ArrayList<Image>> {
258
            
259
                @Override
260
                protected ArrayList<Image> doInBackground(Void... arg0) {
261
                        return (new ImageManager()).createList(true);
262
                }
263
            
264
                @Override
265
                protected void onPostExecute(ArrayList<Image> result) {
266
                        if (result != null && result.size() > 0) {
267
                                TreeMap<String, Image> imageMap = new TreeMap<String, Image>();
268
                                for (int i = 0; i < result.size(); i++) {
269
                                        Image image = result.get(i);
270
                                        imageMap.put(image.getId(), image);
271
                                }
272
                                Image.setImages(imageMap);
273
                                new LoadFlavorsTask().execute((Void[]) null);
274
                                //startActivity(tabViewIntent);
275
                        } else {
276
                                showAlert("Login Failure", "There was a problem loading server images.  Please try again.");
277
                        }
278
                        //hideActivityIndicators();
279
                }
280
    }
281

    
282
}