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