/
DataPointsAdapter.java
executable file
·147 lines (115 loc) · 5.62 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
package com.audacious_software.passive_data_kit.activities.generators;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
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 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> {
private Context mContext = 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) {
e.printStackTrace();
try {
generatorClass = Generator.class;
Method fetchView = generatorClass.getDeclaredMethod("fetchView", ViewGroup.class);
View view = (View) fetchView.invoke(null, parent);
Log.e("PDK", "GENERATOR CLASS: " + generatorClass);
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;
}
@Override
public void onBindViewHolder(final DataPointViewHolder holder, int position) {
List<Class<? extends Generator>> activeGenerators = Generators.getInstance(holder.itemView.getContext()).activeGenerators();
this.sortGenerators(this.mContext, activeGenerators);
Class<? extends Generator> generatorClass = activeGenerators.get(position);
try {
Method bindViewHolder = generatorClass.getDeclaredMethod("bindViewHolder", DataPointViewHolder.class);
bindViewHolder.invoke(null, holder);
} catch (Exception e) {
e.printStackTrace();
try {
generatorClass = Generator.class;
Method bindViewHolder = generatorClass.getDeclaredMethod("bindViewHolder", DataPointViewHolder.class);
Log.e("PDK", "GENERATOR CLASS: " + generatorClass);
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() {
return Generators.getInstance(null).activeGenerators().size();
}
private void sortGenerators(final Context context, List<Class<? extends Generator>> generators) {
Collections.sort(generators, 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) {
List<Class<? extends Generator>> activeGenerators = Generators.getInstance(this.mContext).activeGenerators();
this.sortGenerators(this.mContext, activeGenerators);
Class<? extends Generator> generatorClass = activeGenerators.get(position);
return generatorClass.hashCode();
}
public void setContext(Context context) {
this.mContext = context;
}
}