1 /*
2  * Copyright (C) 2013 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 import androidx.preference.Preference.OnPreferenceChangeListener;
28 import androidx.preference.SwitchPreference;
29 
30 import com.android.settings.R;
31 import com.android.settings.SettingsPreferenceFragment;
32 import com.android.settings.search.BaseSearchIndexProvider;
33 import com.android.settingslib.search.SearchIndexable;
34 
35 import com.google.common.primitives.Floats;
36 
37 import java.util.ArrayList;
38 import java.util.List;
39 
40 /** Settings fragment containing captioning properties. */
41 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
42 public class CaptionPropertiesFragment extends SettingsPreferenceFragment
43         implements OnPreferenceChangeListener {
44     private static final String PREF_SWITCH = "captioning_preference_switch";
45     private static final String PREF_TEXT = "captioning_caption_appearance";
46     private static final String PREF_MORE = "captioning_more_options";
47 
48     private CaptioningManager mCaptioningManager;
49 
50     private SwitchPreference mSwitch;
51     private Preference mTextAppearance;
52     private Preference mMoreOptions;
53 
54     private final List<Preference> mPreferenceList = new ArrayList<>();
55     private float[] mFontSizeValuesArray;
56 
57     @Override
getMetricsCategory()58     public int getMetricsCategory() {
59         return SettingsEnums.ACCESSIBILITY_CAPTION_PROPERTIES;
60     }
61 
62     @Override
onCreate(Bundle icicle)63     public void onCreate(Bundle icicle) {
64         super.onCreate(icicle);
65 
66         mCaptioningManager = (CaptioningManager) getSystemService(Context.CAPTIONING_SERVICE);
67 
68         addPreferencesFromResource(R.xml.captioning_settings);
69         initializeAllPreferences();
70         installUpdateListeners();
71         initFontSizeValuesArray();
72     }
73 
74     @Override
onResume()75     public void onResume() {
76         super.onResume();
77         updateAllPreferences();
78     }
79 
initializeAllPreferences()80     private void initializeAllPreferences() {
81         mSwitch = (SwitchPreference) findPreference(PREF_SWITCH);
82         mTextAppearance = (Preference) findPreference(PREF_TEXT);
83         mMoreOptions = (Preference) findPreference(PREF_MORE);
84 
85         mPreferenceList.add(mTextAppearance);
86         mPreferenceList.add(mMoreOptions);
87     }
88 
installUpdateListeners()89     private void installUpdateListeners() {
90         mSwitch.setOnPreferenceChangeListener(this);
91     }
92 
initFontSizeValuesArray()93     private void initFontSizeValuesArray() {
94         final String[] fontSizeValuesStrArray = getPrefContext().getResources().getStringArray(
95                 R.array.captioning_font_size_selector_values);
96         final int length = fontSizeValuesStrArray.length;
97         mFontSizeValuesArray = new float[length];
98         for (int i = 0; i < length; ++i) {
99             mFontSizeValuesArray[i] = Float.parseFloat(fontSizeValuesStrArray[i]);
100         }
101     }
102 
updateAllPreferences()103     private void updateAllPreferences() {
104         mSwitch.setChecked(mCaptioningManager.isEnabled());
105         mTextAppearance.setSummary(geTextAppearanceSummary(getPrefContext()));
106     }
107 
108     @Override
onPreferenceChange(Preference preference, Object value)109     public boolean onPreferenceChange(Preference preference, Object value) {
110         final ContentResolver cr = getActivity().getContentResolver();
111         if (mSwitch == preference) {
112             Settings.Secure.putInt(
113                     cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, (boolean) value ? 1 : 0);
114         }
115 
116         return true;
117     }
118 
119     @Override
getHelpResource()120     public int getHelpResource() {
121         return R.string.help_url_caption;
122     }
123 
geTextAppearanceSummary(Context context)124     private CharSequence geTextAppearanceSummary(Context context) {
125         final String[] fontSizeSummaries = context.getResources().getStringArray(
126                 R.array.captioning_font_size_selector_summaries);
127 
128         final float fontSize = mCaptioningManager.getFontScale();
129         final int idx = Floats.indexOf(mFontSizeValuesArray, fontSize);
130 
131         return fontSizeSummaries[idx == /* not exist */ -1 ? 0 : idx];
132     }
133 
134     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
135             new BaseSearchIndexProvider(R.xml.captioning_settings);
136 }
137