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

Tue, 10 Feb 2015 18:12:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 18:12:00 +0100
changeset 0
fb9019fb1bf7
child 8
ec8af0e3fbc2
permissions
-rw-r--r--

Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.

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

mercurial