/
XpEdgeEffect.java
382 lines (335 loc) · 13.7 KB
/
XpEdgeEffect.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package net.xpece.android.widget;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.EdgeEffectCompat;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.RecyclerView;
import android.widget.AbsListView;
import android.widget.EdgeEffect;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;
import net.xpece.android.BuildConfig;
import net.xpece.android.util.XpLog;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
/**
* @author Eugen on 11. 2. 2016.
*/
@SuppressLint("PrivateApi")
public final class XpEdgeEffect {
private XpEdgeEffect() {
throw new AssertionError("No instances!");
}
private static final Class<ScrollView> CLASS_SCROLL_VIEW = ScrollView.class;
private static final Field SCROLL_VIEW_FIELD_EDGE_GLOW_TOP;
private static final Field SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM;
private static final Class<ViewPager> CLASS_VIEW_PAGER = ViewPager.class;
private static final Field VIEW_PAGER_FIELD_LEFT_EDGE;
private static final Field VIEW_PAGER_FIELD_RIGHT_EDGE;
private static final Class<HorizontalScrollView> CLASS_HORIZONTAL_SCROLL_VIEW = HorizontalScrollView.class;
private static final Field HORIZONTAL_SCROLL_VIEW_FIELD_EDGE_GLOW_LEFT;
private static final Field HORIZONTAL_SCROLL_VIEW_FIELD_EDGE_GLOW_RIGHT;
private static final Class<NestedScrollView> CLASS_NESTED_SCROLL_VIEW = NestedScrollView.class;
private static final Field NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_TOP;
private static final Field NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM;
private static final Class<RecyclerView> CLASS_RECYCLER_VIEW = RecyclerView.class;
private static final Field RECYCLER_VIEW_FIELD_EDGE_GLOW_TOP;
private static final Field RECYCLER_VIEW_FIELD_EDGE_GLOW_LEFT;
private static final Field RECYCLER_VIEW_FIELD_EDGE_GLOW_RIGHT;
private static final Field RECYCLER_VIEW_FIELD_EDGE_GLOW_BOTTOM;
private static final Class<AbsListView> CLASS_LIST_VIEW = AbsListView.class;
private static final Field LIST_VIEW_FIELD_EDGE_GLOW_TOP;
private static final Field LIST_VIEW_FIELD_EDGE_GLOW_BOTTOM;
private static final Field EDGE_GLOW_FIELD_EDGE;
private static final Field EDGE_GLOW_FIELD_GLOW;
private static final Field EDGE_EFFECT_COMPAT_FIELD_EDGE_EFFECT;
static {
Field edgeGlowTop = null, edgeGlowBottom = null, edgeGlowLeft = null, edgeGlowRight = null;
for (Field f : CLASS_RECYCLER_VIEW.getDeclaredFields()) {
switch (f.getName()) {
case "mTopGlow":
f.setAccessible(true);
edgeGlowTop = f;
break;
case "mBottomGlow":
f.setAccessible(true);
edgeGlowBottom = f;
break;
case "mLeftGlow":
f.setAccessible(true);
edgeGlowLeft = f;
break;
case "mRightGlow":
f.setAccessible(true);
edgeGlowRight = f;
break;
}
}
RECYCLER_VIEW_FIELD_EDGE_GLOW_TOP = edgeGlowTop;
RECYCLER_VIEW_FIELD_EDGE_GLOW_BOTTOM = edgeGlowBottom;
RECYCLER_VIEW_FIELD_EDGE_GLOW_LEFT = edgeGlowLeft;
RECYCLER_VIEW_FIELD_EDGE_GLOW_RIGHT = edgeGlowRight;
for (Field f : CLASS_VIEW_PAGER.getDeclaredFields()) {
switch (f.getName()) {
case "mLeftEdge":
f.setAccessible(true);
edgeGlowLeft = f;
break;
case "mRightEdge":
f.setAccessible(true);
edgeGlowRight = f;
break;
}
}
VIEW_PAGER_FIELD_LEFT_EDGE = edgeGlowLeft;
VIEW_PAGER_FIELD_RIGHT_EDGE = edgeGlowRight;
for (Field f : CLASS_NESTED_SCROLL_VIEW.getDeclaredFields()) {
switch (f.getName()) {
case "mEdgeGlowTop":
f.setAccessible(true);
edgeGlowTop = f;
break;
case "mEdgeGlowBottom":
f.setAccessible(true);
edgeGlowBottom = f;
break;
}
}
NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_TOP = edgeGlowTop;
NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM = edgeGlowBottom;
for (Field f : CLASS_SCROLL_VIEW.getDeclaredFields()) {
switch (f.getName()) {
case "mEdgeGlowTop":
f.setAccessible(true);
edgeGlowTop = f;
break;
case "mEdgeGlowBottom":
f.setAccessible(true);
edgeGlowBottom = f;
break;
}
}
SCROLL_VIEW_FIELD_EDGE_GLOW_TOP = edgeGlowTop;
SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM = edgeGlowBottom;
for (Field f : CLASS_HORIZONTAL_SCROLL_VIEW.getDeclaredFields()) {
switch (f.getName()) {
case "mEdgeGlowLeft":
f.setAccessible(true);
edgeGlowLeft = f;
break;
case "mEdgeGlowRight":
f.setAccessible(true);
edgeGlowRight = f;
break;
}
}
HORIZONTAL_SCROLL_VIEW_FIELD_EDGE_GLOW_LEFT = edgeGlowLeft;
HORIZONTAL_SCROLL_VIEW_FIELD_EDGE_GLOW_RIGHT = edgeGlowRight;
for (Field f : CLASS_LIST_VIEW.getDeclaredFields()) {
switch (f.getName()) {
case "mEdgeGlowTop":
f.setAccessible(true);
edgeGlowTop = f;
break;
case "mEdgeGlowBottom":
f.setAccessible(true);
edgeGlowBottom = f;
break;
}
}
LIST_VIEW_FIELD_EDGE_GLOW_TOP = edgeGlowTop;
LIST_VIEW_FIELD_EDGE_GLOW_BOTTOM = edgeGlowBottom;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Field edge = null, glow = null;
Class cls = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
cls = EdgeEffect.class;
} else {
try {
cls = Class.forName("android.widget.EdgeGlow");
} catch (ClassNotFoundException e) {
if (BuildConfig.DEBUG) e.printStackTrace();
}
}
if (cls != null) {
for (Field f : cls.getDeclaredFields()) {
switch (f.getName()) {
case "mEdge":
f.setAccessible(true);
edge = f;
break;
case "mGlow":
f.setAccessible(true);
glow = f;
break;
}
}
}
EDGE_GLOW_FIELD_EDGE = edge;
EDGE_GLOW_FIELD_GLOW = glow;
} else {
EDGE_GLOW_FIELD_EDGE = null;
EDGE_GLOW_FIELD_GLOW = null;
}
Field efc = null;
try {
efc = EdgeEffectCompat.class.getDeclaredField("mEdgeEffect");
efc.setAccessible(true);
} catch (NoSuchFieldException e) {
if (BuildConfig.DEBUG) e.printStackTrace();
}
EDGE_EFFECT_COMPAT_FIELD_EDGE_EFFECT = efc;
}
@IntDef({ALWAYS, PRE_HONEYCOMB, PRE_KITKAT, PRE_LOLLIPOP})
@Retention(RetentionPolicy.SOURCE)
public @interface EdgeGlowColorApi {}
public static final int ALWAYS = Integer.MAX_VALUE;
/** Replace yellow glow in vanilla, blue glow on Samsung. */
public static final int PRE_HONEYCOMB = Build.VERSION_CODES.HONEYCOMB;
/** Replace Holo blue glow. */
public static final int PRE_KITKAT = Build.VERSION_CODES.KITKAT;
/** Replace Holo grey glow. */
public static final int PRE_LOLLIPOP = Build.VERSION_CODES.LOLLIPOP;
public static void setColor(
@NonNull AbsListView listView, @ColorInt int color, @EdgeGlowColorApi int when) {
if (Build.VERSION.SDK_INT < when) {
setColor(listView, color);
}
}
public static void setColor(@NonNull AbsListView listView, @ColorInt int color) {
try {
Object ee;
ee = LIST_VIEW_FIELD_EDGE_GLOW_TOP.get(listView);
setColor(ee, color);
ee = LIST_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(listView);
setColor(ee, color);
} catch (Exception ex) {
XpLog.logException(listView, ex);
}
}
public static void setColor(
@NonNull ScrollView scrollView, @ColorInt int color, @EdgeGlowColorApi int when) {
if (Build.VERSION.SDK_INT < when) {
setColor(scrollView, color);
}
}
public static void setColor(@NonNull ScrollView scrollView, @ColorInt int color) {
try {
Object ee;
ee = SCROLL_VIEW_FIELD_EDGE_GLOW_TOP.get(scrollView);
setColor(ee, color);
ee = SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(scrollView);
setColor(ee, color);
} catch (Exception ex) {
XpLog.logException(scrollView, ex);
}
}
public static void setColor(
@NonNull HorizontalScrollView scrollView, @ColorInt int color, @EdgeGlowColorApi int when) {
if (Build.VERSION.SDK_INT < when) {
setColor(scrollView, color);
}
}
public static void setColor(@NonNull HorizontalScrollView scrollView, @ColorInt int color) {
try {
Object ee;
ee = HORIZONTAL_SCROLL_VIEW_FIELD_EDGE_GLOW_LEFT.get(scrollView);
setColor(ee, color);
ee = HORIZONTAL_SCROLL_VIEW_FIELD_EDGE_GLOW_RIGHT.get(scrollView);
setColor(ee, color);
} catch (Exception ex) {
XpLog.logException(scrollView, ex);
}
}
public static void setColor(
@NonNull ViewPager viewPager, @ColorInt int color, @EdgeGlowColorApi int when) {
if (Build.VERSION.SDK_INT < when) {
setColor(viewPager, color);
}
}
public static void setColor(@NonNull ViewPager viewPager, @ColorInt int color) {
try {
Object ee;
ee = VIEW_PAGER_FIELD_LEFT_EDGE.get(viewPager);
setColor(ee, color);
ee = VIEW_PAGER_FIELD_RIGHT_EDGE.get(viewPager);
setColor(ee, color);
} catch (Exception ex) {
XpLog.logException(viewPager, ex);
}
}
public static void setColor(
@NonNull NestedScrollView scrollView, @ColorInt int color, @EdgeGlowColorApi int when) {
if (Build.VERSION.SDK_INT < when) {
setColor(scrollView, color);
}
}
public static void setColor(@NonNull NestedScrollView scrollView, @ColorInt int color) {
try {
Object ee;
ee = NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_TOP.get(scrollView);
setColor(ee, color);
ee = NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(scrollView);
setColor(ee, color);
} catch (Exception ex) {
XpLog.logException(scrollView, ex);
}
}
public static void setColor(
@NonNull RecyclerView scrollView, @ColorInt int color, @EdgeGlowColorApi int when) {
if (Build.VERSION.SDK_INT < when) {
setColor(scrollView, color);
}
}
public static void setColor(@NonNull RecyclerView scrollView, @ColorInt int color) {
try {
Object ee;
ee = RECYCLER_VIEW_FIELD_EDGE_GLOW_TOP.get(scrollView);
setColor(ee, color);
ee = RECYCLER_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(scrollView);
setColor(ee, color);
ee = RECYCLER_VIEW_FIELD_EDGE_GLOW_LEFT.get(scrollView);
setColor(ee, color);
ee = RECYCLER_VIEW_FIELD_EDGE_GLOW_RIGHT.get(scrollView);
setColor(ee, color);
} catch (Exception ex) {
XpLog.logException(scrollView, ex);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void setColor(@NonNull Object edgeEffect, @ColorInt int color) {
if (edgeEffect instanceof EdgeEffectCompat) {
// EdgeEffectCompat
try {
edgeEffect = EDGE_EFFECT_COMPAT_FIELD_EDGE_EFFECT.get(edgeEffect);
} catch (IllegalAccessException e) {
return;
}
}
if (edgeEffect == null) return;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// EdgeGlow or old EdgeEffect
try {
final Drawable mEdge = (Drawable) EDGE_GLOW_FIELD_EDGE.get(edgeEffect);
final Drawable mGlow = (Drawable) EDGE_GLOW_FIELD_GLOW.get(edgeEffect);
mEdge.setColorFilter(color, PorterDuff.Mode.SRC_IN);
mGlow.setColorFilter(color, PorterDuff.Mode.SRC_IN);
mEdge.setCallback(null); // free up any references
mGlow.setCallback(null); // free up any references
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}
} else {
// EdgeEffect
((EdgeEffect) edgeEffect).setColor(color);
}
}
}