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  */
16 package com.android.emergency.preferences;
17 
18 import android.content.Context;
19 import android.content.res.TypedArray;
20 import androidx.annotation.Nullable;
21 import androidx.preference.ListPreference;
22 import android.text.Spannable;
23 import android.text.SpannableString;
24 import android.text.TextUtils;
25 import android.text.style.TtsSpan;
26 import android.util.AttributeSet;
27 
28 import com.android.emergency.R;
29 import com.android.emergency.ReloadablePreferenceInterface;
30 import com.android.internal.annotations.VisibleForTesting;
31 
32 /**
33  * Custom {@link ListPreference} that allows us to refresh and update the summary.
34  */
35 public class EmergencyListPreference extends ListPreference
36         implements ReloadablePreferenceInterface {
37     @Nullable
38     private CharSequence[] mContentDescriptions;
39 
EmergencyListPreference(Context context, AttributeSet attrs)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
reloadFromPreference()54     public void reloadFromPreference() {
55         setValue(getPersistedString(""));
56     }
57 
58     @Override
isNotSet()59     public boolean isNotSet() {
60         return TextUtils.isEmpty(getValue());
61     }
62 
63     @Override
getSummary()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
getContentDescriptions()76     CharSequence[] getContentDescriptions() {
77         return mContentDescriptions;
78     }
79 
createAccessibleEntries(CharSequence entries[], CharSequence[] contentDescriptions)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 
createAccessibleSequence(CharSequence displayText, CharSequence accessibleText)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_EXCLUSIVE_EXCLUSIVE);
95         return str;
96     }
97 }
98