Change notification settings in Phone

Add ringtone summary for Voicemail notification setting
Change Voicemail notification vibrate setting to checkbox

Bug: 7633821
Change-Id: Ifa10eb1989146ab10dc0e4b0218ff85906a7ae8f
diff --git a/res/values/strings.xml b/res/values/strings.xml
index ebf4bff..54e575d 100755
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1425,8 +1425,16 @@
     <!-- 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>
 
-    <!-- Voicemail ringtone title [CHAR LIMIT=40] -->
-    <string name="voicemail_notification_ringtone_title">Ringtone</string>
+    <!-- Voicemail ringtone title. The user clicks on this preference to select
+         which sound to play when a voicemail notification is received.
+         [CHAR LIMIT=40] -->
+    <string name="voicemail_notification_ringtone_title">Sound</string>
+
+    <!--  The string used to describe a notification if it is the default one in the system. For
+          example, if the user selects the default notification, it will appear as something like
+          Default sound(Capella) in the notification summary.
+          [CHAR LIMIT=40] -->
+    <string name="default_notification_description">Default sound (<xliff:g id="default_sound_title">%1$s</xliff:g>)</string>
 
     <!-- The vibrate notification modes for voicemail notifications -->
     <string-array name="voicemail_notification_vibrate_when_entries">
diff --git a/res/xml/call_feature_setting.xml b/res/xml/call_feature_setting.xml
index 780fdee..36fd6b9 100644
--- a/res/xml/call_feature_setting.xml
+++ b/res/xml/call_feature_setting.xml
@@ -81,15 +81,11 @@
           android:persistent="true"
           android:ringtoneType="notification"
           android:defaultValue="content://settings/system/notification_sound" />
-
-      <ListPreference
-          android:key="button_voicemail_notification_vibrate_when_key"
+      <CheckBoxPreference
+          android:key="button_voicemail_notification_vibrate_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" />
+          />
   </PreferenceScreen>
 
   <CheckBoxPreference
diff --git a/src/com/android/phone/CallFeaturesSetting.java b/src/com/android/phone/CallFeaturesSetting.java
index ee1233d..ffed7b3 100644
--- a/src/com/android/phone/CallFeaturesSetting.java
+++ b/src/com/android/phone/CallFeaturesSetting.java
@@ -46,6 +46,7 @@
 import android.preference.Preference;
 import android.preference.PreferenceActivity;
 import android.preference.PreferenceGroup;
+import android.preference.PreferenceManager;
 import android.preference.PreferenceScreen;
 import android.provider.ContactsContract.CommonDataKinds;
 import android.provider.MediaStore;
@@ -157,6 +158,11 @@
     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";
+    // New preference key for voicemail notification vibration
+    /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY =
+            "button_voicemail_notification_vibrate_key";
+    // Old preference key for voicemail notification vibration. Used for migration to the new
+    // preference key only.
     /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY =
             "button_voicemail_notification_vibrate_when_key";
     /* package */ static final String BUTTON_VOICEMAIL_NOTIFICATION_RINGTONE_KEY =
@@ -192,6 +198,7 @@
     private static final int EVENT_FORWARDING_GET_COMPLETED = 502;
 
     private static final int MSG_UPDATE_RINGTONE_SUMMARY = 1;
+    private static final int MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY = 2;
 
     // preferred TTY mode
     // Phone.TTY_MODE_xxx
@@ -236,6 +243,10 @@
     private static final int MSG_VM_OK = 600;
     private static final int MSG_VM_NOCHANGE = 700;
 
+    // voicemail notification vibration string constants
+    private static final String VOICEMAIL_VIBRATION_ALWAYS = "always";
+    private static final String VOICEMAIL_VIBRATION_NEVER = "never";
+
     private EditPhoneNumberPreference mSubMenuVoicemailSettings;
 
     private Runnable mRingtoneLookupRunnable;
@@ -243,9 +254,12 @@
         @Override
         public void handleMessage(Message msg) {
             switch (msg.what) {
-            case MSG_UPDATE_RINGTONE_SUMMARY:
-                mRingtonePreference.setSummary((CharSequence) msg.obj);
-                break;
+                case MSG_UPDATE_RINGTONE_SUMMARY:
+                    mRingtonePreference.setSummary((CharSequence) msg.obj);
+                    break;
+                case MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY:
+                    mVoicemailNotificationRingtone.setSummary((CharSequence) msg.obj);
+                    break;
             }
         }
     };
@@ -261,7 +275,8 @@
     private ListPreference mButtonSipCallOptions;
     private ListPreference mVoicemailProviders;
     private PreferenceScreen mVoicemailSettings;
-    private ListPreference mVoicemailNotificationVibrateWhen;
+    private Preference mVoicemailNotificationRingtone;
+    private CheckBoxPreference mVoicemailNotificationVibrate;
     private SipSharedPreferences mSipSharedPreferences;
 
     private class VoiceMailProvider {
@@ -574,10 +589,6 @@
                 mChangingVMorFwdDueToProviderChange = true;
                 saveVoiceMailAndForwardingNumber(newProviderKey, newProviderSettings);
             }
-        } else if (preference == mVoicemailNotificationVibrateWhen) {
-            mVoicemailNotificationVibrateWhen.setValue((String) objValue);
-            mVoicemailNotificationVibrateWhen.setSummary(
-                    mVoicemailNotificationVibrateWhen.getEntry());
         } else if (preference == mButtonSipCallOptions) {
             handleSipCallOptionsChange(objValue);
         }
@@ -1509,10 +1520,10 @@
         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);
-
+            mVoicemailNotificationRingtone =
+                    findPreference(BUTTON_VOICEMAIL_NOTIFICATION_RINGTONE_KEY);
+            mVoicemailNotificationVibrate =
+                    (CheckBoxPreference) findPreference(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY);
             initVoiceMailProviders();
         }
 
@@ -1626,6 +1637,10 @@
                     updateRingtoneName(RingtoneManager.TYPE_RINGTONE, mRingtonePreference,
                             MSG_UPDATE_RINGTONE_SUMMARY);
                 }
+                if (mVoicemailNotificationRingtone != null) {
+                    updateRingtoneName(RingtoneManager.TYPE_NOTIFICATION,
+                            mVoicemailNotificationRingtone, MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY);
+                }
             }
         };
 
@@ -1643,7 +1658,32 @@
      */
     private void updateRingtoneName(int type, Preference preference, int msg) {
         if (preference == null) return;
-        Uri ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(this, type);
+        final Uri ringtoneUri;
+        boolean defaultRingtone = false;
+        if (type == RingtoneManager.TYPE_RINGTONE) {
+            // For ringtones, we can just lookup the system default because changing the settings
+            // in Call Settings changes the system default.
+            ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(this, type);
+        } else {
+            final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
+                    mPhone.getContext());
+            // for voicemail notifications, we use the value saved in Phone's shared preferences.
+            String uriString = prefs.getString(preference.getKey(), null);
+            if (TextUtils.isEmpty(uriString)) {
+                // silent ringtone
+                ringtoneUri = null;
+            } else {
+                if (uriString.equals(Settings.System.DEFAULT_NOTIFICATION_URI.toString())) {
+                    // If it turns out that the voicemail notification is set to the system
+                    // default notification, we retrieve the actual URI to prevent it from showing
+                    // up as "Unknown Ringtone".
+                    defaultRingtone = true;
+                    ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(this, type);
+                } else {
+                    ringtoneUri = Uri.parse(uriString);
+                }
+            }
+        }
         CharSequence summary = getString(com.android.internal.R.string.ringtone_unknown);
         // Is it a silent ringtone?
         if (ringtoneUri == null) {
@@ -1663,6 +1703,10 @@
                 // Unknown title for the ringtone
             }
         }
+        if (defaultRingtone) {
+            summary = mPhone.getContext().getString(
+                    R.string.default_notification_description, summary);
+        }
         mRingtoneLookupComplete.sendMessage(mRingtoneLookupComplete.obtainMessage(msg, summary));
     }
 
@@ -1744,9 +1788,35 @@
             updatePreferredTtyModeSummary(settingsTtyMode);
         }
 
+        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
+                mPhone.getContext());
+        if (migrateVoicemailVibrationSettingsIfNeeded(prefs)) {
+            mVoicemailNotificationVibrate.setChecked(prefs.getBoolean(
+                    BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, false));
+        }
+
         lookupRingtoneName();
     }
 
+    // Migrate settings from BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY to
+    // BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, if the latter does not exist.
+    // Returns true if migration was performed.
+    public static boolean migrateVoicemailVibrationSettingsIfNeeded(SharedPreferences prefs) {
+        if (!prefs.contains(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY)) {
+            String vibrateWhen = prefs.getString(
+                    BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_WHEN_KEY, VOICEMAIL_VIBRATION_NEVER);
+            // If vibrateWhen is always, then voicemailVibrate should be True.
+            // otherwise if vibrateWhen is "only in silent mode", or "never", then
+            // voicemailVibrate = False.
+            boolean voicemailVibrate = vibrateWhen.equals(VOICEMAIL_VIBRATION_ALWAYS);
+            final SharedPreferences.Editor editor = prefs.edit();
+            editor.putBoolean(BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, voicemailVibrate);
+            editor.commit();
+            return true;
+        }
+        return false;
+    }
+
     /**
      * Obtain the setting for "vibrate when ringing" setting.
      *
@@ -1852,8 +1922,7 @@
             mVoicemailSettings.setEnabled(false);
             mVoicemailSettings.setIntent(null);
 
-            mVoicemailNotificationVibrateWhen.setEnabled(false);
-            mVoicemailNotificationVibrateWhen.setSummary("");
+            mVoicemailNotificationVibrate.setEnabled(false);
         } else {
             if (DBG) {
                 log("updateVMPreferenceWidget: provider for the key \"" + key + "\".."
@@ -1865,9 +1934,7 @@
             mVoicemailSettings.setEnabled(true);
             mVoicemailSettings.setIntent(provider.intent);
 
-            mVoicemailNotificationVibrateWhen.setEnabled(true);
-            mVoicemailNotificationVibrateWhen.setSummary(
-                    mVoicemailNotificationVibrateWhen.getEntry());
+            mVoicemailNotificationVibrate.setEnabled(true);
         }
     }
 
diff --git a/src/com/android/phone/NotificationMgr.java b/src/com/android/phone/NotificationMgr.java
index 044418c..42dcdb6 100644
--- a/src/com/android/phone/NotificationMgr.java
+++ b/src/com/android/phone/NotificationMgr.java
@@ -1277,17 +1277,12 @@
                     .setSound(ringtoneUri);
             Notification notification = builder.getNotification();
 
-            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)) {
+            CallFeaturesSetting.migrateVoicemailVibrationSettingsIfNeeded(prefs);
+            final boolean vibrate = prefs.getBoolean(
+                    CallFeaturesSetting.BUTTON_VOICEMAIL_NOTIFICATION_VIBRATE_KEY, false);
+            if (vibrate) {
                 notification.defaults |= Notification.DEFAULT_VIBRATE;
             }
-
             notification.flags |= Notification.FLAG_NO_CLEAR;
             configureLedNotification(notification);
             mNotificationManager.notify(VOICEMAIL_NOTIFICATION, notification);