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