package org.ccci.gto.android.common.preference;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.preference.ListPreference;
import android.util.AttributeSet;

public class ListPreferenceCompat extends ListPreference {
    public ListPreferenceCompat(final Context context) {
        super(context);
    }

    public ListPreferenceCompat(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ListPreferenceCompat(final Context context, final AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ListPreferenceCompat(final Context context, final AttributeSet attrs, final int defStyleAttr,
                                final int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    /**
     * Returns the summary of this ListPreference. If the summary
     * has a {@linkplain java.lang.String#format String formatting}
     * marker in it (i.e. "%s" or "%1$s"), then the current entry
     * value will be substituted in its place.
     *
     * @return the summary with appropriate string substitution
     */
    @Override
    public CharSequence getSummary() {
        // Android pre-Honeycomb didn't support String formatting for the Summary, so we replicate that behavior here
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
            final CharSequence summary = super.getSummary();
            final CharSequence entry = getEntry();
            if (summary == null || entry == null) {
                return summary;
            } else {
                return String.format(summary.toString(), entry);
            }
        } else {
            return super.getSummary();
        }
    }
}

@@ -178,16 +178,47 @@
 erence;%0A
+import android.text.TextUtils;%0A
 import a
@@ -1936,11 +1936,791 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * Sets the value of the key. This should be one of the entries in%0A     * %7B@link #getEntryValues()%7D.%0A     *%0A     * @param value The value to set for the key.%0A     * @see %3Ca href=%22http://stackoverflow.com/a/21642401/4721910%22%3Ehttp://stackoverflow.com/a/21642401/4721910%3C/a%3E%0A     */%0A    @Override%0A    public void setValue(final String value) %7B%0A        // Android pre-KitKat didn't let the preference know that data had changed, which caused stale Summaries.%0A        if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.KITKAT) %7B%0A            String oldValue = getValue();%0A            super.setValue(value);%0A            if (!TextUtils.equals(value, oldValue)) %7B%0A                notifyChanged();%0A            %7D%0A        %7D else %7B%0A            super.setValue(value);%0A        %7D%0A    %7D
 %0A%7D%0A

support updating the Summary when the value changes pre-KitKat