1 /* 2 * Copyright (C) 2010 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.common.preference; 18 19 import android.app.AlertDialog.Builder; 20 import android.content.Context; 21 import android.preference.ListPreference; 22 import android.provider.ContactsContract; 23 import android.util.AttributeSet; 24 25 import com.android.contacts.common.R; 26 import com.android.contacts.common.preference.ContactsPreferences; 27 28 /** 29 * Custom preference: view-name-as (first name first or last name first). 30 */ 31 public final class DisplayOrderPreference extends ListPreference { 32 33 private ContactsPreferences mPreferences; 34 private Context mContext; 35 DisplayOrderPreference(Context context)36 public DisplayOrderPreference(Context context) { 37 super(context); 38 prepare(); 39 } 40 DisplayOrderPreference(Context context, AttributeSet attrs)41 public DisplayOrderPreference(Context context, AttributeSet attrs) { 42 super(context, attrs); 43 prepare(); 44 } 45 prepare()46 private void prepare() { 47 mContext = getContext(); 48 mPreferences = new ContactsPreferences(mContext); 49 setEntries(new String[]{ 50 mContext.getString(R.string.display_options_view_given_name_first), 51 mContext.getString(R.string.display_options_view_family_name_first), 52 }); 53 setEntryValues(new String[]{ 54 String.valueOf(ContactsPreferences.DISPLAY_ORDER_PRIMARY), 55 String.valueOf(ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE), 56 }); 57 setValue(String.valueOf(mPreferences.getDisplayOrder())); 58 } 59 60 @Override shouldPersist()61 protected boolean shouldPersist() { 62 return false; // This preference takes care of its own storage 63 } 64 65 @Override getSummary()66 public CharSequence getSummary() { 67 switch (mPreferences.getDisplayOrder()) { 68 case ContactsPreferences.DISPLAY_ORDER_PRIMARY: 69 return mContext.getString(R.string.display_options_view_given_name_first); 70 case ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE: 71 return mContext.getString(R.string.display_options_view_family_name_first); 72 } 73 return null; 74 } 75 76 @Override persistString(String value)77 protected boolean persistString(String value) { 78 int newValue = Integer.parseInt(value); 79 if (newValue != mPreferences.getDisplayOrder()) { 80 mPreferences.setDisplayOrder(newValue); 81 notifyChanged(); 82 } 83 return true; 84 } 85 86 @Override 87 // UX recommendation is not to show cancel button on such lists. onPrepareDialogBuilder(Builder builder)88 protected void onPrepareDialogBuilder(Builder builder) { 89 super.onPrepareDialogBuilder(builder); 90 builder.setNegativeButton(null, null); 91 } 92 } 93