1/*
2 * Copyright (C) 2016 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 */
16package com.android.emergency.preferences;
17
18import android.content.Context;
19import android.content.res.TypedArray;
20import android.preference.ListPreference;
21import android.support.annotation.Nullable;
22import android.text.Spannable;
23import android.text.SpannableString;
24import android.text.TextUtils;
25import android.text.style.TtsSpan;
26import android.util.AttributeSet;
27
28import com.android.emergency.R;
29import com.android.emergency.ReloadablePreferenceInterface;
30import com.android.internal.annotations.VisibleForTesting;
31
32/**
33 * Custom {@link ListPreference} that allows us to refresh and update the summary.
34 */
35public class EmergencyListPreference extends ListPreference
36        implements ReloadablePreferenceInterface {
37    @Nullable
38    private CharSequence[] mContentDescriptions;
39
40    public EmergencyListPreference(Context context, AttributeSet attrs) {
41        super(context, attrs);
42        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EmergencyListPreference);
43        mContentDescriptions =
44                a.getTextArray(R.styleable.EmergencyListPreference_entryContentDescriptions);
45        a.recycle();
46
47        if (mContentDescriptions != null) {
48            // Override entries with accessible entries.
49            setEntries(createAccessibleEntries(getEntries(), mContentDescriptions));
50        }
51    }
52
53    @Override
54    public void reloadFromPreference() {
55        setValue(getPersistedString(""));
56    }
57
58    @Override
59    public boolean isNotSet() {
60        return TextUtils.isEmpty(getValue());
61    }
62
63    @Override
64    public CharSequence getSummary() {
65        final String value = getValue();
66        int index = findIndexOfValue(value);
67        if (index < 0) {
68            return super.getSummary();
69        } else {
70            return getEntry();
71        }
72    }
73
74    @Nullable
75    @VisibleForTesting
76    CharSequence[] getContentDescriptions() {
77        return mContentDescriptions;
78    }
79
80    private static CharSequence[] createAccessibleEntries(CharSequence entries[],
81                                                          CharSequence[] contentDescriptions) {
82        CharSequence[] accessibleEntries = new CharSequence[entries.length];
83        for (int i = 0; i < entries.length; i++) {
84            accessibleEntries[i] = createAccessibleSequence(entries[i], contentDescriptions[i]);
85        }
86        return accessibleEntries;
87    }
88
89    private static SpannableString createAccessibleSequence(CharSequence displayText,
90                                                            CharSequence accessibleText) {
91        SpannableString str = new SpannableString(displayText);
92        str.setSpan(new TtsSpan.TextBuilder((String) accessibleText).build(), 0,
93                displayText.length(),
94                Spannable.SPAN_INCLUSIVE_INCLUSIVE);
95        return str;
96    }
97}
98