src/org/gege/caldavsyncadapter/authenticator/AuthenticatorActivity.java

changeset 8
ec8af0e3fbc2
parent 0
fb9019fb1bf7
child 9
bcc778a42b8c
equal deleted inserted replaced
0:083b54c95d9d 1:bc172d9c65b7
1 /** 1 /**
2 * Copyright (c) 2012-2013, Gerald Garcia 2 * Copyright (c) 2012-2013, Gerald Garcia
3 * 3 *
4 * This file is part of Andoid Caldav Sync Adapter Free. 4 * This file is part of Andoid Caldav Sync Adapter Free.
5 * 5 *
6 * Andoid Caldav Sync Adapter Free is free software: you can redistribute 6 * Andoid Caldav Sync Adapter Free is free software: you can redistribute
7 * it and/or modify it under the terms of the GNU General Public License 7 * it and/or modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation, either version 3 of the 8 * as published by the Free Software Foundation, either version 3 of the
14 * GNU General Public License for more details. 14 * GNU General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the GNU General Public License
17 * along with Andoid Caldav Sync Adapter Free. 17 * along with Andoid Caldav Sync Adapter Free.
18 * If not, see <http://www.gnu.org/licenses/>. 18 * If not, see <http://www.gnu.org/licenses/>.
19 * 19 *
20 */ 20 */
21 21
22 package org.gege.caldavsyncadapter.authenticator; 22 package org.gege.caldavsyncadapter.authenticator;
23
24 import java.io.IOException;
25 import java.io.UnsupportedEncodingException;
26 import java.net.MalformedURLException;
27 import java.net.URISyntaxException;
28
29 import javax.xml.parsers.ParserConfigurationException;
30
31 import org.apache.http.conn.HttpHostConnectException;
32 import org.gege.caldavsyncadapter.R;
33 import org.gege.caldavsyncadapter.caldav.CaldavFacade;
34 import org.gege.caldavsyncadapter.caldav.CaldavFacade.TestConnectionResult;
35 import org.xml.sax.SAXException;
36 23
37 import android.accounts.Account; 24 import android.accounts.Account;
38 import android.accounts.AccountManager; 25 import android.accounts.AccountManager;
39 import android.animation.Animator; 26 import android.animation.Animator;
40 import android.animation.AnimatorListenerAdapter; 27 import android.animation.AnimatorListenerAdapter;
43 import android.content.Context; 30 import android.content.Context;
44 import android.content.pm.PackageManager.NameNotFoundException; 31 import android.content.pm.PackageManager.NameNotFoundException;
45 import android.os.AsyncTask; 32 import android.os.AsyncTask;
46 import android.os.Build; 33 import android.os.Build;
47 import android.os.Bundle; 34 import android.os.Bundle;
35 import android.text.Editable;
48 import android.text.TextUtils; 36 import android.text.TextUtils;
37 import android.text.TextWatcher;
49 import android.util.Log; 38 import android.util.Log;
50 import android.view.KeyEvent; 39 import android.view.KeyEvent;
51 import android.view.Menu; 40 import android.view.Menu;
52 import android.view.View; 41 import android.view.View;
53 import android.view.inputmethod.EditorInfo; 42 import android.view.inputmethod.EditorInfo;
43 import android.widget.CheckBox;
54 import android.widget.EditText; 44 import android.widget.EditText;
55 import android.widget.TextView; 45 import android.widget.TextView;
56 import android.widget.Toast; 46 import android.widget.Toast;
47
48 import org.apache.http.conn.HttpHostConnectException;
49 import org.gege.caldavsyncadapter.Constants;
50 import org.gege.caldavsyncadapter.R;
51 import org.gege.caldavsyncadapter.caldav.CaldavFacade;
52 import org.gege.caldavsyncadapter.caldav.CaldavFacade.TestConnectionResult;
53 import org.xml.sax.SAXException;
54
55 import java.io.IOException;
56 import java.io.UnsupportedEncodingException;
57 import java.net.MalformedURLException;
58 import java.net.URISyntaxException;
59 import java.util.Locale;
60
61 import javax.xml.parsers.ParserConfigurationException;
57 62
58 /** 63 /**
59 * Activity which displays a login screen to the user, offering registration as 64 * Activity which displays a login screen to the user, offering registration as
60 * well. 65 * well.
61 */ 66 */
62 public class AuthenticatorActivity extends Activity { 67 public class AuthenticatorActivity extends Activity {
63 68
64 private static final String TAG = "AuthenticatorActivity"; 69 private static final String TAG = "AuthenticatorActivity";
65 70
66 private static final String ACCOUNT_TYPE = "org.gege.caldavsyncadapter.account"; 71 private static final String ACCOUNT_TYPE = "org.gege.caldavsyncadapter.account";
67 72
68 public static final String USER_DATA_URL_KEY = "USER_DATA_URL_KEY"; 73 public static final String USER_DATA_URL_KEY = "USER_DATA_URL_KEY";
69 public static final String USER_DATA_USERNAME = "USER_DATA_USERNAME"; 74 public static final String USER_DATA_USERNAME = "USER_DATA_USERNAME";
70 public static final String USER_DATA_VERSION = "USER_DATA_VERSION"; 75 public static final String USER_DATA_VERSION = "USER_DATA_VERSION";
71 public static final String CURRENT_USER_DATA_VERSION = "1"; 76 public static final String CURRENT_USER_DATA_VERSION = "1";
72 77
73 public static final String ACCOUNT_NAME_SPLITTER = "@"; 78 public static final String ACCOUNT_NAME_SPLITTER = "@";
74 79
75 /** 80 /**
76 * The default email to populate the email field with. 81 * The default email to populate the email field with.
77 */ 82 */
78 public static final String EXTRA_EMAIL = "com.example.android.authenticatordemo.extra.EMAIL"; 83 public static final String EXTRA_EMAIL = "com.example.android.authenticatordemo.extra.EMAIL";
79 84
80 /** 85 /**
81 * Keep track of the login task to ensure we can cancel it if requested. 86 * Keep track of the login task to ensure we can cancel it if requested.
82 */ 87 */
83 private UserLoginTask mAuthTask = null; 88 private UserLoginTask mAuthTask = null;
84 89
85 // Values for email and password at the time of the login attempt. 90 // Values for email and password at the time of the login attempt.
86 private String mUser; 91 private String mUser;
87 private String mPassword; 92 private String mPassword;
88 private Context mContext; 93 private String mTrustAll;
89 94 private Context mContext;
90 // UI references. 95
91 private EditText mUserView; 96 // UI references.
92 private EditText mPasswordView; 97 private EditText mUserView;
93 private View mLoginFormView; 98 private EditText mPasswordView;
94 private View mLoginStatusView; 99 private View mLoginFormView;
95 private TextView mLoginStatusMessageView; 100 private View mLoginStatusView;
96 101 private TextView mLoginStatusMessageView;
97 private AccountManager mAccountManager; 102 private CheckBox mTrustCheckBox;
98 103
99 private String mURL; 104 private AccountManager mAccountManager;
100 private EditText mURLView; 105
101 106 private String mURL;
102 private String mAccountname; 107 private EditText mURLView;
103 private EditText mAccountnameView; 108
104 109 private String mAccountname;
105 public AuthenticatorActivity() { 110 private EditText mAccountnameView;
106 super(); 111
107 112 public AuthenticatorActivity() {
108 } 113 super();
109 114
110 @Override 115 }
111 protected void onCreate(Bundle savedInstanceState) { 116
112 super.onCreate(savedInstanceState); 117 @Override
113 118 protected void onCreate(Bundle savedInstanceState) {
114 mAccountManager = AccountManager.get(this); 119 super.onCreate(savedInstanceState);
115 120
116 setContentView(R.layout.activity_authenticator); 121 mAccountManager = AccountManager.get(this);
117 122
118 // Set up the login form. 123 setContentView(R.layout.activity_authenticator);
119 mUser = getIntent().getStringExtra(EXTRA_EMAIL); 124
120 mUserView = (EditText) findViewById(R.id.user); 125 // Set up the login form.
121 mUserView.setText(mUser); 126 mUser = getIntent().getStringExtra(EXTRA_EMAIL);
122 127 mUserView = (EditText) findViewById(R.id.user);
123 mContext = getBaseContext(); 128 mUserView.setText(mUser);
124 129
125 mPasswordView = (EditText) findViewById(R.id.password); 130 mContext = getBaseContext();
126 mPasswordView 131
127 .setOnEditorActionListener(new TextView.OnEditorActionListener() { 132 mPasswordView = (EditText) findViewById(R.id.password);
128 @Override 133 mPasswordView
129 public boolean onEditorAction(TextView textView, int id, 134 .setOnEditorActionListener(new TextView.OnEditorActionListener() {
130 KeyEvent keyEvent) { 135 @Override
131 if (id == R.id.login || id == EditorInfo.IME_NULL) { 136 public boolean onEditorAction(TextView textView, int id,
132 attemptLogin(); 137 KeyEvent keyEvent) {
133 return true; 138 if (id == R.id.login || id == EditorInfo.IME_NULL) {
134 } 139 attemptLogin();
135 return false; 140 return true;
136 } 141 }
137 }); 142 return false;
138 143 }
139 144 });
140 mURLView = (EditText) findViewById(R.id.url); 145
141 146
142 mAccountnameView = (EditText) findViewById(R.id.accountname); 147 mURLView = (EditText) findViewById(R.id.url);
143 148 // if the URL start with "https" show the option to disable SSL host verification
144 mLoginFormView = findViewById(R.id.login_form); 149 mURLView.addTextChangedListener(new TextWatcher() {
145 mLoginStatusView = findViewById(R.id.login_status); 150 @Override
146 mLoginStatusMessageView = (TextView) findViewById(R.id.login_status_message); 151 public void onTextChanged(CharSequence s, int start, int before, int count) {
147 152 String url = ((EditText) findViewById(R.id.url)).getText().toString();
148 findViewById(R.id.sign_in_button).setOnClickListener( 153 int visible = url.toLowerCase(Locale.getDefault())
149 new View.OnClickListener() { 154 .startsWith("https") ? View.VISIBLE : View.GONE;
150 @Override 155 ((CheckBox) findViewById(R.id.trustall)).setVisibility(visible);
151 public void onClick(View view) { 156 }
152 attemptLogin(); 157
153 } 158 @Override
154 }); 159 public void beforeTextChanged(CharSequence s, int start, int count,
155 160 int after) {
156 161 }
157 } 162
158 163 @Override
159 @Override 164 public void afterTextChanged(Editable s) {
160 public boolean onCreateOptionsMenu(Menu menu) { 165 }
161 super.onCreateOptionsMenu(menu); 166 });
162 getMenuInflater().inflate(R.menu.activity_authenticator, menu); 167
163 return true; 168 mAccountnameView = (EditText) findViewById(R.id.accountname);
164 } 169
165 170 mLoginFormView = findViewById(R.id.login_form);
166 /** 171 mLoginStatusView = findViewById(R.id.login_status);
167 * Attempts to sign in or register the account specified by the login form. 172 mLoginStatusMessageView = (TextView) findViewById(R.id.login_status_message);
168 * If there are form errors (invalid email, missing fields, etc.), the 173
169 * errors are presented and no actual login attempt is made. 174 findViewById(R.id.sign_in_button).setOnClickListener(
170 */ 175 new View.OnClickListener() {
171 public void attemptLogin() { 176 @Override
172 if (mAuthTask != null) { 177 public void onClick(View view) {
173 return; 178 attemptLogin();
174 } 179 }
175 180 }
176 // Reset errors. 181 );
177 mUserView.setError(null); 182
178 mPasswordView.setError(null); 183 mTrustCheckBox = (CheckBox) findViewById(R.id.trustall);
179 184
180 // Store values at the time of the login attempt. 185
181 mUser = mUserView.getText().toString(); 186 }
182 mPassword = mPasswordView.getText().toString(); 187
183 mURL = mURLView.getText().toString(); 188 @Override
184 mAccountname = mAccountnameView.getText().toString(); 189 public boolean onCreateOptionsMenu(Menu menu) {
185 190 super.onCreateOptionsMenu(menu);
186 boolean cancel = false; 191 return true;
187 View focusView = null; 192 }
188 193
189 if (!mAccountname.equals("")) { 194 /**
190 Account TestAccount = new Account(mAccountname, ACCOUNT_TYPE); 195 * Attempts to sign in or register the account specified by the login form.
191 String TestUrl = mAccountManager.getUserData(TestAccount, AuthenticatorActivity.USER_DATA_URL_KEY); 196 * If there are form errors (invalid email, missing fields, etc.), the
192 if (TestUrl != null) { 197 * errors are presented and no actual login attempt is made.
193 mAccountnameView.setError(getString(R.string.error_account_already_in_use)); 198 */
194 focusView = mAccountnameView; 199 public void attemptLogin() {
195 cancel = true; 200 if (mAuthTask != null) {
196 } 201 return;
197 } 202 }
198 203
199 // Check for a valid password. 204 // Reset errors.
200 if (TextUtils.isEmpty(mPassword)) { 205 mUserView.setError(null);
201 mPasswordView.setError(getString(R.string.error_field_required)); 206 mPasswordView.setError(null);
202 focusView = mPasswordView; 207
203 cancel = true; 208 // Store values at the time of the login attempt.
204 } else if (mPassword.length() < 4) { 209 mUser = mUserView.getText().toString();
205 mPasswordView.setError(getString(R.string.error_invalid_password)); 210 mPassword = mPasswordView.getText().toString();
206 focusView = mPasswordView; 211 mURL = mURLView.getText().toString();
207 cancel = true; 212 mAccountname = mAccountnameView.getText().toString();
208 } 213 mTrustAll = (mTrustCheckBox.isChecked() ? "false" : "true");
209 214 boolean cancel = false;
210 // Check for a valid email address. 215 View focusView = null;
211 if (TextUtils.isEmpty(mUser)) { 216
212 mUserView.setError(getString(R.string.error_field_required)); 217 if (!mAccountname.equals("")) {
213 focusView = mUserView; 218 Account TestAccount = new Account(mAccountname, ACCOUNT_TYPE);
214 cancel = true; 219 String TestUrl = mAccountManager.getUserData(TestAccount, AuthenticatorActivity.USER_DATA_URL_KEY);
215 } 220 if (TestUrl != null) {
216 //else if (!mUser.contains("@")) { 221 mAccountnameView.setError(getString(R.string.error_account_already_in_use));
217 // mUserView.setError(getString(R.string.error_invalid_email)); 222 focusView = mAccountnameView;
218 // focusView = mUserView; 223 cancel = true;
219 // cancel = true; 224 }
220 //} 225 }
221 226
222 if (cancel) { 227 // Check for a valid password.
223 // There was an error; don't attempt login and focus the first 228 if (TextUtils.isEmpty(mPassword)) {
224 // form field with an error. 229 mPasswordView.setError(getString(R.string.error_field_required));
225 focusView.requestFocus(); 230 focusView = mPasswordView;
226 } else { 231 cancel = true;
227 // Show a progress spinner, and kick off a background task to 232 }
228 // perform the user login attempt. 233
229 mLoginStatusMessageView.setText(R.string.login_progress_signing_in); 234 // Check for a valid email address.
230 showProgress(true); 235 if (TextUtils.isEmpty(mUser)) {
231 mAuthTask = new UserLoginTask(); 236 mUserView.setError(getString(R.string.error_field_required));
232 mAuthTask.execute((Void) null); 237 focusView = mUserView;
233 } 238 cancel = true;
234 } 239 }
235 240 //else if (!mUser.contains("@")) {
236 /** 241 // mUserView.setError(getString(R.string.error_invalid_email));
237 * Shows the progress UI and hides the login form. 242 // focusView = mUserView;
238 */ 243 // cancel = true;
239 @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) 244 //}
240 private void showProgress(final boolean show) { 245
241 // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow 246 if (cancel) {
242 // for very easy animations. If available, use these APIs to fade-in 247 // There was an error; don't attempt login and focus the first
243 // the progress spinner. 248 // form field with an error.
244 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { 249 focusView.requestFocus();
245 int shortAnimTime = getResources().getInteger( 250 } else {
246 android.R.integer.config_shortAnimTime); 251 // Show a progress spinner, and kick off a background task to
247 252 // perform the user login attempt.
248 mLoginStatusView.setVisibility(View.VISIBLE); 253 mLoginStatusMessageView.setText(R.string.login_progress_signing_in);
249 mLoginStatusView.animate().setDuration(shortAnimTime) 254 showProgress(true);
250 .alpha(show ? 1 : 0) 255 mAuthTask = new UserLoginTask();
251 .setListener(new AnimatorListenerAdapter() { 256 mAuthTask.execute((Void) null);
252 @Override 257 }
253 public void onAnimationEnd(Animator animation) { 258 }
254 mLoginStatusView.setVisibility(show ? View.VISIBLE 259
255 : View.GONE); 260 /**
256 } 261 * Shows the progress UI and hides the login form.
257 }); 262 */
258 263 @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
259 mLoginFormView.setVisibility(View.VISIBLE); 264 private void showProgress(final boolean show) {
260 mLoginFormView.animate().setDuration(shortAnimTime) 265 // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
261 .alpha(show ? 0 : 1) 266 // for very easy animations. If available, use these APIs to fade-in
262 .setListener(new AnimatorListenerAdapter() { 267 // the progress spinner.
263 @Override 268 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
264 public void onAnimationEnd(Animator animation) { 269 int shortAnimTime = getResources().getInteger(
265 mLoginFormView.setVisibility(show ? View.GONE 270 android.R.integer.config_shortAnimTime);
266 : View.VISIBLE); 271
267 } 272 mLoginStatusView.setVisibility(View.VISIBLE);
268 }); 273 mLoginStatusView.animate().setDuration(shortAnimTime)
269 } else { 274 .alpha(show ? 1 : 0)
270 // The ViewPropertyAnimator APIs are not available, so simply show 275 .setListener(new AnimatorListenerAdapter() {
271 // and hide the relevant UI components. 276 @Override
272 mLoginStatusView.setVisibility(show ? View.VISIBLE : View.GONE); 277 public void onAnimationEnd(Animator animation) {
273 mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE); 278 mLoginStatusView.setVisibility(show ? View.VISIBLE
274 } 279 : View.GONE);
275 } 280 }
276 281 });
277 282
278 protected enum LoginResult { 283 mLoginFormView.setVisibility(View.VISIBLE);
279 MalformedURLException, 284 mLoginFormView.animate().setDuration(shortAnimTime)
280 GeneralSecurityException, 285 .alpha(show ? 0 : 1)
281 UnkonwnException, 286 .setListener(new AnimatorListenerAdapter() {
282 WrongCredentials, 287 @Override
283 InvalidResponse, 288 public void onAnimationEnd(Animator animation) {
284 WrongUrl, 289 mLoginFormView.setVisibility(show ? View.GONE
285 ConnectionRefused, 290 : View.VISIBLE);
286 Success_Calendar, 291 }
287 Success_Collection, 292 });
288 Account_Already_In_Use 293 } else {
289 } 294 // The ViewPropertyAnimator APIs are not available, so simply show
290 295 // and hide the relevant UI components.
291 296 mLoginStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
292 /** 297 mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
293 * Represents an asynchronous login/registration task used to authenticate 298 }
294 * the user. 299 }
295 */ 300
296 public class UserLoginTask extends AsyncTask<Void, Void, LoginResult> { 301
297 302 protected enum LoginResult {
298 @Override 303 MalformedURLException,
299 protected LoginResult doInBackground(Void... params) { 304 GeneralSecurityException,
300 305 UnkonwnException,
301 TestConnectionResult result = null; 306 WrongCredentials,
302 307 InvalidResponse,
303 try { 308 WrongUrl,
304 CaldavFacade facade = new CaldavFacade(mUser, mPassword, mURL); 309 ConnectionRefused,
305 String version = ""; 310 Success_Calendar,
306 try { 311 Success_Collection,
307 version = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionName; 312 UNTRUSTED_CERT,
308 } catch (NameNotFoundException e) { 313 Account_Already_In_Use
309 version = "unknown"; 314 }
310 e.printStackTrace(); 315
311 } 316
312 facade.setVersion(version); 317 /**
313 result = facade.testConnection(); 318 * Represents an asynchronous login/registration task used to authenticate
314 Log.i(TAG, "testConnection status="+result); 319 * the user.
315 } catch (HttpHostConnectException e) { 320 */
316 Log.w(TAG,"testConnection", e); 321 public class UserLoginTask extends AsyncTask<Void, Void, LoginResult> {
317 return LoginResult.ConnectionRefused; 322
318 } catch (MalformedURLException e) { 323 @Override
319 Log.w(TAG,"testConnection", e); 324 protected LoginResult doInBackground(Void... params) {
320 return LoginResult.MalformedURLException; 325
321 } catch (UnsupportedEncodingException e) { 326 TestConnectionResult result = null;
322 Log.w(TAG,"testConnection", e); 327
323 return LoginResult.UnkonwnException; 328 try {
324 } catch (ParserConfigurationException e) { 329 CaldavFacade facade = new CaldavFacade(mUser, mPassword, mURL, mTrustAll);
325 Log.w(TAG,"testConnection", e); 330 String version = "";
326 return LoginResult.UnkonwnException; 331 try {
327 } catch (SAXException e) { 332 version = mContext.getPackageManager()
328 Log.w(TAG,"testConnection", e); 333 .getPackageInfo(mContext.getPackageName(), 0).versionName;
329 return LoginResult.InvalidResponse; 334 } catch (NameNotFoundException e) {
330 } catch (IOException e) { 335 version = "unknown";
331 Log.w(TAG,"testConnection", e); 336 e.printStackTrace();
332 return LoginResult.UnkonwnException; 337 }
333 } catch (URISyntaxException e) { 338 facade.setVersion(version);
334 Log.w(TAG,"testConnection", e); 339 result = facade.testConnection();
335 return LoginResult.MalformedURLException; 340 Log.i(TAG, "testConnection status=" + result);
336 } 341 } catch (HttpHostConnectException e) {
337 342 Log.w(TAG, "testConnection", e);
338 if (result == null) { 343 return LoginResult.ConnectionRefused;
339 return LoginResult.UnkonwnException; 344 } catch (MalformedURLException e) {
340 } 345 Log.w(TAG, "testConnection", e);
341 346 return LoginResult.MalformedURLException;
342 switch (result) { 347 } catch (UnsupportedEncodingException e) {
343 348 Log.w(TAG, "testConnection", e);
344 case SUCCESS: 349 return LoginResult.UnkonwnException;
345 boolean OldAccount = false; 350 } catch (ParserConfigurationException e) {
346 LoginResult Result = LoginResult.Success_Calendar; 351 Log.w(TAG, "testConnection", e);
347 352 return LoginResult.UnkonwnException;
348 if (OldAccount) { 353 } catch (SAXException e) {
349 final Account account = new Account(mUser, ACCOUNT_TYPE); 354 Log.w(TAG, "testConnection", e);
350 if (mAccountManager.addAccountExplicitly(account, mPassword, null)) { 355 return LoginResult.InvalidResponse;
351 Log.v(TAG,"new account created"); 356 } catch (IOException e) {
352 mAccountManager.setUserData(account, USER_DATA_URL_KEY, mURL); 357 Log.w(TAG, "testConnection", e);
353 } else { 358 return LoginResult.UnkonwnException;
354 Log.v(TAG,"no new account created"); 359 } catch (URISyntaxException e) {
355 Result = LoginResult.Account_Already_In_Use; 360 Log.w(TAG, "testConnection", e);
356 } 361 return LoginResult.MalformedURLException;
357 } else { 362 }
358 final Account account; 363
359 if (mAccountname.equals("")) { 364 if (result == null) {
360 account = new Account(mUser + ACCOUNT_NAME_SPLITTER + mURL, ACCOUNT_TYPE); 365 return LoginResult.UnkonwnException;
361 } else { 366 }
362 account = new Account(mAccountname, ACCOUNT_TYPE); 367
363 } 368 switch (result) {
364 if (mAccountManager.addAccountExplicitly(account, mPassword, null)) { 369
365 Log.v(TAG,"new account created"); 370 case SSL_ERROR:
366 mAccountManager.setUserData(account, USER_DATA_URL_KEY, mURL); 371 return LoginResult.UNTRUSTED_CERT;
367 mAccountManager.setUserData(account, USER_DATA_USERNAME, mUser); 372 case SUCCESS:
368 mAccountManager.setUserData(account, USER_DATA_VERSION, CURRENT_USER_DATA_VERSION); 373 boolean OldAccount = false;
369 } else { 374 LoginResult Result = LoginResult.Success_Calendar;
370 Log.v(TAG,"no new account created"); 375
371 Result = LoginResult.Account_Already_In_Use; 376 if (OldAccount) {
372 } 377 final Account account = new Account(mUser, ACCOUNT_TYPE);
373 } 378 if (mAccountManager.addAccountExplicitly(account, mPassword, null)) {
374 379 Log.v(TAG, "new account created");
375 return Result; 380 mAccountManager.setUserData(account, USER_DATA_URL_KEY, mURL);
376 381 } else {
377 case WRONG_CREDENTIAL: 382 Log.v(TAG, "no new account created");
378 return LoginResult.WrongCredentials; 383 Result = LoginResult.Account_Already_In_Use;
379 384 }
380 case WRONG_SERVER_STATUS: 385 } else {
381 return LoginResult.InvalidResponse; 386 final Account account;
382 387 if (mAccountname.equals("")) {
383 case WRONG_URL: 388 account = new Account(mUser + ACCOUNT_NAME_SPLITTER + mURL, ACCOUNT_TYPE);
384 return LoginResult.WrongUrl; 389 } else {
385 390 account = new Account(mAccountname, ACCOUNT_TYPE);
386 case WRONG_ANSWER: 391 }
387 return LoginResult.InvalidResponse; 392 if (mAccountManager.addAccountExplicitly(account, mPassword, null)) {
388 393 Log.v(TAG, "new account created");
389 default: 394 mAccountManager.setUserData(account, USER_DATA_URL_KEY, mURL);
390 return LoginResult.UnkonwnException; 395 mAccountManager.setUserData(account, USER_DATA_USERNAME, mUser);
391 396 mAccountManager.setUserData(account, USER_DATA_VERSION, CURRENT_USER_DATA_VERSION);
392 } 397 mAccountManager.setUserData(account, Constants.USER_DATA_TRUST_ALL_KEY, mTrustAll);
393 398 } else {
394 } 399 Log.v(TAG, "no new account created");
395 400 Result = LoginResult.Account_Already_In_Use;
396 401 }
397 @Override 402 }
398 protected void onPostExecute(final LoginResult result) { 403
399 mAuthTask = null; 404 return Result;
400 showProgress(false); 405
401 406 case WRONG_CREDENTIAL:
402 int duration = Toast.LENGTH_SHORT; 407 return LoginResult.WrongCredentials;
403 Toast toast = null; 408
404 409 case WRONG_SERVER_STATUS:
405 switch (result) { 410 return LoginResult.InvalidResponse;
406 case Success_Calendar: 411
407 toast = Toast.makeText(getApplicationContext(), R.string.success_calendar, duration); 412 case WRONG_URL:
408 toast.show(); 413 return LoginResult.WrongUrl;
409 finish(); 414
410 break; 415 case WRONG_ANSWER:
411 416 return LoginResult.InvalidResponse;
412 case Success_Collection: 417
413 toast = Toast.makeText(getApplicationContext(), R.string.success_collection, duration); 418 default:
414 toast.show(); 419 return LoginResult.UnkonwnException;
415 finish(); 420
416 break; 421 }
417 422
418 case MalformedURLException: 423 }
419 424
420 toast = Toast.makeText(getApplicationContext(), R.string.error_incorrect_url_format, duration); 425
421 toast.show(); 426 @Override
422 mURLView.setError(getString(R.string.error_incorrect_url_format)); 427 protected void onPostExecute(final LoginResult result) {
423 mURLView.requestFocus(); 428 mAuthTask = null;
424 break; 429 showProgress(false);
425 case InvalidResponse: 430
426 toast = Toast.makeText(getApplicationContext(), R.string.error_invalid_server_answer, duration); 431 int duration = Toast.LENGTH_SHORT;
427 toast.show(); 432 Toast toast = null;
428 mURLView.setError(getString(R.string.error_invalid_server_answer)); 433
429 mURLView.requestFocus(); 434 switch (result) {
430 break; 435 case Success_Calendar:
431 case WrongUrl: 436 toast = Toast.makeText(getApplicationContext(), R.string.success_calendar, duration);
432 toast = Toast.makeText(getApplicationContext(), R.string.error_wrong_url, duration); 437 toast.show();
433 toast.show(); 438 finish();
434 mURLView.setError(getString(R.string.error_wrong_url)); 439 break;
435 mURLView.requestFocus(); 440
436 break; 441 case Success_Collection:
437 442 toast = Toast.makeText(getApplicationContext(), R.string.success_collection, duration);
438 case WrongCredentials: 443 toast.show();
439 mPasswordView.setError(getString(R.string.error_incorrect_password)); 444 finish();
440 mPasswordView.requestFocus(); 445 break;
441 break; 446
442 447 case MalformedURLException:
443 case ConnectionRefused: 448
444 toast = Toast.makeText(getApplicationContext(), R.string.error_connection_refused, duration); 449 toast = Toast.makeText(getApplicationContext(), R.string.error_incorrect_url_format, duration);
445 toast.show(); 450 toast.show();
446 mURLView.setError(getString(R.string.error_connection_refused)); 451 mURLView.setError(getString(R.string.error_incorrect_url_format));
447 mURLView.requestFocus(); 452 mURLView.requestFocus();
448 break; 453 break;
449 case Account_Already_In_Use: 454 case InvalidResponse:
450 toast = Toast.makeText(getApplicationContext(), R.string.error_account_already_in_use, duration); 455 toast = Toast.makeText(getApplicationContext(), R.string.error_invalid_server_answer, duration);
451 toast.show(); 456 toast.show();
452 mURLView.setError(getString(R.string.error_account_already_in_use)); 457 mURLView.setError(getString(R.string.error_invalid_server_answer));
453 mURLView.requestFocus(); 458 mURLView.requestFocus();
454 break; 459 break;
455 default: 460 case WrongUrl:
456 toast = Toast.makeText(getApplicationContext(), R.string.error_unkown_error, duration); 461 toast = Toast.makeText(getApplicationContext(), R.string.error_wrong_url, duration);
457 toast.show(); 462 toast.show();
458 mURLView.setError(getString(R.string.error_unkown_error)); 463 mURLView.setError(getString(R.string.error_wrong_url));
459 mURLView.requestFocus(); 464 mURLView.requestFocus();
460 break; 465 break;
461 } 466
462 467 case GeneralSecurityException:
463 468 break;
464 469 case UnkonwnException:
465 470 break;
466 } 471 case WrongCredentials:
467 472 mPasswordView.setError(getString(R.string.error_incorrect_password));
468 @Override 473 mPasswordView.requestFocus();
469 protected void onCancelled() { 474 break;
470 mAuthTask = null; 475
471 showProgress(false); 476 case ConnectionRefused:
472 } 477 toast = Toast.makeText(getApplicationContext(), R.string.error_connection_refused, duration);
473 } 478 toast.show();
479 mURLView.setError(getString(R.string.error_connection_refused));
480 mURLView.requestFocus();
481 break;
482 case UNTRUSTED_CERT:
483 toast = Toast.makeText(getApplicationContext(), getString(R.string.error_untrusted_certificate), duration);
484 toast.show();
485 mURLView.setError(getString(R.string.error_ssl));
486 mURLView.requestFocus();
487 break;
488 case Account_Already_In_Use:
489 toast = Toast.makeText(getApplicationContext(), R.string.error_account_already_in_use, duration);
490 toast.show();
491 mURLView.setError(getString(R.string.error_account_already_in_use));
492 mURLView.requestFocus();
493 break;
494 default:
495 toast = Toast.makeText(getApplicationContext(), R.string.error_unkown_error, duration);
496 toast.show();
497 mURLView.setError(getString(R.string.error_unkown_error));
498 mURLView.requestFocus();
499 break;
500 }
501
502
503 }
504
505 @Override
506 protected void onCancelled() {
507 mAuthTask = null;
508 showProgress(false);
509 }
510 }
474 } 511 }

mercurial