-
Notifications
You must be signed in to change notification settings - Fork 230
/
ViewTraversalsCompact.java
44 lines (32 loc) · 1.2 KB
/
ViewTraversalsCompact.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
package com.wanjian.sak.system.traversals;
import android.view.View;
import android.view.ViewRootImpl;
import com.wanjian.sak.unsafe.UnsafeProxy;
import java.lang.reflect.Field;
public class ViewTraversalsCompact {
static Field sTraversalRunnableF;
static {
try {
sTraversalRunnableF = ViewRootImpl.class.getDeclaredField("mTraversalRunnable");
sTraversalRunnableF.setAccessible(true);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void register(ViewRootImpl viewRootImpl, View view, ViewTraversalsListener listener) {
Runnable originTraversal = null;
try {
originTraversal = (Runnable) sTraversalRunnableF.get(viewRootImpl);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
if (originTraversal instanceof MyTraversalRunnable) {
((MyTraversalRunnable) originTraversal).addTraversalsListener(listener);
return;
}
MyTraversalRunnable myTraversal = new MyTraversalRunnable(originTraversal, view);
myTraversal.addTraversalsListener(listener);
long offset = UnsafeProxy.objectFieldOffset(sTraversalRunnableF);
UnsafeProxy.putObject(viewRootImpl, offset, myTraversal);
}
}