From 19084d076a97ba8ea55d84d4cd266ca74071312f Mon Sep 17 00:00:00 2001 From: Sandeep Siddhartha Date: Fri, 10 Oct 2014 17:42:12 -0700 Subject: [PATCH] Cleanup LoginAccountUtils Bug: 17464068 Change-Id: Icf994a7582491afebc5db522c127a30d079ce05c --- .../latin/accounts/LoginAccountUtils.java | 13 +++++----- .../settings/AccountsSettingsFragment.java | 26 ++++++++++++------- .../AccountsSettingsFragmentTests.java | 13 ++++++---- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/java-overridable/src/com/android/inputmethod/latin/accounts/LoginAccountUtils.java b/java-overridable/src/com/android/inputmethod/latin/accounts/LoginAccountUtils.java index 70f152acb..e07a9f358 100644 --- a/java-overridable/src/com/android/inputmethod/latin/accounts/LoginAccountUtils.java +++ b/java-overridable/src/com/android/inputmethod/latin/accounts/LoginAccountUtils.java @@ -16,16 +16,20 @@ package com.android.inputmethod.latin.accounts; -import android.accounts.Account; import android.content.Context; import javax.annotation.Nonnull; -import javax.annotation.Nullable; /** * Utility class for retrieving accounts that may be used for login. */ public class LoginAccountUtils { + /** + * This defines the type of account this class deals with. + * This account type is used when listing the accounts available on the device for login. + */ + public static final String ACCOUNT_TYPE = ""; + private LoginAccountUtils() { // This utility class is not publicly instantiable. } @@ -39,9 +43,4 @@ public class LoginAccountUtils { public static String[] getAccountsForLogin(final Context context) { return new String[0]; } - - @Nullable - public static Account getCurrentAccount(final Context context) { - return null; - } } diff --git a/java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java index c382426c0..5e6521fc4 100644 --- a/java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java +++ b/java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java @@ -101,7 +101,7 @@ public final class AccountsSettingsFragment extends SubScreenFragment { @Override public void onResume() { super.onResume(); - refreshAccountAndDependentPreferences(getCurrentlySelectedAccount()); + refreshAccountAndDependentPreferences(getSignedInAccountName()); } @Override @@ -111,7 +111,7 @@ public final class AccountsSettingsFragment extends SubScreenFragment { prefs.getString(PREF_ACCOUNT_NAME, null)); } else if (TextUtils.equals(key, PREF_ENABLE_CLOUD_SYNC)) { final boolean syncEnabled = prefs.getBoolean(PREF_ENABLE_CLOUD_SYNC, false); - updateSyncPolicy(syncEnabled, LoginAccountUtils.getCurrentAccount(getActivity())); + updateSyncPolicy(syncEnabled, getSignedInAccountName()); } } @@ -182,16 +182,22 @@ public final class AccountsSettingsFragment extends SubScreenFragment { * set or unset the syncable property of the sync authority. * If the account is null, this method is a no-op currently, but we may want * to perform some cleanup in the future. + * + * @param enabled indicates whether the sync preference is enabled or not. + * @param accountToUse indicaes the account to be used for sync, or null if the user + * is not logged in. */ @UsedForTesting - void updateSyncPolicy(boolean enabled, Account accountToUse) { + void updateSyncPolicy(boolean enabled, @Nullable String accountToUse) { if (!ProductionFlags.ENABLE_PERSONAL_DICTIONARY_SYNC) { return; } if (accountToUse != null) { final int syncable = enabled ? 1 : 0; - ContentResolver.setIsSyncable(accountToUse, AUTHORITY, syncable); + ContentResolver.setIsSyncable( + new Account(accountToUse, LoginAccountUtils.ACCOUNT_TYPE), + AUTHORITY, syncable); // TODO: Also add a periodic sync here. // See ContentResolver.addPeriodicSync } else { @@ -202,11 +208,11 @@ public final class AccountsSettingsFragment extends SubScreenFragment { } @Nullable - private String getCurrentlySelectedAccount() { + String getSignedInAccountName() { return getSharedPreferences().getString(LocalSettingsConstants.PREF_ACCOUNT_NAME, null); } - private boolean isSyncEnabled() { + boolean isSyncEnabled() { return getSharedPreferences().getBoolean(PREF_ENABLE_CLOUD_SYNC, false); } @@ -266,12 +272,11 @@ public final class AccountsSettingsFragment extends SubScreenFragment { // Attempt starting sync for the new account if sync was // previously enabled. // If not, stop it. - updateSyncPolicy(isSyncEnabled(), - LoginAccountUtils.getCurrentAccount(getActivity())); + updateSyncPolicy(isSyncEnabled(), getSignedInAccountName()); break; case DialogInterface.BUTTON_NEUTRAL: // Signed out // Stop sync for the account that's being signed out of. - updateSyncPolicy(false, LoginAccountUtils.getCurrentAccount(getActivity())); + updateSyncPolicy(false, getSignedInAccountName()); getSharedPreferences() .edit() .remove(PREF_ACCOUNT_NAME) @@ -288,7 +293,8 @@ public final class AccountsSettingsFragment extends SubScreenFragment { @Override public boolean onPreferenceClick(final Preference preference) { ContentResolver.requestSync( - LoginAccountUtils.getCurrentAccount(getActivity()), AUTHORITY, Bundle.EMPTY); + new Account(getSignedInAccountName(), LoginAccountUtils.ACCOUNT_TYPE), + AUTHORITY, Bundle.EMPTY); return true; } } diff --git a/tests/src/com/android/inputmethod/latin/settings/AccountsSettingsFragmentTests.java b/tests/src/com/android/inputmethod/latin/settings/AccountsSettingsFragmentTests.java index a0b2c2296..273d7faf7 100644 --- a/tests/src/com/android/inputmethod/latin/settings/AccountsSettingsFragmentTests.java +++ b/tests/src/com/android/inputmethod/latin/settings/AccountsSettingsFragmentTests.java @@ -28,6 +28,7 @@ import android.test.suitebuilder.annotation.MediumTest; import android.view.View; import android.widget.ListView; +import com.android.inputmethod.latin.accounts.LoginAccountUtils; import com.android.inputmethod.latin.define.ProductionFlags; import java.util.concurrent.CountDownLatch; @@ -38,7 +39,9 @@ public class AccountsSettingsFragmentTests extends ActivityInstrumentationTestCase2 { private static final String FRAG_NAME = AccountsSettingsFragment.class.getName(); private static final long TEST_TIMEOUT_MILLIS = 5000; - private static final Account TEST_ACCOUNT = new Account("account-for-test", "account-type"); + private static final String TEST_ACCOUNT_NAME = "AccountsSettingsFragmentTests"; + private static final Account TEST_ACCOUNT = + new Account(TEST_ACCOUNT_NAME, LoginAccountUtils.ACCOUNT_TYPE); private AlertDialog mDialog; @@ -154,7 +157,7 @@ public class AccountsSettingsFragmentTests final AccountsSettingsFragment fragment = (AccountsSettingsFragment) getActivity().mFragment; - fragment.updateSyncPolicy(true, TEST_ACCOUNT); + fragment.updateSyncPolicy(true, TEST_ACCOUNT_NAME); // Should be syncable now. assertEquals(1, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY)); @@ -170,7 +173,7 @@ public class AccountsSettingsFragmentTests final AccountsSettingsFragment fragment = (AccountsSettingsFragment) getActivity().mFragment; - fragment.updateSyncPolicy(false, TEST_ACCOUNT); + fragment.updateSyncPolicy(false, TEST_ACCOUNT_NAME); // Should not be syncable now. assertEquals(0, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY)); @@ -186,12 +189,12 @@ public class AccountsSettingsFragmentTests final AccountsSettingsFragment fragment = (AccountsSettingsFragment) getActivity().mFragment; - fragment.updateSyncPolicy(true, TEST_ACCOUNT); + fragment.updateSyncPolicy(true, TEST_ACCOUNT_NAME); // Should be syncable now. assertEquals(1, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY)); - fragment.updateSyncPolicy(false, TEST_ACCOUNT); + fragment.updateSyncPolicy(false, TEST_ACCOUNT_NAME); // Should not be syncable now. assertEquals(0, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY));