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 static com.android.settings.accessibility.AccessibilityUtil.State.OFF; 20 import static com.android.settings.accessibility.AccessibilityUtil.State.ON; 21 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 22 import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE; 23 24 import android.content.ContentResolver; 25 import android.content.Context; 26 import android.provider.Settings; 27 import android.view.View; 28 import android.view.accessibility.CaptioningManager; 29 import android.view.accessibility.CaptioningManager.CaptionStyle; 30 31 import com.android.internal.widget.SubtitleView; 32 import com.android.settings.R; 33 import com.android.settingslib.accessibility.AccessibilityUtils; 34 35 import com.google.common.annotations.VisibleForTesting; 36 37 import java.util.Locale; 38 39 /** Helper class for caption. */ 40 public class CaptionHelper { 41 42 /* WebVtt specifies line height as 5.3% of the viewport height. */ 43 @VisibleForTesting 44 static final float LINE_HEIGHT_RATIO = 0.0533f; 45 46 private final Context mContext; 47 private final ContentResolver mContentResolver; 48 private final CaptioningManager mCaptioningManager; 49 CaptionHelper(Context context)50 public CaptionHelper(Context context) { 51 mContext = context; 52 mContentResolver = mContext.getContentResolver(); 53 mCaptioningManager = context.getSystemService(CaptioningManager.class); 54 } 55 56 /** 57 * Sets the user's preferred captioning enabled state. 58 * 59 * @param enabled Whether to enable or disable captioning manager. 60 */ setEnabled(boolean enabled)61 public void setEnabled(boolean enabled) { 62 if (isEnabled() == enabled) { 63 return; 64 } 65 66 Settings.Secure.putInt(mContext.getContentResolver(), 67 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, enabled ? ON : OFF); 68 } 69 70 /** 71 * Gets if the captioning manager is enabled. 72 * 73 * @return True if the captioning manager is enabled, false otherwise. 74 */ isEnabled()75 public boolean isEnabled() { 76 return mCaptioningManager.isEnabled(); 77 } 78 79 /** 80 * Updates font style of captioning properties for preview screen. 81 * 82 * @param previewText preview text 83 * @param previewWindow preview window 84 * @param styleId font style id 85 */ applyCaptionProperties(SubtitleView previewText, View previewWindow, int styleId)86 public void applyCaptionProperties(SubtitleView previewText, View previewWindow, 87 int styleId) { 88 previewText.setStyle(styleId); 89 90 final float fontScale = mCaptioningManager.getFontScale(); 91 if (previewWindow != null) { 92 // Assume the viewport is clipped with a 16:9 aspect ratio. 93 final float virtualHeight = Math.max(9 * previewWindow.getWidth(), 94 16 * previewWindow.getHeight()) / 16.0f; 95 previewText.setTextSize(virtualHeight * LINE_HEIGHT_RATIO * fontScale); 96 } else { 97 final float textSize = mContext.getResources().getDimension( 98 R.dimen.captioning_preview_text_size); 99 previewText.setTextSize(textSize * fontScale); 100 } 101 102 final Locale locale = mCaptioningManager.getLocale(); 103 if (locale != null) { 104 final CharSequence localizedText = AccessibilityUtils.getTextForLocale( 105 mContext, locale, R.string.captioning_preview_characters); 106 previewText.setText(localizedText); 107 } else { 108 previewText.setText(R.string.captioning_preview_characters); 109 } 110 } 111 112 /** 113 * Sets the user's preferred captioning background color. 114 * 115 * @param color The captioning background color 116 */ setBackgroundColor(int color)117 public void setBackgroundColor(int color) { 118 Settings.Secure.putInt(mContentResolver, 119 Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, color); 120 } 121 122 /** Returns the captioning background color.*/ getBackgroundColor()123 public int getBackgroundColor() { 124 final CaptionStyle attrs = CaptionStyle.getCustomStyle(mContentResolver); 125 return attrs.hasBackgroundColor() ? attrs.backgroundColor : CaptionStyle.COLOR_UNSPECIFIED; 126 } 127 128 /** 129 * Sets the user's preferred captioning foreground color. 130 * 131 * @param color The captioning foreground color 132 */ setForegroundColor(int color)133 public void setForegroundColor(int color) { 134 Settings.Secure.putInt(mContentResolver, 135 Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, color); 136 } 137 138 /** Returns the captioning foreground color.*/ getForegroundColor()139 public int getForegroundColor() { 140 final CaptionStyle attrs = CaptionStyle.getCustomStyle(mContentResolver); 141 return attrs.hasForegroundColor() ? attrs.foregroundColor : CaptionStyle.COLOR_UNSPECIFIED; 142 } 143 144 /** 145 * Sets the user's preferred captioning window color. 146 * 147 * @param color The captioning window color 148 */ setWindowColor(int color)149 public void setWindowColor(int color) { 150 Settings.Secure.putInt(mContentResolver, 151 Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, color); 152 } 153 154 /** Returns the captioning window color.*/ getWindowColor()155 public int getWindowColor() { 156 final CaptionStyle attrs = CaptionStyle.getCustomStyle(mContentResolver); 157 return attrs.hasWindowColor() ? attrs.windowColor : CaptionStyle.COLOR_UNSPECIFIED; 158 } 159 160 /** 161 * Sets the user's preferred captioning edge color. 162 * 163 * @param color The captioning edge color 164 */ setEdgeColor(int color)165 public void setEdgeColor(int color) { 166 Settings.Secure.putInt(mContentResolver, 167 Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_COLOR, color); 168 } 169 170 /** Returns the captioning edge color.*/ getEdgeColor()171 public int getEdgeColor() { 172 final CaptionStyle attrs = CaptionStyle.getCustomStyle(mContentResolver); 173 return attrs.edgeColor; 174 } 175 176 /** 177 * Sets the user's preferred captioning edge type. 178 * 179 * @param type The captioning edge type 180 */ setEdgeType(int type)181 public void setEdgeType(int type) { 182 Settings.Secure.putInt(mContentResolver, 183 Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE, type); 184 } 185 186 /** Returns the captioning edge type.*/ getEdgeType()187 public int getEdgeType() { 188 final CaptionStyle attrs = CaptionStyle.getCustomStyle(mContentResolver); 189 return attrs.edgeType; 190 } 191 192 /** 193 * Sets the captioning raw user style. 194 * 195 * @param type The captioning raw user style 196 */ setRawUserStyle(int type)197 public void setRawUserStyle(int type) { 198 Settings.Secure.putInt(mContentResolver, 199 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, type); 200 } 201 202 /** Returns the captioning raw preset number.*/ getRawUserStyle()203 public int getRawUserStyle() { 204 return mCaptioningManager.getRawUserStyle(); 205 } 206 207 /** Returns the captioning visual properties.*/ getUserStyle()208 public CaptionStyle getUserStyle() { 209 return mCaptioningManager.getUserStyle(); 210 } 211 212 /** Returns the captioning locale language.*/ getLocale()213 public Locale getLocale() { 214 return mCaptioningManager.getLocale(); 215 } 216 217 /** Returns availability for custom caption preferences, depending on current user style. */ getCustomCaptionAvailability()218 public int getCustomCaptionAvailability() { 219 if (com.android.settings.accessibility.Flags.fixA11ySettingsSearch()) { 220 return (getRawUserStyle() == CaptionStyle.PRESET_CUSTOM) 221 ? AVAILABLE : AVAILABLE_UNSEARCHABLE; 222 } else { 223 return AVAILABLE; 224 } 225 } 226 } 227