mobile/android/base/fxa/activities/FxAccountSignInActivity.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.fxa.activities;
michael@0 6
michael@0 7 import java.util.concurrent.Executor;
michael@0 8 import java.util.concurrent.Executors;
michael@0 9
michael@0 10 import org.mozilla.gecko.R;
michael@0 11 import org.mozilla.gecko.background.common.log.Logger;
michael@0 12 import org.mozilla.gecko.background.fxa.FxAccountClient;
michael@0 13 import org.mozilla.gecko.background.fxa.FxAccountClient10.RequestDelegate;
michael@0 14 import org.mozilla.gecko.background.fxa.FxAccountClient20;
michael@0 15 import org.mozilla.gecko.background.fxa.FxAccountClient20.LoginResponse;
michael@0 16 import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException;
michael@0 17 import org.mozilla.gecko.background.fxa.PasswordStretcher;
michael@0 18 import org.mozilla.gecko.fxa.FxAccountConstants;
michael@0 19 import org.mozilla.gecko.fxa.activities.FxAccountSetupTask.FxAccountSignInTask;
michael@0 20 import org.mozilla.gecko.sync.setup.activities.ActivityUtils;
michael@0 21
michael@0 22 import android.content.Intent;
michael@0 23 import android.os.Bundle;
michael@0 24 import android.view.View;
michael@0 25 import android.view.View.OnClickListener;
michael@0 26 import android.widget.AutoCompleteTextView;
michael@0 27 import android.widget.Button;
michael@0 28 import android.widget.EditText;
michael@0 29 import android.widget.ProgressBar;
michael@0 30 import android.widget.TextView;
michael@0 31
michael@0 32 /**
michael@0 33 * Activity which displays sign in screen to the user.
michael@0 34 */
michael@0 35 public class FxAccountSignInActivity extends FxAccountAbstractSetupActivity {
michael@0 36 protected static final String LOG_TAG = FxAccountSignInActivity.class.getSimpleName();
michael@0 37
michael@0 38 private static final int CHILD_REQUEST_CODE = 3;
michael@0 39
michael@0 40 /**
michael@0 41 * {@inheritDoc}
michael@0 42 */
michael@0 43 @Override
michael@0 44 public void onCreate(Bundle icicle) {
michael@0 45 Logger.debug(LOG_TAG, "onCreate(" + icicle + ")");
michael@0 46
michael@0 47 super.onCreate(icicle);
michael@0 48 setContentView(R.layout.fxaccount_sign_in);
michael@0 49
michael@0 50 emailEdit = (AutoCompleteTextView) ensureFindViewById(null, R.id.email, "email edit");
michael@0 51 passwordEdit = (EditText) ensureFindViewById(null, R.id.password, "password edit");
michael@0 52 showPasswordButton = (Button) ensureFindViewById(null, R.id.show_password, "show password button");
michael@0 53 remoteErrorTextView = (TextView) ensureFindViewById(null, R.id.remote_error, "remote error text view");
michael@0 54 button = (Button) ensureFindViewById(null, R.id.button, "sign in button");
michael@0 55 progressBar = (ProgressBar) ensureFindViewById(null, R.id.progress, "progress bar");
michael@0 56
michael@0 57 minimumPasswordLength = 1; // Minimal restriction on passwords entered to sign in.
michael@0 58 createSignInButton();
michael@0 59 addListeners();
michael@0 60 updateButtonState();
michael@0 61 createShowPasswordButton();
michael@0 62 linkifyPolicy();
michael@0 63
michael@0 64 View createAccountInsteadLink = ensureFindViewById(null, R.id.create_account_link, "create account instead link");
michael@0 65 createAccountInsteadLink.setOnClickListener(new OnClickListener() {
michael@0 66 @Override
michael@0 67 public void onClick(View v) {
michael@0 68 Intent intent = new Intent(FxAccountSignInActivity.this, FxAccountCreateAccountActivity.class);
michael@0 69 intent.putExtra("email", emailEdit.getText().toString());
michael@0 70 intent.putExtra("password", passwordEdit.getText().toString());
michael@0 71 // Per http://stackoverflow.com/a/8992365, this triggers a known bug with
michael@0 72 // the soft keyboard not being shown for the started activity. Why, Android, why?
michael@0 73 intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
michael@0 74 startActivityForResult(intent, CHILD_REQUEST_CODE);
michael@0 75 }
michael@0 76 });
michael@0 77
michael@0 78 // Only set email/password in onCreate; we don't want to overwrite edited values onResume.
michael@0 79 if (getIntent() != null && getIntent().getExtras() != null) {
michael@0 80 Bundle bundle = getIntent().getExtras();
michael@0 81 emailEdit.setText(bundle.getString("email"));
michael@0 82 passwordEdit.setText(bundle.getString("password"));
michael@0 83 }
michael@0 84
michael@0 85 TextView view = (TextView) findViewById(R.id.forgot_password_link);
michael@0 86 ActivityUtils.linkTextView(view, R.string.fxaccount_sign_in_forgot_password, R.string.fxaccount_link_forgot_password);
michael@0 87 }
michael@0 88
michael@0 89 /**
michael@0 90 * We might have switched to the CreateAccount activity; if that activity
michael@0 91 * succeeds, feed its result back to the authenticator.
michael@0 92 */
michael@0 93 @Override
michael@0 94 public void onActivityResult(int requestCode, int resultCode, Intent data) {
michael@0 95 Logger.debug(LOG_TAG, "onActivityResult: " + requestCode);
michael@0 96 if (requestCode != CHILD_REQUEST_CODE || resultCode != RESULT_OK) {
michael@0 97 super.onActivityResult(requestCode, resultCode, data);
michael@0 98 return;
michael@0 99 }
michael@0 100 this.setResult(resultCode, data);
michael@0 101 this.finish();
michael@0 102 }
michael@0 103
michael@0 104 public void signIn(String email, String password) {
michael@0 105 String serverURI = FxAccountConstants.DEFAULT_AUTH_SERVER_ENDPOINT;
michael@0 106 PasswordStretcher passwordStretcher = makePasswordStretcher(password);
michael@0 107 // This delegate creates a new Android account on success, opens the
michael@0 108 // appropriate "success!" activity, and finishes this activity.
michael@0 109 RequestDelegate<LoginResponse> delegate = new AddAccountDelegate(email, passwordStretcher, serverURI) {
michael@0 110 @Override
michael@0 111 public void handleError(Exception e) {
michael@0 112 showRemoteError(e, R.string.fxaccount_sign_in_unknown_error);
michael@0 113 }
michael@0 114
michael@0 115 @Override
michael@0 116 public void handleFailure(FxAccountClientRemoteException e) {
michael@0 117 showRemoteError(e, R.string.fxaccount_sign_in_unknown_error);
michael@0 118 }
michael@0 119 };
michael@0 120
michael@0 121 Executor executor = Executors.newSingleThreadExecutor();
michael@0 122 FxAccountClient client = new FxAccountClient20(serverURI, executor);
michael@0 123 try {
michael@0 124 hideRemoteError();
michael@0 125 new FxAccountSignInTask(this, this, email, passwordStretcher, client, delegate).execute();
michael@0 126 } catch (Exception e) {
michael@0 127 showRemoteError(e, R.string.fxaccount_sign_in_unknown_error);
michael@0 128 }
michael@0 129 }
michael@0 130
michael@0 131 protected void createSignInButton() {
michael@0 132 button.setOnClickListener(new OnClickListener() {
michael@0 133 @Override
michael@0 134 public void onClick(View v) {
michael@0 135 final String email = emailEdit.getText().toString();
michael@0 136 final String password = passwordEdit.getText().toString();
michael@0 137 signIn(email, password);
michael@0 138 }
michael@0 139 });
michael@0 140 }
michael@0 141 }

mercurial