Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / RackspaceCloudActivity.java @ 008df6cf

History | View | Annotate | Download (8.6 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.app.Dialog;
9
import android.content.Context;
10
import android.content.DialogInterface;
11
import android.content.Intent;
12
import android.content.SharedPreferences;
13
import android.content.SharedPreferences.Editor;
14
import android.os.AsyncTask;
15
import android.os.Bundle;
16
import android.util.Log;
17
import android.view.KeyEvent;
18
import android.view.Menu;
19
import android.view.MenuItem;
20
import android.view.View;
21
import android.widget.Button;
22
import android.widget.EditText;
23
import android.widget.ProgressBar;
24
import android.widget.TextView;
25
import android.widget.TextView.OnEditorActionListener;
26

    
27
import com.rackspace.cloud.servers.api.client.Account;
28
import com.rackspace.cloud.servers.api.client.Flavor;
29
import com.rackspace.cloud.servers.api.client.FlavorManager;
30
import com.rackspace.cloud.servers.api.client.Image;
31
import com.rackspace.cloud.servers.api.client.ImageManager;
32
import com.rackspace.cloud.servers.api.client.http.Authentication;
33

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

    
41
        private static final int SHOW_PREFERENCES = 1;
42

    
43
        private Intent tabViewIntent;
44
        private boolean authenticating;
45
                
46
    /** Called when the activity is first created. */
47
    @Override
48
    public void onCreate(Bundle savedInstanceState) {
49
        super.onCreate(savedInstanceState);
50
        setContentView(R.layout.main);
51
        ((Button) findViewById(R.id.button)).setOnClickListener(this);
52
        ((EditText) findViewById(R.id.login_apikey)).setOnEditorActionListener(this);
53
        loadLoginPreferences();
54
        restoreState(savedInstanceState);
55
        
56
        // use the TabViewActivity when Cloud Files is added
57
        // tabViewIntent = new Intent(this, TabViewActivity.class);
58
        
59
        tabViewIntent = new Intent(this, TabViewActivity.class);
60
    }
61

    
62
        @Override
63
        protected void onSaveInstanceState(Bundle outState) {
64
                super.onSaveInstanceState(outState);
65
                outState.putBoolean("authenticating", authenticating);
66
        }
67

    
68
    public boolean onCreateOptionsMenu(Menu menu) {
69
        menu.add(0, SHOW_PREFERENCES, 0, "Preferences");
70
        return true;
71
    }
72
        
73
    public boolean onOptionsItemSelected(MenuItem item) {
74
        
75
            switch (item.getItemId()) {
76
                    case SHOW_PREFERENCES:
77
                            showPreferences();
78
                            break;
79
                        }        
80
            return true;
81
    }
82

    
83
    public void showPreferences() {
84
        Intent settingsActivity = new Intent(getBaseContext(),
85
                Preferences.class);
86
        startActivity(settingsActivity);
87
    }
88
    
89
    private void restoreState(Bundle state) {
90
            if (state != null && state.containsKey("authenticating") && state.getBoolean("authenticating")) {
91
                    showActivityIndicators();
92
            } else {
93
                    hideActivityIndicators();
94
            }
95
    }
96
    
97
    public void login() {
98
            if (hasValidInput()) {
99
                showActivityIndicators();
100
                setLoginPreferences();
101
                new AuthenticateTask().execute((Void[]) null);
102
            } else {
103
                    showAlert("Fields Missing", "User Name and API Key are required.");
104
            }
105
    }
106
    
107
    public void onClick(View view) {
108
            login();
109
    }
110
    
111
        public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
112
                login();
113
                return false;
114
        }    
115

    
116
        private void loadLoginPreferences() {
117
            SharedPreferences sp = this.getPreferences(Context.MODE_PRIVATE);
118
            String username = sp.getString(OPT_USERNAME, OPT_USERNAME_DEF);            
119
            String apiKey = sp.getString(OPT_API_KEY, OPT_API_KEY_DEF);
120
            EditText usernameText = (EditText) findViewById(R.id.login_username);
121
            usernameText.setText(username);
122
            EditText apiKeyText = (EditText) findViewById(R.id.login_apikey);
123
            apiKeyText.setText(apiKey);
124
    }
125
    
126
    private void setLoginPreferences() {
127
        SharedPreferences prefs = getSharedPreferences(
128
                Preferences.SHARED_PREFERENCES_NAME,
129
                Context.MODE_PRIVATE);
130
        String resultType = prefs.getString(
131
                Preferences.PREF_KEY_RESULTS_TYPE,
132
                String.valueOf(Preferences.COUNTRY_US));
133
        int resultTypeInt = Integer.parseInt(resultType);
134
        
135
        
136
        //Default Auth Server
137
        String authServer = Preferences.COUNTRY_US_AUTH_SERVER; 
138
        if (resultTypeInt == Preferences.COUNTRY_UK)
139
                authServer = Preferences.COUNTRY_UK_AUTH_SERVER;
140
        
141
        String customAuthServer = prefs.getString(Preferences.PREF_KEY_AUTH_SERVER, "http://");
142
        if (!customAuthServer.equals("http://"))
143
                authServer = customAuthServer;
144
        
145
        Log.d("RackSpace-Cloud", "Using AuthServer: " + authServer);
146
        
147
            String username = ((EditText) findViewById(R.id.login_username)).getText().toString();
148
            String apiKey = ((EditText) findViewById(R.id.login_apikey)).getText().toString();
149
            Account.setUsername(username);
150
            Account.setApiKey(apiKey);
151
            Account.setAuthServer(authServer);
152
            
153
            Editor e = this.getPreferences(Context.MODE_PRIVATE).edit();
154
            e.putString(OPT_USERNAME, username);
155
            e.putString(OPT_API_KEY, apiKey);
156
            e.commit();                
157
    }
158
    
159
    private void showAlert(String title, String message) {
160
                AlertDialog alert = new AlertDialog.Builder(this).create();
161
                alert.setTitle(title);
162
                alert.setMessage(message);
163
                alert.setButton("OK", new DialogInterface.OnClickListener() {
164
              public void onClick(DialogInterface dialog, int which) {
165
                return;
166
            } }); 
167
                alert.show();
168
                hideActivityIndicators();
169
    }
170
    
171
    private boolean hasValidInput() {
172
            String username = ((EditText) findViewById(R.id.login_username)).getText().toString();
173
            String apiKey = ((EditText) findViewById(R.id.login_apikey)).getText().toString();
174
            return !"".equals(username) && !"".equals(apiKey);
175
    }
176

    
177
    private void setActivityIndicatorsVisibility(int visibility) {
178
        ProgressBar pb = (ProgressBar) findViewById(R.id.login_progress_bar);
179
            TextView tv = (TextView) findViewById(R.id.login_authenticating_label);
180
        pb.setVisibility(visibility);
181
        tv.setVisibility(visibility);
182
    }
183

    
184
    private void showActivityIndicators() {
185
            setActivityIndicatorsVisibility(View.VISIBLE);
186
    }
187
    
188
    private void hideActivityIndicators() {
189
            setActivityIndicatorsVisibility(View.INVISIBLE);
190
    }
191
    
192
    private class AuthenticateTask extends AsyncTask<Void, Void, Boolean> {
193
            
194
                @Override
195
                protected Boolean doInBackground(Void... arg0) {
196
                        authenticating = true;
197
                        return new Boolean(Authentication.authenticate());
198
                }
199
            
200
                @Override
201
                protected void onPostExecute(Boolean result) {
202
                        authenticating = false;
203
                        if (result.booleanValue()) {
204
                                //startActivity(tabViewIntent);
205
                        new LoadImagesTask().execute((Void[]) null);                                
206
                        } else {
207
                                showAlert("Login Failure", "Authentication failed.  Please check your User Name and API Key.");
208
                        }
209
                }
210
    }
211

    
212
    private class LoadFlavorsTask extends AsyncTask<Void, Void, ArrayList<Flavor>> {
213
            
214
                @Override
215
                protected ArrayList<Flavor> doInBackground(Void... arg0) {
216
                        return (new FlavorManager()).createList(true);
217
                }
218
            
219
                @Override
220
                protected void onPostExecute(ArrayList<Flavor> result) {
221
                        if (result != null && result.size() > 0) {
222
                                TreeMap<String, Flavor> flavorMap = new TreeMap<String, Flavor>();
223
                                for (int i = 0; i < result.size(); i++) {
224
                                        Flavor flavor = result.get(i);
225
                                        flavorMap.put(flavor.getId(), flavor);
226
                                }
227
                                Flavor.setFlavors(flavorMap);
228
                                startActivity(tabViewIntent);
229
                        } else {
230
                                showAlert("Login Failure", "There was a problem loading server flavors.  Please try again.");
231
                        }
232
                        hideActivityIndicators();
233
                }
234
    }
235

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

    
261
}