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