1 /* 2 * Copyright (C) 2017 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.development; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.content.res.Resources; 22 import android.provider.Settings; 23 import android.support.annotation.VisibleForTesting; 24 import android.support.v7.preference.ListPreference; 25 import android.support.v7.preference.Preference; 26 import android.view.accessibility.AccessibilityManager; 27 28 import com.android.settings.R; 29 import com.android.settings.core.PreferenceControllerMixin; 30 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 31 32 public class SimulateColorSpacePreferenceController extends DeveloperOptionsPreferenceController 33 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { 34 35 private static final String SIMULATE_COLOR_SPACE = "simulate_color_space"; 36 37 @VisibleForTesting 38 static final int SETTING_VALUE_OFF = 0; 39 @VisibleForTesting 40 static final int SETTING_VALUE_ON = 1; 41 SimulateColorSpacePreferenceController(Context context)42 public SimulateColorSpacePreferenceController(Context context) { 43 super(context); 44 } 45 46 @Override getPreferenceKey()47 public String getPreferenceKey() { 48 return SIMULATE_COLOR_SPACE; 49 } 50 51 @Override onPreferenceChange(Preference preference, Object newValue)52 public boolean onPreferenceChange(Preference preference, Object newValue) { 53 writeSimulateColorSpace(newValue); 54 return true; 55 } 56 57 @Override updateState(Preference preference)58 public void updateState(Preference preference) { 59 updateSimulateColorSpace(); 60 } 61 62 @Override onDeveloperOptionsDisabled()63 public void onDeveloperOptionsDisabled() { 64 super.onDeveloperOptionsDisabled(); 65 if (usingDevelopmentColorSpace()) { 66 writeSimulateColorSpace(-1); 67 } 68 } 69 updateSimulateColorSpace()70 private void updateSimulateColorSpace() { 71 final ContentResolver cr = mContext.getContentResolver(); 72 final boolean enabled = Settings.Secure.getInt( 73 cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, SETTING_VALUE_OFF) 74 != SETTING_VALUE_OFF; 75 final ListPreference listPreference = (ListPreference) mPreference; 76 if (enabled) { 77 final String mode = Integer.toString(Settings.Secure.getInt( 78 cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, 79 AccessibilityManager.DALTONIZER_DISABLED)); 80 listPreference.setValue(mode); 81 final int index = listPreference.findIndexOfValue(mode); 82 if (index < 0) { 83 final Resources res = mContext.getResources(); 84 // We're using a mode controlled by accessibility preferences. 85 listPreference.setSummary(res.getString(R.string.daltonizer_type_overridden, 86 res.getString(R.string.accessibility_display_daltonizer_preference_title))); 87 } else { 88 listPreference.setSummary("%s"); 89 } 90 } else { 91 listPreference.setValue( 92 Integer.toString(AccessibilityManager.DALTONIZER_DISABLED)); 93 } 94 } 95 writeSimulateColorSpace(Object value)96 private void writeSimulateColorSpace(Object value) { 97 final ContentResolver cr = mContext.getContentResolver(); 98 final int newMode = Integer.parseInt(value.toString()); 99 if (newMode < 0) { 100 Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 101 SETTING_VALUE_OFF); 102 } else { 103 Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 104 SETTING_VALUE_ON); 105 Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, newMode); 106 } 107 } 108 109 /** 110 * @return <code>true</code> if the color space preference is currently 111 * controlled by development settings 112 */ usingDevelopmentColorSpace()113 private boolean usingDevelopmentColorSpace() { 114 final ContentResolver cr = mContext.getContentResolver(); 115 final boolean enabled = Settings.Secure.getInt( 116 cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, SETTING_VALUE_OFF) 117 != SETTING_VALUE_OFF; 118 if (enabled) { 119 final String mode = Integer.toString(Settings.Secure.getInt( 120 cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, 121 AccessibilityManager.DALTONIZER_DISABLED)); 122 final int index = ((ListPreference) mPreference).findIndexOfValue(mode); 123 if (index >= 0) { 124 // We're using a mode controlled by developer preferences. 125 return true; 126 } 127 } 128 return false; 129 } 130 } 131