From 7d28a1c27936fce22af99d0ae5ec63e609eeac3e Mon Sep 17 00:00:00 2001 From: Vikram Aggarwal Date: Fri, 18 Nov 2011 11:10:29 -0800 Subject: [PATCH] Accurately update the preference summary string when changing the vibrate setting. Fix b/5639748 Change-Id: Ice6d4770c156104495d476826a636487f4df5f1b --- .../setup/AccountSettingsFragment.java | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/com/android/email/activity/setup/AccountSettingsFragment.java b/src/com/android/email/activity/setup/AccountSettingsFragment.java index 5f8f18992..ed5e9f0e0 100644 --- a/src/com/android/email/activity/setup/AccountSettingsFragment.java +++ b/src/com/android/email/activity/setup/AccountSettingsFragment.java @@ -506,15 +506,35 @@ public class AccountSettingsFragment extends PreferenceFragment { mAccountVibrateWhen = (ListPreference) findPreference(PREFERENCE_VIBRATE_WHEN); Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); if (vibrator.hasVibrator()) { - boolean flagsVibrate = 0 != (mAccount.getFlags() & Account.FLAGS_VIBRATE_ALWAYS); - boolean flagsVibrateSilent = + // Calculate the value to set based on the choices, and set the value. + final boolean vibrateAlways = 0 != (mAccount.getFlags() & Account.FLAGS_VIBRATE_ALWAYS); + final boolean vibrateWhenSilent = 0 != (mAccount.getFlags() & Account.FLAGS_VIBRATE_WHEN_SILENT); - mAccountVibrateWhen.setValue( - flagsVibrate ? PREFERENCE_VALUE_VIBRATE_WHEN_ALWAYS : - flagsVibrateSilent ? PREFERENCE_VALUE_VIBRATE_WHEN_SILENT : - PREFERENCE_VALUE_VIBRATE_WHEN_NEVER); - mAccountVibrateWhen.setOnPreferenceChangeListener(mPreferenceChangeListener); + final String vibrateSetting = + vibrateAlways ? PREFERENCE_VALUE_VIBRATE_WHEN_ALWAYS : + vibrateWhenSilent ? PREFERENCE_VALUE_VIBRATE_WHEN_SILENT : + PREFERENCE_VALUE_VIBRATE_WHEN_NEVER; + mAccountVibrateWhen.setValue(vibrateSetting); + + // Update the summary string. + final int index = mAccountVibrateWhen.findIndexOfValue(vibrateSetting); + mAccountVibrateWhen.setSummary(mAccountVibrateWhen.getEntries()[index]); + + // When the value is changed, update the summary in addition to the setting. + mAccountVibrateWhen.setOnPreferenceChangeListener( + new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + final String vibrateSetting = newValue.toString(); + final int index = mAccountVibrateWhen.findIndexOfValue(vibrateSetting); + mAccountVibrateWhen.setSummary(mAccountVibrateWhen.getEntries()[index]); + mAccountVibrateWhen.setValue(vibrateSetting); + onPreferenceChanged(PREFERENCE_VIBRATE_WHEN, newValue); + return false; + } + }); } else { + // No vibrator present. Remove the preference altogether. PreferenceCategory notificationsCategory = (PreferenceCategory) findPreference(PREFERENCE_CATEGORY_NOTIFICATIONS); notificationsCategory.removePreference(mAccountVibrateWhen);