Back to project page Android-SDK.
The source code is released under:
Apache License
If you think the Android project Android-SDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2014./*from w w w . ja v a 2s . c om*/ * * BaasBox - info@baasbox.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and limitations under the License. */ package com.baasbox.android.samples.aloa.activities; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.annotation.TargetApi; import android.os.Build; import android.os.Bundle; 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.EditText; import android.widget.TextView; import com.baasbox.android.BaasHandler; import com.baasbox.android.BaasResult; import com.baasbox.android.BaasUser; import com.baasbox.android.RequestToken; import com.baasbox.android.samples.aloa.Data; import com.baasbox.android.samples.aloa.R; import com.baasbox.android.samples.aloa.utils.BaseActivity; /** * A login screen that offers login via email/password. */ public class LoginActivity extends BaseActivity{ private static final String CURRENT_AUTH_REQUEST = "current_auth_request"; /** * Keep track of the login task to ensure we can cancel it if requested. */ private RequestToken mAuth = null; // UI references. private EditText mEmailView; private EditText mPasswordView; private EditText mFlowerView; private View mProgressView; private View mLoginFormView; private BaasHandler<BaasUser> loginHandler = new BaasHandler<BaasUser>() { @Override public void handle(BaasResult<BaasUser> result) { mAuth = null; showProgress(false); if (result.isSuccess()){ finish(); } else { mPasswordView.setError(getString(R.string.error_incorrect_password)); mPasswordView.requestFocus(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); // Set up the login form. mEmailView = (EditText) findViewById(R.id.in_email); // populateAutoComplete(); mPasswordView = (EditText) findViewById(R.id.in_password); 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; } }); mFlowerView=(EditText)findViewById(R.id.in_register_extra_info); mFlowerView.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) { if (id == R.id.register || id == EditorInfo.IME_NULL) { attemptRegister(); return true; } return false; }; }); View signIn = findViewById(R.id.btn_signin); View register = findViewById(R.id.btn_register); View.OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { if (v.getId()==R.id.btn_signin){ attemptLogin(); } else { attemptRegister(); } } }; signIn.setOnClickListener(listener); register.setOnClickListener(listener); mLoginFormView = findViewById(R.id.login_form); mProgressView = findViewById(R.id.login_progress); if (savedInstanceState!=null) { mAuth = RequestToken.loadAndResume(savedInstanceState, CURRENT_AUTH_REQUEST, loginHandler); } if (mAuth!=null){ showProgress(true); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (mAuth!=null&& mAuth.suspendAndSave(outState,CURRENT_AUTH_REQUEST)){ showProgress(false); } } private void attemptRegister() { if (mAuth != null){ return; } mEmailView.setError(null); mPasswordView.setError(null); mFlowerView.setError(null); String email = mEmailView.getText().toString(); String password= mPasswordView.getText().toString(); String flower = mFlowerView.getText().toString(); boolean cancel = false; View focusView = null; if (TextUtils.isEmpty(password) || !isPasswordValid(password)){ mPasswordView.setError(getString(R.string.error_invalid_password)); focusView=mPasswordView; cancel = true; } 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 (TextUtils.isEmpty(flower)){ mFlowerView.setError(getString(R.string.error_field_required)); focusView = mFlowerView; cancel = true; } if (cancel) { focusView.requestFocus(); } else { showProgress(true); BaasUser user = BaasUser.withUserName(email) .setPassword(password); user.getScope(BaasUser.Scope.PUBLIC).put(Data.FLOWERS, flower); mAuth =user.signup(loginHandler); } } /** * 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. */ private void attemptLogin() { if (mAuth != null) { return; } // 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) || !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; } 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 { // Show a progress spinner, and kick off a background task to // perform the user login attempt. showProgress(true); mAuth = BaasUser.withUserName(email) .setPassword(password) .login(loginHandler); } } private boolean isEmailValid(String email) { return email.contains("@"); } private boolean isPasswordValid(String password) { return password.length() > 4; } /** * Shows the progress UI and hides the login form. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) private 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); } } // /** // * Represents an asynchronous login/registration task used to authenticate // * the user. // */ // public class UserLoginTask extends AsyncTask<Void, Void, Boolean> { // // private final String mEmail; // private final String mPassword; // // UserLoginTask(String email, String password) { // mEmail = email; // mPassword = password; // } // // @Override // protected Boolean doInBackground(Void... params) { // // TODO: attempt authentication against a network service. // // try { // // Simulate network access. // Thread.sleep(2000); // } catch (InterruptedException e) { // return false; // } // // for (String credential : DUMMY_CREDENTIALS) { // String[] pieces = credential.split(":"); // if (pieces[0].equals(mEmail)) { // // Account exists, return true if the password matches. // return pieces[1].equals(mPassword); // } // } // // // TODO: register the new account here. // return true; // } // // @Override // protected void onPostExecute(final Boolean success) { // mAuthTask = null; // showProgress(false); // // if (success) { // finish(); // } else { // mPasswordView.setError(getString(R.string.error_incorrect_password)); // mPasswordView.requestFocus(); // } // } // // @Override // protected void onCancelled() { // mAuthTask = null; // showProgress(false); // } // } }