/
DataPointsAdapter.java
executable file
·169 lines (131 loc) · 6.49 KB
/
DataPointsAdapter.java
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.audacious_software.passive_data_kit.activities.generators;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import com.audacious_software.passive_data_kit.Logger;
import com.audacious_software.passive_data_kit.generators.Generator;
import com.audacious_software.passive_data_kit.generators.Generators;
import com.audacious_software.passive_data_kit.generators.diagnostics.AppEvent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
public class DataPointsAdapter extends RecyclerView.Adapter<DataPointViewHolder> {
public static final String SORT_BY_UPDATED = "com.audacious_software.passive_data_kit.activities.generators.DataPointsAdapter";
public static final boolean SORT_BY_UPDATED_DEFAULT = true;
private Context mContext = null;
private List<Class<? extends Generator>> mActiveGenerators = null;
@Override
public DataPointViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Class<? extends Generator> generatorClass = Generators.getInstance(this.mContext).fetchCustomViewClass(viewType);
try {
Method fetchView = generatorClass.getDeclaredMethod("fetchView", ViewGroup.class);
View view = (View) fetchView.invoke(null, parent);
return new DataPointViewHolder(view);
} catch (Exception e) {
try {
generatorClass = Generator.class;
Method fetchView = generatorClass.getDeclaredMethod("fetchView", ViewGroup.class);
View view = (View) fetchView.invoke(null, parent);
return new DataPointViewHolder(view);
} catch (NoSuchMethodException e1) {
Logger.getInstance(parent.getContext()).logThrowable(e1);
} catch (InvocationTargetException e1) {
Logger.getInstance(parent.getContext()).logThrowable(e1);
} catch (IllegalAccessException e1) {
Logger.getInstance(parent.getContext()).logThrowable(e1);
}
}
return null;
}
private List<Class<? extends Generator>> getGenerators(Context context) {
if (this.mActiveGenerators == null) {
this.mActiveGenerators = Generators.getInstance(context).activeGenerators();
}
this.sortGenerators();
return this.mActiveGenerators;
}
@Override
public void onBindViewHolder(final DataPointViewHolder holder, int position) {
this.getGenerators(holder.itemView.getContext());
Class<? extends Generator> generatorClass = this.mActiveGenerators.get(position);
try {
Method bindViewHolder = generatorClass.getDeclaredMethod("bindViewHolder", DataPointViewHolder.class);
bindViewHolder.invoke(null, holder);
} catch (Exception e) {
AppEvent.getInstance(this.mContext).logThrowable(e);
try {
generatorClass = Generator.class;
Method bindViewHolder = generatorClass.getDeclaredMethod("bindViewHolder", DataPointViewHolder.class);
bindViewHolder.invoke(null, holder);
} catch (NoSuchMethodException e1) {
Logger.getInstance(holder.itemView.getContext()).logThrowable(e1);
} catch (InvocationTargetException e1) {
Logger.getInstance(holder.itemView.getContext()).logThrowable(e1);
} catch (IllegalAccessException e1) {
Logger.getInstance(holder.itemView.getContext()).logThrowable(e1);
}
}
}
@Override
public int getItemCount() {
this.getGenerators(this.mContext);
return this.mActiveGenerators.size();
}
public void sortGenerators() {
final Context context = this.mContext;
if (this.mActiveGenerators == null) {
this.mActiveGenerators = Generators.getInstance(this.mContext).activeGenerators();
}
final DataPointsAdapter me = this;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (prefs.getBoolean(DataPointsAdapter.SORT_BY_UPDATED, DataPointsAdapter.SORT_BY_UPDATED_DEFAULT)) {
Collections.sort(me.mActiveGenerators, new Comparator<Class<? extends Generator>>() {
@Override
public int compare(Class<? extends Generator> one, Class<? extends Generator> two) {
long oneUpdated = 0;
try {
Method oneGenerated = one.getDeclaredMethod("latestPointGenerated", Context.class);
oneUpdated = (long) oneGenerated.invoke(null, context);
} catch (NoSuchMethodException e) {
// e.printStackTrace();
} catch (InvocationTargetException e) {
// e.printStackTrace();
} catch (IllegalAccessException e) {
// e.printStackTrace();
}
long twoUpdated = 0;
try {
Method twoGenerated = two.getDeclaredMethod("latestPointGenerated", Context.class);
twoUpdated = (long) twoGenerated.invoke(null, context);
} catch (NoSuchMethodException e) {
// e.printStackTrace();
} catch (InvocationTargetException e) {
// e.printStackTrace();
} catch (IllegalAccessException e) {
// e.printStackTrace();
}
if (oneUpdated < twoUpdated) {
return 1;
} else if (oneUpdated > twoUpdated) {
return -1;
}
return 0;
}
});
}
}
public int getItemViewType (int position) {
this.getGenerators(this.mContext);
Class<? extends Generator> generatorClass = this.mActiveGenerators.get(position);
return generatorClass.hashCode();
}
public void setContext(Context context) {
this.mContext = context;
}
}