Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (9.5 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

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

    
45
        private static final int SHOW_PREFERENCES = 1;
46

    
47
        private Intent tabViewIntent;
48
        private boolean authenticating;
49
        private Context context;
50
                
51
    /** Called when the activity is first created. */
52
    @Override
53
    public void onCreate(Bundle savedInstanceState) {
54
        super.onCreate(savedInstanceState);
55
        setContentView(R.layout.main);
56
        
57
        final CheckBox show_clear = (CheckBox) findViewById(R.id.show_clear);
58
        final EditText loginApiKey = (EditText) findViewById(R.id.login_apikey);
59
        
60
        context = getApplicationContext();
61
        
62
        show_clear.setOnClickListener(new OnClickListener() {
63
                        @Override
64
                        public void onClick(View v) {
65
                        if (((CheckBox) v).isChecked()) {
66
                                loginApiKey.setTransformationMethod(new SingleLineTransformationMethod());
67
                        } else {
68
                                loginApiKey.setTransformationMethod(new PasswordTransformationMethod());        
69
                        }
70
                        loginApiKey.requestFocus();
71
                    }        
72
                });
73
        
74
        ((Button) findViewById(R.id.button)).setOnClickListener(this);
75
        
76
                loginApiKey.setOnEditorActionListener(this);
77
        loadLoginPreferences();
78
        restoreState(savedInstanceState);
79
        
80
        // use the TabViewActivity when Cloud Files is added
81
        // tabViewIntent = new Intent(this, TabViewActivity.class);
82
        
83
        tabViewIntent = new Intent(this, TabViewActivity.class);
84
    }
85

    
86
        @Override
87
        protected void onSaveInstanceState(Bundle outState) {
88
                super.onSaveInstanceState(outState);
89
                outState.putBoolean("authenticating", authenticating);
90
        }
91

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

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

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

    
202
    private void setActivityIndicatorsVisibility(int visibility) {
203
        ProgressBar pb = (ProgressBar) findViewById(R.id.login_progress_bar);
204
            TextView tv = (TextView) findViewById(R.id.login_authenticating_label);
205
        pb.setVisibility(visibility);
206
        tv.setVisibility(visibility);
207
    }
208

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

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

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

    
286
}