1 /*
2  * Copyright (C) 2021 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.car.settings.accessibility;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.provider.Settings;
22 
23 import androidx.annotation.VisibleForTesting;
24 import androidx.preference.ListPreference;
25 
26 import com.android.car.settings.R;
27 import com.android.car.settings.common.FragmentController;
28 import com.android.car.settings.common.PreferenceController;
29 
30 /**
31  * Preference controller for setting the captions text style. This is achieved through the settings
32  * secure constant {@link Settings.Secure#ACCESSIBILITY_CAPTIONING_PRESET}.
33  */
34 public class CaptionsTextStyleListPreferenceController extends
35         PreferenceController<ListPreference> {
36 
37     private static final int DEFAULT_SELECTOR_INDEX = 0;
38     private static final int DEFAULT_STYLE_PRESET = 4;
39 
40     @VisibleForTesting
41     final String[] mFontStyleTitles;
42     private final String[] mFontStyleStringValues;
43     private final int[] mFontStyleIntValues;
44 
CaptionsTextStyleListPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)45     public CaptionsTextStyleListPreferenceController(Context context, String preferenceKey,
46             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
47         super(context, preferenceKey, fragmentController, uxRestrictions);
48         mFontStyleTitles = new String[]{
49                 context.getString(R.string.captions_settings_text_style_by_app),
50                 context.getString(R.string.captions_settings_text_style_white_on_black),
51                 context.getString(R.string.captions_settings_text_style_black_on_white),
52                 context.getString(R.string.captions_settings_text_style_yellow_on_black),
53                 context.getString(R.string.captions_settings_text_style_yellow_on_blue)
54         };
55         mFontStyleStringValues = new String[]{
56                 "4",
57                 "0",
58                 "1",
59                 "2",
60                 "3"
61         };
62         mFontStyleIntValues = new int[mFontStyleStringValues.length];
63         for (int i = 0; i < mFontStyleStringValues.length; i++) {
64             mFontStyleIntValues[i] = Integer.parseInt(mFontStyleStringValues[i]);
65         }
66     }
67 
68     @Override
getPreferenceType()69     protected Class<ListPreference> getPreferenceType() {
70         return ListPreference.class;
71     }
72 
73     @Override
updateState(ListPreference preference)74     protected void updateState(ListPreference preference) {
75         preference.setEntries(mFontStyleTitles);
76         preference.setEntryValues(mFontStyleStringValues);
77         int currentFontStyleIndex = getCurrentSelectedFontStyleIndex();
78         preference.setValueIndex(currentFontStyleIndex);
79         preference.setSummary(getSummary(currentFontStyleIndex));
80     }
81 
82     @Override
handlePreferenceChanged(ListPreference preference, Object newValue)83     public boolean handlePreferenceChanged(ListPreference preference, Object newValue) {
84         int newFontValue = Integer.parseInt((String) newValue);
85         Settings.Secure.putInt(getContext().getContentResolver(),
86                 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, newFontValue);
87         return true;
88     }
89 
getSummary(int currentFontStyleIndex)90     private CharSequence getSummary(int currentFontStyleIndex) {
91         return mFontStyleTitles[currentFontStyleIndex];
92     }
93 
getCurrentSelectedFontStyleIndex()94     private int getCurrentSelectedFontStyleIndex() {
95         int currentFontStyle = Settings.Secure.getInt(getContext().getContentResolver(),
96                 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, DEFAULT_STYLE_PRESET);
97 
98         int selectorIndex = DEFAULT_SELECTOR_INDEX;
99         for (int i = 0; i < mFontStyleIntValues.length; i++) {
100             if (mFontStyleIntValues[i] == currentFontStyle) {
101                 selectorIndex = i;
102                 break;
103             }
104         }
105 
106         return selectorIndex;
107     }
108 }
109