| 33 | import java.util.HashSet; |
| 34 | |
| 35 | public final class ViewInjectorImpl implements ViewInjector { |
| 36 | |
| 37 | private static final HashSet<Class<?>> IGNORED = new HashSet<Class<?>>(); |
| 38 | |
| 39 | static { |
| 40 | IGNORED.add(Object.class); |
| 41 | IGNORED.add(Activity.class); |
| 42 | IGNORED.add(android.app.Fragment.class); |
| 43 | try { |
| 44 | IGNORED.add(Class.forName("android.support.v4.app.Fragment")); |
| 45 | IGNORED.add(Class.forName("android.support.v4.app.FragmentActivity")); |
| 46 | } catch (Throwable ignored) { |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | private static final Object lock = new Object(); |
| 51 | private static volatile ViewInjectorImpl instance; |
| 52 | |
| 53 | private ViewInjectorImpl() { |
| 54 | } |
| 55 | |
| 56 | public static void registerInstance() { |
| 57 | if (instance == null) { |
| 58 | synchronized (lock) { |
| 59 | if (instance == null) { |
| 60 | instance = new ViewInjectorImpl(); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | x.Ext.setViewInjector(instance); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void inject(View view) { |
| 69 | injectObject(view, view.getClass(), new ViewFinder(view)); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void inject(Activity activity) { |
| 74 | //获取Activity的ContentView的注解 |
| 75 | Class<?> handlerType = activity.getClass(); |
| 76 | try { |
| 77 | ContentView contentView = findContentView(handlerType); |
| 78 | if (contentView != null) { |
| 79 | int viewId = contentView.value(); |
| 80 | if (viewId > 0) { |
| 81 | activity.setContentView(viewId); |
| 82 | } |
| 83 | } |
| 84 | } catch (Throwable ex) { |
| 85 | LogUtil.e(ex.getMessage(), ex); |
| 86 | } |
| 87 | |
| 88 | injectObject(activity, handlerType, new ViewFinder(activity)); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void inject(Object handler, View view) { |