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 public void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 48 addPreferencesFromResource(R.xml.accessibility_daltonizer_settings); 49 50 mType = (ListPreference) findPreference("type"); 51 52 initPreferences(); 53 } 54 55 @Override 56 protected void onPreferenceToggled(String preferenceKey, boolean enabled) { 57 Settings.Secure.putInt(getContentResolver(), ENABLED, enabled ? 1 : 0); 58 } 59 60 @Override 61 public boolean onPreferenceChange(Preference preference, Object newValue) { 62 if (preference == mType) { 63 Settings.Secure.putInt(getContentResolver(), TYPE, Integer.parseInt((String) newValue)); 64 preference.setSummary("%s"); 65 } 66 67 return true; 68 } 69 70 @Override 71 public void onViewCreated(View view, Bundle savedInstanceState) { 72 super.onViewCreated(view, savedInstanceState); 73 74 setTitle(getString(R.string.accessibility_display_daltonizer_preference_title)); 75 } 76 77 @Override 78 protected void onInstallSwitchBarToggleSwitch() { 79 super.onInstallSwitchBarToggleSwitch(); 80 81 mSwitchBar.setCheckedInternal( 82 Settings.Secure.getInt(getContentResolver(), ENABLED, 0) == 1); 83 mSwitchBar.addOnSwitchChangeListener(this); 84 } 85 86 @Override 87 protected void onRemoveSwitchBarToggleSwitch() { 88 super.onRemoveSwitchBarToggleSwitch(); 89 mSwitchBar.removeOnSwitchChangeListener(this); 90 } 91 92 private void initPreferences() { 93 final String value = Integer.toString( 94 Settings.Secure.getInt(getContentResolver(), TYPE, DEFAULT_TYPE)); 95 mType.setValue(value); 96 mType.setOnPreferenceChangeListener(this); 97 final int index = mType.findIndexOfValue(value); 98 if (index < 0) { 99 // We're using a mode controlled by developer preferences. 100 mType.setSummary(getString(R.string.daltonizer_type_overridden, 101 getString(R.string.simulate_color_space))); 102 } 103 } 104 105 @Override 106 public void onSwitchChanged(Switch switchView, boolean isChecked) { 107 onPreferenceToggled(mPreferenceKey, isChecked); 108 } 109 } 110