Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / ListAccountsActivity.java @ fbe4e332

History | View | Annotate | Download (12.7 kB)

1 7dbfc514 koutsoub
package com.rackspace.cloud.android;
2 441d5327 Adam Menz
3 441d5327 Adam Menz
import java.io.FileInputStream;
4 441d5327 Adam Menz
import java.io.FileNotFoundException;
5 441d5327 Adam Menz
import java.io.FileOutputStream;
6 441d5327 Adam Menz
import java.io.IOException;
7 441d5327 Adam Menz
import java.io.ObjectInputStream;
8 441d5327 Adam Menz
import java.io.ObjectOutputStream;
9 441d5327 Adam Menz
import java.io.StreamCorruptedException;
10 441d5327 Adam Menz
import java.util.ArrayList;
11 441d5327 Adam Menz
import java.util.TreeMap;
12 441d5327 Adam Menz
13 f2020b69 Adam Menz
import android.app.ProgressDialog;
14 441d5327 Adam Menz
import android.content.Context;
15 441d5327 Adam Menz
import android.content.DialogInterface;
16 3d654446 Adam Menz
import android.content.DialogInterface.OnCancelListener;
17 441d5327 Adam Menz
import android.content.Intent;
18 441d5327 Adam Menz
import android.os.AsyncTask;
19 441d5327 Adam Menz
import android.os.Bundle;
20 3e180b04 Adam Menz
import android.util.Log;
21 fea03400 Adam Menz
import android.view.ContextMenu;
22 fea03400 Adam Menz
import android.view.ContextMenu.ContextMenuInfo;
23 441d5327 Adam Menz
import android.view.LayoutInflater;
24 441d5327 Adam Menz
import android.view.Menu;
25 441d5327 Adam Menz
import android.view.MenuInflater;
26 441d5327 Adam Menz
import android.view.MenuItem;
27 441d5327 Adam Menz
import android.view.View;
28 441d5327 Adam Menz
import android.view.ViewGroup;
29 48601850 Mike Mayo
import android.view.ViewGroup.LayoutParams;
30 48601850 Mike Mayo
import android.view.WindowManager;
31 fea03400 Adam Menz
import android.widget.AdapterView.AdapterContextMenuInfo;
32 441d5327 Adam Menz
import android.widget.ArrayAdapter;
33 441d5327 Adam Menz
import android.widget.ImageView;
34 441d5327 Adam Menz
import android.widget.ListView;
35 b2a2d2f1 Adam Menz
import android.widget.ProgressBar;
36 441d5327 Adam Menz
import android.widget.TextView;
37 441d5327 Adam Menz
38 48601850 Mike Mayo
import com.rackspace.cloud.android.R;
39 48601850 Mike Mayo
import com.rackspace.cloud.servers.api.client.Account;
40 48601850 Mike Mayo
import com.rackspace.cloud.servers.api.client.CloudServersException;
41 48601850 Mike Mayo
import com.rackspace.cloud.servers.api.client.http.Authentication;
42 441d5327 Adam Menz
43 6b8dad86 Adam Menz
//
44 c2b54e85 Adam Menz
public class ListAccountsActivity extends CloudListActivity{
45 441d5327 Adam Menz
46 730daee8 Adam Menz
        private final String FILENAME = "accounts.data";
47 0edf6b39 Adam Menz
        private static final String PAGE_ROOT = "/Root";
48 b2a2d2f1 Adam Menz
        
49 441d5327 Adam Menz
        private boolean authenticating;
50 006434d8 Adam Menz
        private ArrayList<Account> accounts;
51 006434d8 Adam Menz
        private Intent tabViewIntent;
52 e7534f91 Adam Menz
        private ProgressDialog dialog;
53 e7534f91 Adam Menz
        private Context context;
54 16615abd Adam Menz
        //used to track the current asynctask
55 16615abd Adam Menz
        @SuppressWarnings("rawtypes")
56 16615abd Adam Menz
        private AsyncTask task;
57 f00e3176 Adam Menz
58 441d5327 Adam Menz
        public void onCreate(Bundle savedInstanceState) {
59 0edf6b39 Adam Menz
                super.onCreate(savedInstanceState);
60 0edf6b39 Adam Menz
                trackPageView(PAGE_ROOT);
61 0edf6b39 Adam Menz
                onRestoreInstanceState(savedInstanceState);
62 0edf6b39 Adam Menz
                registerForContextMenu(getListView());
63 0edf6b39 Adam Menz
                context = getApplicationContext();
64 0edf6b39 Adam Menz
                tabViewIntent = new Intent(this, TabViewActivity.class);
65 0edf6b39 Adam Menz
        }
66 e7534f91 Adam Menz
67 441d5327 Adam Menz
        @Override
68 441d5327 Adam Menz
        protected void onSaveInstanceState(Bundle outState) {
69 441d5327 Adam Menz
                super.onSaveInstanceState(outState);
70 441d5327 Adam Menz
                outState.putBoolean("authenticating", authenticating);
71 54986b23 Adam Menz
                outState.putSerializable("accounts", accounts);
72 0edf6b39 Adam Menz
73 e7534f91 Adam Menz
                //need to set authenticating back to true because it is set to false
74 c2b54e85 Adam Menz
                //in hideAccountDialog()
75 e7534f91 Adam Menz
                if(authenticating){
76 c2b54e85 Adam Menz
                        hideAccountDialog();
77 e7534f91 Adam Menz
                        authenticating = true;
78 e7534f91 Adam Menz
                }
79 441d5327 Adam Menz
                writeAccounts();
80 441d5327 Adam Menz
        }
81 0edf6b39 Adam Menz
82 54986b23 Adam Menz
        @SuppressWarnings("unchecked")
83 e7534f91 Adam Menz
        @Override
84 e7534f91 Adam Menz
        protected void onRestoreInstanceState(Bundle state) {
85 54986b23 Adam Menz
86 54986b23 Adam Menz
                /*
87 54986b23 Adam Menz
                 * need reference to the app so you can access
88 54986b23 Adam Menz
                 * isLoggingIn
89 54986b23 Adam Menz
                 */
90 c2b54e85 Adam Menz
91 54986b23 Adam Menz
92 441d5327 Adam Menz
                if (state != null && state.containsKey("authenticating") && state.getBoolean("authenticating")) {
93 c2b54e85 Adam Menz
                        showAccountDialog();
94 0edf6b39 Adam Menz
                } else {
95 c2b54e85 Adam Menz
                        hideAccountDialog();
96 0edf6b39 Adam Menz
                }
97 441d5327 Adam Menz
                if (state != null && state.containsKey("accounts")) {
98 54986b23 Adam Menz
                        accounts = (ArrayList<Account>)state.getSerializable("accounts");
99 0edf6b39 Adam Menz
                        if (accounts.size() == 0) {
100 0edf6b39 Adam Menz
                                displayNoAccountsCell();
101 0edf6b39 Adam Menz
                        } else {
102 0edf6b39 Adam Menz
                                getListView().setDividerHeight(1); // restore divider lines 
103 0edf6b39 Adam Menz
                                setListAdapter(new AccountAdapter());
104 0edf6b39 Adam Menz
                        }
105 0edf6b39 Adam Menz
                } else {
106 0edf6b39 Adam Menz
                        loadAccounts();        
107 0edf6b39 Adam Menz
                }         
108 0edf6b39 Adam Menz
        }
109 3e180b04 Adam Menz
110 e7534f91 Adam Menz
        @Override
111 e7534f91 Adam Menz
        protected void onStart(){
112 e7534f91 Adam Menz
                super.onStart();
113 e7534f91 Adam Menz
                if(authenticating){
114 c2b54e85 Adam Menz
                        showAccountDialog();
115 e7534f91 Adam Menz
                }
116 e7534f91 Adam Menz
        }
117 0edf6b39 Adam Menz
118 e7534f91 Adam Menz
        @Override
119 e7534f91 Adam Menz
        protected void onStop(){
120 e7534f91 Adam Menz
                super.onStop();
121 e7534f91 Adam Menz
                if(authenticating){
122 c2b54e85 Adam Menz
                        hideAccountDialog();
123 e7534f91 Adam Menz
                        authenticating = true;
124 e7534f91 Adam Menz
                }
125 e7534f91 Adam Menz
        }
126 0edf6b39 Adam Menz
127 441d5327 Adam Menz
        private void loadAccounts() {
128 fea03400 Adam Menz
                //check and see if there are any in memory
129 441d5327 Adam Menz
                if(accounts == null){
130 441d5327 Adam Menz
                        accounts = readAccounts();
131 441d5327 Adam Menz
                }
132 441d5327 Adam Menz
                //if nothing was written before accounts will still be null
133 441d5327 Adam Menz
                if(accounts == null){
134 441d5327 Adam Menz
                        accounts = new ArrayList<Account>();
135 441d5327 Adam Menz
                }
136 fea03400 Adam Menz
137 441d5327 Adam Menz
                setAccountList();
138 441d5327 Adam Menz
        }
139 441d5327 Adam Menz
140 441d5327 Adam Menz
        private void setAccountList() {
141 441d5327 Adam Menz
                if (accounts.size() == 0) {
142 441d5327 Adam Menz
                        displayNoAccountsCell();
143 441d5327 Adam Menz
                } else {
144 441d5327 Adam Menz
                        getListView().setDividerHeight(1); // restore divider lines 
145 441d5327 Adam Menz
                        this.setListAdapter(new AccountAdapter());
146 441d5327 Adam Menz
                }
147 441d5327 Adam Menz
        }
148 441d5327 Adam Menz
149 441d5327 Adam Menz
        private void writeAccounts(){
150 441d5327 Adam Menz
                FileOutputStream fos;
151 730daee8 Adam Menz
                ObjectOutputStream out = null;
152 441d5327 Adam Menz
                try{
153 441d5327 Adam Menz
                        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
154 441d5327 Adam Menz
                        out = new ObjectOutputStream(fos);
155 441d5327 Adam Menz
                        out.writeObject(accounts);
156 441d5327 Adam Menz
                        out.flush();
157 441d5327 Adam Menz
                        out.close();
158 441d5327 Adam Menz
                } catch (FileNotFoundException e) {
159 d88d3ae1 Adam Menz
                        showAlert("Error", "Could not save accounts.");
160 441d5327 Adam Menz
                        e.printStackTrace();
161 441d5327 Adam Menz
                } catch (IOException e) {
162 d88d3ae1 Adam Menz
                        showAlert("Error", "Could not save accounts.");
163 441d5327 Adam Menz
                        e.printStackTrace();
164 441d5327 Adam Menz
                }
165 441d5327 Adam Menz
        }
166 441d5327 Adam Menz
167 441d5327 Adam Menz
        private ArrayList<Account> readAccounts(){
168 441d5327 Adam Menz
                FileInputStream fis;
169 441d5327 Adam Menz
                ObjectInputStream in;
170 441d5327 Adam Menz
                try {
171 441d5327 Adam Menz
                        fis = openFileInput(FILENAME);
172 441d5327 Adam Menz
                        in = new ObjectInputStream(fis);
173 bf03b262 Adam Menz
                        @SuppressWarnings("unchecked")
174 441d5327 Adam Menz
                        ArrayList<Account> file = (ArrayList<Account>)in.readObject();
175 441d5327 Adam Menz
                        in.close();
176 b722cab3 Adam Menz
                        return file; 
177 441d5327 Adam Menz
                } catch (FileNotFoundException e) {
178 d88d3ae1 Adam Menz
                        //showAlert("Error", "Could not load accounts.");
179 441d5327 Adam Menz
                        e.printStackTrace();
180 441d5327 Adam Menz
                        return null;
181 441d5327 Adam Menz
                } catch (StreamCorruptedException e) {
182 d88d3ae1 Adam Menz
                        showAlert("Error", "Could not load accounts.");
183 441d5327 Adam Menz
                        e.printStackTrace();
184 441d5327 Adam Menz
                } catch (IOException e) {
185 d88d3ae1 Adam Menz
                        showAlert("Error", "Could not load accounts.");
186 441d5327 Adam Menz
                        e.printStackTrace();
187 441d5327 Adam Menz
                } catch (ClassNotFoundException e) {
188 d88d3ae1 Adam Menz
                        showAlert("Error", "Could not load accounts.");
189 441d5327 Adam Menz
                        e.printStackTrace();
190 441d5327 Adam Menz
                }
191 441d5327 Adam Menz
                return null;
192 0edf6b39 Adam Menz
193 441d5327 Adam Menz
        }
194 441d5327 Adam Menz
195 441d5327 Adam Menz
        private void displayNoAccountsCell() {
196 0edf6b39 Adam Menz
                String a[] = new String[1];
197 0edf6b39 Adam Menz
                a[0] = "No Accounts";
198 0edf6b39 Adam Menz
                setListAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.noaccountscell, R.id.no_accounts_label, a));
199 0edf6b39 Adam Menz
                getListView().setTextFilterEnabled(true);
200 0edf6b39 Adam Menz
                getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row
201 0edf6b39 Adam Menz
                getListView().setItemsCanFocus(false);
202 0edf6b39 Adam Menz
        }
203 0edf6b39 Adam Menz
204 441d5327 Adam Menz
        protected void onListItemClick(ListView l, View v, int position, long id) {
205 730daee8 Adam Menz
                if (accounts != null && accounts.size() > 0) {
206 e7534f91 Adam Menz
                        //setActivityIndicatorsVisibility(View.VISIBLE, v);
207 730daee8 Adam Menz
                        Account.setAccount(accounts.get(position));
208 f3633a16 Adam Menz
                        Log.d("info", "the server is " + Account.getAccount().getAuthServerV2());
209 730daee8 Adam Menz
                        login();
210 730daee8 Adam Menz
                }                
211 0edf6b39 Adam Menz
        }
212 0edf6b39 Adam Menz
213 441d5327 Adam Menz
        public void login() {
214 0edf6b39 Adam Menz
                //showActivityIndicators();
215 0edf6b39 Adam Menz
                //setLoginPreferences();
216 0edf6b39 Adam Menz
                new AuthenticateTask().execute((Void[]) null);
217 0edf6b39 Adam Menz
        }
218 0edf6b39 Adam Menz
219 fea03400 Adam Menz
        //setup menu for when menu button is pressed
220 441d5327 Adam Menz
        public boolean onCreateOptionsMenu(Menu menu) {
221 441d5327 Adam Menz
                super.onCreateOptionsMenu(menu);
222 441d5327 Adam Menz
                MenuInflater inflater = getMenuInflater();
223 441d5327 Adam Menz
                inflater.inflate(R.menu.accounts_list_menu, menu);
224 441d5327 Adam Menz
                return true;
225 441d5327 Adam Menz
        } 
226 0edf6b39 Adam Menz
227 0edf6b39 Adam Menz
        @Override 
228 0edf6b39 Adam Menz
        //in options menu, when add account is selected go to add account activity
229 0edf6b39 Adam Menz
        public boolean onOptionsItemSelected(MenuItem item) {
230 0edf6b39 Adam Menz
                switch (item.getItemId()) {
231 0edf6b39 Adam Menz
                case R.id.add_account:
232 fbe4e332 koutsoub
                        Intent intent = new Intent(this, AddAccountActivity.class);
233 5d668a55 koutsoub
                        startActivityForResult(intent, 78); // arbitrary number; never used again
234 0edf6b39 Adam Menz
                        return true;
235 fbe4e332 koutsoub
                case R.id.login_pithos:
236 fbe4e332 koutsoub
                        Intent intent2 = new Intent(this, PithosLoginActivity.class);
237 fbe4e332 koutsoub
                        intent2.putExtra("login", "https://pithos.dev.grnet.gr/im/login");
238 fbe4e332 koutsoub
                        intent2.putExtra("auth", Preferences.PITHOS_DEV_SERVER);
239 fbe4e332 koutsoub
                        startActivityForResult(intent2, 78); // arbitrary number; never used again
240 fbe4e332 koutsoub
                        return true;
241 0edf6b39 Adam Menz
242 0edf6b39 Adam Menz
                case R.id.contact_rackspace:
243 0edf6b39 Adam Menz
                        startActivity(new Intent(this, ContactActivity.class));
244 0edf6b39 Adam Menz
                        return true;
245 0edf6b39 Adam Menz
246 0edf6b39 Adam Menz
                case R.id.add_password:
247 0edf6b39 Adam Menz
                        startActivity(new Intent(this, CreatePasswordActivity.class));
248 0edf6b39 Adam Menz
                        return true;
249 0edf6b39 Adam Menz
                }        
250 0edf6b39 Adam Menz
                return false;
251 0edf6b39 Adam Menz
        } 
252 0edf6b39 Adam Menz
253 0edf6b39 Adam Menz
        //the context menu for a long press on an account
254 fea03400 Adam Menz
        public void onCreateContextMenu(ContextMenu menu, View v,
255 fea03400 Adam Menz
                        ContextMenuInfo menuInfo) {
256 fea03400 Adam Menz
                super.onCreateContextMenu(menu, v, menuInfo);
257 fea03400 Adam Menz
                MenuInflater inflater = getMenuInflater();
258 fea03400 Adam Menz
                inflater.inflate(R.menu.account_context_menu, menu);
259 fea03400 Adam Menz
        }
260 fea03400 Adam Menz
261 fea03400 Adam Menz
        //removes the selected account from account list if remove is clicked
262 fea03400 Adam Menz
        public boolean onContextItemSelected(MenuItem item) {
263 0ddfaa70 Matt Lee
                if (accounts.size() == 0) {
264 0ddfaa70 Matt Lee
                        displayNoAccountsCell();
265 0ddfaa70 Matt Lee
                        return true;
266 0ddfaa70 Matt Lee
                } else {
267 0ddfaa70 Matt Lee
                        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
268 0ddfaa70 Matt Lee
                        accounts.remove(info.position);
269 0ddfaa70 Matt Lee
                        writeAccounts();
270 0ddfaa70 Matt Lee
                        loadAccounts();
271 0ddfaa70 Matt Lee
                        return true;
272 0ddfaa70 Matt Lee
                }
273 fea03400 Adam Menz
        }
274 fea03400 Adam Menz
275 441d5327 Adam Menz
        class AccountAdapter extends ArrayAdapter<Account> {
276 441d5327 Adam Menz
277 441d5327 Adam Menz
                AccountAdapter() {
278 441d5327 Adam Menz
                        super(ListAccountsActivity.this, R.layout.listaccountcell, accounts);
279 441d5327 Adam Menz
                }
280 0edf6b39 Adam Menz
281 441d5327 Adam Menz
                public View getView(int position, View convertView, ViewGroup parent) {
282 0edf6b39 Adam Menz
283 441d5327 Adam Menz
                        LayoutInflater inflater = getLayoutInflater();
284 441d5327 Adam Menz
                        View row = inflater.inflate(R.layout.listaccountcell, parent, false);
285 441d5327 Adam Menz
286 441d5327 Adam Menz
                        TextView label = (TextView) row.findViewById(R.id.label);
287 441d5327 Adam Menz
                        label.setText(accounts.get(position).getUsername());
288 0edf6b39 Adam Menz
289 441d5327 Adam Menz
                        TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
290 441d5327 Adam Menz
                        sublabel.setText(getAccountServer(accounts.get(position)));
291 0edf6b39 Adam Menz
292 441d5327 Adam Menz
                        ImageView icon = (ImageView) row.findViewById(R.id.account_type_icon);
293 d88d3ae1 Adam Menz
                        icon.setImageResource(setAccountIcon(accounts.get(position)));
294 0edf6b39 Adam Menz
295 441d5327 Adam Menz
                        return row;
296 441d5327 Adam Menz
                }
297 441d5327 Adam Menz
        }
298 0edf6b39 Adam Menz
299 441d5327 Adam Menz
        public String getAccountServer(Account account){
300 441d5327 Adam Menz
                String authServer = account.getAuthServer();
301 f3633a16 Adam Menz
                if(authServer == null){
302 f3633a16 Adam Menz
                        authServer = account.getAuthServerV2();
303 f3633a16 Adam Menz
                }
304 d88d3ae1 Adam Menz
                String result;
305 f3633a16 Adam Menz
                                
306 f3633a16 Adam Menz
                if(authServer.equals(Preferences.COUNTRY_UK_AUTH_SERVER) || authServer.equals(Preferences.COUNTRY_UK_AUTH_SERVER_V2)){
307 d88d3ae1 Adam Menz
                        result = "Rackspace Cloud (UK)";
308 d88d3ae1 Adam Menz
                }
309 f3633a16 Adam Menz
                else if(authServer.equals(Preferences.COUNTRY_US_AUTH_SERVER) || authServer.equals(Preferences.COUNTRY_US_AUTH_SERVER_V2)){
310 d88d3ae1 Adam Menz
                        result = "Rackspace Cloud (US)";
311 441d5327 Adam Menz
                }
312 008fa428 koutsoub
                else if(authServer.equals(Preferences.PITHOS_SERVER)){
313 008fa428 koutsoub
                        result = "Pithos+";
314 008fa428 koutsoub
                }
315 008fa428 koutsoub
                else if(authServer.equals(Preferences.PITHOS_DEV_SERVER)){
316 008fa428 koutsoub
                        result = "Pithos+ Dev";
317 008fa428 koutsoub
                }
318 441d5327 Adam Menz
                else{
319 008fa428 koutsoub
                        result = "Custom:" +authServer;
320 d88d3ae1 Adam Menz
                        //setCustomIcon();
321 441d5327 Adam Menz
                }
322 441d5327 Adam Menz
                return result;
323 441d5327 Adam Menz
        }
324 0edf6b39 Adam Menz
325 d88d3ae1 Adam Menz
        //display rackspace logo for cloud accounts and openstack logo for others
326 d88d3ae1 Adam Menz
        private int setAccountIcon(Account account){
327 f3633a16 Adam Menz
                String authServer = account.getAuthServer();
328 f3633a16 Adam Menz
                if(authServer == null){
329 f3633a16 Adam Menz
                        authServer = account.getAuthServerV2();
330 f3633a16 Adam Menz
                }
331 f3633a16 Adam Menz
                if(authServer.equals(Preferences.COUNTRY_UK_AUTH_SERVER) 
332 f3633a16 Adam Menz
                                || authServer.equals(Preferences.COUNTRY_US_AUTH_SERVER)
333 f3633a16 Adam Menz
                                || authServer.equals(Preferences.COUNTRY_US_AUTH_SERVER_V2)
334 f3633a16 Adam Menz
                                                || authServer.equals(Preferences.COUNTRY_UK_AUTH_SERVER_V2)){
335 d88d3ae1 Adam Menz
                        return R.drawable.rackspacecloud_icon;
336 d88d3ae1 Adam Menz
                }
337 7dbfc514 koutsoub
                if(authServer.equals(Preferences.PITHOS_DEV_SERVER) 
338 7dbfc514 koutsoub
                                || authServer.equals(Preferences.PITHOS_SERVER)){
339 008fa428 koutsoub
                        return R.drawable.pithos_icon;
340 008fa428 koutsoub
                }
341 d88d3ae1 Adam Menz
                else{
342 d88d3ae1 Adam Menz
                        return R.drawable.openstack_icon;
343 d88d3ae1 Adam Menz
                }
344 d88d3ae1 Adam Menz
        }
345 441d5327 Adam Menz
346 441d5327 Adam Menz
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
347 730daee8 Adam Menz
                super.onActivityResult(requestCode, resultCode, data);
348 0edf6b39 Adam Menz
349 3e180b04 Adam Menz
                if(requestCode == 187){
350 c2b54e85 Adam Menz
                        hideAccountDialog(); 
351 3e180b04 Adam Menz
                }
352 0edf6b39 Adam Menz
353 730daee8 Adam Menz
                if (resultCode == RESULT_OK && requestCode == 78) {          
354 730daee8 Adam Menz
                        Account acc = new Account();
355 730daee8 Adam Menz
                        Bundle b = data.getBundleExtra("accountInfo");
356 f3633a16 Adam Menz
                        acc.setPassword(b.getString("apiKey"));
357 730daee8 Adam Menz
                        acc.setUsername(b.getString("username"));
358 9c74202f koutsoub
                        acc.setAuthServer(b.getString("server"));
359 f3633a16 Adam Menz
                        Log.d("info", "the set server was " + b.getString("server"));
360 9c74202f koutsoub
                        Log.d("info", "the server is " + acc.getAuthServer());
361 5d668a55 koutsoub
                        boolean found = false;
362 5d668a55 koutsoub
                        for(Account a : accounts){
363 5d668a55 koutsoub
                                if(a.getUsername().equals(acc.getUsername())&&a.getAuthServer().equals(acc.getAuthServer())){
364 5d668a55 koutsoub
                                        a.setPassword(acc.getPassword());
365 5d668a55 koutsoub
                                        found=true;
366 5d668a55 koutsoub
                                }
367 5d668a55 koutsoub
                        }
368 5d668a55 koutsoub
                        if(!found)
369 5d668a55 koutsoub
                                accounts.add(acc);
370 730daee8 Adam Menz
                        writeAccounts();
371 730daee8 Adam Menz
                        loadAccounts();
372 730daee8 Adam Menz
                }
373 441d5327 Adam Menz
        }        
374 0edf6b39 Adam Menz
375 c2b54e85 Adam Menz
        private void showAccountDialog() {
376 54986b23 Adam Menz
                app.setIsLoggingIn(true);
377 e7534f91 Adam Menz
                authenticating = true;
378 3e180b04 Adam Menz
                if(dialog == null || !dialog.isShowing()){
379 b2a2d2f1 Adam Menz
                        dialog = new ProgressDialog(this);
380 b2a2d2f1 Adam Menz
                        dialog.setProgressStyle(R.style.NewDialog);
381 54986b23 Adam Menz
                        dialog.setOnCancelListener(new OnCancelListener() {
382 54986b23 Adam Menz
383 54986b23 Adam Menz
                                @Override
384 54986b23 Adam Menz
                                public void onCancel(DialogInterface dialog) {
385 54986b23 Adam Menz
                                        app.setIsLoggingIn(false);
386 16615abd Adam Menz
                                        //need to cancel the old task or we may get a double login
387 16615abd Adam Menz
                                        task.cancel(true);
388 c2b54e85 Adam Menz
                                        hideAccountDialog();
389 54986b23 Adam Menz
                                }
390 54986b23 Adam Menz
                        });
391 b2a2d2f1 Adam Menz
                        dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
392 b2a2d2f1 Adam Menz
                        dialog.show();
393 b2a2d2f1 Adam Menz
                        dialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
394 3e180b04 Adam Menz
                }
395 b2a2d2f1 Adam Menz
396 b2a2d2f1 Adam Menz
                
397 54986b23 Adam Menz
        }
398 54986b23 Adam Menz
399 c2b54e85 Adam Menz
        private void hideAccountDialog() {
400 54986b23 Adam Menz
                if(dialog != null){
401 54986b23 Adam Menz
                        dialog.dismiss();
402 3e180b04 Adam Menz
                }
403 54986b23 Adam Menz
                authenticating = false;
404 54986b23 Adam Menz
        }
405 e7534f91 Adam Menz
406 441d5327 Adam Menz
        private class AuthenticateTask extends AsyncTask<Void, Void, Boolean> {
407 0edf6b39 Adam Menz
408 441d5327 Adam Menz
                @Override
409 e7534f91 Adam Menz
                protected void onPreExecute(){
410 16615abd Adam Menz
                        Log.d("info", "Starting authenticate");
411 16615abd Adam Menz
                        task = this;
412 c2b54e85 Adam Menz
                        showAccountDialog();
413 e7534f91 Adam Menz
                }
414 0edf6b39 Adam Menz
415 e7534f91 Adam Menz
                @Override
416 441d5327 Adam Menz
                protected Boolean doInBackground(Void... arg0) {
417 f3633a16 Adam Menz
                        try {
418 f3633a16 Adam Menz
                                return new Boolean(Authentication.authenticate(context));
419 f3633a16 Adam Menz
                        } catch (CloudServersException e) {
420 f3633a16 Adam Menz
                                e.printStackTrace();
421 f3633a16 Adam Menz
                                return false;
422 f3633a16 Adam Menz
                        }
423 441d5327 Adam Menz
                }
424 0edf6b39 Adam Menz
425 441d5327 Adam Menz
                @Override
426 441d5327 Adam Menz
                protected void onPostExecute(Boolean result) {
427 441d5327 Adam Menz
                        if (result.booleanValue()) {
428 441d5327 Adam Menz
                                //startActivity(tabViewIntent);
429 d81b2ba1 koutsoub
                                /*if(app.isLoggingIn()){
430 54986b23 Adam Menz
                                        new LoadImagesTask().execute((Void[]) null);
431 54986b23 Adam Menz
                                } else {
432 c2b54e85 Adam Menz
                                        hideAccountDialog();
433 d81b2ba1 koutsoub
                                }*/
434 d81b2ba1 koutsoub
                                hideAccountDialog();
435 d81b2ba1 koutsoub
                                if(app.isLoggingIn()){
436 d81b2ba1 koutsoub
                                        startActivityForResult(tabViewIntent, 187);
437 54986b23 Adam Menz
                                }
438 441d5327 Adam Menz
                        } else {
439 c2b54e85 Adam Menz
                                hideAccountDialog();
440 16615abd Adam Menz
                                if(app.isLoggingIn()){
441 16615abd Adam Menz
                                        showAlert("Login Failure", "Authentication failed.  Please check your User Name and API Key.");
442 16615abd Adam Menz
                                }
443 441d5327 Adam Menz
                        }
444 441d5327 Adam Menz
                }
445 0edf6b39 Adam Menz
        }
446 0edf6b39 Adam Menz
447 fbe9be48 koutsoub
        
448 0edf6b39 Adam Menz
449 0edf6b39 Adam Menz
        }