Cache old VM ringtone name to avoid stutter in settings dialog

VoicemailRingtonePreference is recreated after the user selects an
option, but with an empty summary. The later async update to set the
summary will adjust the height of the preference, which makes the dialog
appear to flash. This change caches the old name in
VoicemailSettingsActivity and passes it to VoicemailRingtonePreference
upon initialization synchronously to avoid the flash

Bug: 27535183
Change-Id: Ic35fdbc6a31e7e1287f1de3ab7aa0c03222f3572
diff --git a/src/com/android/phone/settings/VoicemailRingtonePreference.java b/src/com/android/phone/settings/VoicemailRingtonePreference.java
index 4ee4e64..bb82d4f 100644
--- a/src/com/android/phone/settings/VoicemailRingtonePreference.java
+++ b/src/com/android/phone/settings/VoicemailRingtonePreference.java
@@ -17,8 +17,13 @@
  * it is created or updated.
  */
 public class VoicemailRingtonePreference extends RingtonePreference {
+    public interface VoicemailRingtoneNameChangeListener {
+        void onVoicemailRingtoneNameChanged(CharSequence name);
+    }
+
     private static final int MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY = 1;
 
+    private VoicemailRingtoneNameChangeListener mVoicemailRingtoneNameChangeListener;
     private Runnable mVoicemailRingtoneLookupRunnable;
     private Handler mVoicemailRingtoneLookupComplete;
 
@@ -32,6 +37,10 @@
             public void handleMessage(Message msg) {
                 switch (msg.what) {
                     case MSG_UPDATE_VOICEMAIL_RINGTONE_SUMMARY:
+                        if (mVoicemailRingtoneNameChangeListener != null) {
+                            mVoicemailRingtoneNameChangeListener.onVoicemailRingtoneNameChanged(
+                                    (CharSequence) msg.obj);
+                        }
                         setSummary((CharSequence) msg.obj);
                         break;
                 }
@@ -39,7 +48,7 @@
         };
     }
 
-    public void init(Phone phone) {
+    public void init(Phone phone, CharSequence oldRingtoneName) {
         mPhone = phone;
 
         // Requesting the ringtone will trigger migration if necessary.
@@ -48,6 +57,7 @@
         final Preference preference = this;
         final String preferenceKey =
                 VoicemailNotificationSettingsUtil.getVoicemailRingtoneSharedPrefsKey(mPhone);
+        setSummary(oldRingtoneName);
         mVoicemailRingtoneLookupRunnable = new Runnable() {
             @Override
             public void run() {
@@ -63,6 +73,10 @@
         updateRingtoneName();
     }
 
+    public void setVoicemailRingtoneNameChangeListener(VoicemailRingtoneNameChangeListener l) {
+        mVoicemailRingtoneNameChangeListener = l;
+    }
+
     @Override
     protected Uri onRestoreRingtone() {
         return VoicemailNotificationSettingsUtil.getRingtoneUri(mPhone);
diff --git a/src/com/android/phone/settings/VoicemailSettingsActivity.java b/src/com/android/phone/settings/VoicemailSettingsActivity.java
index fc53f15..459f3c1 100644
--- a/src/com/android/phone/settings/VoicemailSettingsActivity.java
+++ b/src/com/android/phone/settings/VoicemailSettingsActivity.java
@@ -58,7 +58,8 @@
         implements DialogInterface.OnClickListener,
                 Preference.OnPreferenceChangeListener,
                 EditPhoneNumberPreference.OnDialogClosedListener,
-                EditPhoneNumberPreference.GetDefaultNumberListener {
+                EditPhoneNumberPreference.GetDefaultNumberListener,
+                VoicemailRingtonePreference.VoicemailRingtoneNameChangeListener {
     private static final String LOG_TAG = VoicemailSettingsActivity.class.getSimpleName();
     private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
 
@@ -185,6 +186,8 @@
     private CallForwardInfo[] mNewFwdSettings;
     private String mNewVMNumber;
 
+    private CharSequence mOldVmRingtoneName = "";
+
     /**
      * Used to indicate that the voicemail preference should be shown.
      */
@@ -252,7 +255,8 @@
 
         mVoicemailNotificationRingtone = (VoicemailRingtonePreference) findPreference(
                 getResources().getString(R.string.voicemail_notification_ringtone_key));
-        mVoicemailNotificationRingtone.init(mPhone);
+        mVoicemailNotificationRingtone.setVoicemailRingtoneNameChangeListener(this);
+        mVoicemailNotificationRingtone.init(mPhone, mOldVmRingtoneName);
 
         mVoicemailNotificationVibrate = (CheckBoxPreference) findPreference(
                 getResources().getString(R.string.voicemail_notification_vibrate_key));
@@ -538,6 +542,11 @@
         super.onActivityResult(requestCode, resultCode, data);
     }
 
+    @Override
+    public void onVoicemailRingtoneNameChanged(CharSequence name) {
+        mOldVmRingtoneName = name;
+    }
+
     /**
      * Simulates user clicking on a passed preference.
      * Usually needed when the preference is a dialog preference and we want to invoke