1 /*
2  * Copyright (C) 2021 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.car.settings.accessibility;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.provider.Settings;
22 
23 import com.android.car.settings.common.ColoredSwitchPreference;
24 import com.android.car.settings.common.FragmentController;
25 import com.android.car.settings.common.PreferenceController;
26 
27 /**
28  * Enables/disables default captions settings for video apps via the relevant settings constant.
29  */
30 public class ShowCaptionsSwitchPreferenceController extends
31         PreferenceController<ColoredSwitchPreference> {
32 
ShowCaptionsSwitchPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)33     public ShowCaptionsSwitchPreferenceController(Context context,
34             String preferenceKey,
35             FragmentController fragmentController,
36             CarUxRestrictions uxRestrictions) {
37         super(context, preferenceKey, fragmentController, uxRestrictions);
38     }
39 
40     @Override
updateState(ColoredSwitchPreference preference)41     protected void updateState(ColoredSwitchPreference preference) {
42         getPreference().setChecked(Settings.Secure.getInt(getContext().getContentResolver(),
43                 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, 0) != 0);
44     }
45 
46     @Override
handlePreferenceChanged(ColoredSwitchPreference preference, Object newValue)47     protected boolean handlePreferenceChanged(ColoredSwitchPreference preference,
48             Object newValue) {
49         boolean captionsEnabled = (Boolean) newValue;
50         Settings.Secure.putInt(getContext().getContentResolver(),
51                 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, captionsEnabled ? 1 : 0);
52         return true;
53     }
54 
55     @Override
getPreferenceType()56     protected Class<ColoredSwitchPreference> getPreferenceType() {
57         return ColoredSwitchPreference.class;
58     }
59 }
60