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