Cleanup LoginAccountUtils

Bug: 17464068
Change-Id: Icf994a7582491afebc5db522c127a30d079ce05c
main
Sandeep Siddhartha 2014-10-10 17:42:12 -07:00
parent d9015233f5
commit 19084d076a
3 changed files with 30 additions and 22 deletions

View File

@ -16,16 +16,20 @@
package com.android.inputmethod.latin.accounts; package com.android.inputmethod.latin.accounts;
import android.accounts.Account;
import android.content.Context; import android.content.Context;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/** /**
* Utility class for retrieving accounts that may be used for login. * Utility class for retrieving accounts that may be used for login.
*/ */
public class LoginAccountUtils { 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() { private LoginAccountUtils() {
// This utility class is not publicly instantiable. // This utility class is not publicly instantiable.
} }
@ -39,9 +43,4 @@ public class LoginAccountUtils {
public static String[] getAccountsForLogin(final Context context) { public static String[] getAccountsForLogin(final Context context) {
return new String[0]; return new String[0];
} }
@Nullable
public static Account getCurrentAccount(final Context context) {
return null;
}
} }

View File

@ -101,7 +101,7 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
refreshAccountAndDependentPreferences(getCurrentlySelectedAccount()); refreshAccountAndDependentPreferences(getSignedInAccountName());
} }
@Override @Override
@ -111,7 +111,7 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
prefs.getString(PREF_ACCOUNT_NAME, null)); prefs.getString(PREF_ACCOUNT_NAME, null));
} else if (TextUtils.equals(key, PREF_ENABLE_CLOUD_SYNC)) { } else if (TextUtils.equals(key, PREF_ENABLE_CLOUD_SYNC)) {
final boolean syncEnabled = prefs.getBoolean(PREF_ENABLE_CLOUD_SYNC, false); 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. * 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 * If the account is null, this method is a no-op currently, but we may want
* to perform some cleanup in the future. * 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 @UsedForTesting
void updateSyncPolicy(boolean enabled, Account accountToUse) { void updateSyncPolicy(boolean enabled, @Nullable String accountToUse) {
if (!ProductionFlags.ENABLE_PERSONAL_DICTIONARY_SYNC) { if (!ProductionFlags.ENABLE_PERSONAL_DICTIONARY_SYNC) {
return; return;
} }
if (accountToUse != null) { if (accountToUse != null) {
final int syncable = enabled ? 1 : 0; 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. // TODO: Also add a periodic sync here.
// See ContentResolver.addPeriodicSync // See ContentResolver.addPeriodicSync
} else { } else {
@ -202,11 +208,11 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
} }
@Nullable @Nullable
private String getCurrentlySelectedAccount() { String getSignedInAccountName() {
return getSharedPreferences().getString(LocalSettingsConstants.PREF_ACCOUNT_NAME, null); return getSharedPreferences().getString(LocalSettingsConstants.PREF_ACCOUNT_NAME, null);
} }
private boolean isSyncEnabled() { boolean isSyncEnabled() {
return getSharedPreferences().getBoolean(PREF_ENABLE_CLOUD_SYNC, false); 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 // Attempt starting sync for the new account if sync was
// previously enabled. // previously enabled.
// If not, stop it. // If not, stop it.
updateSyncPolicy(isSyncEnabled(), updateSyncPolicy(isSyncEnabled(), getSignedInAccountName());
LoginAccountUtils.getCurrentAccount(getActivity()));
break; break;
case DialogInterface.BUTTON_NEUTRAL: // Signed out case DialogInterface.BUTTON_NEUTRAL: // Signed out
// Stop sync for the account that's being signed out of. // Stop sync for the account that's being signed out of.
updateSyncPolicy(false, LoginAccountUtils.getCurrentAccount(getActivity())); updateSyncPolicy(false, getSignedInAccountName());
getSharedPreferences() getSharedPreferences()
.edit() .edit()
.remove(PREF_ACCOUNT_NAME) .remove(PREF_ACCOUNT_NAME)
@ -288,7 +293,8 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
@Override @Override
public boolean onPreferenceClick(final Preference preference) { public boolean onPreferenceClick(final Preference preference) {
ContentResolver.requestSync( ContentResolver.requestSync(
LoginAccountUtils.getCurrentAccount(getActivity()), AUTHORITY, Bundle.EMPTY); new Account(getSignedInAccountName(), LoginAccountUtils.ACCOUNT_TYPE),
AUTHORITY, Bundle.EMPTY);
return true; return true;
} }
} }

View File

@ -28,6 +28,7 @@ import android.test.suitebuilder.annotation.MediumTest;
import android.view.View; import android.view.View;
import android.widget.ListView; import android.widget.ListView;
import com.android.inputmethod.latin.accounts.LoginAccountUtils;
import com.android.inputmethod.latin.define.ProductionFlags; import com.android.inputmethod.latin.define.ProductionFlags;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
@ -38,7 +39,9 @@ public class AccountsSettingsFragmentTests
extends ActivityInstrumentationTestCase2<TestFragmentActivity> { extends ActivityInstrumentationTestCase2<TestFragmentActivity> {
private static final String FRAG_NAME = AccountsSettingsFragment.class.getName(); private static final String FRAG_NAME = AccountsSettingsFragment.class.getName();
private static final long TEST_TIMEOUT_MILLIS = 5000; 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; private AlertDialog mDialog;
@ -154,7 +157,7 @@ public class AccountsSettingsFragmentTests
final AccountsSettingsFragment fragment = final AccountsSettingsFragment fragment =
(AccountsSettingsFragment) getActivity().mFragment; (AccountsSettingsFragment) getActivity().mFragment;
fragment.updateSyncPolicy(true, TEST_ACCOUNT); fragment.updateSyncPolicy(true, TEST_ACCOUNT_NAME);
// Should be syncable now. // Should be syncable now.
assertEquals(1, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY)); assertEquals(1, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY));
@ -170,7 +173,7 @@ public class AccountsSettingsFragmentTests
final AccountsSettingsFragment fragment = final AccountsSettingsFragment fragment =
(AccountsSettingsFragment) getActivity().mFragment; (AccountsSettingsFragment) getActivity().mFragment;
fragment.updateSyncPolicy(false, TEST_ACCOUNT); fragment.updateSyncPolicy(false, TEST_ACCOUNT_NAME);
// Should not be syncable now. // Should not be syncable now.
assertEquals(0, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY)); assertEquals(0, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY));
@ -186,12 +189,12 @@ public class AccountsSettingsFragmentTests
final AccountsSettingsFragment fragment = final AccountsSettingsFragment fragment =
(AccountsSettingsFragment) getActivity().mFragment; (AccountsSettingsFragment) getActivity().mFragment;
fragment.updateSyncPolicy(true, TEST_ACCOUNT); fragment.updateSyncPolicy(true, TEST_ACCOUNT_NAME);
// Should be syncable now. // Should be syncable now.
assertEquals(1, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY)); assertEquals(1, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY));
fragment.updateSyncPolicy(false, TEST_ACCOUNT); fragment.updateSyncPolicy(false, TEST_ACCOUNT_NAME);
// Should not be syncable now. // Should not be syncable now.
assertEquals(0, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY)); assertEquals(0, ContentResolver.getIsSyncable(TEST_ACCOUNT, AUTHORITY));