/** * A login screen that offers login via email/password. */ publicclassLoginActivityextendsAppCompatActivityimplementsLoaderCallbacks<Cursor> {
/** * Id to identity READ_CONTACTS permission request. */ privatestaticfinalintREQUEST_READ_CONTACTS=0;
/** * A dummy authentication store containing known user names and passwords. * TODO: remove after connecting to a real authentication system. */ privatestaticfinal String[] DUMMY_CREDENTIALS = newString[]{ "foo@example.com:hello", "bar@example.com:world" }; /** * Keep track of the login task to ensure we can cancel it if requested. */ privateUserLoginTaskmAuthTask=null;
/** * Callback received when a permissions request has been completed. */ @Override publicvoidonRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNullint[] grantResults) { if (requestCode == REQUEST_READ_CONTACTS) { if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { populateAutoComplete(); } } }
/** * Attempts to sign in or register the account specified by the login form. * If there are form errors (invalid email, missing fields, etc.), the * errors are presented and no actual login attempt is made. */ privatevoidattemptLogin() { if (mAuthTask != null) { return; }
// Store values at the time of the login attempt. Stringemail= mEmailView.getText().toString(); Stringpassword= mPasswordView.getText().toString();
booleancancel=false; ViewfocusView=null;
// Check for a valid password, if the user entered one. if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) { mPasswordView.setError(getString(R.string.error_invalid_password)); focusView = mPasswordView; cancel = true; }
// Check for a valid email address. if (TextUtils.isEmpty(email)) { mEmailView.setError(getString(R.string.error_field_required)); focusView = mEmailView; cancel = true; } elseif (!isEmailValid(email)) { mEmailView.setError(getString(R.string.error_invalid_email)); focusView = mEmailView; cancel = true; }
if (cancel) { // There was an error; don't attempt login and focus the first // form field with an error. focusView.requestFocus(); } else { // Show a progress spinner, and kick off a background task to // perform the user login attempt. showProgress(true); //mAuthTask = new UserLoginTask(email, password); //mAuthTask.execute((Void) null);
privatebooleanisEmailValid(String email) { //TODO: Replace this with your own logic return email.contains("@"); }
privatebooleanisPasswordValid(String password) { //TODO: Replace this with your own logic return password.length() > 4; }
/** * Shows the progress UI and hides the login form. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) privatevoidshowProgress(finalboolean show) { // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow // for very easy animations. If available, use these APIs to fade-in // the progress spinner. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { intshortAnimTime= getResources().getInteger(android.R.integer.config_shortAnimTime);
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE); mProgressView.animate().setDuration(shortAnimTime).alpha( show ? 1 : 0).setListener(newAnimatorListenerAdapter() { @Override publicvoidonAnimationEnd(Animator animation) { mProgressView.setVisibility(show ? View.VISIBLE : View.GONE); } }); } else { // The ViewPropertyAnimator APIs are not available, so simply show // and hide the relevant UI components. mProgressView.setVisibility(show ? View.VISIBLE : View.GONE); mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE); } }
@Override public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { returnnewCursorLoader(this, // Retrieve data rows for the device user's 'profile' contact. Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI, ContactsContract.Contacts.Data.CONTENT_DIRECTORY), ProfileQuery.PROJECTION,
// Show primary email addresses first. Note that there won't be // a primary email address if the user hasn't specified one. ContactsContract.Contacts.Data.IS_PRIMARY + " DESC"); }
privatevoidaddEmailsToAutoComplete(List<String> emailAddressCollection) { //Create adapter to tell the AutoCompleteTextView what to show in its dropdown list. ArrayAdapter<String> adapter = newArrayAdapter<>(LoginActivity.this, android.R.layout.simple_dropdown_item_1line, emailAddressCollection);
/** * Represents an asynchronous login/registration task used to authenticate * the user. */ publicclassUserLoginTaskextendsAsyncTask<Void, Void, Boolean> {