1 /*
2  * Copyright (C) 2016 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 package com.google.android.exoplayer2.text;
17 
18 import android.graphics.Color;
19 import android.graphics.Typeface;
20 import android.view.accessibility.CaptioningManager;
21 import android.view.accessibility.CaptioningManager.CaptionStyle;
22 import androidx.annotation.IntDef;
23 import androidx.annotation.Nullable;
24 import androidx.annotation.RequiresApi;
25 import com.google.android.exoplayer2.util.Util;
26 import java.lang.annotation.Documented;
27 import java.lang.annotation.Retention;
28 import java.lang.annotation.RetentionPolicy;
29 
30 /**
31  * A compatibility wrapper for {@link CaptionStyle}.
32  */
33 public final class CaptionStyleCompat {
34 
35   /**
36    * The type of edge, which may be none. One of {@link #EDGE_TYPE_NONE}, {@link
37    * #EDGE_TYPE_OUTLINE}, {@link #EDGE_TYPE_DROP_SHADOW}, {@link #EDGE_TYPE_RAISED} or {@link
38    * #EDGE_TYPE_DEPRESSED}.
39    */
40   @Documented
41   @Retention(RetentionPolicy.SOURCE)
42   @IntDef({
43     EDGE_TYPE_NONE,
44     EDGE_TYPE_OUTLINE,
45     EDGE_TYPE_DROP_SHADOW,
46     EDGE_TYPE_RAISED,
47     EDGE_TYPE_DEPRESSED
48   })
49   public @interface EdgeType {}
50   /**
51    * Edge type value specifying no character edges.
52    */
53   public static final int EDGE_TYPE_NONE = 0;
54   /**
55    * Edge type value specifying uniformly outlined character edges.
56    */
57   public static final int EDGE_TYPE_OUTLINE = 1;
58   /**
59    * Edge type value specifying drop-shadowed character edges.
60    */
61   public static final int EDGE_TYPE_DROP_SHADOW = 2;
62   /**
63    * Edge type value specifying raised bevel character edges.
64    */
65   public static final int EDGE_TYPE_RAISED = 3;
66   /**
67    * Edge type value specifying depressed bevel character edges.
68    */
69   public static final int EDGE_TYPE_DEPRESSED = 4;
70 
71   /**
72    * Use color setting specified by the track and fallback to default caption style.
73    */
74   public static final int USE_TRACK_COLOR_SETTINGS = 1;
75 
76   /** Default caption style. */
77   public static final CaptionStyleCompat DEFAULT =
78       new CaptionStyleCompat(
79           Color.WHITE,
80           Color.BLACK,
81           Color.TRANSPARENT,
82           EDGE_TYPE_NONE,
83           Color.WHITE,
84           /* typeface= */ null);
85 
86   /**
87    * The preferred foreground color.
88    */
89   public final int foregroundColor;
90 
91   /**
92    * The preferred background color.
93    */
94   public final int backgroundColor;
95 
96   /**
97    * The preferred window color.
98    */
99   public final int windowColor;
100 
101   /**
102    * The preferred edge type. One of:
103    * <ul>
104    * <li>{@link #EDGE_TYPE_NONE}
105    * <li>{@link #EDGE_TYPE_OUTLINE}
106    * <li>{@link #EDGE_TYPE_DROP_SHADOW}
107    * <li>{@link #EDGE_TYPE_RAISED}
108    * <li>{@link #EDGE_TYPE_DEPRESSED}
109    * </ul>
110    */
111   @EdgeType public final int edgeType;
112 
113   /**
114    * The preferred edge color, if using an edge type other than {@link #EDGE_TYPE_NONE}.
115    */
116   public final int edgeColor;
117 
118   /** The preferred typeface, or {@code null} if unspecified. */
119   @Nullable public final Typeface typeface;
120 
121   /**
122    * Creates a {@link CaptionStyleCompat} equivalent to a provided {@link CaptionStyle}.
123    *
124    * @param captionStyle A {@link CaptionStyle}.
125    * @return The equivalent {@link CaptionStyleCompat}.
126    */
127   @RequiresApi(19)
createFromCaptionStyle( CaptioningManager.CaptionStyle captionStyle)128   public static CaptionStyleCompat createFromCaptionStyle(
129       CaptioningManager.CaptionStyle captionStyle) {
130     if (Util.SDK_INT >= 21) {
131       return createFromCaptionStyleV21(captionStyle);
132     } else {
133       // Note - Any caller must be on at least API level 19 or greater (because CaptionStyle did
134       // not exist in earlier API levels).
135       return createFromCaptionStyleV19(captionStyle);
136     }
137   }
138 
139   /**
140    * @param foregroundColor See {@link #foregroundColor}.
141    * @param backgroundColor See {@link #backgroundColor}.
142    * @param windowColor See {@link #windowColor}.
143    * @param edgeType See {@link #edgeType}.
144    * @param edgeColor See {@link #edgeColor}.
145    * @param typeface See {@link #typeface}.
146    */
CaptionStyleCompat( int foregroundColor, int backgroundColor, int windowColor, @EdgeType int edgeType, int edgeColor, @Nullable Typeface typeface)147   public CaptionStyleCompat(
148       int foregroundColor,
149       int backgroundColor,
150       int windowColor,
151       @EdgeType int edgeType,
152       int edgeColor,
153       @Nullable Typeface typeface) {
154     this.foregroundColor = foregroundColor;
155     this.backgroundColor = backgroundColor;
156     this.windowColor = windowColor;
157     this.edgeType = edgeType;
158     this.edgeColor = edgeColor;
159     this.typeface = typeface;
160   }
161 
162   @RequiresApi(19)
163   @SuppressWarnings("ResourceType")
createFromCaptionStyleV19( CaptioningManager.CaptionStyle captionStyle)164   private static CaptionStyleCompat createFromCaptionStyleV19(
165       CaptioningManager.CaptionStyle captionStyle) {
166     return new CaptionStyleCompat(
167         captionStyle.foregroundColor, captionStyle.backgroundColor, Color.TRANSPARENT,
168         captionStyle.edgeType, captionStyle.edgeColor, captionStyle.getTypeface());
169   }
170 
171   @RequiresApi(21)
172   @SuppressWarnings("ResourceType")
createFromCaptionStyleV21( CaptioningManager.CaptionStyle captionStyle)173   private static CaptionStyleCompat createFromCaptionStyleV21(
174       CaptioningManager.CaptionStyle captionStyle) {
175     return new CaptionStyleCompat(
176         captionStyle.hasForegroundColor() ? captionStyle.foregroundColor : DEFAULT.foregroundColor,
177         captionStyle.hasBackgroundColor() ? captionStyle.backgroundColor : DEFAULT.backgroundColor,
178         captionStyle.hasWindowColor() ? captionStyle.windowColor : DEFAULT.windowColor,
179         captionStyle.hasEdgeType() ? captionStyle.edgeType : DEFAULT.edgeType,
180         captionStyle.hasEdgeColor() ? captionStyle.edgeColor : DEFAULT.edgeColor,
181         captionStyle.getTypeface());
182   }
183 
184 }
185