Introduce vibration options for voicemail notification

Bug: 6014374
Change-Id: I1e0dff2e958b804e140b2d0d1d286f3392efecd1
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 979628c..57382df 100755
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1596,4 +1596,28 @@
     <!-- Description of the decline on target in the Slide unlock screen. [CHAR LIMIT=NONE] -->
     <string name="description_target_decline">Decline</string>
 
+    <!-- Dialog title for the vibration settings for voicemail notifications [CHAR LIMIT=40] -->
+    <string name="voicemail_notification_vibrate_when_title" msgid="8731372580674292759">Vibrate</string>
+    <!-- Dialog title for the vibration settings for voice mail notifications [CHAR LIMIT=40]-->
+    <string name="voicemail_notification_vibarte_when_dialog_title" msgid="8995274609647451109">Vibrate</string>
+
+    <!-- The vibrate notification modes for voicemail notifications -->
+    <string-array name="voicemail_notification_vibrate_when_entries">
+        <!-- Always [CHAR LIMIT=40] -->
+        <item msgid="2539376794936035639">Always</item>
+        <!-- Only when the phone is in Silent mode [CHAR LIMIT=40] -->
+        <item msgid="8091719131860840185">Only when silent</item>
+        <!-- Never [CHAR LIMIT=40] -->
+        <item msgid="4552962311897985633">Never</item>
+    </string-array>
+
+    <!-- The default value value for voicemail notification. -->
+    <string name="voicemail_notification_vibrate_when_default" translatable="false">never</string>
+
+    <!-- Actual values used in our code for voicemail notifications. DO NOT TRANSLATE -->
+    <string-array name="voicemail_notification_vibrate_when_values" translatable="false">
+        <item>always</item>
+        <item>silent</item>
+        <item>never</item>
+    </string-array>
 </resources>
diff --git a/res/xml/call_feature_setting.xml b/res/xml/call_feature_setting.xml
index a078ee4..56ca152 100644
--- a/res/xml/call_feature_setting.xml
+++ b/res/xml/call_feature_setting.xml
@@ -73,6 +73,14 @@
                 android:singleLine="true"
                 android:autoText="false" />
       </PreferenceScreen>
+      <ListPreference
+          android:key="button_voicemail_notification_vibrate_when_key"
+          android:title="@string/voicemail_notification_vibrate_when_title"
+          android:persistent="true"
+          android:entries="@array/voicemail_notification_vibrate_when_entries"
+          android:entryValues="@array/voicemail_notification_vibrate_when_values"
+          android:defaultValue="@string/voicemail_notification_vibrate_when_default"
+          android:dialogTitle="@string/voicemail_notification_vibarte_when_dialog_title" />
   </PreferenceCategory>
 
   <PreferenceCategory android:key="button_misc_category_key"
diff --git a/src/com/android/phone/CallFeaturesSetting.java b/src/com/android/phone/CallFeaturesSetting.java
index bcddb3e..cb19557 100644
--- a/src/com/android/phone/CallFeaturesSetting.java
+++ b/src/com/android/phone/CallFeaturesSetting.java
@@ -142,6 +142,8 @@
     private static final String BUTTON_VOICEMAIL_KEY = "button_voicemail_key";
     private static final String BUTTON_VOICEMAIL_PROVIDER_KEY = "button_voicemail_provider_key";
     private static final String BUTTON_VOICEMAIL_SETTING_KEY = "button_voicemail_setting_key";
+    /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY =
+            "button_voicemail_notification_vibrate_when_key";
     private static final String BUTTON_FDN_KEY   = "button_fdn_key";
     private static final String BUTTON_RESPOND_VIA_SMS_KEY   = "button_respond_via_sms_key";
 
@@ -221,6 +223,7 @@
     private ListPreference mButtonSipCallOptions;
     private ListPreference mVoicemailProviders;
     private PreferenceScreen mVoicemailSettings;
+    private ListPreference mVoicemailNotificationVibrateWhen;
     private SipSharedPreferences mSipSharedPreferences;
 
     private class VoiceMailProvider {
@@ -500,6 +503,10 @@
                 mChangingVMorFwdDueToProviderChange = true;
                 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
             }
+        } else if (preference == mVoicemailNotificationVibrateWhen) {
+            mVoicemailNotificationVibrateWhen.setValue((String) objValue);
+            mVoicemailNotificationVibrateWhen.setSummary(
+                    mVoicemailNotificationVibrateWhen.getEntry());
         } else if (preference == mButtonSipCallOptions) {
             handleSipCallOptionsChange(objValue);
         }
@@ -1390,6 +1397,9 @@
         if (mVoicemailProviders != null) {
             mVoicemailProviders.setOnPreferenceChangeListener(this);
             mVoicemailSettings = (PreferenceScreen)findPreference(BUTTON_VOICEMAIL_SETTING_KEY);
+            mVoicemailNotificationVibrateWhen =
+                    (ListPreference) findPreference(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY);
+            mVoicemailNotificationVibrateWhen.setOnPreferenceChangeListener(this);
 
             initVoiceMailProviders();
         }
@@ -1640,6 +1650,9 @@
             mVoicemailSettings.setSummary("");
             mVoicemailSettings.setEnabled(false);
             mVoicemailSettings.setIntent(null);
+
+            mVoicemailNotificationVibrateWhen.setEnabled(false);
+            mVoicemailNotificationVibrateWhen.setSummary("");
         } else {
             final String providerName = provider.name;
             mVoicemailProviders.setSummary(providerName);
@@ -1647,6 +1660,10 @@
                     R.string.voicemail_settings_for, providerName));
             mVoicemailSettings.setEnabled(true);
             mVoicemailSettings.setIntent(provider.intent);
+
+            mVoicemailNotificationVibrateWhen.setEnabled(true);
+            mVoicemailNotificationVibrateWhen.setSummary(
+                    mVoicemailNotificationVibrateWhen.getEntry());
         }
     }
 
diff --git a/src/com/android/phone/NotificationMgr.java b/src/com/android/phone/NotificationMgr.java
index fc7cc87..39fcbd1 100644
--- a/src/com/android/phone/NotificationMgr.java
+++ b/src/com/android/phone/NotificationMgr.java
@@ -1122,7 +1122,19 @@
                     pendingIntent  // contentIntent
                     );
             notification.defaults |= Notification.DEFAULT_SOUND;
-            notification.defaults |= Notification.DEFAULT_VIBRATE;
+
+            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
+            String vibrateWhen = prefs.getString(
+                    CallFeaturesSetting.BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY, "never");
+            boolean vibrateAlways = vibrateWhen.equals("always");
+            boolean vibrateSilent = vibrateWhen.equals("silent");
+            AudioManager audioManager =
+                    (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
+            boolean nowSilent = audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE;
+            if (vibrateAlways || (vibrateSilent && nowSilent)) {
+                notification.defaults |= Notification.DEFAULT_VIBRATE;
+            }
+
             notification.flags |= Notification.FLAG_NO_CLEAR;
             configureLedNotification(notification);
             mNotificationManager.notify(VOICEMAIL_NOTIFICATION, notification);