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 android.app.AlertDialog.Builder;
     20 import android.content.Context;
     21 import android.preference.ListPreference;
     22 import android.util.AttributeSet;
     23 
     24 import com.android.contacts.R;
     25 
     26 /**
     27  * Custom preference: sort-by.
     28  */
     29 public final class SortOrderPreference extends ListPreference {
     30 
     31     private ContactsPreferences mPreferences;
     32     private Context mContext;
     33 
     34     public SortOrderPreference(Context context) {
     35         super(context);
     36         prepare();
     37     }
     38 
     39     public SortOrderPreference(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_sort_by_given_name),
     49                 mContext.getString(R.string.display_options_sort_by_family_name),
     50         });
     51         setEntryValues(new String[]{
     52                 String.valueOf(ContactsPreferences.SORT_ORDER_PRIMARY),
     53                 String.valueOf(ContactsPreferences.SORT_ORDER_ALTERNATIVE),
     54         });
     55         setValue(String.valueOf(mPreferences.getSortOrder()));
     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.getSortOrder()) {
     66             case ContactsPreferences.SORT_ORDER_PRIMARY:
     67                 return mContext.getString(R.string.display_options_sort_by_given_name);
     68             case ContactsPreferences.SORT_ORDER_ALTERNATIVE:
     69                 return mContext.getString(R.string.display_options_sort_by_family_name);
     70         }
     71         return null;
     72     }
     73 
     74     @Override
     75     protected boolean persistString(String value) {
     76         int newValue = Integer.parseInt(value);
     77         if (newValue != mPreferences.getSortOrder()) {
     78             mPreferences.setSortOrder(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 }
     91