/
Generators.java
executable file
·303 lines (239 loc) · 11.7 KB
/
Generators.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package com.audacious_software.passive_data_kit.generators;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.util.SparseArray;
import com.audacious_software.passive_data_kit.Logger;
import com.audacious_software.passive_data_kit.diagnostics.DiagnosticAction;
import com.audacious_software.passive_data_kit.generators.diagnostics.AppEvent;
import com.audacious_software.pdk.passivedatakit.R;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
public class Generators {
private Context mContext = null;
private boolean mStarted = false;
private final ArrayList<String> mGenerators = new ArrayList<>();
private final HashSet<String> mActiveGenerators = new HashSet<>();
private SharedPreferences mSharedPreferences = null;
private final HashMap<String, Class<? extends Generator>> mGeneratorMap = new HashMap<>();
private final SparseArray<Class<? extends Generator>> mViewTypeMap = new SparseArray<>();
private final HashSet<GeneratorUpdatedListener> mGeneratorUpdatedListeners = new HashSet<>();
private final HashMap<String, PowerManager.WakeLock> mWakeLocks = new HashMap<>();
@SuppressWarnings("unchecked")
public void start() {
synchronized (this) {
if (!this.mStarted) {
this.mStarted = true;
this.mGenerators.clear();
this.mGenerators.add(AppEvent.class.getCanonicalName());
Collections.addAll(this.mGenerators, this.mContext.getResources().getStringArray(R.array.pdk_available_generators));
Collections.addAll(this.mGenerators, this.mContext.getResources().getStringArray(R.array.pdk_app_generators));
for (String className : this.mGenerators) {
//noinspection TryWithIdenticalCatches
try {
Class<Generator> probeClass = (Class<Generator>) Class.forName(className);
Method isEnabled = probeClass.getDeclaredMethod("isEnabled", Context.class);
Boolean enabled = (Boolean) isEnabled.invoke(null, this.mContext);
if (enabled) {
this.startGenerator(className);
} else {
this.stopGenerator(className);
}
} catch (ClassNotFoundException e) {
Logger.getInstance(this.mContext).logThrowable(e);
} catch (NoSuchMethodException e) {
Logger.getInstance(this.mContext).logThrowable(e);
} catch (InvocationTargetException e) {
Logger.getInstance(this.mContext).logThrowable(e);
} catch (IllegalAccessException e) {
Logger.getInstance(this.mContext).logThrowable(e);
}
}
}
}
}
@SuppressWarnings("unchecked")
private void startGenerator(String className) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
if (!this.mActiveGenerators.contains(className)) {
Class<Generator> generatorClass = (Class<Generator>) Class.forName(className);
Method isRunning = generatorClass.getDeclaredMethod("isRunning", Context.class);
Boolean running = (Boolean) isRunning.invoke(null, this.mContext);
if (running) {
this.stopGenerator(className);
}
else {
Method start = generatorClass.getDeclaredMethod("start", Context.class);
start.invoke(null, this.mContext);
this.mActiveGenerators.add(className);
}
}
}
@SuppressWarnings("unchecked")
private void stopGenerator(String className) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
if (this.mActiveGenerators.contains(className)) {
Class<Generator> probeClass = (Class<Generator>) Class.forName(className);
Method stop = probeClass.getDeclaredMethod("stop", probeClass);
stop.invoke(null, this.mContext);
this.mActiveGenerators.remove(className);
}
}
public SharedPreferences getSharedPreferences(Context context) {
if (this.mSharedPreferences == null)
this.mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
return this.mSharedPreferences;
}
@SuppressWarnings({"TryWithIdenticalCatches", "unchecked"})
public ArrayList<DiagnosticAction> diagnostics() {
ArrayList<DiagnosticAction> actions = new ArrayList<>();
for (String className : this.mActiveGenerators) {
try {
Class<Generator> generatorClass = (Class<Generator>) Class.forName(className);
Method diagnostics = generatorClass.getDeclaredMethod("diagnostics", Context.class);
Collection<DiagnosticAction> generatorActions = (Collection<DiagnosticAction>) diagnostics.invoke(null, this.mContext);
actions.addAll(generatorActions);
} catch (ClassNotFoundException e) {
// e.printStackTrace();
} catch (InvocationTargetException e) {
// e.printStackTrace();
} catch (NoSuchMethodException e) {
// e.printStackTrace();
} catch (IllegalAccessException e) {
// e.printStackTrace();
}
}
return actions;
}
@SuppressWarnings("SameReturnValue")
public String getSource() {
return "unknown-user-please-set-me";
}
public String getGeneratorFullName(String identifier) {
String pdkName = this.mContext.getString(R.string.pdk_name);
String pdkVersion = this.mContext.getString(R.string.pdk_version);
String appName = this.mContext.getString(this.mContext.getApplicationInfo().labelRes);
String version = this.mContext.getString(R.string.unknown_version);
try {
PackageInfo pInfo = this.mContext.getPackageManager().getPackageInfo(this.mContext.getPackageName(), 0);
version = pInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
Logger.getInstance(this.mContext).logThrowable(e);
}
return identifier + ": " + appName + "/" + version + " " + pdkName + "/" + pdkVersion;
}
public void registerCustomViewClass(String identifier, Class<? extends Generator> generatorClass) {
this.mGeneratorMap.put(identifier, generatorClass);
this.mViewTypeMap.put(generatorClass.hashCode(), generatorClass);
}
@SuppressWarnings("unused")
public Class<? extends Generator> fetchCustomViewClass(String identifier) {
Class<? extends Generator> generatorClass = this.mGeneratorMap.get(identifier);
if (generatorClass == null)
generatorClass = Generator.class;
return generatorClass;
}
public Class<? extends Generator> fetchCustomViewClass(int viewType) {
Class<? extends Generator> generatorClass = this.mViewTypeMap.get(viewType);
if (generatorClass == null)
generatorClass = Generator.class;
return generatorClass;
}
@SuppressWarnings({"TryWithIdenticalCatches", "unchecked", "unused"})
public Generator getGenerator(String className) {
if (this.mActiveGenerators.contains(className)) {
try {
Class<Generator> probeClass = (Class<Generator>) Class.forName(className);
Method getInstance = probeClass.getDeclaredMethod("getInstance", Context.class);
return (Generator) getInstance.invoke(null, this.mContext);
} catch (ClassNotFoundException e) {
// e.printStackTrace();
} catch (NoSuchMethodException e) {
// e.printStackTrace();
} catch (IllegalAccessException e) {
// e.printStackTrace();
} catch (InvocationTargetException e) {
// e.printStackTrace();
}
}
return null;
}
@SuppressWarnings("unchecked")
public List<Class<? extends Generator>> activeGenerators() {
ArrayList<Class<? extends Generator>> active = new ArrayList<>();
for (String className : this.mActiveGenerators) {
try {
active.add((Class<? extends Generator>) Class.forName(className));
} catch (ClassNotFoundException e) {
// e.printStackTrace();
}
}
return active;
}
@SuppressWarnings("SameParameterValue")
public void notifyGeneratorUpdated(String identifier, long timestamp, Bundle bundle) {
for (GeneratorUpdatedListener listener : this.mGeneratorUpdatedListeners) {
listener.onGeneratorUpdated(identifier, timestamp, bundle);
}
}
public void notifyGeneratorUpdated(String identifier, Bundle bundle) {
long timestamp = System.currentTimeMillis();
synchronized(this.mGeneratorUpdatedListeners) {
for (GeneratorUpdatedListener listener : this.mGeneratorUpdatedListeners) {
listener.onGeneratorUpdated(identifier, timestamp, bundle);
}
}
}
@SuppressWarnings("SameParameterValue")
public void acquireWakeLock(String tag, int lockType) {
this.releaseWakeLock(tag);
PowerManager power = (PowerManager) this.mContext.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock lock = power.newWakeLock(lockType, tag);
this.mWakeLocks.put(tag, lock);
}
public void releaseWakeLock(String tag) {
if (this.mWakeLocks.containsKey(tag)) {
PowerManager.WakeLock lock = this.mWakeLocks.get(tag);
if (lock.isHeld()) {
lock.release();
}
this.mWakeLocks.remove(tag);
}
}
private static class GeneratorsHolder {
@SuppressLint("StaticFieldLeak")
public static final Generators instance = new Generators();
}
@SuppressWarnings("SameReturnValue")
public static Generators getInstance(Context context) {
if (context != null) {
GeneratorsHolder.instance.setContext(context);
}
return GeneratorsHolder.instance;
}
private void setContext(Context context) {
this.mContext = context.getApplicationContext();
}
public void addNewGeneratorUpdatedListener(Generators.GeneratorUpdatedListener listener) {
synchronized(this.mGeneratorUpdatedListeners) {
this.mGeneratorUpdatedListeners.add(listener);
}
}
public void removeGeneratorUpdatedListener(Generators.GeneratorUpdatedListener listener) {
synchronized (this.mGeneratorUpdatedListeners) {
this.mGeneratorUpdatedListeners.remove(listener);
}
}
public interface GeneratorUpdatedListener {
void onGeneratorUpdated(String identifier, long timestamp, Bundle data);
}
}