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 size. This is achieved through the settings
32  * secure constant {@link Settings.Secure#ACCESSIBILITY_CAPTIONING_FONT_SCALE}.
33  */
34 public class CaptionsTextSizeListPreferenceController extends PreferenceController<ListPreference> {
35 
36     private static final int DEFAULT_SELECTOR_INDEX = 2;
37     private static final float DEFAULT_TEXT_SIZE = 1.0F;
38 
39     @VisibleForTesting
40     final String[] mFontSizeTitles;
41     private final String[] mFontSizeStringValues;
42     private final float[] mFontSizeFloatValues;
43 
CaptionsTextSizeListPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)44     public CaptionsTextSizeListPreferenceController(Context context, String preferenceKey,
45             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
46         super(context, preferenceKey, fragmentController, uxRestrictions);
47         mFontSizeTitles = new String[]{
48                 context.getString(R.string.captions_settings_text_size_very_small),
49                 context.getString(R.string.captions_settings_text_size_small),
50                 context.getString(R.string.captions_settings_text_size_default),
51                 context.getString(R.string.captions_settings_text_size_large),
52                 context.getString(R.string.captions_settings_text_size_very_large)
53         };
54         mFontSizeStringValues = new String[]{
55                 "0.25",
56                 "0.5",
57                 "1.0",
58                 "1.5",
59                 "2.0"
60         };
61         mFontSizeFloatValues = new float[mFontSizeStringValues.length];
62         for (int i = 0; i < mFontSizeStringValues.length; i++) {
63             mFontSizeFloatValues[i] = Float.parseFloat(mFontSizeStringValues[i]);
64         }
65     }
66 
67     @Override
getPreferenceType()68     protected Class<ListPreference> getPreferenceType() {
69         return ListPreference.class;
70     }
71 
72     @Override
updateState(ListPreference preference)73     protected void updateState(ListPreference preference) {
74         preference.setEntries(mFontSizeTitles);
75         preference.setEntryValues(mFontSizeStringValues);
76         int currentFontSizeIndex = getCurrentSelectedFontSizeIndex();
77         preference.setValueIndex(currentFontSizeIndex);
78         preference.setSummary(getSummary(currentFontSizeIndex));
79     }
80 
81     @Override
handlePreferenceChanged(ListPreference preference, Object newValue)82     public boolean handlePreferenceChanged(ListPreference preference, Object newValue) {
83         float newFontValue = Float.parseFloat((String) newValue);
84         Settings.Secure.putFloat(getContext().getContentResolver(),
85                 Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE, newFontValue);
86         return true;
87     }
88 
getSummary(int currentFontSizeIndex)89     private CharSequence getSummary(int currentFontSizeIndex) {
90         return mFontSizeTitles[currentFontSizeIndex];
91     }
92 
getCurrentSelectedFontSizeIndex()93     private int getCurrentSelectedFontSizeIndex() {
94         float currentFontScale = Settings.Secure.getFloat(getContext().getContentResolver(),
95                 Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE, DEFAULT_TEXT_SIZE);
96 
97         int selectorIndex = DEFAULT_SELECTOR_INDEX;
98         for (int i = 0; i < mFontSizeFloatValues.length; i++) {
99             if (mFontSizeFloatValues[i] == currentFontScale) {
100                 selectorIndex = i;
101                 break;
102             }
103         }
104 
105         return selectorIndex;
106     }
107 }
108