• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.xtremelabs.robolectric.shadows;
2 
3 import java.util.ArrayList;
4 
5 import android.preference.Preference;
6 import android.preference.PreferenceGroup;
7 import android.text.TextUtils;
8 
9 import com.xtremelabs.robolectric.internal.Implementation;
10 import com.xtremelabs.robolectric.internal.Implements;
11 import com.xtremelabs.robolectric.internal.RealObject;
12 
13 /**
14  * See: http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob_plain;f=core/java/android/preference/PreferenceGroup.java;hb=HEAD
15  */
16 @Implements(PreferenceGroup.class)
17 public class ShadowPreferenceGroup extends ShadowPreference {
18 
19     @RealObject private PreferenceGroup realPreferenceGroup;
20 
21     private ArrayList<Preference> preferenceList = new ArrayList<Preference>();
22 
23 	@Implementation
addItemFromInflater(Preference preference)24 	public void addItemFromInflater(Preference preference) {
25 		addPreference(preference);
26 	}
27 
28 	@Implementation
addPreference(Preference preference)29 	public boolean addPreference(Preference preference) {
30 		if (preferenceList.contains(preference)) {
31             return true;
32         }
33 
34 		// TODO currently punting on ordering logic
35 		preferenceList.add(preference);
36 
37 		return true;
38 	}
39 
40 	@Implementation
getPreference(int index)41 	public Preference getPreference(int index) {
42 		return preferenceList.get(index);
43 	}
44 
45 	@Implementation
getPreferenceCount()46 	public int getPreferenceCount() {
47 		return preferenceList.size();
48 	}
49 
50 	@Implementation
removePreference(Preference preference)51 	public boolean removePreference(Preference preference) {
52 		return preferenceList.remove(preference);
53 	}
54 
55 	@Implementation
removeAll()56 	public void removeAll() {
57 		preferenceList.clear();
58 	}
59 
60 	/**
61 	 * Note: copied wholesale from Android source
62 	 * @param key
63 	 * @return
64 	 */
65 	@Implementation
findPreference(CharSequence key)66 	public Preference findPreference(CharSequence key) {
67         if (TextUtils.equals(getKey(), key)) {
68             return realPreferenceGroup;
69         }
70         final int preferenceCount = getPreferenceCount();
71         for (int i = 0; i < preferenceCount; i++) {
72             final Preference preference = getPreference(i);
73             final String curKey = preference.getKey();
74 
75             if (curKey != null && curKey.equals(key)) {
76                 return preference;
77             }
78 
79             if (preference instanceof PreferenceGroup) {
80                 final Preference returnedPreference = ((PreferenceGroup)preference)
81                         .findPreference(key);
82                 if (returnedPreference != null) {
83                     return returnedPreference;
84                 }
85             }
86         }
87 
88         return null;
89     }
90 
91 	/**
92 	 * Note: copied wholesale from Android source
93 	 */
94 	@Implementation
setEnabled(boolean enabled)95 	public void setEnabled(boolean enabled) {
96 		super.setEnabled(enabled);
97 
98 		final int preferenceCount = getPreferenceCount();
99         for (int i = 0; i < preferenceCount; i++) {
100             getPreference(i).setEnabled(enabled);
101         }
102 	}
103 }
104