save works
This commit is contained in:
@@ -1,98 +1,20 @@
|
||||
package com.stardust.autojs.core.ui.widget;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.CallSuper;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewParent;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import com.stardust.autojs.core.eventloop.EventEmitter;
|
||||
import com.stardust.autojs.core.ui.JsEvent;
|
||||
import com.stardust.autojs.core.ui.inflater.util.Dimensions;
|
||||
import com.stardust.autojs.core.ui.inflater.util.Drawables;
|
||||
import com.stardust.autojs.core.ui.inflater.util.Gravities;
|
||||
import com.stardust.autojs.core.ui.inflater.util.Ids;
|
||||
import com.stardust.autojs.core.ui.xview.ViewAttributes;
|
||||
import com.stardust.autojs.rhino.NativeJavaObjectWithPrototype;
|
||||
|
||||
import org.mozilla.javascript.NativeJavaObject;
|
||||
import org.mozilla.javascript.ScriptRuntime;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
import org.mozilla.javascript.Undefined;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class NativeView extends NativeJavaObjectWithPrototype {
|
||||
|
||||
protected interface Attribute {
|
||||
String get();
|
||||
|
||||
void set(String value);
|
||||
}
|
||||
|
||||
|
||||
protected class BaseAttribute implements Attribute {
|
||||
|
||||
private String mValue;
|
||||
private final AttributeSetter mAttributeSetter;
|
||||
|
||||
public BaseAttribute(AttributeSetter attributeSetter) {
|
||||
mAttributeSetter = attributeSetter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get() {
|
||||
return mValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String value) {
|
||||
mValue = value;
|
||||
mAttributeSetter.set(value);
|
||||
}
|
||||
}
|
||||
|
||||
protected interface AttributeGetter {
|
||||
String get();
|
||||
}
|
||||
|
||||
|
||||
protected interface AttributeSetter {
|
||||
void set(String value);
|
||||
}
|
||||
|
||||
protected interface ValueConverter<T> {
|
||||
T convert(String value);
|
||||
}
|
||||
|
||||
protected interface ValueApplier<T> {
|
||||
void apply(T value);
|
||||
}
|
||||
|
||||
protected static class MappingAttributeSetter<T> implements AttributeSetter {
|
||||
|
||||
private final ValueConverter<T> mValueConverter;
|
||||
private final ValueApplier<T> mValueApplier;
|
||||
|
||||
public MappingAttributeSetter(ValueConverter<T> valueConverter, ValueApplier<T> valueApplier) {
|
||||
mValueConverter = valueConverter;
|
||||
mValueApplier = valueApplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String value) {
|
||||
mValueApplier.apply(mValueConverter.convert(value));
|
||||
}
|
||||
}
|
||||
|
||||
public static class ScrollEvent {
|
||||
public int scrollX;
|
||||
public int scrollY;
|
||||
@@ -108,17 +30,14 @@ public class NativeView extends NativeJavaObjectWithPrototype {
|
||||
}
|
||||
|
||||
|
||||
private Map<String, Attribute> mAttributes = new HashMap<>();
|
||||
private final com.stardust.autojs.runtime.ScriptRuntime mRuntime;
|
||||
private final Drawables mDrawables;
|
||||
private final ViewAttributes mViewAttributes;
|
||||
private final View mView;
|
||||
private final EventEmitter mEventEmitter;
|
||||
private final NativeJavaObject mNativeEventEmitter;
|
||||
|
||||
public NativeView(Scriptable scope, View javaObject, Class<?> staticType, com.stardust.autojs.runtime.ScriptRuntime runtime) {
|
||||
super(scope, javaObject, staticType);
|
||||
mRuntime = runtime;
|
||||
mDrawables = mRuntime.ui.getResourceParser().getDrawables();
|
||||
mViewAttributes = ViewAttributes.fromView(runtime.ui.getResourceParser(), javaObject);
|
||||
mEventEmitter = runtime.events.emitter();
|
||||
mView = javaObject;
|
||||
mNativeEventEmitter = new NativeJavaObject(scope, mEventEmitter, mEventEmitter.getClass());
|
||||
@@ -128,7 +47,6 @@ public class NativeView extends NativeJavaObjectWithPrototype {
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
private void init() {
|
||||
onRegisterAttrs();
|
||||
mView.setOnTouchListener((v, event) -> {
|
||||
JsEvent e = new JsEvent(getParentScope(), event, event.getClass());
|
||||
emit("touch", e, v);
|
||||
@@ -151,34 +69,10 @@ public class NativeView extends NativeJavaObjectWithPrototype {
|
||||
}
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
protected void onRegisterAttrs() {
|
||||
registerAttr("id", Ids::parse, mView::setId);
|
||||
registerAttr("gravity", Gravities::parse, this::setGravity);
|
||||
registerAttrs(new String[]{"width", "layout_width", "w"}, this::parseDimension, this::setWidth);
|
||||
registerAttrs(new String[]{"height", "layout_height", "h"}, this::parseDimension, this::setHeight);
|
||||
registerDrawableAttrs(new String[]{"bg", "background"}, mView::setBackground);
|
||||
registerAttr("layout_gravity", Gravities::parse, this::setLayoutGravity);
|
||||
registerAttr("layout_weight", Float::parseFloat, this::setLayoutWeight);
|
||||
}
|
||||
|
||||
|
||||
private int parseDimension(String dim) {
|
||||
switch (dim) {
|
||||
case "wrap_content":
|
||||
return ViewGroup.LayoutParams.WRAP_CONTENT;
|
||||
case "fill_parent":
|
||||
case "match_parent":
|
||||
return ViewGroup.LayoutParams.MATCH_PARENT;
|
||||
default:
|
||||
return Dimensions.parseToPixel(dim, mView.getResources().getDisplayMetrics(), (ViewGroup) mView.getParent(), true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean has(String name, Scriptable start) {
|
||||
if (mAttributes.containsKey(name)) {
|
||||
if (mViewAttributes.contains(name)) {
|
||||
return true;
|
||||
}
|
||||
return super.has(name, start) || mNativeEventEmitter.has(name, start);
|
||||
@@ -186,7 +80,7 @@ public class NativeView extends NativeJavaObjectWithPrototype {
|
||||
|
||||
@Override
|
||||
public void put(String name, Scriptable start, Object value) {
|
||||
Attribute attribute = mAttributes.get(name);
|
||||
ViewAttributes.Attribute attribute = mViewAttributes.get(name);
|
||||
if (attribute != null) {
|
||||
attribute.set(ScriptRuntime.toString(value));
|
||||
return;
|
||||
@@ -200,127 +94,17 @@ public class NativeView extends NativeJavaObjectWithPrototype {
|
||||
|
||||
@Override
|
||||
public Object get(String name, Scriptable start) {
|
||||
Attribute attribute = mAttributes.get(name);
|
||||
ViewAttributes.Attribute attribute = mViewAttributes.get(name);
|
||||
if (attribute != null) {
|
||||
return attribute.get();
|
||||
}
|
||||
Object o = mNativeEventEmitter.get(name, start);
|
||||
if(o != Scriptable.NOT_FOUND){
|
||||
if (o != Scriptable.NOT_FOUND) {
|
||||
return o;
|
||||
}
|
||||
return super.get(name, start);
|
||||
}
|
||||
|
||||
protected void registerAttr(String name, Attribute attribute) {
|
||||
mAttributes.put(name, attribute);
|
||||
}
|
||||
|
||||
protected void registerAttr(String name, AttributeGetter getter, AttributeSetter setter) {
|
||||
mAttributes.put(name, new Attribute() {
|
||||
@Override
|
||||
public String get() {
|
||||
return getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String value) {
|
||||
setter.set(value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void registerAttr(String name, AttributeSetter setter) {
|
||||
mAttributes.put(name, new BaseAttribute(setter));
|
||||
}
|
||||
|
||||
protected <T> void registerAttr(String name, ValueConverter<T> converter, ValueApplier<T> applier) {
|
||||
mAttributes.put(name, new BaseAttribute(new MappingAttributeSetter<>(converter, applier)));
|
||||
}
|
||||
|
||||
protected <T> void registerAttrs(String[] names, ValueConverter<T> converter, ValueApplier<T> applier) {
|
||||
registerAttrs(names, new MappingAttributeSetter<>(converter, applier));
|
||||
}
|
||||
|
||||
protected <T> void registerAttrs(String[] names, AttributeSetter setter) {
|
||||
registerAttrs(names, new BaseAttribute(setter));
|
||||
}
|
||||
|
||||
protected <T> void registerAttrs(String[] names, Attribute attribute) {
|
||||
for (String name : names) {
|
||||
mAttributes.put(name, attribute);
|
||||
}
|
||||
}
|
||||
|
||||
protected void registerDrawableAttr(String name, ValueApplier<Drawable> applier) {
|
||||
mAttributes.put(name, new BaseAttribute(new MappingAttributeSetter<>(
|
||||
this::parseDrawable, applier)));
|
||||
}
|
||||
|
||||
|
||||
private void registerDrawableAttrs(String[] names, ValueApplier<Drawable> applier) {
|
||||
registerAttrs(names, new BaseAttribute(new MappingAttributeSetter<>(
|
||||
this::parseDrawable, applier)));
|
||||
}
|
||||
|
||||
|
||||
protected Drawable parseDrawable(String value) {
|
||||
return mDrawables.parse(mView, value);
|
||||
}
|
||||
|
||||
private boolean setGravity(int g) {
|
||||
try {
|
||||
Method setGravity = mView.getClass().getMethod("setGravity", int.class);
|
||||
setGravity.invoke(mView, g);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void setLayoutGravity(int gravity) {
|
||||
ViewParent parent = mView.getParent();
|
||||
ViewGroup.LayoutParams layoutParams = mView.getLayoutParams();
|
||||
if (parent instanceof LinearLayout) {
|
||||
((LinearLayout.LayoutParams) layoutParams).gravity = gravity;
|
||||
mView.setLayoutParams(layoutParams);
|
||||
} else if (parent instanceof FrameLayout) {
|
||||
((FrameLayout.LayoutParams) layoutParams).gravity = gravity;
|
||||
mView.setLayoutParams(layoutParams);
|
||||
} else {
|
||||
try {
|
||||
Field field = layoutParams.getClass().getField("gravity");
|
||||
field.set(layoutParams, gravity);
|
||||
mView.setLayoutParams(layoutParams);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
//TODO throw or ?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setLayoutWeight(float weight) {
|
||||
ViewParent parent = mView.getParent();
|
||||
ViewGroup.LayoutParams layoutParams = mView.getLayoutParams();
|
||||
if (parent instanceof LinearLayout) {
|
||||
((LinearLayout.LayoutParams) layoutParams).weight = weight;
|
||||
mView.setLayoutParams(layoutParams);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void setWidth(int width) {
|
||||
ViewGroup.LayoutParams layoutParams = mView.getLayoutParams();
|
||||
layoutParams.width = width;
|
||||
mView.setLayoutParams(layoutParams);
|
||||
}
|
||||
|
||||
|
||||
private void setHeight(int height) {
|
||||
ViewGroup.LayoutParams layoutParams = mView.getLayoutParams();
|
||||
layoutParams.height = height;
|
||||
mView.setLayoutParams(layoutParams);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View unwrap() {
|
||||
|
||||
@@ -0,0 +1,261 @@
|
||||
package com.stardust.autojs.core.ui.xview;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.CallSuper;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewParent;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import com.stardust.autojs.R;
|
||||
import com.stardust.autojs.core.ui.inflater.ResourceParser;
|
||||
import com.stardust.autojs.core.ui.inflater.util.Dimensions;
|
||||
import com.stardust.autojs.core.ui.inflater.util.Drawables;
|
||||
import com.stardust.autojs.core.ui.inflater.util.Gravities;
|
||||
import com.stardust.autojs.core.ui.inflater.util.Ids;
|
||||
import com.stardust.autojs.core.ui.widget.NativeView;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ViewAttributes {
|
||||
|
||||
public interface Attribute {
|
||||
String get();
|
||||
|
||||
void set(String value);
|
||||
}
|
||||
|
||||
|
||||
protected class BaseAttribute implements Attribute {
|
||||
|
||||
private String mValue;
|
||||
private final AttributeSetter mAttributeSetter;
|
||||
|
||||
public BaseAttribute(AttributeSetter attributeSetter) {
|
||||
mAttributeSetter = attributeSetter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get() {
|
||||
return mValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String value) {
|
||||
mValue = value;
|
||||
mAttributeSetter.set(value);
|
||||
}
|
||||
}
|
||||
|
||||
protected interface AttributeGetter {
|
||||
String get();
|
||||
}
|
||||
|
||||
|
||||
protected interface AttributeSetter {
|
||||
void set(String value);
|
||||
}
|
||||
|
||||
protected interface ValueConverter<T> {
|
||||
T convert(String value);
|
||||
}
|
||||
|
||||
protected interface ValueApplier<T> {
|
||||
void apply(T value);
|
||||
}
|
||||
|
||||
protected static class MappingAttributeSetter<T> implements AttributeSetter {
|
||||
|
||||
private final ValueConverter<T> mValueConverter;
|
||||
private final ValueApplier<T> mValueApplier;
|
||||
|
||||
public MappingAttributeSetter(ValueConverter<T> valueConverter, ValueApplier<T> valueApplier) {
|
||||
mValueConverter = valueConverter;
|
||||
mValueApplier = valueApplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String value) {
|
||||
mValueApplier.apply(mValueConverter.convert(value));
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Attribute> mAttributes = new HashMap<>();
|
||||
private final Drawables mDrawables;
|
||||
private final View mView;
|
||||
|
||||
public ViewAttributes(Drawables drawables, View view) {
|
||||
mDrawables = drawables;
|
||||
mView = view;
|
||||
init();
|
||||
}
|
||||
|
||||
public static ViewAttributes fromView(ResourceParser parser, View view) {
|
||||
ViewAttributes attributes;
|
||||
Object tag = view.getTag(R.id.view_tag_view_attrs);
|
||||
if (!(tag instanceof ViewAttributes)) {
|
||||
attributes = new ViewAttributes(parser.getDrawables(), view);
|
||||
view.setTag(R.id.view_tag_view_attrs, attributes);
|
||||
} else {
|
||||
attributes = (ViewAttributes) tag;
|
||||
}
|
||||
return attributes;
|
||||
}
|
||||
|
||||
|
||||
public boolean contains(String name) {
|
||||
return mAttributes.containsKey(name);
|
||||
}
|
||||
|
||||
public Attribute get(String name) {
|
||||
return mAttributes.get(name);
|
||||
}
|
||||
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
private void init() {
|
||||
onRegisterAttrs();
|
||||
}
|
||||
|
||||
|
||||
@CallSuper
|
||||
protected void onRegisterAttrs() {
|
||||
registerAttr("id", Ids::parse, mView::setId);
|
||||
registerAttr("gravity", Gravities::parse, this::setGravity);
|
||||
registerAttrs(new String[]{"width", "layout_width", "w"}, this::parseDimension, this::setWidth);
|
||||
registerAttrs(new String[]{"height", "layout_height", "h"}, this::parseDimension, this::setHeight);
|
||||
registerDrawableAttrs(new String[]{"bg", "background"}, mView::setBackground);
|
||||
registerAttr("layout_gravity", Gravities::parse, this::setLayoutGravity);
|
||||
registerAttr("layout_weight", Float::parseFloat, this::setLayoutWeight);
|
||||
}
|
||||
|
||||
|
||||
private int parseDimension(String dim) {
|
||||
switch (dim) {
|
||||
case "wrap_content":
|
||||
return ViewGroup.LayoutParams.WRAP_CONTENT;
|
||||
case "fill_parent":
|
||||
case "match_parent":
|
||||
return ViewGroup.LayoutParams.MATCH_PARENT;
|
||||
default:
|
||||
return Dimensions.parseToPixel(dim, mView.getResources().getDisplayMetrics(), (ViewGroup) mView.getParent(), true);
|
||||
}
|
||||
}
|
||||
|
||||
protected void registerAttr(String name, Attribute attribute) {
|
||||
mAttributes.put(name, attribute);
|
||||
}
|
||||
|
||||
protected void registerAttr(String name, AttributeGetter getter, AttributeSetter setter) {
|
||||
mAttributes.put(name, new Attribute() {
|
||||
@Override
|
||||
public String get() {
|
||||
return getter.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String value) {
|
||||
setter.set(value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void registerAttr(String name, AttributeSetter setter) {
|
||||
mAttributes.put(name, new BaseAttribute(setter));
|
||||
}
|
||||
|
||||
protected <T> void registerAttr(String name, ValueConverter<T> converter, ValueApplier<T> applier) {
|
||||
mAttributes.put(name, new BaseAttribute(new MappingAttributeSetter<>(converter, applier)));
|
||||
}
|
||||
|
||||
protected <T> void registerAttrs(String[] names, ValueConverter<T> converter, ValueApplier<T> applier) {
|
||||
registerAttrs(names, new MappingAttributeSetter<>(converter, applier));
|
||||
}
|
||||
|
||||
protected <T> void registerAttrs(String[] names, AttributeSetter setter) {
|
||||
registerAttrs(names, new BaseAttribute(setter));
|
||||
}
|
||||
|
||||
protected <T> void registerAttrs(String[] names, Attribute attribute) {
|
||||
for (String name : names) {
|
||||
mAttributes.put(name, attribute);
|
||||
}
|
||||
}
|
||||
|
||||
protected void registerDrawableAttr(String name, ValueApplier<Drawable> applier) {
|
||||
mAttributes.put(name, new BaseAttribute(new MappingAttributeSetter<>(
|
||||
this::parseDrawable, applier)));
|
||||
}
|
||||
|
||||
|
||||
private void registerDrawableAttrs(String[] names, ValueApplier<Drawable> applier) {
|
||||
registerAttrs(names, new BaseAttribute(new MappingAttributeSetter<>(
|
||||
this::parseDrawable, applier)));
|
||||
}
|
||||
|
||||
|
||||
protected Drawable parseDrawable(String value) {
|
||||
return mDrawables.parse(mView, value);
|
||||
}
|
||||
|
||||
private boolean setGravity(int g) {
|
||||
try {
|
||||
Method setGravity = mView.getClass().getMethod("setGravity", int.class);
|
||||
setGravity.invoke(mView, g);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void setLayoutGravity(int gravity) {
|
||||
ViewParent parent = mView.getParent();
|
||||
ViewGroup.LayoutParams layoutParams = mView.getLayoutParams();
|
||||
if (parent instanceof LinearLayout) {
|
||||
((LinearLayout.LayoutParams) layoutParams).gravity = gravity;
|
||||
mView.setLayoutParams(layoutParams);
|
||||
} else if (parent instanceof FrameLayout) {
|
||||
((FrameLayout.LayoutParams) layoutParams).gravity = gravity;
|
||||
mView.setLayoutParams(layoutParams);
|
||||
} else {
|
||||
try {
|
||||
Field field = layoutParams.getClass().getField("gravity");
|
||||
field.set(layoutParams, gravity);
|
||||
mView.setLayoutParams(layoutParams);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
//TODO throw or ?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setLayoutWeight(float weight) {
|
||||
ViewParent parent = mView.getParent();
|
||||
ViewGroup.LayoutParams layoutParams = mView.getLayoutParams();
|
||||
if (parent instanceof LinearLayout) {
|
||||
((LinearLayout.LayoutParams) layoutParams).weight = weight;
|
||||
mView.setLayoutParams(layoutParams);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void setWidth(int width) {
|
||||
ViewGroup.LayoutParams layoutParams = mView.getLayoutParams();
|
||||
layoutParams.width = width;
|
||||
mView.setLayoutParams(layoutParams);
|
||||
}
|
||||
|
||||
|
||||
private void setHeight(int height) {
|
||||
ViewGroup.LayoutParams layoutParams = mView.getLayoutParams();
|
||||
layoutParams.height = height;
|
||||
mView.setLayoutParams(layoutParams);
|
||||
}
|
||||
|
||||
}
|
||||
4
autojs/src/main/res/values/ids.xml
Normal file
4
autojs/src/main/res/values/ids.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<item name="view_tag_view_attrs" type="id"/>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user