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.tv.settings.accessibility; 18 19 import static com.android.tv.settings.util.InstrumentationUtils.logEntrySelected; 20 import static com.android.tv.settings.util.InstrumentationUtils.logToggleInteracted; 21 22 import android.os.Bundle; 23 import android.provider.Settings; 24 import android.text.TextUtils; 25 import android.app.tvsettings.TvSettingsEnums; 26 27 import androidx.annotation.Keep; 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceGroup; 30 import androidx.preference.TwoStatePreference; 31 32 import com.android.tv.settings.R; 33 import com.android.tv.settings.RadioPreference; 34 import com.android.tv.settings.SettingsPreferenceFragment; 35 36 /** 37 * Fragment for configuring the accessibility color correction 38 */ 39 @Keep 40 public class AccessibilityColorCorrectionPreferenceFragment extends SettingsPreferenceFragment { 41 private static final String TOGGLE_COLOR_CORRECTION_KEY = "toggle_color_correction"; 42 private static final String COLOR_MODE_GROUP_KEY = "color_correction_mode"; 43 private static final String COLOR_MODE_DEUTERANOMALY_KEY 44 = "color_correction_mode_deuteranomaly"; 45 private static final String COLOR_MODE_PROTANOMALY_KEY = "color_correction_mode_protanomaly"; 46 private static final String COLOR_MODE_TRITANOMALY_KEY = "color_correction_mode_tritanomaly"; 47 private static final String COLOR_MODE_GRAYSCALE_KEY = "color_correction_mode_grayscale"; 48 49 private static final int DALTONIZER_ENABLED = 1; 50 private static final int DALTONIZER_DISABLED = 0; 51 52 // These constant values are defined in the arrays.xml of the Settings app. 53 private static final int COLOR_CORRECTION_GRAYSCALE = 0; 54 private static final int COLOR_CORRECTION_PROTANOMALY = 11; 55 private static final int COLOR_CORRECTION_DEUTERANOMALY = 12; 56 private static final int COLOR_CORRECTION_TRITANOMALY = 13; 57 58 private PreferenceGroup mColorModeGroup; 59 private RadioPreference mColorModeDeuteranomaly; 60 private RadioPreference mColorModeProtanomaly; 61 private RadioPreference mColorModeTritanomaly; 62 private RadioPreference mColorModeGrayscale; 63 64 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)65 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 66 setPreferencesFromResource(R.xml.accessibility_color_correction, rootKey); 67 68 final TwoStatePreference enableColorCorrectionPreference = 69 (TwoStatePreference) findPreference(TOGGLE_COLOR_CORRECTION_KEY); 70 71 enableColorCorrectionPreference.setChecked( 72 Settings.Secure.getInt(getContext().getContentResolver(), 73 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0) == DALTONIZER_ENABLED); 74 75 mColorModeGroup = (PreferenceGroup) findPreference(COLOR_MODE_GROUP_KEY); 76 mColorModeDeuteranomaly = (RadioPreference) findPreference(COLOR_MODE_DEUTERANOMALY_KEY); 77 mColorModeProtanomaly = (RadioPreference) findPreference(COLOR_MODE_PROTANOMALY_KEY); 78 mColorModeTritanomaly = (RadioPreference) findPreference(COLOR_MODE_TRITANOMALY_KEY); 79 mColorModeGrayscale = (RadioPreference) findPreference(COLOR_MODE_GRAYSCALE_KEY); 80 } 81 82 @Override onPreferenceTreeClick(Preference preference)83 public boolean onPreferenceTreeClick(Preference preference) { 84 final String key = preference.getKey(); 85 if (TextUtils.isEmpty(key)) { 86 return super.onPreferenceTreeClick(preference); 87 } 88 switch (key) { 89 case TOGGLE_COLOR_CORRECTION_KEY: 90 logToggleInteracted( 91 TvSettingsEnums.SYSTEM_A11Y_COLOR_CORRECTION_ON_OFF, 92 ((TwoStatePreference) preference).isChecked()); 93 setColorCorrectionStatus(((TwoStatePreference) preference).isChecked()); 94 return true; 95 case COLOR_MODE_DEUTERANOMALY_KEY: 96 logEntrySelected(TvSettingsEnums.SYSTEM_A11Y_COLOR_CORRECTION_DEUTERANOMALY); 97 setColorCorrectionMode(COLOR_CORRECTION_DEUTERANOMALY); 98 mColorModeDeuteranomaly.setChecked(true); 99 mColorModeDeuteranomaly.clearOtherRadioPreferences(mColorModeGroup); 100 return true; 101 case COLOR_MODE_PROTANOMALY_KEY: 102 logEntrySelected(TvSettingsEnums.SYSTEM_A11Y_COLOR_CORRECTION_PROTANOMALY); 103 setColorCorrectionMode(COLOR_CORRECTION_PROTANOMALY); 104 mColorModeProtanomaly.setChecked(true); 105 mColorModeProtanomaly.clearOtherRadioPreferences(mColorModeGroup); 106 return true; 107 case COLOR_MODE_TRITANOMALY_KEY: 108 logEntrySelected(TvSettingsEnums.SYSTEM_A11Y_COLOR_CORRECTION_TRITANOMALY); 109 setColorCorrectionMode(COLOR_CORRECTION_TRITANOMALY); 110 mColorModeTritanomaly.setChecked(true); 111 mColorModeTritanomaly.clearOtherRadioPreferences(mColorModeGroup); 112 return true; 113 case COLOR_MODE_GRAYSCALE_KEY: 114 logEntrySelected(TvSettingsEnums.SYSTEM_A11Y_COLOR_CORRECTION_GRAYSCALE); 115 setColorCorrectionMode(COLOR_CORRECTION_GRAYSCALE); 116 mColorModeGrayscale.setChecked(true); 117 mColorModeGrayscale.clearOtherRadioPreferences(mColorModeGroup); 118 return true; 119 } 120 return super.onPreferenceTreeClick(preference); 121 } 122 setColorCorrectionStatus(boolean enabled)123 private void setColorCorrectionStatus(boolean enabled) { 124 Settings.Secure.putInt(getContext().getContentResolver(), 125 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, enabled 126 ? DALTONIZER_ENABLED : DALTONIZER_DISABLED); 127 } 128 setColorCorrectionMode(int mode)129 private void setColorCorrectionMode(int mode) { 130 Settings.Secure.putInt(getContext().getContentResolver(), 131 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, mode); 132 } 133 }