/TalkBack/src/com/google/android/marvin/talkback/TalkBackPreferencesActivity.java
Java | 165 lines | 90 code | 20 blank | 55 comment | 18 complexity | 2e06b0b765bf349af3b7eb660fb926f7 MD5 | raw file
1/* 2 * Copyright 2010 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.google.android.marvin.talkback; 18 19import android.hardware.Sensor; 20import android.hardware.SensorManager; 21import android.os.Build; 22import android.os.Bundle; 23import android.os.Vibrator; 24import android.preference.CheckBoxPreference; 25import android.preference.ListPreference; 26import android.preference.Preference; 27import android.preference.Preference.OnPreferenceChangeListener; 28import android.preference.PreferenceActivity; 29import android.preference.PreferenceGroup; 30 31/** 32 * Activity used to set TalkBack's service preferences. This activity is loaded 33 * when the TalkBackService is first connected to and allows the user to select 34 * a font zoom size. 35 * 36 * @author alanv@google.com (Alan Viverette) 37 */ 38public class TalkBackPreferencesActivity extends PreferenceActivity { 39 /** 40 * Loads the preferences from the XML preference definition and defines an 41 * onPreferenceChangeListener for the font zoom size that restarts the 42 * TalkBack service. 43 */ 44 @Override 45 public void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 48 addPreferencesFromResource(R.xml.preferences); 49 50 fixListSummaries(getPreferenceScreen()); 51 52 checkVibrationSupport(); 53 checkProximitySupport(); 54 checkDeveloperSupport(); 55 } 56 57 /** 58 * Since the "%s" summary is currently broken, this sets the preference 59 * change listener for all {@link ListPreference} views to fill in the 60 * summary with the current entry value. 61 */ 62 private void fixListSummaries(PreferenceGroup group) { 63 if (group == null) { 64 return; 65 } 66 67 final int count = group.getPreferenceCount(); 68 69 for (int i = 0; i < count; i++) { 70 final Preference preference = group.getPreference(i); 71 72 if (preference instanceof PreferenceGroup) { 73 fixListSummaries((PreferenceGroup) preference); 74 } else if (preference instanceof ListPreference) { 75 preference.setOnPreferenceChangeListener(mPreferenceChangeListener); 76 } 77 } 78 } 79 80 /** 81 * Ensure that the vibration setting does not appear on devices without a 82 * vibrator. 83 */ 84 private void checkVibrationSupport() { 85 final Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); 86 final PreferenceGroup category = 87 (PreferenceGroup) findPreferenceByResId(R.string.pref_category_feedback_key); 88 final CheckBoxPreference prefVibration = 89 (CheckBoxPreference) findPreferenceByResId(R.string.pref_vibration_key); 90 final Preference prefVibrationPatterns = 91 findPreferenceByResId(R.string.pref_vibration_patterns_key); 92 93 if (vibrator == null || !vibrator.hasVibrator()) { 94 prefVibration.setChecked(false); 95 category.removePreference(prefVibrationPatterns); 96 category.removePreference(prefVibration); 97 } 98 } 99 100 /** 101 * Ensure that the proximity sensor setting does not appear on devices 102 * without a proximity sensor. 103 */ 104 private void checkProximitySupport() { 105 final SensorManager manager = (SensorManager) getSystemService(SENSOR_SERVICE); 106 final Sensor proximity = manager.getDefaultSensor(Sensor.TYPE_PROXIMITY); 107 final PreferenceGroup category = 108 (PreferenceGroup) findPreferenceByResId(R.string.pref_category_when_to_speak_key); 109 final CheckBoxPreference prefProximity = 110 (CheckBoxPreference) findPreferenceByResId(R.string.pref_proximity_key); 111 112 if (proximity == null) { 113 prefProximity.setChecked(false); 114 category.removePreference(prefProximity); 115 } 116 } 117 118 /** 119 * Ensure that the developer settings category only appears on debug and 120 * engineering builds. 121 */ 122 private void checkDeveloperSupport() { 123 final boolean isDebugBuild = Build.TYPE.contains("debug") || Build.TYPE.contains("eng"); 124 final Preference developer = findPreferenceByResId(R.string.pref_category_developer_key); 125 126 if (!isDebugBuild) { 127 getPreferenceScreen().removePreference(developer); 128 } 129 } 130 131 /** 132 * Returns the preference associated with the specified resource identifier. 133 * 134 * @param resId A string resource identifier. 135 * @return The preference associated with the specified resource identifier. 136 */ 137 private Preference findPreferenceByResId(int resId) { 138 return findPreference(getString(resId)); 139 } 140 141 /** 142 * Listens for preference changes and updates the summary to reflect the 143 * current setting. This shouldn't be necessary, since preferences are 144 * supposed to automatically do this when the summary is set to "%s". 145 */ 146 private final OnPreferenceChangeListener mPreferenceChangeListener = 147 new OnPreferenceChangeListener() { 148 @Override 149 public boolean onPreferenceChange(Preference preference, Object newValue) { 150 if (preference instanceof ListPreference && newValue instanceof String) { 151 final ListPreference listPreference = (ListPreference) preference; 152 final int index = listPreference.findIndexOfValue((String) newValue); 153 final CharSequence[] entries = listPreference.getEntries(); 154 155 if (index >= 0 && index < entries.length) { 156 preference.setSummary(entries[index].toString().replaceAll("%", "%%")); 157 } else { 158 preference.setSummary(""); 159 } 160 } 161 162 return true; 163 } 164 }; 165}