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.provider.ContactsContract; 23495c732e471c1c68db91ca6e824440130fdb7146Yorke Leeimport android.util.AttributeSet; 24495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 25495c732e471c1c68db91ca6e824440130fdb7146Yorke Leeimport com.android.contacts.common.R; 26495c732e471c1c68db91ca6e824440130fdb7146Yorke Leeimport com.android.contacts.common.preference.ContactsPreferences; 27495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 28495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee/** 29495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee * Custom preference: view-name-as (first name first or last name first). 30495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee */ 31495c732e471c1c68db91ca6e824440130fdb7146Yorke Leepublic final class DisplayOrderPreference extends ListPreference { 32495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 33495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee private ContactsPreferences mPreferences; 34495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee private Context mContext; 35495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 36495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee public DisplayOrderPreference(Context context) { 37495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee super(context); 38495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee prepare(); 39495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 40495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 41495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee public DisplayOrderPreference(Context context, AttributeSet attrs) { 42495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee super(context, attrs); 43495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee prepare(); 44495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 45495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 46495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee private void prepare() { 47495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee mContext = getContext(); 48495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee mPreferences = new ContactsPreferences(mContext); 49495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee setEntries(new String[]{ 50495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee mContext.getString(R.string.display_options_view_given_name_first), 51495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee mContext.getString(R.string.display_options_view_family_name_first), 52495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee }); 53495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee setEntryValues(new String[]{ 54495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee String.valueOf(ContactsPreferences.DISPLAY_ORDER_PRIMARY), 55495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee String.valueOf(ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE), 56495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee }); 57495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee setValue(String.valueOf(mPreferences.getDisplayOrder())); 58495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 59495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 60495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee @Override 61495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee protected boolean shouldPersist() { 62495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee return false; // This preference takes care of its own storage 63495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 64495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 65495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee @Override 66495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee public CharSequence getSummary() { 67495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee switch (mPreferences.getDisplayOrder()) { 68495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee case ContactsPreferences.DISPLAY_ORDER_PRIMARY: 69495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee return mContext.getString(R.string.display_options_view_given_name_first); 70495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee case ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE: 71495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee return mContext.getString(R.string.display_options_view_family_name_first); 72495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 73495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee return null; 74495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 75495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 76495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee @Override 77495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee protected boolean persistString(String value) { 78495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee int newValue = Integer.parseInt(value); 79495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee if (newValue != mPreferences.getDisplayOrder()) { 80495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee mPreferences.setDisplayOrder(newValue); 81495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee notifyChanged(); 82495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 83495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee return true; 84495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 85495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee 86495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee @Override 87495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee // UX recommendation is not to show cancel button on such lists. 88495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee protected void onPrepareDialogBuilder(Builder builder) { 89495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee super.onPrepareDialogBuilder(builder); 90495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee builder.setNegativeButton(null, null); 91495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee } 92495c732e471c1c68db91ca6e824440130fdb7146Yorke Lee} 93