1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/fxa/activities/FxAccountSignInActivity.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,141 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.fxa.activities; 1.9 + 1.10 +import java.util.concurrent.Executor; 1.11 +import java.util.concurrent.Executors; 1.12 + 1.13 +import org.mozilla.gecko.R; 1.14 +import org.mozilla.gecko.background.common.log.Logger; 1.15 +import org.mozilla.gecko.background.fxa.FxAccountClient; 1.16 +import org.mozilla.gecko.background.fxa.FxAccountClient10.RequestDelegate; 1.17 +import org.mozilla.gecko.background.fxa.FxAccountClient20; 1.18 +import org.mozilla.gecko.background.fxa.FxAccountClient20.LoginResponse; 1.19 +import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException; 1.20 +import org.mozilla.gecko.background.fxa.PasswordStretcher; 1.21 +import org.mozilla.gecko.fxa.FxAccountConstants; 1.22 +import org.mozilla.gecko.fxa.activities.FxAccountSetupTask.FxAccountSignInTask; 1.23 +import org.mozilla.gecko.sync.setup.activities.ActivityUtils; 1.24 + 1.25 +import android.content.Intent; 1.26 +import android.os.Bundle; 1.27 +import android.view.View; 1.28 +import android.view.View.OnClickListener; 1.29 +import android.widget.AutoCompleteTextView; 1.30 +import android.widget.Button; 1.31 +import android.widget.EditText; 1.32 +import android.widget.ProgressBar; 1.33 +import android.widget.TextView; 1.34 + 1.35 +/** 1.36 + * Activity which displays sign in screen to the user. 1.37 + */ 1.38 +public class FxAccountSignInActivity extends FxAccountAbstractSetupActivity { 1.39 + protected static final String LOG_TAG = FxAccountSignInActivity.class.getSimpleName(); 1.40 + 1.41 + private static final int CHILD_REQUEST_CODE = 3; 1.42 + 1.43 + /** 1.44 + * {@inheritDoc} 1.45 + */ 1.46 + @Override 1.47 + public void onCreate(Bundle icicle) { 1.48 + Logger.debug(LOG_TAG, "onCreate(" + icicle + ")"); 1.49 + 1.50 + super.onCreate(icicle); 1.51 + setContentView(R.layout.fxaccount_sign_in); 1.52 + 1.53 + emailEdit = (AutoCompleteTextView) ensureFindViewById(null, R.id.email, "email edit"); 1.54 + passwordEdit = (EditText) ensureFindViewById(null, R.id.password, "password edit"); 1.55 + showPasswordButton = (Button) ensureFindViewById(null, R.id.show_password, "show password button"); 1.56 + remoteErrorTextView = (TextView) ensureFindViewById(null, R.id.remote_error, "remote error text view"); 1.57 + button = (Button) ensureFindViewById(null, R.id.button, "sign in button"); 1.58 + progressBar = (ProgressBar) ensureFindViewById(null, R.id.progress, "progress bar"); 1.59 + 1.60 + minimumPasswordLength = 1; // Minimal restriction on passwords entered to sign in. 1.61 + createSignInButton(); 1.62 + addListeners(); 1.63 + updateButtonState(); 1.64 + createShowPasswordButton(); 1.65 + linkifyPolicy(); 1.66 + 1.67 + View createAccountInsteadLink = ensureFindViewById(null, R.id.create_account_link, "create account instead link"); 1.68 + createAccountInsteadLink.setOnClickListener(new OnClickListener() { 1.69 + @Override 1.70 + public void onClick(View v) { 1.71 + Intent intent = new Intent(FxAccountSignInActivity.this, FxAccountCreateAccountActivity.class); 1.72 + intent.putExtra("email", emailEdit.getText().toString()); 1.73 + intent.putExtra("password", passwordEdit.getText().toString()); 1.74 + // Per http://stackoverflow.com/a/8992365, this triggers a known bug with 1.75 + // the soft keyboard not being shown for the started activity. Why, Android, why? 1.76 + intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 1.77 + startActivityForResult(intent, CHILD_REQUEST_CODE); 1.78 + } 1.79 + }); 1.80 + 1.81 + // Only set email/password in onCreate; we don't want to overwrite edited values onResume. 1.82 + if (getIntent() != null && getIntent().getExtras() != null) { 1.83 + Bundle bundle = getIntent().getExtras(); 1.84 + emailEdit.setText(bundle.getString("email")); 1.85 + passwordEdit.setText(bundle.getString("password")); 1.86 + } 1.87 + 1.88 + TextView view = (TextView) findViewById(R.id.forgot_password_link); 1.89 + ActivityUtils.linkTextView(view, R.string.fxaccount_sign_in_forgot_password, R.string.fxaccount_link_forgot_password); 1.90 + } 1.91 + 1.92 + /** 1.93 + * We might have switched to the CreateAccount activity; if that activity 1.94 + * succeeds, feed its result back to the authenticator. 1.95 + */ 1.96 + @Override 1.97 + public void onActivityResult(int requestCode, int resultCode, Intent data) { 1.98 + Logger.debug(LOG_TAG, "onActivityResult: " + requestCode); 1.99 + if (requestCode != CHILD_REQUEST_CODE || resultCode != RESULT_OK) { 1.100 + super.onActivityResult(requestCode, resultCode, data); 1.101 + return; 1.102 + } 1.103 + this.setResult(resultCode, data); 1.104 + this.finish(); 1.105 + } 1.106 + 1.107 + public void signIn(String email, String password) { 1.108 + String serverURI = FxAccountConstants.DEFAULT_AUTH_SERVER_ENDPOINT; 1.109 + PasswordStretcher passwordStretcher = makePasswordStretcher(password); 1.110 + // This delegate creates a new Android account on success, opens the 1.111 + // appropriate "success!" activity, and finishes this activity. 1.112 + RequestDelegate<LoginResponse> delegate = new AddAccountDelegate(email, passwordStretcher, serverURI) { 1.113 + @Override 1.114 + public void handleError(Exception e) { 1.115 + showRemoteError(e, R.string.fxaccount_sign_in_unknown_error); 1.116 + } 1.117 + 1.118 + @Override 1.119 + public void handleFailure(FxAccountClientRemoteException e) { 1.120 + showRemoteError(e, R.string.fxaccount_sign_in_unknown_error); 1.121 + } 1.122 + }; 1.123 + 1.124 + Executor executor = Executors.newSingleThreadExecutor(); 1.125 + FxAccountClient client = new FxAccountClient20(serverURI, executor); 1.126 + try { 1.127 + hideRemoteError(); 1.128 + new FxAccountSignInTask(this, this, email, passwordStretcher, client, delegate).execute(); 1.129 + } catch (Exception e) { 1.130 + showRemoteError(e, R.string.fxaccount_sign_in_unknown_error); 1.131 + } 1.132 + } 1.133 + 1.134 + protected void createSignInButton() { 1.135 + button.setOnClickListener(new OnClickListener() { 1.136 + @Override 1.137 + public void onClick(View v) { 1.138 + final String email = emailEdit.getText().toString(); 1.139 + final String password = passwordEdit.getText().toString(); 1.140 + signIn(email, password); 1.141 + } 1.142 + }); 1.143 + } 1.144 +}