michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.preferences; michael@0: michael@0: import android.content.Context; michael@0: import android.preference.PreferenceCategory; michael@0: import android.util.AttributeSet; michael@0: michael@0: public abstract class CustomListCategory extends PreferenceCategory { michael@0: protected CustomListPreference mDefaultReference; michael@0: michael@0: public CustomListCategory(Context context) { michael@0: super(context); michael@0: } michael@0: michael@0: public CustomListCategory(Context context, AttributeSet attrs) { michael@0: super(context, attrs); michael@0: } michael@0: michael@0: public CustomListCategory(Context context, AttributeSet attrs, int defStyle) { michael@0: super(context, attrs, defStyle); michael@0: } michael@0: michael@0: @Override michael@0: protected void onAttachedToActivity() { michael@0: super.onAttachedToActivity(); michael@0: michael@0: setOrderingAsAdded(true); michael@0: } michael@0: michael@0: /** michael@0: * Set the default to some available list item. Used if the current default is removed or michael@0: * disabled. michael@0: */ michael@0: protected void setFallbackDefault() { michael@0: if (getPreferenceCount() > 0) { michael@0: CustomListPreference aItem = (CustomListPreference) getPreference(0); michael@0: setDefault(aItem); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Removes the given item from the set of available list items. michael@0: * This only updates the UI, so callers are responsible for persisting any state. michael@0: * michael@0: * @param item The given item to remove. michael@0: */ michael@0: public void uninstall(CustomListPreference item) { michael@0: removePreference(item); michael@0: if (item == mDefaultReference) { michael@0: // If the default is being deleted, set a new default. michael@0: setFallbackDefault(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Sets the given item as the current default. michael@0: * This only updates the UI, so callers are responsible for persisting any state. michael@0: * michael@0: * @param item The intended new default. michael@0: */ michael@0: public void setDefault(CustomListPreference item) { michael@0: if (mDefaultReference != null) { michael@0: mDefaultReference.setIsDefault(false); michael@0: } michael@0: michael@0: item.setIsDefault(true); michael@0: mDefaultReference = item; michael@0: } michael@0: }