Fix preservation of phonetic name on device rotation in contact editor

This fix is similar to the fix for preserving structured names
on device rotation. I644bfea2af4e759e1d7d94aa09840ed15efd079d

We basically detect whether fields are visible or not before
trying to update the underlying name.

Bug: 5600858
Change-Id: Ic455cc87fa2872441b5d2a0e7da67ffc1798ec3d
diff --git a/src/com/android/contacts/editor/PhoneticNameEditorView.java b/src/com/android/contacts/editor/PhoneticNameEditorView.java
index cfc9b13..59545d4 100644
--- a/src/com/android/contacts/editor/PhoneticNameEditorView.java
+++ b/src/com/android/contacts/editor/PhoneticNameEditorView.java
@@ -155,6 +155,10 @@
         }
     }
 
+    public static boolean isUnstructuredPhoneticNameColumn(String column) {
+        return DataKind.PSEUDO_COLUMN_PHONETIC_NAME.equals(column);
+    }
+
     public PhoneticNameEditorView(Context context) {
         super(context);
     }
@@ -176,6 +180,31 @@
         super.setValues(kind, entry, state, readOnly, vig);
     }
 
+    @Override
+    public void onFieldChanged(String column, String value) {
+        if (!isFieldChanged(column, value)) {
+            return;
+        }
+
+        if (hasShortAndLongForms()) {
+            PhoneticValuesDelta entry = (PhoneticValuesDelta) getEntry();
+
+            // Determine whether the user is modifying the structured or unstructured phonetic
+            // name field. See a similar approach in {@link StructuredNameEditor#onFieldChanged}.
+            // This is because on device rotation, a hidden TextView's onRestoreInstanceState() will
+            // be called and incorrectly restore a null value for the hidden field, which ultimately
+            // modifies the underlying phonetic name. Hence, ignore onFieldChanged() update requests
+            // from fields that aren't visible.
+            boolean isEditingUnstructuredPhoneticName = !areOptionalFieldsVisible();
+
+            if (isEditingUnstructuredPhoneticName == isUnstructuredPhoneticNameColumn(column)) {
+                // Call into the superclass to update the field and rebuild the underlying
+                // phonetic name.
+                super.onFieldChanged(column, value);
+            }
+        }
+    }
+
     public boolean hasData() {
         ValuesDelta entry = getEntry();