1 /*
2  * Copyright (C) 2019 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.app.settings.SettingsEnums;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.provider.Settings;
24 import android.view.accessibility.CaptioningManager;
25 
26 import androidx.preference.Preference;
27 
28 import com.android.settings.R;
29 import com.android.settings.SettingsPreferenceFragment;
30 import com.android.settings.search.BaseSearchIndexProvider;
31 import com.android.settingslib.search.SearchIndexable;
32 
33 /** Settings fragment containing more options of captioning properties. */
34 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
35 public class CaptionMoreOptionsFragment extends SettingsPreferenceFragment
36         implements Preference.OnPreferenceChangeListener {
37     private static final String PREF_LOCALE = "captioning_locale";
38 
39     private CaptioningManager mCaptioningManager;
40 
41     private LocalePreference mLocale;
42 
43     @Override
getMetricsCategory()44     public int getMetricsCategory() {
45         return SettingsEnums.ACCESSIBILITY_CAPTION_MORE_OPTIONS;
46     }
47 
48     @Override
onCreate(Bundle icicle)49     public void onCreate(Bundle icicle) {
50         super.onCreate(icicle);
51 
52         mCaptioningManager = (CaptioningManager) getSystemService(Context.CAPTIONING_SERVICE);
53 
54         addPreferencesFromResource(R.xml.captioning_more_options);
55         initializeAllPreferences();
56         updateAllPreferences();
57         installUpdateListeners();
58     }
59 
initializeAllPreferences()60     private void initializeAllPreferences() {
61         mLocale = (LocalePreference) findPreference(PREF_LOCALE);
62     }
63 
installUpdateListeners()64     private void installUpdateListeners() {
65         mLocale.setOnPreferenceChangeListener(this);
66     }
67 
updateAllPreferences()68     private void updateAllPreferences() {
69         final String rawLocale = mCaptioningManager.getRawLocale();
70         mLocale.setValue(rawLocale == null ? "" : rawLocale);
71     }
72 
73     @Override
onPreferenceChange(Preference preference, Object value)74     public boolean onPreferenceChange(Preference preference, Object value) {
75         final ContentResolver cr = getActivity().getContentResolver();
76         if (mLocale == preference) {
77             Settings.Secure.putString(
78                     cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, (String) value);
79         }
80 
81         return true;
82     }
83 
84     @Override
getHelpResource()85     public int getHelpResource() {
86         return R.string.help_url_caption;
87     }
88 
89     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
90             new BaseSearchIndexProvider(R.xml.captioning_more_options);
91 }
92