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 static com.android.tv.settings.util.InstrumentationUtils.logEntrySelected;
20 
21 import android.app.tvsettings.TvSettingsEnums;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.os.Bundle;
25 import android.provider.Settings;
26 
27 import androidx.annotation.Keep;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceScreen;
30 
31 import com.android.tv.settings.R;
32 import com.android.tv.settings.RadioPreference;
33 import com.android.tv.settings.SettingsPreferenceFragment;
34 import com.android.tv.settings.overlay.FlavorUtils;
35 
36 /**
37  * The "Text scaling" screen in TV Settings.
38  */
39 @Keep
40 public class FontScalePreferenceFragment extends SettingsPreferenceFragment implements
41         Preference.OnPreferenceChangeListener {
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         PreferenceScreen fontScaleScreen = getPreferenceManager().getPreferenceScreen();
49         final Context themedContext = getPreferenceManager().getContext();
50         final String[] entryValues = getContext().getResources()
51                 .getStringArray(R.array.font_scale_entry_values);
52         final String[] entries = getContext().getResources()
53                 .getStringArray(R.array.font_scale_entries);
54         initFontScaleValue(getContext());
55 
56         for (int i = 0; i < entryValues.length; i++) {
57             final RadioPreference preference = new RadioPreference(themedContext);
58             preference.setOnPreferenceChangeListener(this);
59             preference.setPersistent(false);
60             preference.setKey(entryValues[i]);
61             int scaleValue = (int) (Float.valueOf(entryValues[i]) * 100);
62             String summary = getContext().getResources()
63                     .getString(R.string.font_scale_item_detail, scaleValue);
64             preference.setSummaryOff(summary);
65             preference.setSummaryOn(summary);
66             preference.setTitle(entries[i]);
67             if (Float.compare(mCurrentFontScaleValue, Float.parseFloat(entryValues[i])) == 0) {
68                 preference.setChecked(true);
69             }
70             initPreview(preference, Float.parseFloat(entryValues[i]));
71             fontScaleScreen.addPreference(preference);
72         }
73     }
74 
initPreview(RadioPreference preference, float previewFontScaleValue)75     private void initPreview(RadioPreference preference, float previewFontScaleValue) {
76         if (FlavorUtils.isTwoPanel(getContext())) {
77             preference.setFragment(FontScalePreviewFragment.class.getName());
78         }
79         Bundle extras = preference.getExtras();
80         extras.putString(FontScalePreviewFragment.PREVIEW_FONT_SCALE_VALUE,
81                 String.valueOf(previewFontScaleValue));
82         extras.putFloat(
83                 FontScalePreviewFragment.CURRENT_FONT_SCALE_VALUE, mCurrentFontScaleValue);
84     }
85 
86 
initFontScaleValue(Context context)87     private void initFontScaleValue(Context context) {
88         final ContentResolver resolver = getContext().getContentResolver();
89         mCurrentFontScaleValue =
90                 Settings.System.getFloat(resolver, Settings.System.FONT_SCALE, 1.0f);
91     }
92 
93     @Override
onPreferenceChange(Preference preference, Object newValue)94     public boolean onPreferenceChange(Preference preference, Object newValue) {
95         RadioPreference radioPreference = (RadioPreference) preference;
96         if (radioPreference.isChecked()) {
97             return false;
98         }
99         PreferenceScreen fontScaleScreen = getPreferenceManager().getPreferenceScreen();
100         radioPreference.clearOtherRadioPreferences(fontScaleScreen);
101         mCurrentFontScaleValue = Float.parseFloat(preference.getKey());
102         commit();
103         initPreview(radioPreference, mCurrentFontScaleValue);
104         radioPreference.setChecked(true);
105         logNewFontScaleSelection(preference.getKey());
106         return true;
107     }
108 
commit()109     protected void commit() {
110         if (getContext() == null) return;
111         final ContentResolver resolver = getContext().getContentResolver();
112         Settings.System.putFloat(resolver, Settings.System.FONT_SCALE, mCurrentFontScaleValue);
113     }
114 
logNewFontScaleSelection(String fontScale)115     private void logNewFontScaleSelection(String fontScale) {
116         final int[] textScalingOptions = {
117                 TvSettingsEnums.DISPLAY_SOUND_TEXT_SCALING_SMALL,
118                 TvSettingsEnums.DISPLAY_SOUND_TEXT_SCALING_DEFAULT,
119                 TvSettingsEnums.DISPLAY_SOUND_TEXT_SCALING_LARGE,
120                 TvSettingsEnums.DISPLAY_SOUND_TEXT_SCALING_LARGEST,
121         };
122         final String[] entryValues = getContext().getResources()
123                 .getStringArray(R.array.font_scale_entry_values);
124         for (int i = 0; i < entryValues.length; i++) {
125             if (fontScale.equals(entryValues[i])) {
126                 logEntrySelected(textScalingOptions[i]);
127                 break;
128             }
129         }
130     }
131 
132     @Override
getPageId()133     protected int getPageId() {
134         return TvSettingsEnums.DISPLAY_SOUND_TEXT_SCALING;
135     }
136 }
137