Bug 962644 - Part 0: extract firefoxAccountsExist and friends. r=nalexander

This commit is contained in:
Richard Newman 2014-01-31 16:02:42 -08:00
parent cb004c2c6f
commit e835f2949d
5 changed files with 56 additions and 29 deletions

View File

@ -563,6 +563,7 @@ sync_java_files = [
'fxa/authenticator/FxAccountAuthenticatorService.java',
'fxa/authenticator/FxAccountLoginDelegate.java',
'fxa/authenticator/FxAccountLoginException.java',
'fxa/FirefoxAccounts.java',
'fxa/login/BaseRequestDelegate.java',
'fxa/login/Cohabiting.java',
'fxa/login/Doghouse.java',

View File

@ -0,0 +1,46 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.fxa;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
/**
* Simple public accessors for Firefox account objects.
*/
public class FirefoxAccounts {
/**
* Return true if at least one Firefox account exists.
*
* @param context Android context.
* @return true if at least one Firefox account exists.
*/
public static boolean firefoxAccountsExist(final Context context) {
return getFirefoxAccounts(context).length > 0;
}
/**
* Return Firefox accounts.
*
* @param context Android context.
* @return Firefox account objects.
*/
public static Account[] getFirefoxAccounts(final Context context) {
return AccountManager.get(context).getAccountsByType(FxAccountConstants.ACCOUNT_TYPE);
}
/**
* @param context Android context.
* @return the configured Firefox account if one exists, or null otherwise.
*/
public static Account getFirefoxAccount(final Context context) {
Account[] accounts = getFirefoxAccounts(context);
if (accounts.length > 0) {
return accounts[0];
}
return null;
}
}

View File

@ -6,8 +6,8 @@ package org.mozilla.gecko.fxa.activities;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.background.fxa.FxAccountAgeLockoutHelper;
import org.mozilla.gecko.fxa.FirefoxAccounts;
import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
import org.mozilla.gecko.fxa.authenticator.FxAccountAuthenticator;
import org.mozilla.gecko.sync.setup.activities.ActivityUtils;
import android.accounts.Account;
@ -45,12 +45,12 @@ public abstract class FxAccountAbstractActivity extends Activity {
*/
protected void redirectIfAppropriate() {
if (cannotResumeWhenAccountsExist || cannotResumeWhenNoAccountsExist) {
Account accounts[] = FxAccountAuthenticator.getFirefoxAccounts(this);
if (cannotResumeWhenAccountsExist && accounts.length > 0) {
final Account account = FirefoxAccounts.getFirefoxAccount(this);
if (cannotResumeWhenAccountsExist && account != null) {
redirectToActivity(FxAccountStatusActivity.class);
return;
}
if (cannotResumeWhenNoAccountsExist && accounts.length < 1) {
if (cannotResumeWhenNoAccountsExist && account == null) {
redirectToActivity(FxAccountGetStartedActivity.class);
return;
}
@ -136,10 +136,10 @@ public abstract class FxAccountAbstractActivity extends Activity {
* Helper to fetch (unique) Android Firefox Account if one exists, or return null.
*/
protected AndroidFxAccount getAndroidFxAccount() {
Account accounts[] = FxAccountAuthenticator.getFirefoxAccounts(this);
if (accounts.length < 1 || accounts[0] == null) {
Account account = FirefoxAccounts.getFirefoxAccount(this);
if (account == null) {
return null;
}
return new AndroidFxAccount(this, accounts[0]);
return new AndroidFxAccount(this, account);
}
}

View File

@ -10,8 +10,8 @@ import org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.R;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.background.fxa.FxAccountAgeLockoutHelper;
import org.mozilla.gecko.fxa.FirefoxAccounts;
import org.mozilla.gecko.fxa.FxAccountConstants;
import org.mozilla.gecko.fxa.authenticator.FxAccountAuthenticator;
import org.mozilla.gecko.sync.Utils;
import org.mozilla.gecko.sync.setup.activities.ActivityUtils;
@ -64,7 +64,7 @@ public class FxAccountGetStartedActivity extends AccountAuthenticatorActivity {
Intent intent = null;
if (FxAccountAgeLockoutHelper.isLockedOut(SystemClock.elapsedRealtime())) {
intent = new Intent(this, FxAccountCreateAccountNotAllowedActivity.class);
} else if (FxAccountAuthenticator.firefoxAccountsExist(this)) {
} else if (FirefoxAccounts.firefoxAccountsExist(this)) {
intent = new Intent(this, FxAccountStatusActivity.class);
}
if (intent != null) {

View File

@ -117,24 +117,4 @@ public class FxAccountAuthenticator extends AbstractAccountAuthenticator {
return null;
}
/**
* Return Firefox Accounts.
*
* @param context Android context.
* @return Firefox Account objects.
*/
public static Account[] getFirefoxAccounts(final Context context) {
return AccountManager.get(context).getAccountsByType(FxAccountConstants.ACCOUNT_TYPE);
}
/**
* Return true if at least one Firefox Account exists.
*
* @param context Android context.
* @return true if at least one Firefox Account exists.
*/
public static boolean firefoxAccountsExist(final Context context) {
return getFirefoxAccounts(context).length > 0;
}
}