1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.contacts.preference;
18 
19 import android.app.AlertDialog.Builder;
20 import android.content.Context;
21 import android.preference.ListPreference;
22 import android.util.AttributeSet;
23 
24 import com.android.contacts.R;
25 
26 /**
27  * Custom preference: phonetic name fields.
28  */
29 public final class PhoneticNameDisplayPreference extends ListPreference {
30 
31     public static final int SHOW_ALWAYS = 0;
32     public static final int HIDE_IF_EMPTY = 1;
33 
34     private Context mContext;
35     private ContactsPreferences mPreferences;
36 
PhoneticNameDisplayPreference(Context context)37     public PhoneticNameDisplayPreference(Context context) {
38         super(context);
39         prepare();
40     }
41 
PhoneticNameDisplayPreference(Context context, AttributeSet attrs)42     public PhoneticNameDisplayPreference(Context context, AttributeSet attrs) {
43         super(context, attrs);
44         prepare();
45     }
46 
prepare()47     private void prepare() {
48         mContext = getContext();
49         mPreferences = new ContactsPreferences(mContext);
50         setEntries(new String[]{
51                 mContext.getString(R.string.editor_options_always_show_phonetic_names),
52                 mContext.getString(R.string.editor_options_hide_phonetic_names_if_empty)
53         });
54         setEntryValues(new String[]{
55                 String.valueOf(SHOW_ALWAYS),
56                 String.valueOf(HIDE_IF_EMPTY),
57         });
58         setValue(String.valueOf(mPreferences.getPhoneticNameDisplayPreference()));
59     }
60 
61     @Override
shouldPersist()62     protected boolean shouldPersist() {
63         return false;   // This preference takes care of its own storage
64     }
65 
66     @Override
getSummary()67     public CharSequence getSummary() {
68         switch (mPreferences.getPhoneticNameDisplayPreference()) {
69             case SHOW_ALWAYS:
70                 return mContext.getString(R.string.editor_options_always_show_phonetic_names);
71             case HIDE_IF_EMPTY:
72                 return mContext.getString(R.string.editor_options_hide_phonetic_names_if_empty);
73         }
74         return null;
75     }
76 
77     @Override
persistString(String value)78     protected boolean persistString(String value) {
79         final int newValue = Integer.parseInt(value);
80         if (newValue != mPreferences.getPhoneticNameDisplayPreference()) {
81             mPreferences.setPhoneticNameDisplayPreference(newValue);
82             notifyChanged();
83         }
84         return true;
85     }
86 
87     // UX recommendation is not to show cancel button on such lists.
88     @Override
onPrepareDialogBuilder(Builder builder)89     protected void onPrepareDialogBuilder(Builder builder) {
90         super.onPrepareDialogBuilder(builder);
91         builder.setNegativeButton(null, null);
92     }
93 }
94