5
5
import android .util .AttributeSet ;
6
6
import android .util .TypedValue ;
7
7
8
- import java .lang .reflect .Field ;
9
-
10
8
/**
11
9
* Util class to retrieve some values of attributes in preference xml.
12
10
* To achieve this, we need to:
13
- * 1. Obtain the array android.R$styleable.Preference through reflection.
14
- * Cache is introduced to reduce the performance overhead introduced by reflection.
15
- * 2. Obtain the resource id of certain attributes that we care such as title and key using
16
- * reflection. Again, cache is introduced.
17
- * 3. Obtain the value of those attribute {@link TypedArray#peekValue(int)}.
11
+ * 1. Obtain the resource id of certain attributes that we care such as title and key.
12
+ * 2. Obtain the value of those attribute {@link TypedArray#peekValue(int)}.
18
13
*/
19
14
public class PreferenceXmlUtil {
20
- private static Integer sPreferenceTitleId ;
21
- private static Integer sPreferenceKeyId ;
22
- private static int [] sPreferenceStyleArray ;
23
15
24
16
public static String getDataTitle (Context context , AttributeSet attrs )
25
17
throws ReflectiveOperationException {
26
- return getData (context , attrs , getPreferenceTitleId () );
18
+ return getData (context , attrs , android . R . attr . title );
27
19
}
28
20
29
21
public static String getDataKey (Context context , AttributeSet attrs )
30
22
throws ReflectiveOperationException {
31
- return getData (context , attrs , getPreferenceKeyId () );
23
+ return getData (context , attrs , android . R . attr . key );
32
24
}
33
25
34
- private static String getData (Context context , AttributeSet set , int resId )
26
+ private static String getData (Context context , AttributeSet set , int attribute )
35
27
throws ReflectiveOperationException {
36
- int [] attrs = getPreferenceStyleArray ();
37
- final TypedArray sa = context .obtainStyledAttributes (set , attrs );
28
+ final TypedArray sa = context .obtainStyledAttributes (set , new int [] {attribute });
38
29
try {
39
- final TypedValue tv = sa .peekValue (resId );
30
+ final TypedValue tv = sa .peekValue (0 );
40
31
CharSequence data = null ;
41
32
if (tv != null && tv .type == TypedValue .TYPE_STRING ) {
42
33
if (tv .resourceId != 0 ) {
@@ -50,35 +41,4 @@ private static String getData(Context context, AttributeSet set, int resId)
50
41
sa .recycle ();
51
42
}
52
43
}
53
-
54
- private static int getPreferenceTitleId () throws ReflectiveOperationException {
55
- if (sPreferenceTitleId == null ) {
56
- sPreferenceTitleId = getStyleableId ("Preference_title" );
57
- }
58
- return sPreferenceTitleId ;
59
- }
60
-
61
- private static int getPreferenceKeyId () throws ReflectiveOperationException {
62
- if (sPreferenceKeyId == null ) {
63
- sPreferenceKeyId = getStyleableId ("Preference_key" );
64
- }
65
- return sPreferenceKeyId ;
66
- }
67
-
68
- private static int [] getPreferenceStyleArray () throws ReflectiveOperationException {
69
- if (sPreferenceStyleArray == null ) {
70
- sPreferenceStyleArray = getStyleableArray ("Preference" );
71
- }
72
- return sPreferenceStyleArray ;
73
- }
74
-
75
- private static int getStyleableId (String name ) throws ReflectiveOperationException {
76
- Field field = Class .forName ("android.R$styleable" ).getDeclaredField (name );
77
- return (int ) field .get (null );
78
- }
79
-
80
- private static final int [] getStyleableArray (String name ) throws ReflectiveOperationException {
81
- Field field = Class .forName ("android.R$styleable" ).getDeclaredField (name );
82
- return (int []) field .get (null );
83
- }
84
44
}
0 commit comments