1/******************************************************************************* 2 * Copyright (C) 2014 Google Inc. 3 * Licensed to The Android Open Source Project. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 *******************************************************************************/ 17 18package com.android.mail.ui.settings; 19 20import android.content.Context; 21import android.content.res.TypedArray; 22import android.preference.ListPreference; 23import android.util.AttributeSet; 24 25import com.android.mail.R; 26 27/** 28 * A fancy ListPreference that displays its summary from among the entries in the "entrySummaries" 29 * array attribute. 30 * 31 */ 32public class FancySummaryListPreference extends ListPreference { 33 34 private CharSequence[] mEntrySummaries; 35 36 public FancySummaryListPreference(Context context) { 37 this(context, null); 38 } 39 40 public FancySummaryListPreference(Context context, AttributeSet attrs) { 41 super(context, attrs); 42 43 TypedArray a = context.obtainStyledAttributes(attrs, 44 R.styleable.FancySummaryListPreference, 0, 0); 45 mEntrySummaries = a.getTextArray(R.styleable.FancySummaryListPreference_entrySummaries); 46 } 47 48 public void setEntrySummaries(CharSequence[] summaries) { 49 mEntrySummaries = summaries; 50 setSummary(getSummaryForValue(getValue())); 51 } 52 53 public void setEntrySummaries(int summariesResId) { 54 setEntrySummaries(getContext().getResources().getTextArray(summariesResId)); 55 } 56 57 public CharSequence[] getEntrySummaries() { 58 return mEntrySummaries; 59 } 60 61 @Override 62 public void setValue(String value) { 63 super.setValue(value); 64 setSummary(getSummaryForValue(value)); 65 } 66 67 private CharSequence getSummaryForValue(String value) { 68 int i = findIndexOfValue(value); 69 return (i >= 0 && i < mEntrySummaries.length) ? mEntrySummaries[i] : null; 70 } 71 72} 73