Back to project page WarDroid.
The source code is released under:
MIT License
If you think the Android project WarDroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* ** Copyright 2012, Joel Pedraza/* w w w . j a va 2 s . c o m*/ ** ** 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.deathsnacks.wardroid.utils; import java.util.ArrayList; import java.util.Map; import android.content.SharedPreferences; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Build; import android.preference.CheckBoxPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceCategory; import android.preference.PreferenceGroup; import android.preference.PreferenceScreen; import android.preference.RingtonePreference; import android.preference.TwoStatePreference; import android.text.TextUtils; import android.util.Log; import com.deathsnacks.wardroid.R; import com.deathsnacks.wardroid.utils.preferences.MultiSelectListPreference; import afzkl.development.colorpickerview.preference.ColorPickerPreference; public final class UnifiedPreferenceUtils { private UnifiedPreferenceUtils() { // This class cannot be instantiated } /** * A preference value change listener that updates the preference's summary * to reflect its new value. */ private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object value) { // Do not bind toggles. if (preference instanceof CheckBoxPreference || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && preference instanceof TwoStatePreference)) { return true; } if (preference instanceof ColorPickerPreference || preference instanceof MultiSelectListPreference) return true; String stringValue = value.toString(); if (preference instanceof ListPreference) { // For list preferences, look up the correct display value in // the preference's 'entries' list. ListPreference listPreference = (ListPreference) preference; int index = listPreference.findIndexOfValue(stringValue); // Set the summary to reflect the new value. preference .setSummary(index >= 0 ? listPreference.getEntries()[index] : null); } else if (preference instanceof RingtonePreference) { // For ringtone preferences, look up the correct display value // using RingtoneManager. if (TextUtils.isEmpty(stringValue)) { // Empty values correspond to 'silent' (no ringtone). preference.setSummary(R.string.ringtone_silent); } else { Ringtone ringtone = RingtoneManager.getRingtone( preference.getContext(), Uri.parse(stringValue)); if (ringtone == null) { // Clear the summary if there was a lookup error. preference.setSummary(null); } else { // Set the summary to reflect the new ringtone display // name. String name = ringtone .getTitle(preference.getContext()); preference.setSummary(name); } } } else { // For all other preferences, set the summary to the value's // simple string representation. preference.setSummary(stringValue); } return true; } }; /** * Binds a preference's summary to its value. More specifically, when the * preference's value is changed, its summary (line of text below the * preference title) is updated to reflect the value. The summary is also * immediately updated upon calling this method. The exact display format is * dependent on the type of preference. * * @see #sBindPreferenceSummaryToValueListener */ private static void bindPreferenceSummaryToValue(Preference preference, Object value) { // Set the listener to watch for value changes preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener); // Trigger the listener immediately with the preference's // current value. if (value != null) { sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, value); } } /** * Iterate over every preference and bind the summary to it's value. * This is the default behavior for Activities and Fragments. * * @param screen The PreferenceScreen */ public static void bindAllPreferenceSummariesToValues(PreferenceScreen screen) { ArrayList<Preference> list = getPreferenceList(screen, new ArrayList<Preference>()); Map<String, ?> prefs = screen.getSharedPreferences().getAll(); for (int i = 0; i < list.size(); i++) { Preference preference = list.get(i); Object value = prefs.get(preference.getKey()); bindPreferenceSummaryToValue(preference, value); } } private static ArrayList<Preference> getPreferenceList(Preference p, ArrayList<Preference> list) { if( p instanceof PreferenceCategory || p instanceof PreferenceScreen) { PreferenceGroup pGroup = (PreferenceGroup) p; int pCount = pGroup.getPreferenceCount(); for(int i = 0; i < pCount; i++) { getPreferenceList(pGroup.getPreference(i), list); // recursive call } } else { list.add(p); } return list; } }