/TalkBack/src/com/google/android/marvin/talkback/TalkBackPreferencesActivity.java

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