1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/preferences/CustomListCategory.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,72 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.preferences; 1.9 + 1.10 +import android.content.Context; 1.11 +import android.preference.PreferenceCategory; 1.12 +import android.util.AttributeSet; 1.13 + 1.14 +public abstract class CustomListCategory extends PreferenceCategory { 1.15 + protected CustomListPreference mDefaultReference; 1.16 + 1.17 + public CustomListCategory(Context context) { 1.18 + super(context); 1.19 + } 1.20 + 1.21 + public CustomListCategory(Context context, AttributeSet attrs) { 1.22 + super(context, attrs); 1.23 + } 1.24 + 1.25 + public CustomListCategory(Context context, AttributeSet attrs, int defStyle) { 1.26 + super(context, attrs, defStyle); 1.27 + } 1.28 + 1.29 + @Override 1.30 + protected void onAttachedToActivity() { 1.31 + super.onAttachedToActivity(); 1.32 + 1.33 + setOrderingAsAdded(true); 1.34 + } 1.35 + 1.36 + /** 1.37 + * Set the default to some available list item. Used if the current default is removed or 1.38 + * disabled. 1.39 + */ 1.40 + protected void setFallbackDefault() { 1.41 + if (getPreferenceCount() > 0) { 1.42 + CustomListPreference aItem = (CustomListPreference) getPreference(0); 1.43 + setDefault(aItem); 1.44 + } 1.45 + } 1.46 + 1.47 + /** 1.48 + * Removes the given item from the set of available list items. 1.49 + * This only updates the UI, so callers are responsible for persisting any state. 1.50 + * 1.51 + * @param item The given item to remove. 1.52 + */ 1.53 + public void uninstall(CustomListPreference item) { 1.54 + removePreference(item); 1.55 + if (item == mDefaultReference) { 1.56 + // If the default is being deleted, set a new default. 1.57 + setFallbackDefault(); 1.58 + } 1.59 + } 1.60 + 1.61 + /** 1.62 + * Sets the given item as the current default. 1.63 + * This only updates the UI, so callers are responsible for persisting any state. 1.64 + * 1.65 + * @param item The intended new default. 1.66 + */ 1.67 + public void setDefault(CustomListPreference item) { 1.68 + if (mDefaultReference != null) { 1.69 + mDefaultReference.setIsDefault(false); 1.70 + } 1.71 + 1.72 + item.setIsDefault(true); 1.73 + mDefaultReference = item; 1.74 + } 1.75 +}