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