Back to project page MyTesla.
The source code is released under:
MIT License
If you think the Android project MyTesla listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.myappfactory.tesla; /* w w w.ja va 2 s . c o m*/ import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.annotation.TargetApi; import android.content.ContentResolver; import android.database.Cursor; import android.graphics.Typeface; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; import android.os.Build.VERSION; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.provider.ContactsContract; import android.support.v4.app.FragmentActivity; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.text.TextUtils; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.EditorInfo; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import org.androidannotations.annotations.Background; import org.androidannotations.annotations.EActivity; import org.androidannotations.annotations.UiThread; import org.androidannotations.annotations.ViewById; import org.springframework.util.LinkedMultiValueMap; import java.util.ArrayList; import java.util.List; /** * A login screen that offers login via email/password. */ @EActivity public class LoginActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> { // UI references. @ViewById(R.id.teslaTitle) TextView teslaTitle; @ViewById(R.id.email) AutoCompleteTextView mEmailView; @ViewById(R.id.password) EditText mPasswordView; @ViewById(R.id.login_form) View mLoginFormView; @ViewById(R.id.email_sign_in_button) Button mEmailSignInButton; @ViewById(R.id.progress_container) View mProgressView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); Typeface face= Typeface.createFromAsset(getAssets(), "fonts/TeslaNikolaCaps.ttf"); teslaTitle.setTypeface(face); // Set up the login form. populateAutoComplete(); mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) { if (id == R.id.login || id == EditorInfo.IME_NULL) { attemptLogin(); return true; } return false; } }); mEmailSignInButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { attemptLogin(); } }); } private void populateAutoComplete() { if (VERSION.SDK_INT >= 14) { // Use ContactsContract.Profile (API 14+) getSupportLoaderManager().initLoader(0, null, this); } else if (VERSION.SDK_INT >= 8) { // Use AccountManager (API 8+) new SetupEmailAutoCompleteTask().execute(null, null); } } /** * 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. */ public void attemptLogin() { // Reset errors. mEmailView.setError(null); mPasswordView.setError(null); // Store values at the time of the login attempt. String email = mEmailView.getText().toString(); String password = mPasswordView.getText().toString(); boolean cancel = false; View focusView = null; // Check for a valid password, if the user entered one. if (TextUtils.isEmpty(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; } else if (!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 { mHandler.sendEmptyMessage(0); doLogin(email, password); } } private boolean isEmailValid(String email) { return email.contains("@"); } private Handler mHandler = new Handler() { public void handleMessage (Message msg) { switch (msg.what) { case 1: showProgress(false); break; case 0: default: showProgress(true); break; } } }; /** * Shows the progress UI and hides the login form. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) public void showProgress(final boolean 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) { int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime); mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE); mLoginFormView.animate().setDuration(shortAnimTime).alpha( show ? 0 : 1).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE); } }); mProgressView.setVisibility(show ? View.VISIBLE : View.GONE); mProgressView.animate().setDuration(shortAnimTime).alpha( show ? 1 : 0).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(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); } } @TargetApi(14) @Override public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { return new CursorLoader(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 + " = ?", new String[]{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"); } @Override public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) { List<String> emails = new ArrayList<String>(); cursor.moveToFirst(); while (!cursor.isAfterLast()) { emails.add(cursor.getString(ProfileQuery.ADDRESS)); cursor.moveToNext(); } addEmailsToAutoComplete(emails); } @Override public void onLoaderReset(Loader<Cursor> cursorLoader) { } private interface ProfileQuery { String[] PROJECTION = { ContactsContract.CommonDataKinds.Email.ADDRESS, ContactsContract.CommonDataKinds.Email.IS_PRIMARY, }; int ADDRESS = 0; int IS_PRIMARY = 1; } /** * Use an AsyncTask to fetch the user's email addresses on a background thread, and update * the email text field with results on the main UI thread. */ class SetupEmailAutoCompleteTask extends AsyncTask<Void, Void, List<String>> { @Override protected List<String> doInBackground(Void... voids) { ArrayList<String> emailAddressCollection = new ArrayList<String>(); // Get all emails from the user's contacts and copy them to a list. ContentResolver cr = getContentResolver(); Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null); while (emailCur.moveToNext()) { String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract .CommonDataKinds.Email.DATA)); emailAddressCollection.add(email); } emailCur.close(); return emailAddressCollection; } @Override protected void onPostExecute(List<String> emailAddressCollection) { addEmailsToAutoComplete(emailAddressCollection); } } private void addEmailsToAutoComplete(List<String> emailAddressCollection) { //Create adapter to tell the AutoCompleteTextView what to show in its dropdown list. ArrayAdapter<String> adapter = new ArrayAdapter<String>(LoginActivity.this, android.R.layout.simple_dropdown_item_1line, emailAddressCollection); mEmailView.setAdapter(adapter); } @Background protected void doLogin (String email, String password) { TeslaApplication application = (TeslaApplication) getApplication(); LinkedMultiValueMap<String, String> loginForm = new LinkedMultiValueMap<String, String>(); loginForm.add("user_session[email]", email); loginForm.add("user_session[password]", password); application.getLoginClient().postLoginForm(loginForm); String userCredentials = application.getLoginClient().getCookie("user_credentials"); if (userCredentials != null) { //Login was successful...set cookie in the other clients application.setCookie("user_credentials", userCredentials, true); finish(); } else { //failed loginFailed(); } } @UiThread protected void loginFailed () { mHandler.sendEmptyMessage(1); mEmailView.setError(getString(R.string.error_login_failed)); mPasswordView.setError(getString(R.string.error_login_failed)); mEmailView.requestFocus(); } }