1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package com.android.camera.ui;
import static com.android.camera.ui.GLRootView.dpToPixel;
import android.content.Context;
import com.android.camera.IconListPreference;
import com.android.camera.ListPreference;
import com.android.camera.R;
import com.android.camera.Util;
import java.util.ArrayList;
public class PreferenceAdapter
implements GLListView.Model, GLListView.OnItemSelectedListener {
private static final int ICON_NONE = 0;
private static final int HORIZONTAL_PADDINGS = 4;
private static final int VERTICAL_PADDINGS = 2;
private static int sHorizontalPaddings = -1;
private static int sVerticalPaddings;
private final ArrayList<GLView> mContent = new ArrayList<GLView>();
private final ListPreference mPreference;
private String mOverride;
private static void initializeStaticVariable(Context context) {
if (sHorizontalPaddings >= 0) return;
sHorizontalPaddings = dpToPixel(context, HORIZONTAL_PADDINGS);
sVerticalPaddings = dpToPixel(context, VERTICAL_PADDINGS);
}
public PreferenceAdapter(Context context, ListPreference preference) {
initializeStaticVariable(context);
mPreference = preference;
generateContent(context, preference);
}
public void overrideSettings(String settings) {
if (Util.equals(settings, mOverride)) return;
mOverride = settings;
CharSequence[] values = mPreference.getEntryValues();
String value = mPreference.getValue();
if (settings == null) {
for (int i = 1, n = mContent.size(); i < n; ++i) {
GLOptionItem item = (GLOptionItem) mContent.get(i);
item.setChecked(values[i - 1].equals(value));
item.setEnabled(true);
}
} else {
for (int i = 1, n = mContent.size(); i < n; ++i) {
GLOptionItem item = (GLOptionItem) mContent.get(i);
boolean checked = values[i - 1].equals(settings);
item.setChecked(checked);
item.setEnabled(checked);
}
}
}
private void generateContent(Context context, ListPreference preference) {
GLOptionHeader header = new GLOptionHeader(context, preference);
header.setBackground(new NinePatchTexture(
context, R.drawable.optionheader_background));
header.setPaddings(sHorizontalPaddings,
sVerticalPaddings, sHorizontalPaddings, sVerticalPaddings);
mContent.add(header);
CharSequence[] entries = preference.getEntries();
CharSequence[] values = preference.getEntryValues();
String value = preference.getValue();
int [] icons = null;
if (preference instanceof IconListPreference) {
IconListPreference iPref = (IconListPreference) preference;
icons = iPref.getIconIds();
}
for (int i = 0, n = entries.length; i < n; ++i) {
GLOptionItem item = new GLOptionItem(
context, icons == null ? ICON_NONE : icons[i],
entries[i].toString());
item.setPaddings(sHorizontalPaddings,
sVerticalPaddings, sHorizontalPaddings, sVerticalPaddings);
item.setChecked(values[i].equals(value));
mContent.add(item);
}
}
public void onItemSelected(GLView view, int position) {
if (mOverride != null) return;
ListPreference pref = mPreference;
CharSequence[] values = pref.getEntryValues();
if (position < values.length + 1) {
int index = position - 1;
int oldIndex = pref.findIndexOfValue(pref.getValue());
if (oldIndex != index) {
pref.setValueIndex(index);
((GLOptionItem) mContent.get(1 + oldIndex)).setChecked(false);
((GLOptionItem) view).setChecked(true);
}
return;
}
}
public GLView getView(int index) {
return mContent.get(index);
}
public boolean isSelectable(int index) {
return mContent.get(index) instanceof GLOptionItem;
}
public int size() {
return mContent.size();
}
}
|