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 
22 import androidx.preference.PreferenceScreen;
23 
24 import com.android.settings.R;
25 import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
26 import com.android.settings.core.BasePreferenceController;
27 
28 /** Preference controller for captioning background opacity. */
29 public class CaptioningBackgroundOpacityController extends BasePreferenceController
30         implements OnValueChangedListener {
31 
32     private final CaptionHelper mCaptionHelper;
33 
CaptioningBackgroundOpacityController(Context context, String preferenceKey)34     public CaptioningBackgroundOpacityController(Context context, String preferenceKey) {
35         super(context, preferenceKey);
36         mCaptionHelper = new CaptionHelper(context);
37     }
38 
39     @Override
getAvailabilityStatus()40     public int getAvailabilityStatus() {
41         return AVAILABLE;
42     }
43 
44     @Override
displayPreference(PreferenceScreen screen)45     public void displayPreference(PreferenceScreen screen) {
46         super.displayPreference(screen);
47         final ColorPreference preference = screen.findPreference(getPreferenceKey());
48         final Resources res = mContext.getResources();
49         final int[] opacityValues = res.getIntArray(R.array.captioning_opacity_selector_values);
50         final String[] opacityTitles = res.getStringArray(
51                 R.array.captioning_opacity_selector_titles);
52         preference.setTitles(opacityTitles);
53         preference.setValues(opacityValues);
54         final int backBackgroundColor = mCaptionHelper.getBackgroundColor();
55         final int opacity = CaptionUtils.parseOpacity(backBackgroundColor);
56         preference.setValue(opacity);
57         preference.setOnValueChangedListener(this);
58     }
59 
60     @Override
onValueChanged(ListDialogPreference preference, int value)61     public void onValueChanged(ListDialogPreference preference, int value) {
62         final int backBackgroundColor = mCaptionHelper.getBackgroundColor();
63         final int color = CaptionUtils.parseColor(backBackgroundColor);
64         final int merged = CaptionUtils.mergeColorOpacity(color, value);
65         mCaptionHelper.setBackgroundColor(merged);
66         mCaptionHelper.setEnabled(true);
67     }
68 }
69