1 /*
2  * Copyright (C) 2022 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.settings.accessibility;
18 
19 import android.content.Context;
20 import android.provider.Settings;
21 import android.view.accessibility.CaptioningManager;
22 
23 import androidx.preference.ListPreference;
24 import androidx.preference.Preference;
25 
26 import com.android.settings.core.BasePreferenceController;
27 
28 /** Preference controller for captioning font size. */
29 public class CaptioningFontSizeController extends BasePreferenceController
30         implements Preference.OnPreferenceChangeListener {
31 
32     private final CaptioningManager mCaptioningManager;
33     private final CaptionHelper mCaptionHelper;
34 
CaptioningFontSizeController(Context context, String preferenceKey)35     public CaptioningFontSizeController(Context context, String preferenceKey) {
36         super(context, preferenceKey);
37         mCaptioningManager = context.getSystemService(CaptioningManager.class);
38         mCaptionHelper = new CaptionHelper(context);
39     }
40 
41     @Override
getAvailabilityStatus()42     public int getAvailabilityStatus() {
43         return AVAILABLE;
44     }
45 
46     @Override
updateState(Preference preference)47     public void updateState(Preference preference) {
48         super.updateState(preference);
49         final ListPreference listPreference = (ListPreference) preference;
50         final float fontSize = mCaptioningManager.getFontScale();
51 
52         listPreference.setValue(Float.toString(fontSize));
53     }
54 
55     @Override
onPreferenceChange(Preference preference, Object newValue)56     public boolean onPreferenceChange(Preference preference, Object newValue) {
57         Settings.Secure.putFloat(
58                 mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE,
59                 Float.parseFloat((String) newValue));
60         mCaptionHelper.setEnabled(true);
61         return true;
62     }
63 }
64