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