Home | History | Annotate | Download | only in preference
      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 com.android.contacts.R;
     20 
     21 import android.content.Context;
     22 import android.preference.ListPreference;
     23 import android.provider.ContactsContract;
     24 import android.util.AttributeSet;
     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(ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY),
     53                 String.valueOf(ContactsContract.Preferences.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 ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY:
     67                 return mContext.getString(R.string.display_options_view_given_name_first);
     68             case ContactsContract.Preferences.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