Yorke Lee | b3d841a | 2014-07-10 11:38:55 -0700 | [diff] [blame] | 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 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 17 | package com.android.contacts.preference; |
Yorke Lee | b3d841a | 2014-07-10 11:38:55 -0700 | [diff] [blame] | 18 | |
| 19 | import android.app.AlertDialog.Builder; |
| 20 | import android.content.Context; |
| 21 | import android.preference.ListPreference; |
Yorke Lee | b3d841a | 2014-07-10 11:38:55 -0700 | [diff] [blame] | 22 | import android.util.AttributeSet; |
| 23 | |
Arthur Wang | 3f6a244 | 2016-12-05 14:51:59 -0800 | [diff] [blame] | 24 | import com.android.contacts.R; |
Yorke Lee | b3d841a | 2014-07-10 11:38:55 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Custom preference: view-name-as (first name first or last name first). |
| 28 | */ |
| 29 | public final class DisplayOrderPreference extends ListPreference { |
| 30 | |
| 31 | private ContactsPreferences mPreferences; |
| 32 | private Context mContext; |
| 33 | |
| 34 | public DisplayOrderPreference(Context context) { |
| 35 | super(context); |
| 36 | prepare(); |
| 37 | } |
| 38 | |
| 39 | public DisplayOrderPreference(Context context, AttributeSet attrs) { |
| 40 | super(context, attrs); |
| 41 | prepare(); |
| 42 | } |
| 43 | |
| 44 | private void prepare() { |
| 45 | mContext = getContext(); |
| 46 | mPreferences = new ContactsPreferences(mContext); |
| 47 | setEntries(new String[]{ |
| 48 | mContext.getString(R.string.display_options_view_given_name_first), |
| 49 | mContext.getString(R.string.display_options_view_family_name_first), |
| 50 | }); |
| 51 | setEntryValues(new String[]{ |
| 52 | String.valueOf(ContactsPreferences.DISPLAY_ORDER_PRIMARY), |
| 53 | String.valueOf(ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE), |
| 54 | }); |
| 55 | setValue(String.valueOf(mPreferences.getDisplayOrder())); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected boolean shouldPersist() { |
| 60 | return false; // This preference takes care of its own storage |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public CharSequence getSummary() { |
| 65 | switch (mPreferences.getDisplayOrder()) { |
| 66 | case ContactsPreferences.DISPLAY_ORDER_PRIMARY: |
| 67 | return mContext.getString(R.string.display_options_view_given_name_first); |
| 68 | case ContactsPreferences.DISPLAY_ORDER_ALTERNATIVE: |
| 69 | return mContext.getString(R.string.display_options_view_family_name_first); |
| 70 | } |
| 71 | return null; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | protected boolean persistString(String value) { |
| 76 | int newValue = Integer.parseInt(value); |
| 77 | if (newValue != mPreferences.getDisplayOrder()) { |
| 78 | mPreferences.setDisplayOrder(newValue); |
| 79 | notifyChanged(); |
| 80 | } |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | // UX recommendation is not to show cancel button on such lists. |
| 86 | protected void onPrepareDialogBuilder(Builder builder) { |
| 87 | super.onPrepareDialogBuilder(builder); |
| 88 | builder.setNegativeButton(null, null); |
| 89 | } |
| 90 | } |