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.widget.CompoundButton; 21 import android.widget.CompoundButton.OnCheckedChangeListener; 22 23 import androidx.preference.PreferenceScreen; 24 25 import com.android.settings.R; 26 import com.android.settings.core.TogglePreferenceController; 27 import com.android.settings.widget.SettingsMainSwitchPreference; 28 29 /** Preference controller for captioning more options. */ 30 public class CaptioningTogglePreferenceController extends TogglePreferenceController 31 implements OnCheckedChangeListener { 32 33 private final CaptionHelper mCaptionHelper; 34 CaptioningTogglePreferenceController(Context context, String preferenceKey)35 public CaptioningTogglePreferenceController(Context context, String preferenceKey) { 36 super(context, preferenceKey); 37 mCaptionHelper = new CaptionHelper(context); 38 } 39 40 @Override getAvailabilityStatus()41 public int getAvailabilityStatus() { 42 return AVAILABLE; 43 } 44 45 @Override isChecked()46 public boolean isChecked() { 47 return mCaptionHelper.isEnabled(); 48 } 49 50 @Override setChecked(boolean isChecked)51 public boolean setChecked(boolean isChecked) { 52 mCaptionHelper.setEnabled(isChecked); 53 return true; 54 } 55 56 @Override displayPreference(PreferenceScreen screen)57 public void displayPreference(PreferenceScreen screen) { 58 super.displayPreference(screen); 59 60 SettingsMainSwitchPreference preference = screen.findPreference(getPreferenceKey()); 61 preference.addOnSwitchChangeListener(this); 62 preference.setChecked(isChecked()); 63 } 64 65 @Override onCheckedChanged(CompoundButton buttonView, boolean isChecked)66 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 67 if (isChecked != isChecked()) { 68 setChecked(isChecked); 69 } 70 } 71 72 @Override getSliceHighlightMenuRes()73 public int getSliceHighlightMenuRes() { 74 return R.string.menu_key_accessibility; 75 } 76 } 77