1 /* 2 * Copyright (C) 2013 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.os.Bundle; 20 import android.provider.Settings; 21 import android.support.v7.preference.ListPreference; 22 import android.support.v7.preference.Preference; 23 import android.view.accessibility.AccessibilityManager; 24 import android.widget.Switch; 25 26 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 27 import com.android.settings.R; 28 import com.android.settings.widget.SwitchBar; 29 30 public class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePreferenceFragment 31 implements Preference.OnPreferenceChangeListener, SwitchBar.OnSwitchChangeListener { 32 private static final String ENABLED = Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED; 33 private static final String TYPE = Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER; 34 private static final int DEFAULT_TYPE = AccessibilityManager.DALTONIZER_CORRECT_DEUTERANOMALY; 35 36 private ListPreference mType; 37 38 @Override getMetricsCategory()39 public int getMetricsCategory() { 40 return MetricsEvent.ACCESSIBILITY_TOGGLE_DALTONIZER; 41 } 42 43 @Override getHelpResource()44 public int getHelpResource() { 45 return R.string.help_url_color_correction; 46 } 47 48 @Override onCreate(Bundle savedInstanceState)49 public void onCreate(Bundle savedInstanceState) { 50 super.onCreate(savedInstanceState); 51 52 mType = (ListPreference) findPreference("type"); 53 54 if (!AccessibilitySettings.isColorTransformAccelerated(getActivity())) { 55 mFooterPreferenceMixin.createFooterPreference().setTitle( 56 R.string.accessibility_display_daltonizer_preference_subtitle); 57 } 58 initPreferences(); 59 } 60 61 @Override getPreferenceScreenResId()62 protected int getPreferenceScreenResId() { 63 return R.xml.accessibility_daltonizer_settings; 64 } 65 66 @Override onPreferenceToggled(String preferenceKey, boolean enabled)67 protected void onPreferenceToggled(String preferenceKey, boolean enabled) { 68 Settings.Secure.putInt(getContentResolver(), ENABLED, enabled ? 1 : 0); 69 } 70 71 @Override onPreferenceChange(Preference preference, Object newValue)72 public boolean onPreferenceChange(Preference preference, Object newValue) { 73 if (preference == mType) { 74 Settings.Secure.putInt(getContentResolver(), TYPE, Integer.parseInt((String) newValue)); 75 preference.setSummary("%s"); 76 } 77 78 return true; 79 } 80 81 @Override onInstallSwitchBarToggleSwitch()82 protected void onInstallSwitchBarToggleSwitch() { 83 super.onInstallSwitchBarToggleSwitch(); 84 85 mSwitchBar.setCheckedInternal( 86 Settings.Secure.getInt(getContentResolver(), ENABLED, 0) == 1); 87 mSwitchBar.addOnSwitchChangeListener(this); 88 } 89 90 @Override onRemoveSwitchBarToggleSwitch()91 protected void onRemoveSwitchBarToggleSwitch() { 92 super.onRemoveSwitchBarToggleSwitch(); 93 mSwitchBar.removeOnSwitchChangeListener(this); 94 } 95 96 @Override updateSwitchBarText(SwitchBar switchBar)97 protected void updateSwitchBarText(SwitchBar switchBar) { 98 switchBar.setSwitchBarText(R.string.accessibility_daltonizer_master_switch_title, 99 R.string.accessibility_daltonizer_master_switch_title); 100 } 101 initPreferences()102 private void initPreferences() { 103 final String value = Integer.toString( 104 Settings.Secure.getInt(getContentResolver(), TYPE, DEFAULT_TYPE)); 105 mType.setValue(value); 106 mType.setOnPreferenceChangeListener(this); 107 final int index = mType.findIndexOfValue(value); 108 if (index < 0) { 109 // We're using a mode controlled by developer preferences. 110 mType.setSummary(getString(R.string.daltonizer_type_overridden, 111 getString(R.string.simulate_color_space))); 112 } 113 } 114 115 @Override onSwitchChanged(Switch switchView, boolean isChecked)116 public void onSwitchChanged(Switch switchView, boolean isChecked) { 117 onPreferenceToggled(mPreferenceKey, isChecked); 118 } 119 } 120