Added preference for the display of phonetic name fields.
am: bf9dddc9e9

Change-Id: I035353bb0e260086ecd76297fb481b7c3b1757ae
diff --git a/res/values/donottranslate_config.xml b/res/values/donottranslate_config.xml
index 5e46f81..f81e01f 100644
--- a/res/values/donottranslate_config.xml
+++ b/res/values/donottranslate_config.xml
@@ -43,6 +43,9 @@
     <!-- If true, the default sort order is primary (i.e. by given name) -->
     <bool name="config_default_display_order_primary">true</bool>
 
+    <!-- If true, phonetic name fields are not visible when empty -->
+    <bool name="config_default_hide_phonetic_name_if_empty">true</bool>
+
     <!-- If true, the order of name fields in the editor is primary (i.e. given name first) -->
     <bool name="config_editor_field_order_primary">true</bool>
 
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 4fca68b..538336c 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1318,6 +1318,17 @@
     <!-- Label of the "sort by" display option -->
     <string name="display_options_sort_list_by">Sort by</string>
 
+    <!-- Label of the "phonetic name fields" display option. [CHAR LIMIT=50] -->
+    <string name="display_options_phonetic_name_fields">Phonetic name</string>
+
+    <!-- Option to initially show the phonetic name input fields on the contact editor even if the
+    contact does not have a phonetic name. [CHAR LIMIT=25]  -->
+    <string name="editor_options_always_show_phonetic_names">Always show</string>
+
+    <!-- Option to initially hide the phonetic name input fields on the contact editor if the
+    contact being edited doesn't have a phonetic name. [CHAR LIMIT=25]  -->
+    <string name="editor_options_hide_phonetic_names_if_empty">Hide if empty</string>
+
     <!-- An allowable value for the "sort list by" contact display option  -->
     <string name="display_options_sort_by_given_name">First name</string>
 
diff --git a/res/xml/preference_display_options.xml b/res/xml/preference_display_options.xml
index ca9365c..c969cd2 100644
--- a/res/xml/preference_display_options.xml
+++ b/res/xml/preference_display_options.xml
@@ -49,6 +49,12 @@
         android:title="@string/display_options_view_names_as"
         android:dialogTitle="@string/display_options_view_names_as" />
 
+    <com.android.contacts.preference.PhoneticNameDisplayPreference
+        android:icon="@null"
+        android:key="phoneticNameDisplay"
+        android:title="@string/display_options_phonetic_name_fields"
+        android:dialogTitle="@string/display_options_phonetic_name_fields"/>
+
     <Preference
         android:icon="@null"
         android:key="import"
diff --git a/src/com/android/contacts/editor/KindSectionView.java b/src/com/android/contacts/editor/KindSectionView.java
index 7e9d53e..94f8a32 100644
--- a/src/com/android/contacts/editor/KindSectionView.java
+++ b/src/com/android/contacts/editor/KindSectionView.java
@@ -36,6 +36,7 @@
 import com.android.contacts.model.ValuesDelta;
 import com.android.contacts.model.account.AccountType;
 import com.android.contacts.model.dataitem.DataKind;
+import com.android.contacts.preference.ContactsPreferences;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -348,8 +349,7 @@
                     nameValuesDelta, rawContactDelta.getRawContactId(), mListener));
         }
         nameView.setDeletable(false);
-        nameView.setValues(
-                accountType.getKindForMimetype(DataKind.PSEUDO_MIME_TYPE_NAME),
+        nameView.setValues(accountType.getKindForMimetype(DataKind.PSEUDO_MIME_TYPE_NAME),
                 nameValuesDelta, rawContactDelta, /* readOnly =*/ false, mViewIdGenerator);
 
         // Correct start margin since there is a second icon in the structured name layout
@@ -376,6 +376,8 @@
         layoutParams.setMargins(0, 0, 0, 0);
         phoneticNameView.setLayoutParams(layoutParams);
         mEditors.addView(phoneticNameView);
+        // Display of phonetic name fields is controlled from settings preferences.
+        mHideIfEmpty = new ContactsPreferences(getContext()).shouldHidePhoneticNamesIfEmpty();
     }
 
     private void addGroupEditorView(RawContactDelta rawContactDelta, DataKind dataKind) {
diff --git a/src/com/android/contacts/preference/ContactsPreferences.java b/src/com/android/contacts/preference/ContactsPreferences.java
index 48fb205..ab52b1b 100644
--- a/src/com/android/contacts/preference/ContactsPreferences.java
+++ b/src/com/android/contacts/preference/ContactsPreferences.java
@@ -69,6 +69,8 @@
 
     public static final boolean PREF_DISPLAY_ONLY_PHONES_DEFAULT = false;
 
+    public static final String PHONETIC_NAME_DISPLAY_KEY = "Phonetic_name_display";
+
     /**
      * Value to use when a preference is unassigned and needs to be read from the shared preferences
      */
@@ -77,6 +79,8 @@
     private final Context mContext;
     private int mSortOrder = PREFERENCE_UNASSIGNED;
     private int mDisplayOrder = PREFERENCE_UNASSIGNED;
+    private int mPhoneticNameDisplayPreference = PREFERENCE_UNASSIGNED;
+
     private AccountWithDataSet mDefaultAccount = null;
     private ChangeListener mListener = null;
     private Handler mHandler;
@@ -165,6 +169,34 @@
         mBackupManager.dataChanged();
     }
 
+    public int getDefaultPhoneticNameDisplayPreference() {
+        if (mContext.getResources().getBoolean(R.bool.config_default_hide_phonetic_name_if_empty)) {
+            return PhoneticNameDisplayPreference.HIDE_IF_EMPTY;
+        } else {
+            return PhoneticNameDisplayPreference.SHOW_ALWAYS;
+        }
+    }
+
+    public void setPhoneticNameDisplayPreference(int phoneticNameDisplayPreference) {
+        mPhoneticNameDisplayPreference = phoneticNameDisplayPreference;
+        final Editor editor = mPreferences.edit();
+        editor.putInt(PHONETIC_NAME_DISPLAY_KEY, phoneticNameDisplayPreference);
+        editor.commit();
+        mBackupManager.dataChanged();
+    }
+
+    public int getPhoneticNameDisplayPreference() {
+        if (mPhoneticNameDisplayPreference == PREFERENCE_UNASSIGNED) {
+            mPhoneticNameDisplayPreference = mPreferences.getInt(PHONETIC_NAME_DISPLAY_KEY,
+                    getDefaultPhoneticNameDisplayPreference());
+        }
+        return mPhoneticNameDisplayPreference;
+    }
+
+    public boolean shouldHidePhoneticNamesIfEmpty() {
+        return getPhoneticNameDisplayPreference() == PhoneticNameDisplayPreference.HIDE_IF_EMPTY;
+    }
+
     public boolean isDefaultAccountUserChangeable() {
         return mIsDefaultAccountUserChangeable;
     }
@@ -326,6 +358,16 @@
             setDisplayOrder(displayOrder);
         }
 
+        if (!mPreferences.contains(PHONETIC_NAME_DISPLAY_KEY)) {
+            int phoneticNameFieldsDisplay = getDefaultPhoneticNameDisplayPreference();
+            try {
+                phoneticNameFieldsDisplay = Settings.System.getInt(mContext.getContentResolver(),
+                        PHONETIC_NAME_DISPLAY_KEY);
+            } catch (SettingNotFoundException e) {
+            }
+            setPhoneticNameDisplayPreference(phoneticNameFieldsDisplay);
+        }
+
         if (!mPreferences.contains(mDefaultAccountKey)) {
             final SharedPreferences previousPrefs =
                     PreferenceManager.getDefaultSharedPreferences(mContext);
diff --git a/src/com/android/contacts/preference/PhoneticNameDisplayPreference.java b/src/com/android/contacts/preference/PhoneticNameDisplayPreference.java
new file mode 100644
index 0000000..42bbcc3
--- /dev/null
+++ b/src/com/android/contacts/preference/PhoneticNameDisplayPreference.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.preference;
+
+import android.app.AlertDialog.Builder;
+import android.content.Context;
+import android.preference.ListPreference;
+import android.util.AttributeSet;
+
+import com.android.contacts.R;
+
+/**
+ * Custom preference: phonetic name fields.
+ */
+public final class PhoneticNameDisplayPreference extends ListPreference {
+
+    public static final int SHOW_ALWAYS = 0;
+    public static final int HIDE_IF_EMPTY = 1;
+
+    private Context mContext;
+    private ContactsPreferences mPreferences;
+
+    public PhoneticNameDisplayPreference(Context context) {
+        super(context);
+        prepare();
+    }
+
+    public PhoneticNameDisplayPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        prepare();
+    }
+
+    private void prepare() {
+        mContext = getContext();
+        mPreferences = new ContactsPreferences(mContext);
+        setEntries(new String[]{
+                mContext.getString(R.string.editor_options_always_show_phonetic_names),
+                mContext.getString(R.string.editor_options_hide_phonetic_names_if_empty)
+        });
+        setEntryValues(new String[]{
+                String.valueOf(SHOW_ALWAYS),
+                String.valueOf(HIDE_IF_EMPTY),
+        });
+        setValue(String.valueOf(mPreferences.getPhoneticNameDisplayPreference()));
+    }
+
+    @Override
+    protected boolean shouldPersist() {
+        return false;   // This preference takes care of its own storage
+    }
+
+    @Override
+    public CharSequence getSummary() {
+        switch (mPreferences.getPhoneticNameDisplayPreference()) {
+            case SHOW_ALWAYS:
+                return mContext.getString(R.string.editor_options_always_show_phonetic_names);
+            case HIDE_IF_EMPTY:
+                return mContext.getString(R.string.editor_options_hide_phonetic_names_if_empty);
+        }
+        return null;
+    }
+
+    @Override
+    protected boolean persistString(String value) {
+        final int newValue = Integer.parseInt(value);
+        if (newValue != mPreferences.getPhoneticNameDisplayPreference()) {
+            mPreferences.setPhoneticNameDisplayPreference(newValue);
+            notifyChanged();
+        }
+        return true;
+    }
+
+    // UX recommendation is not to show cancel button on such lists.
+    @Override
+    protected void onPrepareDialogBuilder(Builder builder) {
+        super.onPrepareDialogBuilder(builder);
+        builder.setNegativeButton(null, null);
+    }
+}