|
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/. */ |
|
4 |
|
5 package org.mozilla.gecko.preferences; |
|
6 |
|
7 import android.content.Context; |
|
8 import android.preference.PreferenceCategory; |
|
9 import android.util.AttributeSet; |
|
10 |
|
11 public abstract class CustomListCategory extends PreferenceCategory { |
|
12 protected CustomListPreference mDefaultReference; |
|
13 |
|
14 public CustomListCategory(Context context) { |
|
15 super(context); |
|
16 } |
|
17 |
|
18 public CustomListCategory(Context context, AttributeSet attrs) { |
|
19 super(context, attrs); |
|
20 } |
|
21 |
|
22 public CustomListCategory(Context context, AttributeSet attrs, int defStyle) { |
|
23 super(context, attrs, defStyle); |
|
24 } |
|
25 |
|
26 @Override |
|
27 protected void onAttachedToActivity() { |
|
28 super.onAttachedToActivity(); |
|
29 |
|
30 setOrderingAsAdded(true); |
|
31 } |
|
32 |
|
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 } |
|
43 |
|
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 } |
|
57 |
|
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 } |
|
68 |
|
69 item.setIsDefault(true); |
|
70 mDefaultReference = item; |
|
71 } |
|
72 } |