Don't hide sync settings, v2 (cleaner)

Bug:19961809
Change-Id: I02d6bd773dc20112c8fa3b83ac305d0cdbbd58d0
main
Jatin Matani 2015-03-27 16:32:12 -07:00
parent 1b8cd52d7f
commit 66f139889d
3 changed files with 126 additions and 71 deletions

View File

@ -82,6 +82,10 @@
<string name="add_account_to_enable_sync"> <string name="add_account_to_enable_sync">
Please add a Google account to this device to enable this feature Please add a Google account to this device to enable this feature
</string> </string>
<!-- Text to tell the user that sync feature is disabled because they have Google Apps for Business account(s) on their device. [CHAR LIMIT=75]-->
<string name="cloud_sync_summary_disabled_work_profile">
Sync is not available for devices with Google Apps for Business accounts
</string>
<!-- Option name for including other IMEs in the language switch list [CHAR LIMIT=30] --> <!-- Option name for including other IMEs in the language switch list [CHAR LIMIT=30] -->
<string name="include_other_imes_in_language_switch_list">Switch to other input methods</string> <string name="include_other_imes_in_language_switch_list">Switch to other input methods</string>

View File

@ -26,7 +26,8 @@
android:key="account_switcher" android:key="account_switcher"
android:persistent="false" android:persistent="false"
android:title="@string/switch_accounts" android:title="@string/switch_accounts"
android:summary="@string/no_accounts_selected" /> android:summary="@string/no_accounts_selected"
android:enabled="false" />
<!-- Summary will be set programmatically to reflect the account status --> <!-- Summary will be set programmatically to reflect the account status -->
<CheckBoxPreference <CheckBoxPreference
@ -34,7 +35,8 @@
android:title="@string/cloud_sync_title" android:title="@string/cloud_sync_title"
android:defaultValue="false" android:defaultValue="false"
android:persistent="true" android:persistent="true"
android:disableDependentsState="false" /> android:disableDependentsState="false"
android:enabled="false" />
<!-- This preference (acts like a button) enables the user to initiate an one time sync. --> <!-- This preference (acts like a button) enables the user to initiate an one time sync. -->
<Preference android:key="pref_sync_now" <Preference android:key="pref_sync_now"

View File

@ -42,6 +42,8 @@ import com.android.inputmethod.latin.accounts.LoginAccountUtils;
import com.android.inputmethod.latin.define.ProductionFlags; import com.android.inputmethod.latin.define.ProductionFlags;
import com.android.inputmethod.latin.utils.ManagedProfileUtils; import com.android.inputmethod.latin.utils.ManagedProfileUtils;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.Nullable; import javax.annotation.Nullable;
/** /**
@ -96,12 +98,26 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
*/ */
private Preference mAccountSwitcher; private Preference mAccountSwitcher;
/**
* Stores if we are currently detecting a managed profile.
*/
private AtomicBoolean mManagedProfileBeingDetected = new AtomicBoolean(true);
/**
* Stores if we have successfully detected if the device has a managed profile.
*/
private AtomicBoolean mHasManagedProfile = new AtomicBoolean(false);
@Override @Override
public void onCreate(final Bundle icicle) { public void onCreate(final Bundle icicle) {
super.onCreate(icicle); super.onCreate(icicle);
addPreferencesFromResource(R.xml.prefs_screen_accounts); addPreferencesFromResource(R.xml.prefs_screen_accounts);
mAccountSwitcher = findPreference(PREF_ACCCOUNT_SWITCHER);
mEnableSyncPreference = (TwoStatePreference) findPreference(PREF_ENABLE_SYNC_NOW);
mSyncNowPreference = findPreference(PREF_SYNC_NOW);
mClearSyncDataPreference = findPreference(PREF_CLEAR_SYNC_DATA);
if (ProductionFlags.IS_METRICS_LOGGING_SUPPORTED) { if (ProductionFlags.IS_METRICS_LOGGING_SUPPORTED) {
final Preference enableMetricsLogging = final Preference enableMetricsLogging =
findPreference(Settings.PREF_ENABLE_METRICS_LOGGING); findPreference(Settings.PREF_ENABLE_METRICS_LOGGING);
@ -118,8 +134,7 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) { if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) {
removeSyncPreferences(); removeSyncPreferences();
} else { } else {
// Temporarily disable the preferences till we can // Disable by default till we are sure we can enable this.
// check that we don't have a work profile.
disableSyncPreferences(); disableSyncPreferences();
new ManagedProfileCheckerTask(this).execute(); new ManagedProfileCheckerTask(this).execute();
} }
@ -129,7 +144,7 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
* Task to check work profile. If found, it removes the sync prefs. If not, * Task to check work profile. If found, it removes the sync prefs. If not,
* it enables them. * it enables them.
*/ */
private static class ManagedProfileCheckerTask extends AsyncTask<Void, Void, Void> { private static class ManagedProfileCheckerTask extends AsyncTask<Void, Void, Boolean> {
private final AccountsSettingsFragment mFragment; private final AccountsSettingsFragment mFragment;
private ManagedProfileCheckerTask(final AccountsSettingsFragment fragment) { private ManagedProfileCheckerTask(final AccountsSettingsFragment fragment) {
@ -137,56 +152,70 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
} }
@Override @Override
protected Void doInBackground(Void... params) { protected void onPreExecute() {
if (ManagedProfileUtils.getInstance().hasWorkProfile(mFragment.getActivity())) { mFragment.mManagedProfileBeingDetected.set(true);
mFragment.removeSyncPreferences(); }
} else { @Override
mFragment.refreshAccountAndDependentPreferences( protected Boolean doInBackground(Void... params) {
mFragment.getSignedInAccountName()); return ManagedProfileUtils.getInstance().hasWorkProfile(mFragment.getActivity());
} }
return null;
@Override
protected void onPostExecute(final Boolean hasWorkProfile) {
mFragment.mHasManagedProfile.set(hasWorkProfile);
mFragment.mManagedProfileBeingDetected.set(false);
mFragment.refreshSyncSettingsUI();
} }
} }
private void enableSyncPreferences() { private void enableSyncPreferences(final String[] accountsForLogin,
mAccountSwitcher = findPreference(PREF_ACCCOUNT_SWITCHER); final String currentAccountName) {
if (mAccountSwitcher == null) { if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) {
// Preference has been removed because the device has a managed profile.
return; return;
} }
mAccountSwitcher.setEnabled(true); mAccountSwitcher.setEnabled(true);
mEnableSyncPreference = (TwoStatePreference) findPreference(PREF_ENABLE_SYNC_NOW);
mEnableSyncPreference.setEnabled(true); mEnableSyncPreference.setEnabled(true);
mEnableSyncPreference.setOnPreferenceClickListener(mEnableSyncClickListener); mEnableSyncPreference.setOnPreferenceClickListener(mEnableSyncClickListener);
mSyncNowPreference = findPreference(PREF_SYNC_NOW);
mSyncNowPreference.setEnabled(true); mSyncNowPreference.setEnabled(true);
mSyncNowPreference.setOnPreferenceClickListener(mSyncNowListener); mSyncNowPreference.setOnPreferenceClickListener(mSyncNowListener);
mClearSyncDataPreference = findPreference(PREF_CLEAR_SYNC_DATA); mClearSyncDataPreference.setEnabled(true);
mSyncNowPreference.setEnabled(true);
mClearSyncDataPreference.setOnPreferenceClickListener(mDeleteSyncDataListener); mClearSyncDataPreference.setOnPreferenceClickListener(mDeleteSyncDataListener);
if (currentAccountName != null) {
mAccountSwitcher.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(final Preference preference) {
if (accountsForLogin.length > 0) {
// TODO: Add addition of account.
createAccountPicker(accountsForLogin, getSignedInAccountName(),
new AccountChangedListener(null)).show();
}
return true;
}
});
}
} }
/**
* Two reasons for disable - work profile or no accounts on device.
*/
private void disableSyncPreferences() { private void disableSyncPreferences() {
mAccountSwitcher = findPreference(PREF_ACCCOUNT_SWITCHER); if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) {
if (mAccountSwitcher == null) {
// Preference has been removed because the device has a managed profile.
return; return;
} }
mAccountSwitcher.setEnabled(false); mAccountSwitcher.setEnabled(false);
mEnableSyncPreference = (TwoStatePreference) findPreference(PREF_ENABLE_SYNC_NOW);
mEnableSyncPreference.setEnabled(false); mEnableSyncPreference.setEnabled(false);
mSyncNowPreference = findPreference(PREF_SYNC_NOW);
mSyncNowPreference.setEnabled(false);
mClearSyncDataPreference = findPreference(PREF_CLEAR_SYNC_DATA);
mSyncNowPreference.setEnabled(false); mSyncNowPreference.setEnabled(false);
mClearSyncDataPreference.setEnabled(false);
} }
/**
* Called only when ProductionFlag is turned off.
*/
private void removeSyncPreferences() { private void removeSyncPreferences() {
removePreference(PREF_ACCCOUNT_SWITCHER); removePreference(PREF_ACCCOUNT_SWITCHER);
removePreference(PREF_ENABLE_CLOUD_SYNC); removePreference(PREF_ENABLE_CLOUD_SYNC);
@ -197,20 +226,20 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
refreshAccountAndDependentPreferences(getSignedInAccountName()); refreshSyncSettingsUI();
} }
@Override @Override
public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
if (TextUtils.equals(key, PREF_ACCOUNT_NAME)) { if (TextUtils.equals(key, PREF_ACCOUNT_NAME)) {
refreshAccountAndDependentPreferences(prefs.getString(PREF_ACCOUNT_NAME, null)); refreshSyncSettingsUI();
} 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);
mEnableSyncPreference = (TwoStatePreference) findPreference(PREF_ENABLE_SYNC_NOW); mEnableSyncPreference = (TwoStatePreference) findPreference(PREF_ENABLE_SYNC_NOW);
if (syncEnabled) { final boolean syncEnabled = prefs.getBoolean(PREF_ENABLE_CLOUD_SYNC, false);
mEnableSyncPreference.setSummary(R.string.cloud_sync_summary); if (isSyncEnabled()) {
mEnableSyncPreference.setSummary(getString(R.string.cloud_sync_summary));
} else { } else {
mEnableSyncPreference.setSummary(R.string.cloud_sync_summary_disabled); mEnableSyncPreference.setSummary(getString(R.string.cloud_sync_summary_disabled));
} }
AccountStateChangedListener.onSyncPreferenceChanged(getSignedInAccountName(), AccountStateChangedListener.onSyncPreferenceChanged(getSignedInAccountName(),
syncEnabled); syncEnabled);
@ -218,47 +247,67 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
} }
/** /**
* Summarizes what account is being used and turns off dependent preferences if no account * Checks different states like whether account is present or managed profile is present
* is currently selected. * and sets the sync settings accordingly.
*/ */
private void refreshAccountAndDependentPreferences(@Nullable final String currentAccount) { private void refreshSyncSettingsUI() {
// TODO(cvnguyen): Write tests. if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) {
if (!ProductionFlags.ENABLE_ACCOUNT_SIGN_IN) { return;
}
final String[] accountsForLogin =
LoginAccountUtils.getAccountsForLogin(getActivity());
final String currentAccount = getSignedInAccountName();
if (!mManagedProfileBeingDetected.get() &&
!mHasManagedProfile.get() && accountsForLogin.length > 0) {
// Sync can be used by user; enable all preferences.
enableSyncPreferences(accountsForLogin, currentAccount);
} else {
// Sync cannot be used by user; disable all preferences.
disableSyncPreferences();
}
refreshSyncSettingsMessaging(mManagedProfileBeingDetected.get(),
mHasManagedProfile.get(), accountsForLogin.length > 0,
currentAccount);
}
/**
* @param managedProfileBeingDetected whether we are in process of determining work profile.
* @param hasManagedProfile whether the device has work profile.
* @param hasAccountsForLogin whether the device has enough accounts for login.
* @param currentAccount the account currently selected in the application.
*/
private void refreshSyncSettingsMessaging(boolean managedProfileBeingDetected,
boolean hasManagedProfile, boolean hasAccountsForLogin, String currentAccount) {
if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) {
return; return;
} }
final String[] accountsForLogin = // If we are determining eligiblity, we show empty summaries.
LoginAccountUtils.getAccountsForLogin(getActivity()); // Once we have some deterministic result, we set summaries based on different results.
if (managedProfileBeingDetected) {
if (accountsForLogin.length > 0) { mEnableSyncPreference.setSummary("");
enableSyncPreferences(); mAccountSwitcher.setSummary("");
if (mAccountSwitcher == null) { } else if (hasManagedProfile) {
return; mEnableSyncPreference.setSummary(
} getString(R.string.cloud_sync_summary_disabled_work_profile));
mAccountSwitcher.setOnPreferenceClickListener(new OnPreferenceClickListener() { } else if (!hasAccountsForLogin) {
@Override
public boolean onPreferenceClick(final Preference preference) {
if (accountsForLogin.length > 0) {
// TODO: Add addition of account.
createAccountPicker(accountsForLogin, currentAccount,
new AccountChangedListener(null)).show();
}
return true;
}
});
} else {
mAccountSwitcher.setEnabled(false);
disableSyncPreferences();
mEnableSyncPreference.setSummary(getString(R.string.add_account_to_enable_sync)); mEnableSyncPreference.setSummary(getString(R.string.add_account_to_enable_sync));
} else if (isSyncEnabled()) {
mEnableSyncPreference.setSummary(getString(R.string.cloud_sync_summary));
} else {
mEnableSyncPreference.setSummary(getString(R.string.cloud_sync_summary_disabled));
} }
if (currentAccount == null) { // Set some interdependent settings.
// No account is currently selected; switch enable sync preference off. // No account automatically turns off sync.
mAccountSwitcher.setSummary(getString(R.string.no_accounts_selected)); if (!managedProfileBeingDetected && !hasManagedProfile) {
mEnableSyncPreference.setChecked(false); if (currentAccount != null) {
} else { mAccountSwitcher.setSummary(getString(R.string.account_selected, currentAccount));
// Set the currently selected account as the summary text. } else {
mAccountSwitcher.setSummary(getString(R.string.account_selected, currentAccount)); mEnableSyncPreference.setChecked(false);
mAccountSwitcher.setSummary(getString(R.string.no_accounts_selected));
}
} }
} }