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.tv.settings.device.displaysound;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.provider.Settings;
24 
25 import androidx.annotation.Keep;
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceGroup;
28 
29 import com.android.tv.settings.R;
30 import com.android.tv.settings.RadioPreference;
31 import com.android.tv.settings.SettingsPreferenceFragment;
32 
33 /**
34  * The "Text scaling" screen in TV Settings.
35  */
36 @Keep
37 public class FontScalePreferenceFragment extends SettingsPreferenceFragment implements
38                 Preference.OnPreferenceChangeListener {
39     private static final String FONT_SCALE_RADIO_GROUP = "font_scale_radio_group";
40     private static final String FONT_SCALE_GROUP = "font_scale_group";
41 
42     /** Value of FONT_SCALE. */
43     private float mCurrentFontScaleValue;
44 
45     @Override
onCreatePreferences(Bundle bundle, String s)46     public void onCreatePreferences(Bundle bundle, String s) {
47         setPreferencesFromResource(R.xml.font_scale, null);
48         PreferenceGroup fontScaleGroup = (PreferenceGroup) findPreference(FONT_SCALE_GROUP);
49         final Context themedContext = getPreferenceManager().getContext();
50         final String[] entryValues = getContext().getResources()
51                 .getStringArray(R.array.font_scale_entry_values);
52         initFontScaleValue(getContext());
53 
54         for (int i = 0; i < entryValues.length; i++) {
55             final RadioPreference preference = new RadioPreference(themedContext);
56             preference.setPersistent(false);
57             preference.setRadioGroup(FONT_SCALE_RADIO_GROUP);
58             preference.setOnPreferenceChangeListener(this);
59             preference.setKey(entryValues[i]);
60             int scaleValue = (int) (Float.valueOf(entryValues[i]) * 100);
61             String summary = getContext().getResources()
62                     .getString(R.string.font_scale_item_detail, scaleValue);
63             preference.setSummaryOff(summary);
64             preference.setSummaryOn(summary);
65             preference.setTitle(mapScaleIndexToTitle(i));
66             preference.setFragment(FontScalePreviewFragment.class.getName());
67             Bundle extras = preference.getExtras();
68             extras.putString(FontScalePreviewFragment.PREVIEW_FONT_SCALE_VALUE, entryValues[i]);
69             extras.putFloat(
70                     FontScalePreviewFragment.CURRENT_FONT_SCALE_VALUE, mCurrentFontScaleValue);
71 
72             if (entryValues[i].equals(String.valueOf(mCurrentFontScaleValue))) {
73                 preference.setChecked(true);
74             }
75             fontScaleGroup.addPreference(preference);
76         }
77     }
78 
79     // Temporary method to accommodate additional font scale settings.
80     // TODO: remove once font scale values are finalized.
mapScaleIndexToTitle(int scaleValueIndex)81     private String mapScaleIndexToTitle(int scaleValueIndex) {
82         final String[] entries = getContext().getResources()
83                 .getStringArray(R.array.font_scale_entries);
84         if (scaleValueIndex < 2) {
85             return entries[scaleValueIndex];
86         }
87         if (scaleValueIndex > 3) {
88             return entries[3];
89         } else {
90             return entries[2];
91         }
92     }
93 
initFontScaleValue(Context context)94     private void initFontScaleValue(Context context) {
95         final ContentResolver resolver = getContext().getContentResolver();
96         mCurrentFontScaleValue =
97                 Settings.System.getFloat(resolver, Settings.System.FONT_SCALE, 1.0f);
98     }
99 
100     @Override
onPreferenceChange(Preference preference, Object newValue)101     public boolean onPreferenceChange(Preference preference, Object newValue) {
102         RadioPreference radioPreference = (RadioPreference) preference;
103         if (radioPreference.isChecked()) {
104             return false;
105         }
106         PreferenceGroup fontScaleGroup = (PreferenceGroup) findPreference(FONT_SCALE_GROUP);
107         radioPreference.clearOtherRadioPreferences(fontScaleGroup);
108         mCurrentFontScaleValue = Float.parseFloat(preference.getKey());
109         commit();
110         return true;
111     }
112 
commit()113     protected void commit() {
114         if (getContext() == null) return;
115         final ContentResolver resolver = getContext().getContentResolver();
116         Settings.System.putFloat(resolver, Settings.System.FONT_SCALE, mCurrentFontScaleValue);
117     }
118 
119     @Override
getPageId()120     protected int getPageId() {
121         return SettingsEnums.ACCESSIBILITY_FONT_SIZE;
122     }
123 }
124