Add: tasker plugin support, injectable webview

This commit is contained in:
hyb1996
2017-04-02 12:41:00 +08:00
parent 121949f9c0
commit e7500e6e21
127 changed files with 5293 additions and 1667 deletions

View File

@@ -9,83 +9,114 @@ import java.lang.reflect.Method;
/**
* Created by Stardust on 2017/1/30.
*
* <p>
* 哈你说我为什么不用AA框架之前还不知道嘛所以现在用了。
*/
public class ViewBinder {
public static void bind(Object o) {
Method findViewById;
public interface ViewSupplier {
View findViewById(int id);
}
public static void bind(final Object o) {
final Method findViewById;
try {
findViewById = o.getClass().getMethod("findViewById", int.class);
findViewById.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new RuntimeException("You must implement findViewById to use view binding", e);
}
bind(o, new ViewSupplier() {
@Override
public View findViewById(int id) {
try {
return (View) findViewById.invoke(o, id);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
});
}
public static void bind(Object o, final View view) {
bind(o, new ViewSupplier() {
@Override
public View findViewById(int id) {
return view.findViewById(id);
}
});
}
public static void bind(Object o, ViewSupplier viewSupplier) {
Method[] methods = o.getClass().getDeclaredMethods();
bindId(o, findViewById);
bindId(o, viewSupplier);
for (Method method : methods) {
method.setAccessible(true);
bindClick(o, method, findViewById);
bindCheck(o, method, findViewById);
bindClick(o, method, viewSupplier);
bindCheck(o, method, viewSupplier);
}
}
private static void bindId(Object o, Method findViewById) {
public static void bindId(Object o, final View v) {
bindId(o, new ViewSupplier() {
@Override
public View findViewById(int id) {
return v.findViewById(id);
}
});
}
private static void bindId(Object o, ViewSupplier viewSupplier) {
for (Field field : o.getClass().getDeclaredFields()) {
field.setAccessible(true);
ViewBinding.Id id = field.getAnnotation(ViewBinding.Id.class);
if (id == null || id.value() == 0)
continue;
try {
field.set(o, findViewById.invoke(o, id.value()));
} catch (IllegalAccessException | InvocationTargetException e) {
field.set(o, viewSupplier.findViewById(id.value()));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
private static void bindCheck(final Object o, final Method method, Method findViewById) {
private static void bindCheck(final Object o, final Method method, ViewSupplier viewSupplier) {
ViewBinding.Check annotation = method.getAnnotation(ViewBinding.Check.class);
if (annotation == null || annotation.value() == 0)
return;
int id = annotation.value();
try {
CompoundButton button = (CompoundButton) findViewById.invoke(o, id);
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
method.invoke(o, isChecked);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
CompoundButton button = (CompoundButton) viewSupplier.findViewById(id);
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
method.invoke(o, isChecked);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
});
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
});
}
private static void bindClick(final Object o, final Method method, Method findViewById) {
private static void bindClick(final Object o, final Method method, ViewSupplier viewSupplier) {
ViewBinding.Click annotation = method.getAnnotation(ViewBinding.Click.class);
if (annotation == null || annotation.value() == 0)
return;
int id = annotation.value();
try {
View view = (View) findViewById.invoke(o, id);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
invokeMethod(o, method);
}
});
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
View view = viewSupplier.findViewById(id);
if (view == null)
return;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
invokeMethod(o, method);
}
});
}
private static void invokeMethod(Object o, Method method) {