Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.preferences; |
michael@0 | 6 | |
michael@0 | 7 | import android.content.Context; |
michael@0 | 8 | import android.preference.PreferenceCategory; |
michael@0 | 9 | import android.util.AttributeSet; |
michael@0 | 10 | |
michael@0 | 11 | public abstract class CustomListCategory extends PreferenceCategory { |
michael@0 | 12 | protected CustomListPreference mDefaultReference; |
michael@0 | 13 | |
michael@0 | 14 | public CustomListCategory(Context context) { |
michael@0 | 15 | super(context); |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | public CustomListCategory(Context context, AttributeSet attrs) { |
michael@0 | 19 | super(context, attrs); |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | public CustomListCategory(Context context, AttributeSet attrs, int defStyle) { |
michael@0 | 23 | super(context, attrs, defStyle); |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | @Override |
michael@0 | 27 | protected void onAttachedToActivity() { |
michael@0 | 28 | super.onAttachedToActivity(); |
michael@0 | 29 | |
michael@0 | 30 | setOrderingAsAdded(true); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | /** |
michael@0 | 34 | * Set the default to some available list item. Used if the current default is removed or |
michael@0 | 35 | * disabled. |
michael@0 | 36 | */ |
michael@0 | 37 | protected void setFallbackDefault() { |
michael@0 | 38 | if (getPreferenceCount() > 0) { |
michael@0 | 39 | CustomListPreference aItem = (CustomListPreference) getPreference(0); |
michael@0 | 40 | setDefault(aItem); |
michael@0 | 41 | } |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | /** |
michael@0 | 45 | * Removes the given item from the set of available list items. |
michael@0 | 46 | * This only updates the UI, so callers are responsible for persisting any state. |
michael@0 | 47 | * |
michael@0 | 48 | * @param item The given item to remove. |
michael@0 | 49 | */ |
michael@0 | 50 | public void uninstall(CustomListPreference item) { |
michael@0 | 51 | removePreference(item); |
michael@0 | 52 | if (item == mDefaultReference) { |
michael@0 | 53 | // If the default is being deleted, set a new default. |
michael@0 | 54 | setFallbackDefault(); |
michael@0 | 55 | } |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | /** |
michael@0 | 59 | * Sets the given item as the current default. |
michael@0 | 60 | * This only updates the UI, so callers are responsible for persisting any state. |
michael@0 | 61 | * |
michael@0 | 62 | * @param item The intended new default. |
michael@0 | 63 | */ |
michael@0 | 64 | public void setDefault(CustomListPreference item) { |
michael@0 | 65 | if (mDefaultReference != null) { |
michael@0 | 66 | mDefaultReference.setIsDefault(false); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | item.setIsDefault(true); |
michael@0 | 70 | mDefaultReference = item; |
michael@0 | 71 | } |
michael@0 | 72 | } |