Change Email vibrate setting from tri-state to checkbox

Migrate the old value as necessary.
Update the unit tests.

Bug: 7624838
Change-Id: I1c276ffde3496cbc66846ed4a007088d39f20382
diff --git a/emailcommon/src/com/android/emailcommon/provider/Account.java b/emailcommon/src/com/android/emailcommon/provider/Account.java
index 97a6f12..1215155 100755
--- a/emailcommon/src/com/android/emailcommon/provider/Account.java
+++ b/emailcommon/src/com/android/emailcommon/provider/Account.java
@@ -70,7 +70,7 @@
     // Whether or not the user has asked for notifications of new mail in this account
     public final static int FLAGS_NOTIFY_NEW_MAIL = 1<<0;
     // Whether or not the user has asked for vibration notifications with all new mail
-    public final static int FLAGS_VIBRATE_ALWAYS = 1<<1;
+    public final static int FLAGS_VIBRATE = 1<<1;
     // Bit mask for the account's deletion policy (see DELETE_POLICY_x below)
     public static final int FLAGS_DELETE_POLICY_MASK = 1<<2 | 1<<3;
     public static final int FLAGS_DELETE_POLICY_SHIFT = 2;
@@ -82,8 +82,6 @@
     // required by the server; in this state, the user MUST be alerted to the need to update
     // security settings.  Sync adapters SHOULD NOT attempt to sync when this flag is set.
     public static final int FLAGS_SECURITY_HOLD = 1<<5;
-    // Whether or not the user has asked for vibration notifications when the ringer is silent
-    public static final int FLAGS_VIBRATE_WHEN_SILENT = 1<<6;
     // Whether the account supports "smart forward" (i.e. the server appends the original
     // message along with any attachments to the outgoing message)
     public static final int FLAGS_SUPPORTS_SMART_FORWARD = 1<<7;
@@ -373,8 +371,7 @@
     /**
      * @return the flags for this account
      * @see #FLAGS_NOTIFY_NEW_MAIL
-     * @see #FLAGS_VIBRATE_ALWAYS
-     * @see #FLAGS_VIBRATE_WHEN_SILENT
+     * @see #FLAGS_VIBRATE
      */
     public int getFlags() {
         return mFlags;
@@ -383,8 +380,7 @@
     /**
      * Set the flags for this account
      * @see #FLAGS_NOTIFY_NEW_MAIL
-     * @see #FLAGS_VIBRATE_ALWAYS
-     * @see #FLAGS_VIBRATE_WHEN_SILENT
+     * @see #FLAGS_VIBRATE
      * @param newFlags the new value for the flags
      */
     public void setFlags(int newFlags) {
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index c881e6e..33a1928 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -109,23 +109,6 @@
         <item>6</item>
     </string-array>
 
-    <!-- The vibrate notification modes -->
-    <string-array name="account_settings_vibrate_when_entries">
-        <!-- Always -->
-        <item>@string/account_settings_vibrate_when_always</item>
-        <!-- Only when the phone is in Silent mode -->
-        <item>@string/account_settings_vibrate_when_silent</item>
-        <!-- Never -->
-        <item>@string/account_settings_vibrate_when_never</item>
-    </string-array>
-
-    <!-- The vibrate notification values -->
-    <string-array translatable="false" name="account_settings_vibrate_when_values">
-        <item>always</item>
-        <item>silent</item>
-        <item>never</item>
-    </string-array>
-
     <!-- Auto-advance options (DO NOT change the order.  Code assumes this order)-->
     <string-array name="general_preference_auto_advance_entries">
         <!-- Move to newer -->
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 74280a4..c39ee4a 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -988,17 +988,6 @@
 
     <!-- On Settings screen, vibrate pop-up menu label -->
     <string name="account_settings_vibrate_when_label">Vibrate</string>
-    <!-- On Settings screen, vibrate pop-up menu summary text -->
-    <string name="account_settings_vibrate_when_summary">Also vibrate when email arrives</string>
-    <!-- On Settings screen, vibrate pop-up menu option "always" text -->
-    <string name="account_settings_vibrate_when_always">Always</string>
-    <!-- On Settings screen, vibrate pop-up menu option "silent" text -->
-    <string name="account_settings_vibrate_when_silent">Only when silent</string>
-    <!-- On Settings screen, vibrate pop-up menu option "never" text -->
-    <string name="account_settings_vibrate_when_never">Never</string>
-
-    <!-- Dialog title for the Vibrate dialog -->
-    <string name="account_settings_vibrate_when_dlg_title">Vibrate</string>
 
     <!-- On Settings screen, setting option name -->
     <string name="account_settings_ringtone">Choose ringtone</string>
diff --git a/res/xml/account_settings_preferences.xml b/res/xml/account_settings_preferences.xml
index 7b6fa9b..66a71f3 100644
--- a/res/xml/account_settings_preferences.xml
+++ b/res/xml/account_settings_preferences.xml
@@ -107,16 +107,11 @@
             android:ringtoneType="notification"
             android:defaultValue="content://settings/system/notification_sound" />
 
-        <ListPreference
-            android:key="account_settings_vibrate_when"
-            android:layout="?android:attr/preferenceLayoutChild"
+        <CheckBoxPreference
+            android:key="account_settings_vibrate"
             android:dependency="account_notify"
-            android:defaultValue="never"
-            android:title="@string/account_settings_vibrate_when_label"
-            android:summary="@string/account_settings_vibrate_when_summary"
-            android:entries="@array/account_settings_vibrate_when_entries"
-            android:entryValues="@array/account_settings_vibrate_when_values"
-            android:dialogTitle="@string/account_settings_vibrate_when_dlg_title" />
+            android:defaultValue="false"
+            android:title="@string/account_settings_vibrate_when_label" />
 
     </PreferenceCategory>
 
diff --git a/src/com/android/email/NotificationController.java b/src/com/android/email/NotificationController.java
index 1dc2149..529d721 100644
--- a/src/com/android/email/NotificationController.java
+++ b/src/com/android/email/NotificationController.java
@@ -668,12 +668,10 @@
     void setupSoundAndVibration(Notification.Builder builder, Account account) {
         final int flags = account.mFlags;
         final String ringtoneUri = account.mRingtoneUri;
-        final boolean vibrate = (flags & Account.FLAGS_VIBRATE_ALWAYS) != 0;
-        final boolean vibrateWhenSilent = (flags & Account.FLAGS_VIBRATE_WHEN_SILENT) != 0;
-        final boolean isRingerSilent = getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
+        final boolean vibrate = (flags & Account.FLAGS_VIBRATE) != 0;
 
         int defaults = Notification.DEFAULT_LIGHTS;
-        if (vibrate || (vibrateWhenSilent && isRingerSilent)) {
+        if (vibrate) {
             defaults |= Notification.DEFAULT_VIBRATE;
         }
 
diff --git a/src/com/android/email/activity/setup/AccountSettingsFragment.java b/src/com/android/email/activity/setup/AccountSettingsFragment.java
index 7e19076..02ef74c 100644
--- a/src/com/android/email/activity/setup/AccountSettingsFragment.java
+++ b/src/com/android/email/activity/setup/AccountSettingsFragment.java
@@ -78,7 +78,8 @@
     private static final String PREFERENCE_CATEGORY_DATA_USAGE = "data_usage";
     private static final String PREFERENCE_CATEGORY_NOTIFICATIONS = "account_notifications";
     private static final String PREFERENCE_NOTIFY = "account_notify";
-    private static final String PREFERENCE_VIBRATE_WHEN = "account_settings_vibrate_when";
+    private static final String PREFERENCE_VIBRATE = "account_settings_vibrate";
+    private static final String PREFERENCE_VIBRATE_OLD = "account_settings_vibrate_when";
     private static final String PREFERENCE_RINGTONE = "account_ringtone";
     private static final String PREFERENCE_CATEGORY_SERVER = "account_servers";
     private static final String PREFERENCE_INCOMING = "incoming";
@@ -88,11 +89,6 @@
     private static final String PREFERENCE_SYNC_EMAIL = "account_sync_email";
     private static final String PREFERENCE_DELETE_ACCOUNT = "delete_account";
 
-    // These strings must match account_settings_vibrate_when_* strings in strings.xml
-    private static final String PREFERENCE_VALUE_VIBRATE_WHEN_ALWAYS = "always";
-    private static final String PREFERENCE_VALUE_VIBRATE_WHEN_SILENT = "silent";
-    private static final String PREFERENCE_VALUE_VIBRATE_WHEN_NEVER = "never";
-
     private EditTextPreference mAccountDescription;
     private EditTextPreference mAccountName;
     private EditTextPreference mAccountSignature;
@@ -101,7 +97,7 @@
     private CheckBoxPreference mAccountBackgroundAttachments;
     private CheckBoxPreference mAccountDefault;
     private CheckBoxPreference mAccountNotify;
-    private ListPreference mAccountVibrateWhen;
+    private CheckBoxPreference mAccountVibrate;
     private RingtonePreference mAccountRingtone;
     private CheckBoxPreference mSyncContacts;
     private CheckBoxPreference mSyncCalendar;
@@ -175,6 +171,8 @@
         }
         super.onCreate(savedInstanceState);
 
+        upgradeVibrateSetting();
+
         // Load the preferences from an XML resource
         addPreferencesFromResource(R.xml.account_settings_preferences);
 
@@ -192,6 +190,20 @@
         mAccountDirty = false;
     }
 
+    /**
+     * Upgrades the old tri-state vibrate setting to the new boolean value.
+     */
+    private void upgradeVibrateSetting() {
+        final SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences();
+
+        if (!sharedPreferences.contains(PREFERENCE_VIBRATE)) {
+            // Try to migrate the old one
+            final boolean vibrate =
+                    "always".equals(sharedPreferences.getString(PREFERENCE_VIBRATE_OLD, ""));
+            sharedPreferences.edit().putBoolean(PREFERENCE_VIBRATE, vibrate);
+        }
+    }
+
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
@@ -502,33 +514,21 @@
         prefs.edit().putString(PREFERENCE_RINGTONE, mAccount.getRingtone()).apply();
 
         // Set the vibrator value, or hide it on devices w/o a vibrator
-        mAccountVibrateWhen = (ListPreference) findPreference(PREFERENCE_VIBRATE_WHEN);
+        mAccountVibrate = (CheckBoxPreference) findPreference(PREFERENCE_VIBRATE);
         Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
         if (vibrator.hasVibrator()) {
             // 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);
-            final String vibrateSetting =
-                    vibrateAlways ? PREFERENCE_VALUE_VIBRATE_WHEN_ALWAYS :
-                        vibrateWhenSilent ? PREFERENCE_VALUE_VIBRATE_WHEN_SILENT :
-                            PREFERENCE_VALUE_VIBRATE_WHEN_NEVER;
-            mAccountVibrateWhen.setValue(vibrateSetting);
+            final boolean vibrate = 0 != (mAccount.getFlags() & Account.FLAGS_VIBRATE);
+            mAccountVibrate.setChecked(vibrate);
 
-            // 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(
+            // When the value is changed, update the setting.
+            mAccountVibrate.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);
+                            final boolean vibrateSetting = (Boolean) newValue;
+                            mAccountVibrate.setChecked(vibrateSetting);
+                            onPreferenceChanged(PREFERENCE_VIBRATE, newValue);
                             return false;
                         }
                     });
@@ -536,7 +536,7 @@
             // No vibrator present. Remove the preference altogether.
             PreferenceCategory notificationsCategory = (PreferenceCategory)
                     findPreference(PREFERENCE_CATEGORY_NOTIFICATIONS);
-            notificationsCategory.removePreference(mAccountVibrateWhen);
+            notificationsCategory.removePreference(mAccountVibrate);
         }
 
         findPreference(PREFERENCE_INCOMING).setOnPreferenceClickListener(
@@ -639,7 +639,7 @@
         // Turn off all controlled flags - will turn them back on while checking UI elements
         int newFlags = mAccount.getFlags() &
                 ~(Account.FLAGS_NOTIFY_NEW_MAIL |
-                        Account.FLAGS_VIBRATE_ALWAYS | Account.FLAGS_VIBRATE_WHEN_SILENT |
+                        Account.FLAGS_VIBRATE |
                         Account.FLAGS_BACKGROUND_ATTACHMENTS);
 
         newFlags |= mAccountBackgroundAttachments.isChecked() ?
@@ -655,10 +655,8 @@
         if (mSyncWindow != null) {
             mAccount.setSyncLookback(Integer.parseInt(mSyncWindow.getValue()));
         }
-        if (mAccountVibrateWhen.getValue().equals(PREFERENCE_VALUE_VIBRATE_WHEN_ALWAYS)) {
-            newFlags |= Account.FLAGS_VIBRATE_ALWAYS;
-        } else if (mAccountVibrateWhen.getValue().equals(PREFERENCE_VALUE_VIBRATE_WHEN_SILENT)) {
-            newFlags |= Account.FLAGS_VIBRATE_WHEN_SILENT;
+        if (mAccountVibrate.isChecked()) {
+            newFlags |= Account.FLAGS_VIBRATE;
         }
         SharedPreferences prefs = mAccountRingtone.getPreferenceManager().getSharedPreferences();
         mAccount.setRingtone(prefs.getString(PREFERENCE_RINGTONE, null));
diff --git a/tests/src/com/android/email/NotificationControllerTest.java b/tests/src/com/android/email/NotificationControllerTest.java
index 8f36300..8ce1823 100644
--- a/tests/src/com/android/email/NotificationControllerTest.java
+++ b/tests/src/com/android/email/NotificationControllerTest.java
@@ -70,8 +70,8 @@
         // === Ringer mode change ===
         mRingerMode = AudioManager.RINGER_MODE_NORMAL;
 
-        // VIBRATE_ALWAYS, with a ringer tone
-        a1.mFlags = Account.FLAGS_VIBRATE_ALWAYS;
+        // VIBRATE, with a ringer tone
+        a1.mFlags = Account.FLAGS_VIBRATE;
 
         nb.setDefaults(0);
         nb.setSound(null);
@@ -83,19 +83,6 @@
         assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
         assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
 
-        // FLAGS_VIBRATE_WHEN_SILENT, with a ringer tone
-        a1.mFlags = Account.FLAGS_VIBRATE_WHEN_SILENT;
-
-        nb.setDefaults(0);
-        nb.setSound(null);
-        mTarget.setupSoundAndVibration(nb, a1);
-        n = nb.getNotification();
-
-        assertEquals(expectedRingtone, n.sound);
-        assertFalse((n.defaults & Notification.DEFAULT_VIBRATE) != 0); // no vibe
-        assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
-        assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
-
         // No VIBRATE flags, with a ringer tone
         a1.mFlags = 0;
 
@@ -112,21 +99,8 @@
         // === Ringer mode change ===
         mRingerMode = AudioManager.RINGER_MODE_VIBRATE;
 
-        // VIBRATE_ALWAYS, with a ringer tone
-        a1.mFlags = Account.FLAGS_VIBRATE_ALWAYS;
-
-        nb.setDefaults(0);
-        nb.setSound(null);
-        mTarget.setupSoundAndVibration(nb, a1);
-        n = nb.getNotification();
-
-        assertEquals(expectedRingtone, n.sound);
-        assertTrue((n.defaults & Notification.DEFAULT_VIBRATE) != 0);
-        assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
-        assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
-
-        // FLAGS_VIBRATE_WHEN_SILENT, with a ringer tone
-        a1.mFlags = Account.FLAGS_VIBRATE_WHEN_SILENT;
+        // VIBRATE, with a ringer tone
+        a1.mFlags = Account.FLAGS_VIBRATE;
 
         nb.setDefaults(0);
         nb.setSound(null);
@@ -154,21 +128,8 @@
         // === Ringer mode change ===
         mRingerMode = AudioManager.RINGER_MODE_SILENT;
 
-        // VIBRATE_ALWAYS, with a ringer tone
-        a1.mFlags = Account.FLAGS_VIBRATE_ALWAYS;
-
-        nb.setDefaults(0);
-        nb.setSound(null);
-        mTarget.setupSoundAndVibration(nb, a1);
-        n = nb.getNotification();
-
-        assertEquals(expectedRingtone, n.sound);
-        assertTrue((n.defaults & Notification.DEFAULT_VIBRATE) != 0);
-        assertTrue((n.flags & Notification.FLAG_SHOW_LIGHTS) != 0); // always set
-        assertTrue((n.defaults & Notification.DEFAULT_LIGHTS) != 0); // always set
-
-        // FLAGS_VIBRATE_WHEN_SILENT, with a ringer tone
-        a1.mFlags = Account.FLAGS_VIBRATE_WHEN_SILENT;
+        // VIBRATE, with a ringer tone
+        a1.mFlags = Account.FLAGS_VIBRATE;
 
         nb.setDefaults(0);
         nb.setSound(null);