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.dashboard.DashboardFragment;
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 DashboardFragment
36         implements Preference.OnPreferenceChangeListener {
37 
38     private static final String TAG = "CaptionMoreOptionsFragment";
39     private static final String PREF_LOCALE = "captioning_locale";
40 
41     private CaptioningManager mCaptioningManager;
42     private LocalePreference mLocale;
43 
44     @Override
getMetricsCategory()45     public int getMetricsCategory() {
46         return SettingsEnums.ACCESSIBILITY_CAPTION_MORE_OPTIONS;
47     }
48 
49     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)50     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
51         super.onCreatePreferences(savedInstanceState, rootKey);
52 
53         mCaptioningManager = (CaptioningManager) getSystemService(Context.CAPTIONING_SERVICE);
54 
55         initializeAllPreferences();
56         updateAllPreferences();
57         installUpdateListeners();
58     }
59 
60     @Override
getPreferenceScreenResId()61     protected int getPreferenceScreenResId() {
62         return R.xml.captioning_more_options;
63     }
64 
65     @Override
getLogTag()66     protected String getLogTag() {
67         return TAG;
68     }
69 
initializeAllPreferences()70     private void initializeAllPreferences() {
71         mLocale = (LocalePreference) findPreference(PREF_LOCALE);
72     }
73 
installUpdateListeners()74     private void installUpdateListeners() {
75         mLocale.setOnPreferenceChangeListener(this);
76     }
77 
updateAllPreferences()78     private void updateAllPreferences() {
79         final String rawLocale = mCaptioningManager.getRawLocale();
80         mLocale.setValue(rawLocale == null ? "" : rawLocale);
81     }
82 
83     @Override
onPreferenceChange(Preference preference, Object value)84     public boolean onPreferenceChange(Preference preference, Object value) {
85         final ContentResolver cr = getActivity().getContentResolver();
86         if (mLocale == preference) {
87             Settings.Secure.putString(
88                     cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, (String) value);
89         }
90 
91         return true;
92     }
93 
94     @Override
getHelpResource()95     public int getHelpResource() {
96         return R.string.help_url_caption;
97     }
98 
99     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
100             new BaseSearchIndexProvider(R.xml.captioning_more_options);
101 }
102