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.CaptionStyle;
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 type face. */
29 public class CaptioningTypefaceController extends BasePreferenceController
30         implements Preference.OnPreferenceChangeListener {
31 
32     private final CaptionHelper mCaptionHelper;
33 
CaptioningTypefaceController(Context context, String preferenceKey)34     public CaptioningTypefaceController(Context context, String preferenceKey) {
35         super(context, preferenceKey);
36         mCaptionHelper = new CaptionHelper(context);
37     }
38 
39     @Override
getAvailabilityStatus()40     public int getAvailabilityStatus() {
41         return AVAILABLE;
42     }
43 
44     @Override
updateState(Preference preference)45     public void updateState(Preference preference) {
46         super.updateState(preference);
47         final ListPreference listPreference = (ListPreference) preference;
48         final CaptionStyle attrs = CaptionStyle.getCustomStyle(mContext.getContentResolver());
49         final String rawTypeface = attrs.mRawTypeface;
50 
51         listPreference.setValue(rawTypeface == null ? "" : rawTypeface);
52     }
53 
54     @Override
onPreferenceChange(Preference preference, Object newValue)55     public boolean onPreferenceChange(Preference preference, Object newValue) {
56         Settings.Secure.putString(
57                 mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_CAPTIONING_TYPEFACE,
58                 (String) newValue);
59         mCaptionHelper.setEnabled(true);
60         return true;
61     }
62 }
63