Making sort order and display order options invisible in CJK countries.

Bug: 2459551
Change-Id: Iad9c71e6ac086b725ad36d28c38a37cb1c93b61c
diff --git a/res/values-ja/config.xml b/res/values-ja/config.xml
new file mode 100644
index 0000000..d132f7f
--- /dev/null
+++ b/res/values-ja/config.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+** Copyright 2010, 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.
+*/
+-->
+
+<resources>
+    <!-- If true, an option is shown in Display Options UI to choose a sort order -->
+    <bool name="config_sort_order_user_changeable">false</bool>
+
+    <!-- If true, the default sort order is primary (i.e. by given name) -->
+    <bool name="config_default_sort_order_primary">false</bool>
+
+    <!-- If true, an option is shown in Display Options UI to choose a name display order -->
+    <bool name="config_display_order_user_changeable">false</bool>
+
+    <!-- If true, the default sort order is primary (i.e. by given name) -->
+    <bool name="config_default_display_order_primary">false</bool>
+</resources>
diff --git a/res/values-ko/config.xml b/res/values-ko/config.xml
new file mode 100644
index 0000000..d132f7f
--- /dev/null
+++ b/res/values-ko/config.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+** Copyright 2010, 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.
+*/
+-->
+
+<resources>
+    <!-- If true, an option is shown in Display Options UI to choose a sort order -->
+    <bool name="config_sort_order_user_changeable">false</bool>
+
+    <!-- If true, the default sort order is primary (i.e. by given name) -->
+    <bool name="config_default_sort_order_primary">false</bool>
+
+    <!-- If true, an option is shown in Display Options UI to choose a name display order -->
+    <bool name="config_display_order_user_changeable">false</bool>
+
+    <!-- If true, the default sort order is primary (i.e. by given name) -->
+    <bool name="config_default_display_order_primary">false</bool>
+</resources>
diff --git a/res/values-zh-rCN/config.xml b/res/values-zh-rCN/config.xml
new file mode 100644
index 0000000..d132f7f
--- /dev/null
+++ b/res/values-zh-rCN/config.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+** Copyright 2010, 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.
+*/
+-->
+
+<resources>
+    <!-- If true, an option is shown in Display Options UI to choose a sort order -->
+    <bool name="config_sort_order_user_changeable">false</bool>
+
+    <!-- If true, the default sort order is primary (i.e. by given name) -->
+    <bool name="config_default_sort_order_primary">false</bool>
+
+    <!-- If true, an option is shown in Display Options UI to choose a name display order -->
+    <bool name="config_display_order_user_changeable">false</bool>
+
+    <!-- If true, the default sort order is primary (i.e. by given name) -->
+    <bool name="config_default_display_order_primary">false</bool>
+</resources>
diff --git a/res/values-zh-rTW/config.xml b/res/values-zh-rTW/config.xml
new file mode 100644
index 0000000..d132f7f
--- /dev/null
+++ b/res/values-zh-rTW/config.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+** Copyright 2010, 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.
+*/
+-->
+
+<resources>
+    <!-- If true, an option is shown in Display Options UI to choose a sort order -->
+    <bool name="config_sort_order_user_changeable">false</bool>
+
+    <!-- If true, the default sort order is primary (i.e. by given name) -->
+    <bool name="config_default_sort_order_primary">false</bool>
+
+    <!-- If true, an option is shown in Display Options UI to choose a name display order -->
+    <bool name="config_display_order_user_changeable">false</bool>
+
+    <!-- If true, the default sort order is primary (i.e. by given name) -->
+    <bool name="config_default_display_order_primary">false</bool>
+</resources>
diff --git a/res/values/config.xml b/res/values/config.xml
index 8449319..e22b2ed 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -82,4 +82,15 @@
          (which can be overridden on a per-product basis.) -->
     <bool name="config_show_onscreen_dial_button">true</bool>
 
+    <!-- If true, an option is shown in Display Options UI to choose a sort order -->
+    <bool name="config_sort_order_user_changeable">true</bool>
+
+    <!-- If true, the default sort order is primary (i.e. by given name) -->
+    <bool name="config_default_sort_order_primary">true</bool>
+
+    <!-- If true, an option is shown in Display Options UI to choose a name display order -->
+    <bool name="config_display_order_user_changeable">true</bool>
+
+    <!-- If true, the default sort order is primary (i.e. by given name) -->
+    <bool name="config_default_display_order_primary">true</bool>
 </resources>
diff --git a/src/com/android/contacts/ui/ContactsPreferences.java b/src/com/android/contacts/ui/ContactsPreferences.java
index 187df37..49a49ef 100644
--- a/src/com/android/contacts/ui/ContactsPreferences.java
+++ b/src/com/android/contacts/ui/ContactsPreferences.java
@@ -16,6 +16,8 @@
 
 package com.android.contacts.ui;
 
+import com.android.contacts.R;
+
 import android.content.ContentResolver;
 import android.content.Context;
 import android.database.ContentObserver;
@@ -34,7 +36,6 @@
     private int mDisplayOrder = -1;
     private SettingsObserver mSettingsObserver;
 
-    // TODO listen to locale changes
     public ContactsPreferences(Context context) {
         mContext = context;
         mContentResolver = context.getContentResolver();
@@ -44,16 +45,22 @@
     }
 
     public boolean isSortOrderUserChangeable() {
-        // TODO this should be locale-specific
-        return true;
+        return mContext.getResources().getBoolean(R.bool.config_sort_order_user_changeable);
     }
 
     private int getDefaultSortOrder() {
-        // TODO this should be locale-specific
-        return ContactsContract.Preferences.SORT_ORDER_PRIMARY;
+        if (mContext.getResources().getBoolean(R.bool.config_default_sort_order_primary)) {
+            return ContactsContract.Preferences.SORT_ORDER_PRIMARY;
+        } else {
+            return ContactsContract.Preferences.SORT_ORDER_ALTERNATIVE;
+        }
     }
 
     public int getSortOrder() {
+        if (!isSortOrderUserChangeable()) {
+            return getDefaultSortOrder();
+        }
+
         if (mSortOrder == -1) {
             try {
                 mSortOrder = Settings.System.getInt(mContext.getContentResolver(),
@@ -72,16 +79,22 @@
     }
 
     public boolean isDisplayOrderUserChangeable() {
-        // TOD this should be locale-specific
-        return true;
+        return mContext.getResources().getBoolean(R.bool.config_display_order_user_changeable);
     }
 
     private int getDefaultDisplayOrder() {
-        // TODO this should be locale-specific
-        return ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY;
+        if (mContext.getResources().getBoolean(R.bool.config_default_display_order_primary)) {
+            return ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY;
+        } else {
+            return ContactsContract.Preferences.DISPLAY_ORDER_ALTERNATIVE;
+        }
     }
 
     public int getDisplayOrder() {
+        if (!isDisplayOrderUserChangeable()) {
+            return getDefaultDisplayOrder();
+        }
+
         if (mDisplayOrder == -1) {
             try {
                 mDisplayOrder = Settings.System.getInt(mContext.getContentResolver(),
diff --git a/src/com/android/contacts/ui/ContactsPreferencesActivity.java b/src/com/android/contacts/ui/ContactsPreferencesActivity.java
index 9422976..aad7b12 100644
--- a/src/com/android/contacts/ui/ContactsPreferencesActivity.java
+++ b/src/com/android/contacts/ui/ContactsPreferencesActivity.java
@@ -115,17 +115,16 @@
         setContentView(R.layout.contacts_preferences);
 
         mList = getExpandableListView();
+        mList.setHeaderDividersEnabled(true);
         mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
         mContactsPrefs = new ContactsPreferences(this);
 
         final LayoutInflater inflater = getLayoutInflater();
 
-        addWithPhonesOnlyPreferenceView(inflater);
-        addDivider(inflater);
-        addSortOrderPreferenceView(inflater);
-        addDivider(inflater);
-        addDisplayOrderPreferenceView(inflater);
-        addDisplayGroupHeader(inflater);
+        createWithPhonesOnlyPreferenceView(inflater);
+        createSortOrderPreferenceView(inflater);
+        createDisplayOrderPreferenceView(inflater);
+        createDisplayGroupHeader(inflater);
 
         findViewById(R.id.btn_done).setOnClickListener(this);
         findViewById(R.id.btn_discard).setOnClickListener(this);
@@ -136,12 +135,9 @@
 
         mSortOrder = mContactsPrefs.getSortOrder();
         mDisplayOrder = mContactsPrefs.getDisplayOrder();
-
-        // Start background query to find account details
-        new QueryGroupsTask(this).execute();
     }
 
-    private LayoutInflater addWithPhonesOnlyPreferenceView(LayoutInflater inflater) {
+    private void createWithPhonesOnlyPreferenceView(LayoutInflater inflater) {
         // Add the "Only contacts with phones" header modifier.
         mHeaderPhones = inflater.inflate(R.layout.display_options_phones_only, mList, false);
         mHeaderPhones.setId(R.id.header_phones);
@@ -154,12 +150,9 @@
             text1.setText(R.string.showFilterPhones);
             text2.setText(R.string.showFilterPhonesDescrip);
         }
-
-        mList.addHeaderView(mHeaderPhones, null, true);
-        return inflater;
     }
 
-    private void addSortOrderPreferenceView(LayoutInflater inflater) {
+    private void createSortOrderPreferenceView(LayoutInflater inflater) {
         mSortOrderView = inflater.inflate(R.layout.preference_with_more_button, mList, false);
 
         View preferenceLayout = mSortOrderView.findViewById(R.id.preference);
@@ -168,10 +161,9 @@
         label.setText(getString(R.string.display_options_sort_list_by));
 
         mSortOrderTextView = (TextView)preferenceLayout.findViewById(R.id.data);
-        mList.addHeaderView(mSortOrderView, null, true);
     }
 
-    private void addDisplayOrderPreferenceView(LayoutInflater inflater) {
+    private void createDisplayOrderPreferenceView(LayoutInflater inflater) {
         mDisplayOrderView = inflater.inflate(R.layout.preference_with_more_button, mList, false);
         View preferenceLayout = mDisplayOrderView.findViewById(R.id.preference);
 
@@ -179,28 +171,44 @@
         label.setText(getString(R.string.display_options_view_names_as));
 
         mDisplayOrderTextView = (TextView)preferenceLayout.findViewById(R.id.data);
-        mList.addHeaderView(mDisplayOrderView, null, true);
     }
 
-    private void addDivider(LayoutInflater inflater) {
-        View divider = inflater.inflate(R.layout.horizontal_divider, mList, false);
-        mList.addHeaderView(divider, null, false);
-    }
-
-    private void addDisplayGroupHeader(LayoutInflater inflater) {
+    private void createDisplayGroupHeader(LayoutInflater inflater) {
         // Add the separator before showing the detailed group list.
         mHeaderSeparator = inflater.inflate(R.layout.list_separator, mList, false);
         {
             final TextView text1 = (TextView)mHeaderSeparator;
             text1.setText(R.string.headerContactGroups);
         }
-        mList.addHeaderView(mHeaderSeparator, null, false);
     }
 
     @Override
     protected void onResume() {
         super.onResume();
+        mList.removeHeaderView(mHeaderPhones);
+        mList.removeHeaderView(mSortOrderView);
+        mList.removeHeaderView(mDisplayOrderView);
+        mList.removeHeaderView(mHeaderSeparator);
+
+        // List adapter needs to be reset, because header views cannot be added
+        // to a list with an existing adapter.
+        setListAdapter(null);
+
+        mList.addHeaderView(mHeaderPhones, null, true);
+        if (mContactsPrefs.isSortOrderUserChangeable()) {
+            mList.addHeaderView(mSortOrderView, null, true);
+        }
+
+        if (mContactsPrefs.isSortOrderUserChangeable()) {
+            mList.addHeaderView(mDisplayOrderView, null, true);
+        }
+
+        mList.addHeaderView(mHeaderSeparator, null, false);
+
         bindView();
+
+        // Start background query to find account details
+        new QueryGroupsTask(this).execute();
     }
 
     private void bindView() {
@@ -343,7 +351,9 @@
 
     public void setListAdapter(DisplayAdapter adapter) {
         mAdapter = adapter;
-        mAdapter.setChildDescripWithPhones(mDisplayPhones.isChecked());
+        if (mAdapter != null) {
+            mAdapter.setChildDescripWithPhones(mDisplayPhones.isChecked());
+        }
         super.setListAdapter(mAdapter);
     }