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