/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.googlecode.eyesfree.espeak; import android.os.Bundle; import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.PreferenceActivity; import android.preference.PreferenceGroup; public class TtsSettingsActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); fixListSummaries(getPreferenceScreen()); } /** * Since the "%s" summary is currently broken, this sets the preference * change listener for all {@link ListPreference} views to fill in the * summary with the current entry value. */ private void fixListSummaries(PreferenceGroup group) { if (group == null) { return; } final int count = group.getPreferenceCount(); for (int i = 0; i < count; i++) { final Preference preference = group.getPreference(i); if (preference instanceof PreferenceGroup) { fixListSummaries((PreferenceGroup) preference); } else if (preference instanceof ListPreference) { preference.setOnPreferenceChangeListener(mPreferenceChangeListener); } } } /** * Listens for preference changes and updates the summary to reflect the * current setting. This shouldn't be necessary, since preferences are * supposed to automatically do this when the summary is set to "%s". */ private final OnPreferenceChangeListener mPreferenceChangeListener = new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { if (preference instanceof ListPreference && newValue instanceof String) { final ListPreference listPreference = (ListPreference) preference; final int index = listPreference.findIndexOfValue((String) newValue); final CharSequence[] entries = listPreference.getEntries(); if (index >= 0 && index < entries.length) { preference.setSummary(entries[index].toString().replaceAll("%", "%%")); } else { preference.setSummary(""); } } return true; } }; }