// LoginActivity.javapublicclassLoginActivityextendsAppCompatActivityimplementsLoaderCallbacks<Cursor>{...@OverridepublicLoader<Cursor>onCreateLoader(inti,Bundlebundle){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,// Select only email addresses.ContactsContract.Contacts.Data.MIMETYPE+" = ?",newString[]{ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE},// 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");}@OverridepublicvoidonLoadFinished(Loader<Cursor>cursorLoader,Cursorcursor){List<String>emails=newArrayList<>();cursor.moveToFirst();while(!cursor.isAfterLast()){emails.add(cursor.getString(ProfileQuery.ADDRESS));cursor.moveToNext();}addEmailsToAutoComplete(emails);}@OverridepublicvoidonLoaderReset(Loader<Cursor>cursorLoader){}...}
// LoginActivity.java@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)privatevoidshowProgress(finalbooleanshow){// 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);mLoginFormView.setVisibility(show?View.GONE:View.VISIBLE);mLoginFormView.animate().setDuration(shortAnimTime).alpha(show?0:1).setListener(newAnimatorListenerAdapter(){@OverridepublicvoidonAnimationEnd(Animatoranimation){mLoginFormView.setVisibility(show?View.GONE:View.VISIBLE);}});mProgressView.setVisibility(show?View.VISIBLE:View.GONE);mProgressView.animate().setDuration(shortAnimTime).alpha(show?1:0).setListener(newAnimatorListenerAdapter(){@OverridepublicvoidonAnimationEnd(Animatoranimation){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);}}
// LoginActivity.javapublicclassUserLoginTaskextendsAsyncTask<Void,Void,Boolean>{privatefinalStringmEmail;privatefinalStringmPassword;UserLoginTask(Stringemail,Stringpassword){mEmail=email;mPassword=password;}@OverrideprotectedBooleandoInBackground(Void...params){// TODO: attempt authentication against a network service.try{// Simulate network access.Thread.sleep(2000);}catch(InterruptedExceptione){returnfalse;}for(Stringcredential:DUMMY_CREDENTIALS){String[]pieces=credential.split(":");if(pieces[0].equals(mEmail)){// Account exists, return true if the password matches.returnpieces[1].equals(mPassword);}}// TODO: register the new account here.returntrue;}}