1b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee/* 2b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee * Copyright (C) 2010 The Android Open Source Project 3b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee * 4b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee * Licensed under the Apache License, Version 2.0 (the "License"); 5b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee * you may not use this file except in compliance with the License. 6b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee * You may obtain a copy of the License at 7b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee * 8b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee * http://www.apache.org/licenses/LICENSE-2.0 9b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee * 10b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee * Unless required by applicable law or agreed to in writing, software 11b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee * distributed under the License is distributed on an "AS IS" BASIS, 12b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee * See the License for the specific language governing permissions and 14b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee * limitations under the License. 15b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee */ 16b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee 1769c182afb0e6d82a341a28b4317aa703af768906Gary Maipackage com.android.contacts.preference; 18b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee 19b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Leeimport android.app.AlertDialog.Builder; 20b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Leeimport android.content.Context; 21b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Leeimport android.preference.ListPreference; 22b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Leeimport android.util.AttributeSet; 23b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee 243f6a2444e0134b7380cdb2e13abf4bf1163336d0Arthur Wangimport com.android.contacts.R; 25b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee 26b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee/** 27b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee * Custom preference: sort-by. 28b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee */ 29b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Leepublic final class SortOrderPreference extends ListPreference { 30b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee 31b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee private ContactsPreferences mPreferences; 32b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee private Context mContext; 33b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee 34b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee public SortOrderPreference(Context context) { 35b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee super(context); 36b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee prepare(); 37b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee } 38b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee 39b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee public SortOrderPreference(Context context, AttributeSet attrs) { 40b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee super(context, attrs); 41b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee prepare(); 42b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee } 43b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee 44b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee private void prepare() { 45b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee mContext = getContext(); 46b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee mPreferences = new ContactsPreferences(mContext); 47b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee setEntries(new String[]{ 48b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee mContext.getString(R.string.display_options_sort_by_given_name), 49b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee mContext.getString(R.string.display_options_sort_by_family_name), 50b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee }); 51b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee setEntryValues(new String[]{ 52b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee String.valueOf(ContactsPreferences.SORT_ORDER_PRIMARY), 53b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee String.valueOf(ContactsPreferences.SORT_ORDER_ALTERNATIVE), 54b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee }); 55b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee setValue(String.valueOf(mPreferences.getSortOrder())); 56b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee } 57b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee 58b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee @Override 59b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee protected boolean shouldPersist() { 60b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee return false; // This preference takes care of its own storage 61b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee } 62b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee 63b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee @Override 64b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee public CharSequence getSummary() { 65b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee switch (mPreferences.getSortOrder()) { 66b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee case ContactsPreferences.SORT_ORDER_PRIMARY: 67b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee return mContext.getString(R.string.display_options_sort_by_given_name); 68b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee case ContactsPreferences.SORT_ORDER_ALTERNATIVE: 69b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee return mContext.getString(R.string.display_options_sort_by_family_name); 70b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee } 71b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee return null; 72b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee } 73b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee 74b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee @Override 75b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee protected boolean persistString(String value) { 76b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee int newValue = Integer.parseInt(value); 77b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee if (newValue != mPreferences.getSortOrder()) { 78b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee mPreferences.setSortOrder(newValue); 79b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee notifyChanged(); 80b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee } 81b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee return true; 82b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee } 83b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee 84b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee @Override 85b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee // UX recommendation is not to show cancel button on such lists. 86b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee protected void onPrepareDialogBuilder(Builder builder) { 87b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee super.onPrepareDialogBuilder(builder); 88b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee builder.setNegativeButton(null, null); 89b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee } 90b3d841a64bb0eb8dd80552b66688b1614e904a35Yorke Lee} 91