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.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.core.BasePreferenceController;
27 
28 /** Controller that shows the captioning locale summary. */
29 public class CaptioningLocalePreferenceController extends BasePreferenceController
30         implements Preference.OnPreferenceChangeListener {
31 
32     private final CaptioningManager mCaptioningManager;
33 
CaptioningLocalePreferenceController(Context context, String preferenceKey)34     public CaptioningLocalePreferenceController(Context context, String preferenceKey) {
35         super(context, preferenceKey);
36         mCaptioningManager = context.getSystemService(CaptioningManager.class);
37     }
38 
39     @Override
getAvailabilityStatus()40     public int getAvailabilityStatus() {
41         return AVAILABLE;
42     }
43 
44     @Override
displayPreference(PreferenceScreen screen)45     public void displayPreference(PreferenceScreen screen) {
46         super.displayPreference(screen);
47         final LocalePreference localePreference = screen.findPreference(getPreferenceKey());
48         final String rawLocale = mCaptioningManager.getRawLocale();
49         localePreference.setValue(rawLocale == null ? "" : rawLocale);
50     }
51 
52     @Override
onPreferenceChange(Preference preference, Object newValue)53     public boolean onPreferenceChange(Preference preference, Object newValue) {
54         final LocalePreference localePreference = (LocalePreference) preference;
55         Settings.Secure.putString(mContext.getContentResolver(),
56                 Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, (String) newValue);
57         localePreference.setValue((String) newValue);
58         return true;
59     }
60 }
61