Merge "Cleanup LoginAccountUtils"
This commit is contained in:
commit
59f5988a16
3 changed files with 30 additions and 22 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<TestFragmentActivity> {
|
||||
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));
|
||||
|
|
Loading…
Reference in a new issue