michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.fxa.activities; michael@0: michael@0: import java.util.concurrent.Executor; michael@0: import java.util.concurrent.Executors; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.background.fxa.FxAccountClient; michael@0: import org.mozilla.gecko.background.fxa.FxAccountClient10.RequestDelegate; michael@0: import org.mozilla.gecko.background.fxa.FxAccountClient20; michael@0: import org.mozilla.gecko.background.fxa.FxAccountClient20.LoginResponse; michael@0: import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException; michael@0: import org.mozilla.gecko.background.fxa.PasswordStretcher; michael@0: import org.mozilla.gecko.fxa.FxAccountConstants; michael@0: import org.mozilla.gecko.fxa.activities.FxAccountSetupTask.FxAccountSignInTask; michael@0: import org.mozilla.gecko.sync.setup.activities.ActivityUtils; michael@0: michael@0: import android.content.Intent; michael@0: import android.os.Bundle; michael@0: import android.view.View; michael@0: import android.view.View.OnClickListener; michael@0: import android.widget.AutoCompleteTextView; michael@0: import android.widget.Button; michael@0: import android.widget.EditText; michael@0: import android.widget.ProgressBar; michael@0: import android.widget.TextView; michael@0: michael@0: /** michael@0: * Activity which displays sign in screen to the user. michael@0: */ michael@0: public class FxAccountSignInActivity extends FxAccountAbstractSetupActivity { michael@0: protected static final String LOG_TAG = FxAccountSignInActivity.class.getSimpleName(); michael@0: michael@0: private static final int CHILD_REQUEST_CODE = 3; michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: @Override michael@0: public void onCreate(Bundle icicle) { michael@0: Logger.debug(LOG_TAG, "onCreate(" + icicle + ")"); michael@0: michael@0: super.onCreate(icicle); michael@0: setContentView(R.layout.fxaccount_sign_in); michael@0: michael@0: emailEdit = (AutoCompleteTextView) ensureFindViewById(null, R.id.email, "email edit"); michael@0: passwordEdit = (EditText) ensureFindViewById(null, R.id.password, "password edit"); michael@0: showPasswordButton = (Button) ensureFindViewById(null, R.id.show_password, "show password button"); michael@0: remoteErrorTextView = (TextView) ensureFindViewById(null, R.id.remote_error, "remote error text view"); michael@0: button = (Button) ensureFindViewById(null, R.id.button, "sign in button"); michael@0: progressBar = (ProgressBar) ensureFindViewById(null, R.id.progress, "progress bar"); michael@0: michael@0: minimumPasswordLength = 1; // Minimal restriction on passwords entered to sign in. michael@0: createSignInButton(); michael@0: addListeners(); michael@0: updateButtonState(); michael@0: createShowPasswordButton(); michael@0: linkifyPolicy(); michael@0: michael@0: View createAccountInsteadLink = ensureFindViewById(null, R.id.create_account_link, "create account instead link"); michael@0: createAccountInsteadLink.setOnClickListener(new OnClickListener() { michael@0: @Override michael@0: public void onClick(View v) { michael@0: Intent intent = new Intent(FxAccountSignInActivity.this, FxAccountCreateAccountActivity.class); michael@0: intent.putExtra("email", emailEdit.getText().toString()); michael@0: intent.putExtra("password", passwordEdit.getText().toString()); michael@0: // Per http://stackoverflow.com/a/8992365, this triggers a known bug with michael@0: // the soft keyboard not being shown for the started activity. Why, Android, why? michael@0: intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); michael@0: startActivityForResult(intent, CHILD_REQUEST_CODE); michael@0: } michael@0: }); michael@0: michael@0: // Only set email/password in onCreate; we don't want to overwrite edited values onResume. michael@0: if (getIntent() != null && getIntent().getExtras() != null) { michael@0: Bundle bundle = getIntent().getExtras(); michael@0: emailEdit.setText(bundle.getString("email")); michael@0: passwordEdit.setText(bundle.getString("password")); michael@0: } michael@0: michael@0: TextView view = (TextView) findViewById(R.id.forgot_password_link); michael@0: ActivityUtils.linkTextView(view, R.string.fxaccount_sign_in_forgot_password, R.string.fxaccount_link_forgot_password); michael@0: } michael@0: michael@0: /** michael@0: * We might have switched to the CreateAccount activity; if that activity michael@0: * succeeds, feed its result back to the authenticator. michael@0: */ michael@0: @Override michael@0: public void onActivityResult(int requestCode, int resultCode, Intent data) { michael@0: Logger.debug(LOG_TAG, "onActivityResult: " + requestCode); michael@0: if (requestCode != CHILD_REQUEST_CODE || resultCode != RESULT_OK) { michael@0: super.onActivityResult(requestCode, resultCode, data); michael@0: return; michael@0: } michael@0: this.setResult(resultCode, data); michael@0: this.finish(); michael@0: } michael@0: michael@0: public void signIn(String email, String password) { michael@0: String serverURI = FxAccountConstants.DEFAULT_AUTH_SERVER_ENDPOINT; michael@0: PasswordStretcher passwordStretcher = makePasswordStretcher(password); michael@0: // This delegate creates a new Android account on success, opens the michael@0: // appropriate "success!" activity, and finishes this activity. michael@0: RequestDelegate delegate = new AddAccountDelegate(email, passwordStretcher, serverURI) { michael@0: @Override michael@0: public void handleError(Exception e) { michael@0: showRemoteError(e, R.string.fxaccount_sign_in_unknown_error); michael@0: } michael@0: michael@0: @Override michael@0: public void handleFailure(FxAccountClientRemoteException e) { michael@0: showRemoteError(e, R.string.fxaccount_sign_in_unknown_error); michael@0: } michael@0: }; michael@0: michael@0: Executor executor = Executors.newSingleThreadExecutor(); michael@0: FxAccountClient client = new FxAccountClient20(serverURI, executor); michael@0: try { michael@0: hideRemoteError(); michael@0: new FxAccountSignInTask(this, this, email, passwordStretcher, client, delegate).execute(); michael@0: } catch (Exception e) { michael@0: showRemoteError(e, R.string.fxaccount_sign_in_unknown_error); michael@0: } michael@0: } michael@0: michael@0: protected void createSignInButton() { michael@0: button.setOnClickListener(new OnClickListener() { michael@0: @Override michael@0: public void onClick(View v) { michael@0: final String email = emailEdit.getText().toString(); michael@0: final String password = passwordEdit.getText().toString(); michael@0: signIn(email, password); michael@0: } michael@0: }); michael@0: } michael@0: }