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