1495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee/* 2495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * Copyright (C) 2010 The Android Open Source Project 3495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * 4495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * Licensed under the Apache License, Version 2.0 (the "License"); 5495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * you may not use this file except in compliance with the License. 6495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * You may obtain a copy of the License at 7495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * 8495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * http://www.apache.org/licenses/LICENSE-2.0 9495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * 10495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * Unless required by applicable law or agreed to in writing, software 11495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * distributed under the License is distributed on an "AS IS" BASIS, 12495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * See the License for the specific language governing permissions and 14495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * limitations under the License. 15495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee */ 16495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 17495c732e471c1c68db91ca6e824440130fdb7146Yorke Leepackage com.android.contacts.common.preference; 18495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 19495c732e471c1c68db91ca6e824440130fdb7146Yorke Leeimport android.app.AlertDialog.Builder; 20495c732e471c1c68db91ca6e824440130fdb7146Yorke Leeimport android.content.Context; 21495c732e471c1c68db91ca6e824440130fdb7146Yorke Leeimport android.preference.ListPreference; 22495c732e471c1c68db91ca6e824440130fdb7146Yorke Leeimport android.util.AttributeSet; 23495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 24495c732e471c1c68db91ca6e824440130fdb7146Yorke Leeimport com.android.contacts.common.R; 25495c732e471c1c68db91ca6e824440130fdb7146Yorke Leeimport com.android.contacts.common.preference.ContactsPreferences; 26495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 27495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee/** 28495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * Custom preference: sort-by. 29495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee */ 30495c732e471c1c68db91ca6e824440130fdb7146Yorke Leepublic final class SortOrderPreference extends ListPreference { 31495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 32495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee private ContactsPreferences mPreferences; 33495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee private Context mContext; 34495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 35495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee public SortOrderPreference(Context context) { 36495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee super(context); 37495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee prepare(); 38495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 39495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 40495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee public SortOrderPreference(Context context, AttributeSet attrs) { 41495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee super(context, attrs); 42495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee prepare(); 43495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 44495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 45495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee private void prepare() { 46495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee mContext = getContext(); 47495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee mPreferences = new ContactsPreferences(mContext); 48495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee setEntries(new String[]{ 49495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee mContext.getString(R.string.display_options_sort_by_given_name), 50495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee mContext.getString(R.string.display_options_sort_by_family_name), 51495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee }); 52495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee setEntryValues(new String[]{ 53495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee String.valueOf(ContactsPreferences.SORT_ORDER_PRIMARY), 54495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee String.valueOf(ContactsPreferences.SORT_ORDER_ALTERNATIVE), 55495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee }); 56495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee setValue(String.valueOf(mPreferences.getSortOrder())); 57495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 58495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 59495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee @Override 60495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee protected boolean shouldPersist() { 61495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee return false; // This preference takes care of its own storage 62495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 63495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 64495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee @Override 65495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee public CharSequence getSummary() { 66495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee switch (mPreferences.getSortOrder()) { 67495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee case ContactsPreferences.SORT_ORDER_PRIMARY: 68495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee return mContext.getString(R.string.display_options_sort_by_given_name); 69495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee case ContactsPreferences.SORT_ORDER_ALTERNATIVE: 70495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee return mContext.getString(R.string.display_options_sort_by_family_name); 71495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 72495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee return null; 73495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 74495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 75495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee @Override 76495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee protected boolean persistString(String value) { 77495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee int newValue = Integer.parseInt(value); 78495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee if (newValue != mPreferences.getSortOrder()) { 79495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee mPreferences.setSortOrder(newValue); 80495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee notifyChanged(); 81495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 82495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee return true; 83495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 84495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 85495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee @Override 86495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee // UX recommendation is not to show cancel button on such lists. 87495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee protected void onPrepareDialogBuilder(Builder builder) { 88495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee super.onPrepareDialogBuilder(builder); 89495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee builder.setNegativeButton(null, null); 90495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 91495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee} 92