Skip to content

Commit 929daf9

Browse files
committed
No longer use reflection to implement search functionality
FIX: 76211248 Change-Id: I91a99036e9b01cddb49e9a506a0cdfe978d348c6
1 parent dbf8f2a commit 929daf9

File tree

1 file changed

+7
-47
lines changed

1 file changed

+7
-47
lines changed

app/src/main/java/com/afwsamples/testdpc/search/PreferenceXmlUtil.java

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,29 @@
55
import android.util.AttributeSet;
66
import android.util.TypedValue;
77

8-
import java.lang.reflect.Field;
9-
108
/**
119
* Util class to retrieve some values of attributes in preference xml.
1210
* 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)}.
1813
*/
1914
public class PreferenceXmlUtil {
20-
private static Integer sPreferenceTitleId;
21-
private static Integer sPreferenceKeyId;
22-
private static int[] sPreferenceStyleArray;
2315

2416
public static String getDataTitle(Context context, AttributeSet attrs)
2517
throws ReflectiveOperationException {
26-
return getData(context, attrs, getPreferenceTitleId());
18+
return getData(context, attrs, android.R.attr.title);
2719
}
2820

2921
public static String getDataKey(Context context, AttributeSet attrs)
3022
throws ReflectiveOperationException {
31-
return getData(context, attrs, getPreferenceKeyId());
23+
return getData(context, attrs, android.R.attr.key);
3224
}
3325

34-
private static String getData(Context context, AttributeSet set, int resId)
26+
private static String getData(Context context, AttributeSet set, int attribute)
3527
throws ReflectiveOperationException {
36-
int[] attrs = getPreferenceStyleArray();
37-
final TypedArray sa = context.obtainStyledAttributes(set, attrs);
28+
final TypedArray sa = context.obtainStyledAttributes(set, new int[] {attribute});
3829
try {
39-
final TypedValue tv = sa.peekValue(resId);
30+
final TypedValue tv = sa.peekValue(0);
4031
CharSequence data = null;
4132
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
4233
if (tv.resourceId != 0) {
@@ -50,35 +41,4 @@ private static String getData(Context context, AttributeSet set, int resId)
5041
sa.recycle();
5142
}
5243
}
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-
}
8444
}

0 commit comments

Comments
 (0)