summaryrefslogtreecommitdiff
path: root/Settings/src/com/android/tv/settings/device/displaysound/FontScalePreferenceFragment.java
blob: b37dba1ef6b8b0995d3c515222749e981377551d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.settings.device.displaysound;

import static com.android.tv.settings.util.InstrumentationUtils.logEntrySelected;

import android.app.tvsettings.TvSettingsEnums;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;

import androidx.annotation.Keep;
import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;

import com.android.tv.settings.R;
import com.android.tv.settings.RadioPreference;
import com.android.tv.settings.SettingsPreferenceFragment;
import com.android.tv.settings.overlay.FlavorUtils;

/**
 * The "Text scaling" screen in TV Settings.
 */
@Keep
public class FontScalePreferenceFragment extends SettingsPreferenceFragment implements
        Preference.OnPreferenceChangeListener {
    private static final String FONT_SCALE_RADIO_GROUP = "font_scale_radio_group";
    private static final String FONT_SCALE_GROUP = "font_scale_group";

    /** Value of FONT_SCALE. */
    private float mCurrentFontScaleValue;

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        setPreferencesFromResource(R.xml.font_scale, null);
        PreferenceGroup fontScaleGroup = (PreferenceGroup) findPreference(FONT_SCALE_GROUP);
        final Context themedContext = getPreferenceManager().getContext();
        final String[] entryValues = getContext().getResources()
                .getStringArray(R.array.font_scale_entry_values);
        final String[] entries = getContext().getResources()
                .getStringArray(R.array.font_scale_entries);
        initFontScaleValue(getContext());

        for (int i = 0; i < entryValues.length; i++) {
            final RadioPreference preference = new RadioPreference(themedContext);
            preference.setOnPreferenceChangeListener(this);
            preference.setPersistent(false);
            preference.setRadioGroup(FONT_SCALE_RADIO_GROUP);
            preference.setKey(entryValues[i]);
            int scaleValue = (int) (Float.valueOf(entryValues[i]) * 100);
            String summary = getContext().getResources()
                    .getString(R.string.font_scale_item_detail, scaleValue);
            preference.setSummaryOff(summary);
            preference.setSummaryOn(summary);
            preference.setTitle(entries[i]);
            if (Float.compare(mCurrentFontScaleValue, Float.parseFloat(entryValues[i])) == 0) {
                preference.setChecked(true);
            }
            initPreview(preference, Float.parseFloat(entryValues[i]));
            fontScaleGroup.addPreference(preference);
        }
    }

    private void initPreview(RadioPreference preference, float previewFontScaleValue) {
        if (FlavorUtils.isTwoPanel(getContext())) {
            preference.setFragment(FontScalePreviewFragment.class.getName());
        }
        Bundle extras = preference.getExtras();
        extras.putString(FontScalePreviewFragment.PREVIEW_FONT_SCALE_VALUE,
                String.valueOf(previewFontScaleValue));
        extras.putFloat(
                FontScalePreviewFragment.CURRENT_FONT_SCALE_VALUE, mCurrentFontScaleValue);
    }


    private void initFontScaleValue(Context context) {
        final ContentResolver resolver = getContext().getContentResolver();
        mCurrentFontScaleValue =
                Settings.System.getFloat(resolver, Settings.System.FONT_SCALE, 1.0f);
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        RadioPreference radioPreference = (RadioPreference) preference;
        if (radioPreference.isChecked()) {
            return false;
        }
        PreferenceGroup fontScaleGroup = (PreferenceGroup) findPreference(FONT_SCALE_GROUP);
        radioPreference.clearOtherRadioPreferences(fontScaleGroup);
        mCurrentFontScaleValue = Float.parseFloat(preference.getKey());
        commit();
        initPreview(radioPreference, mCurrentFontScaleValue);
        radioPreference.setChecked(true);
        logNewFontScaleSelection(preference.getKey());
        return true;
    }

    protected void commit() {
        if (getContext() == null) return;
        final ContentResolver resolver = getContext().getContentResolver();
        Settings.System.putFloat(resolver, Settings.System.FONT_SCALE, mCurrentFontScaleValue);
    }

    private void logNewFontScaleSelection(String fontScale) {
        final int[] textScalingOptions = {
                TvSettingsEnums.DISPLAY_SOUND_TEXT_SCALING_SMALL,
                TvSettingsEnums.DISPLAY_SOUND_TEXT_SCALING_DEFAULT,
                TvSettingsEnums.DISPLAY_SOUND_TEXT_SCALING_LARGE,
                TvSettingsEnums.DISPLAY_SOUND_TEXT_SCALING_LARGEST,
        };
        final String[] entryValues = getContext().getResources()
                .getStringArray(R.array.font_scale_entry_values);
        for (int i = 0; i < entryValues.length; i++) {
            if (fontScale.equals(entryValues[i])) {
                logEntrySelected(textScalingOptions[i]);
                break;
            }
        }
    }

    @Override
    protected int getPageId() {
        return TvSettingsEnums.DISPLAY_SOUND_TEXT_SCALING;
    }
}