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 android.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.Color;
22 import android.view.accessibility.CaptioningManager.CaptionStyle;
23 
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.R;
27 import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
28 import com.android.settings.core.BasePreferenceController;
29 
30 /** Preference controller for captioning background color. */
31 public class CaptioningBackgroundColorController extends BasePreferenceController
32         implements OnValueChangedListener {
33 
34     private final CaptionHelper mCaptionHelper;
35     private int mCachedNonDefaultOpacity = CaptionStyle.COLOR_UNSPECIFIED;
36 
CaptioningBackgroundColorController(Context context, String preferenceKey)37     public CaptioningBackgroundColorController(Context context, String preferenceKey) {
38         super(context, preferenceKey);
39         mCaptionHelper = new CaptionHelper(context);
40     }
41 
42     @Override
getAvailabilityStatus()43     public int getAvailabilityStatus() {
44         return AVAILABLE;
45     }
46 
47     @Override
displayPreference(PreferenceScreen screen)48     public void displayPreference(PreferenceScreen screen) {
49         super.displayPreference(screen);
50         final ColorPreference preference = screen.findPreference(getPreferenceKey());
51         final Resources res = mContext.getResources();
52         final int[] colorValues = res.getIntArray(R.array.captioning_color_selector_values);
53         final String[] colorTitles = res.getStringArray(
54                 R.array.captioning_color_selector_titles);
55         // Add "none" as an additional option for backgrounds.
56         final int[] backgroundColorValues = new int[colorValues.length + 1];
57         final String[] backgroundColorTitles = new String[colorTitles.length + 1];
58         System.arraycopy(colorValues, 0, backgroundColorValues, 1, colorValues.length);
59         System.arraycopy(colorTitles, 0, backgroundColorTitles, 1, colorTitles.length);
60         backgroundColorValues[0] = Color.TRANSPARENT;
61         backgroundColorTitles[0] = mContext.getString(R.string.color_none);
62         preference.setTitles(backgroundColorTitles);
63         preference.setValues(backgroundColorValues);
64         final int backBackgroundColor = mCaptionHelper.getBackgroundColor();
65         final int color = CaptionUtils.parseColor(backBackgroundColor);
66         preference.setValue(color);
67         preference.setOnValueChangedListener(this);
68     }
69 
70     @Override
onValueChanged(ListDialogPreference preference, int value)71     public void onValueChanged(ListDialogPreference preference, int value) {
72         final boolean isNonDefaultColor = CaptionStyle.hasColor(value);
73         final int opacity = getNonDefaultOpacity(isNonDefaultColor);
74         final int merged = CaptionUtils.mergeColorOpacity(value, opacity);
75         mCaptionHelper.setBackgroundColor(merged);
76         mCaptionHelper.setEnabled(true);
77     }
78 
getNonDefaultOpacity(boolean isNonDefaultColor)79     private int getNonDefaultOpacity(boolean isNonDefaultColor) {
80         final int backBackgroundColor = mCaptionHelper.getBackgroundColor();
81         final int opacity = CaptionUtils.parseOpacity(backBackgroundColor);
82         if (isNonDefaultColor) {
83             final int lastOpacity = mCachedNonDefaultOpacity != CaptionStyle.COLOR_UNSPECIFIED
84                     ? mCachedNonDefaultOpacity : opacity;
85             // Reset cached opacity to use current color opacity to merge color.
86             mCachedNonDefaultOpacity = CaptionStyle.COLOR_UNSPECIFIED;
87             return lastOpacity;
88         }
89         // When default captioning color was selected, the opacity become 100% and make opacity
90         // preference disable. Cache the latest opacity to show the correct opacity later.
91         mCachedNonDefaultOpacity = opacity;
92         return opacity;
93     }
94 }
95