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