From bf40d9ed518d1149b322bd94fdc6909ce51eb5de Mon Sep 17 00:00:00 2001 From: Nick Alexander Date: Mon, 20 Jan 2014 21:26:21 -0800 Subject: [PATCH] Bug 956581 - Make FxAccountGetStartedActivity an AuthenticatorActivity. r=rnewman --- .../FxAccountCreateAccountActivity.java | 49 ++++++++++++---- .../FxAccountGetStartedActivity.java | 57 ++++++++++++++++++- .../activities/FxAccountSignInActivity.java | 52 +++++++++++++---- .../activities/FxAccountStatusActivity.java | 2 + .../FxAccountUpdateCredentialsActivity.java | 2 +- 5 files changed, 137 insertions(+), 25 deletions(-) diff --git a/mobile/android/base/fxa/activities/FxAccountCreateAccountActivity.java b/mobile/android/base/fxa/activities/FxAccountCreateAccountActivity.java index a3ac25a4ec2..eb5c67b5361 100644 --- a/mobile/android/base/fxa/activities/FxAccountCreateAccountActivity.java +++ b/mobile/android/base/fxa/activities/FxAccountCreateAccountActivity.java @@ -14,22 +14,22 @@ import org.mozilla.gecko.background.fxa.FxAccountClient20; import org.mozilla.gecko.fxa.FxAccountConstants; import org.mozilla.gecko.fxa.activities.FxAccountSetupTask.FxAccountSignUpTask; import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount; -import org.mozilla.gecko.fxa.authenticator.FxAccountAuthenticator; import org.mozilla.gecko.sync.HTTPFailureException; import org.mozilla.gecko.sync.net.SyncStorageResponse; import android.accounts.Account; +import android.accounts.AccountManager; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; +import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; -import android.widget.Toast; import ch.boye.httpclientandroidlib.HttpResponse; /** @@ -38,6 +38,8 @@ import ch.boye.httpclientandroidlib.HttpResponse; public class FxAccountCreateAccountActivity extends FxAccountAbstractSetupActivity { protected static final String LOG_TAG = FxAccountCreateAccountActivity.class.getSimpleName(); + private static final int CHILD_REQUEST_CODE = 2; + protected EditText yearEdit; /** @@ -65,19 +67,41 @@ public class FxAccountCreateAccountActivity extends FxAccountAbstractSetupActivi updateButtonState(); createShowPasswordButton(); - launchActivityOnClick(ensureFindViewById(null, R.id.sign_in_instead_link, "sign in instead link"), FxAccountSignInActivity.class); + View signInInsteadLink = ensureFindViewById(null, R.id.sign_in_instead_link, "sign in instead link"); + signInInsteadLink.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(FxAccountCreateAccountActivity.this, FxAccountSignInActivity.class); + intent.putExtra("email", emailEdit.getText().toString()); + intent.putExtra("password", passwordEdit.getText().toString()); + // Per http://stackoverflow.com/a/8992365, this triggers a known bug with + // the soft keyboard not being shown for the started activity. Why, Android, why? + intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); + startActivityForResult(intent, CHILD_REQUEST_CODE); + } + }); + + // Only set email/password in onCreate; we don't want to overwrite edited values onResume. + if (getIntent() != null && getIntent().getExtras() != null) { + Bundle bundle = getIntent().getExtras(); + emailEdit.setText(bundle.getString("email")); + passwordEdit.setText(bundle.getString("password")); + } } /** - * {@inheritDoc} + * We might have switched to the SignIn activity; if that activity + * succeeds, feed its result back to the authenticator. */ @Override - public void onResume() { - super.onResume(); - if (FxAccountAuthenticator.getFirefoxAccounts(this).length > 0) { - redirectToActivity(FxAccountStatusActivity.class); + public void onActivityResult(int requestCode, int resultCode, Intent data) { + Logger.debug(LOG_TAG, "onActivityResult: " + requestCode); + if (requestCode != CHILD_REQUEST_CODE || resultCode != RESULT_OK) { + super.onActivityResult(requestCode, resultCode, data); return; } + this.setResult(resultCode, data); + this.finish(); } protected void createYearEdit() { @@ -151,8 +175,13 @@ public class FxAccountCreateAccountActivity extends FxAccountAbstractSetupActivi new AndroidFxAccount(activity, account).dump(); } - Toast.makeText(getApplicationContext(), "Got success creating account.", Toast.LENGTH_LONG).show(); - redirectToActivity(FxAccountStatusActivity.class); + // The GetStarted activity has called us and needs to return a result to the authenticator. + final Intent intent = new Intent(); + intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, email); + intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, FxAccountConstants.ACCOUNT_TYPE); + // intent.putExtra(AccountManager.KEY_AUTHTOKEN, accountType); + setResult(RESULT_OK, intent); + finish(); } } diff --git a/mobile/android/base/fxa/activities/FxAccountGetStartedActivity.java b/mobile/android/base/fxa/activities/FxAccountGetStartedActivity.java index 1a517f13581..e41d1f0ad80 100644 --- a/mobile/android/base/fxa/activities/FxAccountGetStartedActivity.java +++ b/mobile/android/base/fxa/activities/FxAccountGetStartedActivity.java @@ -6,27 +6,78 @@ package org.mozilla.gecko.fxa.activities; import org.mozilla.gecko.R; import org.mozilla.gecko.background.common.log.Logger; +import org.mozilla.gecko.fxa.FxAccountConstants; +import org.mozilla.gecko.fxa.authenticator.FxAccountAuthenticator; +import android.accounts.AccountAuthenticatorActivity; +import android.content.Intent; import android.os.Bundle; +import android.view.View; +import android.view.View.OnClickListener; /** * Activity which displays sign up/sign in screen to the user. */ -public class FxAccountGetStartedActivity extends FxAccountAbstractActivity { +public class FxAccountGetStartedActivity extends AccountAuthenticatorActivity { protected static final String LOG_TAG = FxAccountGetStartedActivity.class.getSimpleName(); + private static final int CHILD_REQUEST_CODE = 1; + /** * {@inheritDoc} */ @Override public void onCreate(Bundle icicle) { + Logger.setThreadLogTag(FxAccountConstants.GLOBAL_LOG_TAG); Logger.debug(LOG_TAG, "onCreate(" + icicle + ")"); super.onCreate(icicle); setContentView(R.layout.fxaccount_get_started); - linkifyTextViews(null, new int[] { R.id.old_firefox }); + // linkifyTextViews(null, new int[] { R.id.old_firefox }); - launchActivityOnClick(ensureFindViewById(null, R.id.get_started_button, "get started button"), FxAccountCreateAccountActivity.class); + View button = findViewById(R.id.get_started_button); + button.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(FxAccountGetStartedActivity.this, FxAccountCreateAccountActivity.class); + // Per http://stackoverflow.com/a/8992365, this triggers a known bug with + // the soft keyboard not being shown for the started activity. Why, Android, why? + intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); + startActivityForResult(intent, CHILD_REQUEST_CODE); + } + }); + } + + public void onResume() { + super.onResume(); + if (FxAccountAuthenticator.getFirefoxAccounts(this).length > 0) { + this.setAccountAuthenticatorResult(null); + setResult(RESULT_CANCELED); + Intent intent = new Intent(this, FxAccountStatusActivity.class); + // Per http://stackoverflow.com/a/8992365, this triggers a known bug with + // the soft keyboard not being shown for the started activity. Why, Android, why? + intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); + startActivity(intent); + finish(); + return; + } + } + + /** + * We started the CreateAccount activity for a result; this returns it to the + * authenticator. + */ + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + Logger.debug(LOG_TAG, "onActivityResult: " + requestCode); + if (requestCode != CHILD_REQUEST_CODE) { + super.onActivityResult(requestCode, resultCode, data); + return; + } + if (data != null) { + this.setAccountAuthenticatorResult(data.getExtras()); + } + this.setResult(requestCode, data); } } diff --git a/mobile/android/base/fxa/activities/FxAccountSignInActivity.java b/mobile/android/base/fxa/activities/FxAccountSignInActivity.java index e2c5234f070..9fd70407284 100644 --- a/mobile/android/base/fxa/activities/FxAccountSignInActivity.java +++ b/mobile/android/base/fxa/activities/FxAccountSignInActivity.java @@ -15,19 +15,19 @@ import org.mozilla.gecko.background.fxa.FxAccountClient20.LoginResponse; import org.mozilla.gecko.fxa.FxAccountConstants; import org.mozilla.gecko.fxa.activities.FxAccountSetupTask.FxAccountSignInTask; import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount; -import org.mozilla.gecko.fxa.authenticator.FxAccountAuthenticator; import org.mozilla.gecko.sync.HTTPFailureException; import org.mozilla.gecko.sync.net.SyncStorageResponse; import android.accounts.Account; +import android.accounts.AccountManager; import android.app.Activity; +import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; -import android.widget.Toast; import ch.boye.httpclientandroidlib.HttpResponse; /** @@ -36,6 +36,8 @@ import ch.boye.httpclientandroidlib.HttpResponse; public class FxAccountSignInActivity extends FxAccountAbstractSetupActivity { protected static final String LOG_TAG = FxAccountSignInActivity.class.getSimpleName(); + private static final int CHILD_REQUEST_CODE = 3; + /** * {@inheritDoc} */ @@ -58,21 +60,44 @@ public class FxAccountSignInActivity extends FxAccountAbstractSetupActivity { updateButtonState(); createShowPasswordButton(); - this.launchActivityOnClick(ensureFindViewById(null, R.id.create_account_link, "create account instead link"), FxAccountCreateAccountActivity.class); + View signInInsteadLink = ensureFindViewById(null, R.id.create_account_link, "create account instead link"); + signInInsteadLink.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(FxAccountSignInActivity.this, FxAccountCreateAccountActivity.class); + intent.putExtra("email", emailEdit.getText().toString()); + intent.putExtra("password", passwordEdit.getText().toString()); + // Per http://stackoverflow.com/a/8992365, this triggers a known bug with + // the soft keyboard not being shown for the started activity. Why, Android, why? + intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); + startActivityForResult(intent, CHILD_REQUEST_CODE); + } + }); + + // Only set email/password in onCreate; we don't want to overwrite edited values onResume. + if (getIntent() != null && getIntent().getExtras() != null) { + Bundle bundle = getIntent().getExtras(); + emailEdit.setText(bundle.getString("email")); + passwordEdit.setText(bundle.getString("password")); + } + // Not yet implemented. - this.launchActivityOnClick(ensureFindViewById(null, R.id.forgot_password_link, "forgot password link"), null); + // this.launchActivityOnClick(ensureFindViewById(null, R.id.forgot_password_link, "forgot password link"), null); } /** - * {@inheritDoc} + * We might have switched to the CreateAccount activity; if that activity + * succeeds, feed its result back to the authenticator. */ @Override - public void onResume() { - super.onResume(); - if (FxAccountAuthenticator.getFirefoxAccounts(this).length > 0) { - redirectToActivity(FxAccountStatusActivity.class); + public void onActivityResult(int requestCode, int resultCode, Intent data) { + Logger.debug(LOG_TAG, "onActivityResult: " + requestCode); + if (requestCode != CHILD_REQUEST_CODE || resultCode != RESULT_OK) { + super.onActivityResult(requestCode, resultCode, data); return; } + this.setResult(resultCode, data); + this.finish(); } protected class SignInDelegate implements RequestDelegate { @@ -119,8 +144,13 @@ public class FxAccountSignInActivity extends FxAccountAbstractSetupActivity { new AndroidFxAccount(activity, account).dump(); } - Toast.makeText(getApplicationContext(), "Got success creating account.", Toast.LENGTH_LONG).show(); - redirectToActivity(FxAccountStatusActivity.class); + // The GetStarted activity has called us and needs to return a result to the authenticator. + final Intent intent = new Intent(); + intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, email); + intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, FxAccountConstants.ACCOUNT_TYPE); + // intent.putExtra(AccountManager.KEY_AUTHTOKEN, accountType); + setResult(RESULT_OK, intent); + finish(); } } diff --git a/mobile/android/base/fxa/activities/FxAccountStatusActivity.java b/mobile/android/base/fxa/activities/FxAccountStatusActivity.java index 2b381f1323f..bd849079799 100644 --- a/mobile/android/base/fxa/activities/FxAccountStatusActivity.java +++ b/mobile/android/base/fxa/activities/FxAccountStatusActivity.java @@ -6,6 +6,7 @@ package org.mozilla.gecko.fxa.activities; import org.mozilla.gecko.R; import org.mozilla.gecko.background.common.log.Logger; +import org.mozilla.gecko.fxa.FxAccountConstants; import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount; import org.mozilla.gecko.fxa.authenticator.FxAccountAuthenticator; @@ -29,6 +30,7 @@ public class FxAccountStatusActivity extends FxAccountAbstractActivity { */ @Override public void onCreate(Bundle icicle) { + Logger.setThreadLogTag(FxAccountConstants.GLOBAL_LOG_TAG); Logger.debug(LOG_TAG, "onCreate(" + icicle + ")"); super.onCreate(icicle); diff --git a/mobile/android/base/fxa/activities/FxAccountUpdateCredentialsActivity.java b/mobile/android/base/fxa/activities/FxAccountUpdateCredentialsActivity.java index 8eb8b35a850..c380475dbf3 100644 --- a/mobile/android/base/fxa/activities/FxAccountUpdateCredentialsActivity.java +++ b/mobile/android/base/fxa/activities/FxAccountUpdateCredentialsActivity.java @@ -66,7 +66,7 @@ public class FxAccountUpdateCredentialsActivity extends FxAccountAbstractSetupAc emailEdit.setEnabled(false); // Not yet implemented. - this.launchActivityOnClick(ensureFindViewById(null, R.id.forgot_password_link, "forgot password link"), null); + // this.launchActivityOnClick(ensureFindViewById(null, R.id.forgot_password_link, "forgot password link"), null); } @Override