add some ui attributes, remove node-android, update rhino to 1.8preview

This commit is contained in:
hyb1996
2017-05-15 16:18:42 +08:00
parent a8a83dd2b5
commit 8ac9a518d7
26 changed files with 373 additions and 221 deletions

View File

@@ -1,20 +1,71 @@
module.exports = function(__runtime__, scope){
var ui = Object(__runtime__.ui);
ui.layout = function(xml){
view = ui.inflate(activity, xml);
ui.setView(view);
ui.setContentView(view);
}
ui.setView = function(view){
ui.setContentView = function(view){
ui.view = view;
activity.setContentView(view);
}
ui.id = function(id){
return ui.view.getChildAt(0).id(id);
var v = ui.view.getChildAt(0).id(id);
if(v){
v = decorate(v);
}
return v;
}
return ui;
ui.run = function(action){
activity.runOnUiThread(action);
}
ui.nonUi = function(action){
ui.runOnNonUiThread(action);
}
ui.postDelay = function(action, delay){
__runtime__.getUiHandler().postDelay(action, delay);
}
function decorate(view){
var view = Object.create(view);
view._id = function(id){
return ui.findViewByStringId(view, id);
}
view.click = function(listener){
if(listener){
view.setOnClickListener(new android.view.View.OnClickListener(listener));
}else{
view.performClick();
}
}
view.longClick = function(listener){
if(listener){
view.setOnLongClickListener(new android.view.View.OnLongClickListener(listener));
}else{
view.performLongClick();
}
}
return view;
}
return new JavaAdapter(org.mozilla.javascript.NativeObject, {
put: function(name, start, value) {
ui[name] = value;
},
get: function(name, start) {
if(!ui[name]){
var widget = ui.id(name);
if(widget){
ui[name] = widget;
return widget;
}
}
return ui[name];
}
});
}

View File

@@ -1,61 +0,0 @@
package com.stardust.autojs.engine;
import com.iwebpp.SimpleDebug;
import com.iwebpp.node.NodeContext;
import com.iwebpp.node.http.IncomingMessage;
import com.stardust.autojs.runtime.ScriptStopException;
import com.stardust.autojs.script.ScriptSource;
import com.stardust.pio.PFile;
import com.stardust.pio.UncheckedIOException;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import java.io.IOException;
/**
* Created by Stardust on 2017/4/2.
*/
public class NodeJsJavaScriptEngine extends RhinoJavaScriptEngine {
static {
SimpleDebug.setDebugLevel(SimpleDebug.DebugLevel.DEBUG);
}
private static String initScript;
private NodeContext mNodeContext = new NodeContext();
public NodeJsJavaScriptEngine(RhinoJavaScriptEngineManager engineManager) {
super(engineManager);
}
@Override
public void init() {
super.init();
put("NodeCurrentContext", mNodeContext);
getContext().evaluateString(getScriptable(), getNodeJsInitScript(), "<node_js_init>", 1, null);
}
@Override
public Object execute(ScriptSource source) {
Object result = super.execute(source);
try {
mNodeContext.execute();
} catch (Throwable throwable) {
throw new ScriptStopException(throwable);
}
return result;
}
private String getNodeJsInitScript() {
if (initScript == null) {
try {
initScript = PFile.read(getEngineManager().getContext().getAssets().open("nodejs_engine_init.js"));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
return initScript;
}
}

View File

@@ -1,24 +0,0 @@
package com.stardust.autojs.engine;
import android.content.Context;
import com.stardust.autojs.runtime.ScriptRuntime;
/**
* Created by Stardust on 2017/3/25.
*/
public class NodeJsJavaScriptEngineManager extends RhinoJavaScriptEngineManager {
public NodeJsJavaScriptEngineManager(Context context) {
super(context);
}
@Override
protected NodeJsJavaScriptEngine createEngineInner() {
NodeJsJavaScriptEngine engine = new NodeJsJavaScriptEngine(this);
return engine;
}
}

View File

@@ -18,6 +18,7 @@ import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.commonjs.module.RequireBuilder;
import org.mozilla.javascript.commonjs.module.provider.SoftCachingModuleScriptProvider;
import org.mozilla.javascript.xmlimpl.XMLLibImpl;
import java.io.File;
import java.io.IOException;
@@ -28,6 +29,8 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.xml.parsers.DocumentBuilderFactory;
/**
* Created by Stardust on 2017/4/2.
*/

View File

@@ -4,7 +4,9 @@ import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.stardust.autojs.R;
import com.stardust.autojs.ScriptEngineService;
import com.stardust.autojs.engine.ScriptEngine;
import com.stardust.autojs.runtime.ScriptRuntime;
@@ -14,7 +16,7 @@ import com.stardust.autojs.script.ScriptSource;
* Created by Stardust on 2017/2/5.
*/
public class ScriptExecuteActivity extends Activity {
public class ScriptExecuteActivity extends AppCompatActivity {
private static ActivityScriptExecution execution;

View File

@@ -24,7 +24,7 @@ public class AndroidContextFactory extends ShellContextFactory {
*/
public AndroidContextFactory(File cacheDirectory) {
this.cacheDirectory = cacheDirectory;
initApplicationClassLoader(createClassLoader(com.faendir.rhino_android.AndroidContextFactory.class.getClassLoader()));
initApplicationClassLoader(createClassLoader(AndroidContextFactory.class.getClassLoader()));
}
/**

View File

@@ -1,18 +1,19 @@
package com.stardust.autojs.runtime.api.ui;
import android.content.Context;
import android.util.Xml;
import android.view.LayoutInflater;
import android.text.InputType;
import android.view.View;
import android.widget.FrameLayout;
import android.view.ViewGroup;
import android.widget.TextView;
import com.nickandjerry.dynamiclayoutinflator.lib.DynamicLayoutInflator;
import com.stardust.autojs.runtime.api.ui.widget.JsFrameLayout;
import com.stardust.autojs.runtime.api.ui.xml.XmlConverter;
import com.stardust.util.MapEntries;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.StringReader;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Stardust on 2017/5/14.
@@ -20,6 +21,38 @@ import java.io.StringReader;
public class ConvertLayoutInflater implements JsLayoutInflater {
static {
DynamicLayoutInflator.createViewRunnables();
DynamicLayoutInflator.viewRunnables.put("inputType", new DynamicLayoutInflator.ViewParamRunnable() {
private final Map<String, Integer> mInputTypes = new MapEntries<String, Integer>()
.entry("text", InputType.TYPE_CLASS_TEXT)
.entry("number", InputType.TYPE_CLASS_NUMBER)
.entry("phone", InputType.TYPE_CLASS_PHONE)
.entry("datetime", InputType.TYPE_CLASS_DATETIME)
.entry("password", InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT)
.entry("email", InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS | InputType.TYPE_CLASS_TEXT)
.entry("date", InputType.TYPE_DATETIME_VARIATION_DATE | InputType.TYPE_CLASS_DATETIME)
.entry("time", InputType.TYPE_DATETIME_VARIATION_TIME| InputType.TYPE_CLASS_DATETIME)
.map();
@Override
public void apply(View view, String value, ViewGroup parent, Map<String, String> attrs) {
if (!(view instanceof TextView))
return;
TextView textView = (TextView) view;
String[] types = value.split("[|]");
int inputType = 0;
for (String type : types) {
if (mInputTypes.containsKey(type)) {
inputType |= mInputTypes.get(type);
}
}
if (inputType != 0)
textView.setInputType(inputType);
}
});
}
@Override
public View inflate(Context context, String xml) {

View File

@@ -10,4 +10,5 @@ import android.view.View;
public interface JsLayoutInflater {
View inflate(Context context, String xml);
}

View File

@@ -1,72 +1,34 @@
package com.stardust.autojs.runtime.api.ui;
import android.content.res.Resources;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import com.nickandjerry.dynamiclayoutinflator.lib.DynamicLayoutInflator;
/**
* Created by Stardust on 2017/5/14.
*/
public class JsViewHelper {
private View mView;
public JsViewHelper(View view) {
}
public void w(String width) {
float w = toPixel(width);
}
public static float toPixel(String dimen) {
if (dimen.equals("*")) {
return ViewGroup.LayoutParams.MATCH_PARENT;
@Nullable
public static View findViewByStringId(View view, String id) {
View result = DynamicLayoutInflator.findViewByIdString(view, id);
if (result != null)
return result;
if (!(view instanceof ViewGroup)) {
return null;
}
if (dimen.equals("auto")) {
return ViewGroup.LayoutParams.WRAP_CONTENT;
ViewGroup group = (ViewGroup) view;
for (int i = 0; i < group.getChildCount(); i++) {
result = findViewByStringId(group.getChildAt(i), id);
if (result != null)
return result;
}
if (Character.isDigit(dimen.charAt(dimen.length() - 1))) {
return Float.parseFloat(dimen);
}
float f = Float.parseFloat(dimen.substring(0, dimen.length() - 2));
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
if (dimen.endsWith("dp")) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, f, metrics);
}
if (dimen.endsWith("sp")) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, f, metrics);
}
throw new IllegalArgumentException(dimen);
return null;
}
public void h(String height) {
}
public void gravity(String gravity) {
}
public void align(String alignment) {
}
public void padding(String padding) {
}
public void margin(String margin) {
}
public void bg(String background) {
}
}

View File

@@ -4,6 +4,10 @@ import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by Stardust on 2017/5/14.
*/
@@ -12,6 +16,7 @@ public class UI {
private Context mContext;
private JsLayoutInflater mJsLayoutInflater;
private ExecutorService mExecutorService;
public UI(Context context, JsLayoutInflater layoutInflater) {
mContext = context;
@@ -30,4 +35,15 @@ public class UI {
public View inflate(Context context, String xml) {
return mJsLayoutInflater.inflate(context, xml);
}
public void runOnNonUiThread(Runnable action) {
if (mExecutorService == null) {
mExecutorService = Executors.newSingleThreadExecutor();
}
mExecutorService.submit(action);
}
public View findViewByStringId(View view, String id) {
return JsViewHelper.findViewByStringId(view, id);
}
}

View File

@@ -0,0 +1,34 @@
package com.stardust.autojs.runtime.api.ui.widget;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.Button;
/**
* Created by Stardust on 2017/5/15.
*/
public class JsButton extends Button {
public JsButton(Context context) {
super(context);
}
public JsButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public JsButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public JsButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public String text() {
return getText().toString();
}
}

View File

@@ -0,0 +1,27 @@
package com.stardust.autojs.runtime.api.ui.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;
/**
* Created by Stardust on 2017/5/15.
*/
public class JsEditText extends android.support.v7.widget.AppCompatEditText {
public JsEditText(Context context) {
super(context);
}
public JsEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public JsEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public String text() {
return getText().toString();
}
}

View File

@@ -12,6 +12,7 @@ import android.view.View;
import android.widget.FrameLayout;
import com.nickandjerry.dynamiclayoutinflator.lib.DynamicLayoutInflator;
import com.stardust.autojs.runtime.api.ui.JsViewHelper;
/**
* Created by Stardust on 2017/5/14.
@@ -36,6 +37,6 @@ public class JsFrameLayout extends FrameLayout {
}
public View id(String id) {
return DynamicLayoutInflator.findViewByIdString(this, id);
return JsViewHelper.findViewByStringId(this, id);
}
}

View File

@@ -9,6 +9,7 @@ import android.view.View;
import android.widget.LinearLayout;
import com.nickandjerry.dynamiclayoutinflator.lib.DynamicLayoutInflator;
import com.stardust.autojs.runtime.api.ui.JsViewHelper;
/**
* Created by Stardust on 2017/5/14.
@@ -34,7 +35,4 @@ public class JsLinearLayout extends LinearLayout {
super(context, attrs, defStyleAttr, defStyleRes);
}
public View id(String id) {
return DynamicLayoutInflator.findViewByIdString(this, id);
}
}

View File

@@ -8,6 +8,7 @@ import android.view.View;
import android.widget.RelativeLayout;
import com.nickandjerry.dynamiclayoutinflator.lib.DynamicLayoutInflator;
import com.stardust.autojs.runtime.api.ui.JsViewHelper;
/**
* Created by Stardust on 2017/5/14.
@@ -32,6 +33,6 @@ public class JsRelativeLayout extends RelativeLayout {
}
public View id(String id) {
return DynamicLayoutInflator.findViewByIdString(this, id);
return JsViewHelper.findViewByStringId(this, id);
}
}

View File

@@ -0,0 +1,35 @@
package com.stardust.autojs.runtime.api.ui.widget;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by Stardust on 2017/5/15.
*/
public class JsTextView extends TextView {
public JsTextView(Context context) {
super(context);
}
public JsTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public JsTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public JsTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public String text() {
return getText().toString();
}
}

View File

@@ -1,15 +1,9 @@
package com.stardust.autojs.runtime.api.ui;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.ViewGroup;
package com.stardust.autojs.runtime.api.ui.xml;
import org.w3c.dom.Node;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
/**
* Created by Stardust on 2017/5/14.
@@ -150,7 +144,7 @@ public interface AttributeHandler {
@Override
public boolean handle(String nodeName, Node attr, StringBuilder layoutXml) {
String[] intervals = attr.getNodeName().split("[ ,]");
String[] intervals = attr.getNodeValue().split("[ ,]");
String[] dimens = new String[intervals.length];
for (int i = 0; i < intervals.length; i++) {
dimens[i] = DimenHandler.convertToAndroidDimen(intervals[i]);

View File

@@ -0,0 +1,91 @@
package com.stardust.autojs.runtime.api.ui.xml;
import org.w3c.dom.Node;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Stardust on 2017/5/15.
*/
public interface NodeHandler {
String handleNode(Node node, StringBuilder layoutXml);
String handleNode(Node node, String namespace, StringBuilder layoutXml);
abstract class Adapter implements NodeHandler {
@Override
public String handleNode(Node node, String namespace, StringBuilder layoutXml) {
String name = handleNode(node, layoutXml);
layoutXml.append(namespace);
return name;
}
}
class NameRouter extends Adapter {
private Map<String, NodeHandler> mNodeHandlerMap = new HashMap<>();
private NodeHandler mDefaultHandler;
@Override
public String handleNode(Node node, StringBuilder layoutXml) {
NodeHandler handler = mNodeHandlerMap.get(node.getNodeName());
if (handler != null) {
return handler.handleNode(node, layoutXml);
}
if (mDefaultHandler == null)
return null;
return mDefaultHandler.handleNode(node, layoutXml);
}
public NameRouter defaultHandler(NodeHandler defaultHandler) {
mDefaultHandler = defaultHandler;
return this;
}
public NameRouter handler(String name, NodeHandler handler) {
mNodeHandlerMap.put(name, handler);
return this;
}
}
class MapNameHandler extends Adapter {
private Map<String, String> mNameMap = new HashMap<>();
@Override
public String handleNode(Node node, StringBuilder layoutXml) {
String name = mNameMap.get(node.getNodeName());
if (name == null) {
name = node.getNodeName();
}
layoutXml.append("<").append(name).append("\n");
return name;
}
public MapNameHandler map(String oldName, String newName) {
mNameMap.put(oldName, newName);
return this;
}
}
class VerticalHandler extends Adapter {
private String mLinearLayoutClassName;
public VerticalHandler(String linearLayoutClassName) {
mLinearLayoutClassName = linearLayoutClassName;
}
@Override
public String handleNode(Node node, StringBuilder layoutXml) {
layoutXml.append("<").append(mLinearLayoutClassName)
.append("\nandroid:orientation=\"vertical\"\n");
return mLinearLayoutClassName;
}
}
}

View File

@@ -1,9 +1,7 @@
package com.stardust.autojs.runtime.api.ui;
package com.stardust.autojs.runtime.api.ui.xml;
import com.stardust.util.MapEntries;
import static com.stardust.autojs.runtime.api.ui.AttributeHandler.*;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
@@ -25,34 +23,35 @@ import javax.xml.parsers.ParserConfigurationException;
public class XmlConverter {
private static final Map<String, String> NODE_NAME_MAP = new MapEntries<String, String>()
.entry("frame", "com.stardust.autojs.runtime.api.ui.widget.JsFrameLayout")
.entry("linear", "com.stardust.autojs.runtime.api.ui.widget.JsLinearLayout")
.entry("relative", "com.stardust.autojs.runtime.api.ui.widget.JsRelativeLayout")
.entry("button", "Button")
.entry("text", "TextView")
.entry("input", "EditText")
.entry("image", "ImageView")
.map();
private static final NodeHandler NODE_HANDLER = new NodeHandler.NameRouter()
.handler("vertical", new NodeHandler.VerticalHandler("com.stardust.autojs.runtime.api.ui.widget.JsLinearLayout"))
.defaultHandler(new NodeHandler.MapNameHandler()
.map("frame", "com.stardust.autojs.runtime.api.ui.widget.JsFrameLayout")
.map("linear", "com.stardust.autojs.runtime.api.ui.widget.JsLinearLayout")
.map("relative", "com.stardust.autojs.runtime.api.ui.widget.JsRelativeLayout")
.map("button", "com.stardust.autojs.runtime.api.ui.widget.JsButton")
.map("text", "com.stardust.autojs.runtime.api.ui.widget.JsTextView")
.map("input", "com.stardust.autojs.runtime.api.ui.widget.JsEditText")
.map("image", "ImageView")
);
private static final AttributeHandler ATTRIBUTE_HANDLER = new AttributeHandler.AttrNameRouter()
.handler("w", new DimenHandler("width"))
.handler("h", new DimenHandler("height"))
.handler("size", new DimenHandler("textSize"))
.handler("id", new IdHandler())
.handler("vertical", new OrientationHandler())
.handler("margin", new MarginPaddingHandler("layout_margin"))
.handler("padding", new MarginPaddingHandler("padding"))
.handler("marginLeft", new DimenHandler("layout_marginLeft"))
.handler("marginRight", new DimenHandler("layout_marginRight"))
.handler("marginTop", new DimenHandler("layout_marginTop"))
.handler("marginBottom", new DimenHandler("layout_marginBottom"))
.handler("paddingLeft", new DimenHandler("paddingLeft"))
.handler("paddingRight", new DimenHandler("paddingRight"))
.handler("paddingTop", new DimenHandler("paddingTop"))
.handler("paddingBottom", new DimenHandler("paddingBottom"))
.defaultHandler(new MappedAttributeHandler()
.handler("w", new AttributeHandler.DimenHandler("width"))
.handler("h", new AttributeHandler.DimenHandler("height"))
.handler("size", new AttributeHandler.DimenHandler("textSize"))
.handler("id", new AttributeHandler.IdHandler())
.handler("vertical", new AttributeHandler.OrientationHandler())
.handler("margin", new AttributeHandler.MarginPaddingHandler("layout_margin"))
.handler("padding", new AttributeHandler.MarginPaddingHandler("padding"))
.handler("marginLeft", new AttributeHandler.DimenHandler("layout_marginLeft"))
.handler("marginRight", new AttributeHandler.DimenHandler("layout_marginRight"))
.handler("marginTop", new AttributeHandler.DimenHandler("layout_marginTop"))
.handler("marginBottom", new AttributeHandler.DimenHandler("layout_marginBottom"))
.handler("paddingLeft", new AttributeHandler.DimenHandler("paddingLeft"))
.handler("paddingRight", new AttributeHandler.DimenHandler("paddingRight"))
.handler("paddingTop", new AttributeHandler.DimenHandler("paddingTop"))
.handler("paddingBottom", new AttributeHandler.DimenHandler("paddingBottom"))
.defaultHandler(new AttributeHandler.MappedAttributeHandler()
.mapName("align", "layout_gravity")
.mapName("bg", "background")
.mapName("color", "textColor")
@@ -73,8 +72,7 @@ public class XmlConverter {
private static void handleNode(Node node, String namespace, StringBuilder layoutXml) {
String nodeName = node.getNodeName();
String mappedNodeName = mapNodeName(nodeName);
layoutXml.append("<").append(mappedNodeName).append(" ").append(namespace).append("\n");
String mappedNodeName = NODE_HANDLER.handleNode(node, namespace, layoutXml);
handleText(nodeName, node.getTextContent(), layoutXml);
handleAttributes(nodeName, node.getAttributes(), layoutXml);
layoutXml.append(">\n");
@@ -117,10 +115,5 @@ public class XmlConverter {
ATTRIBUTE_HANDLER.handle(nodeName, attr, layoutXml);
}
private static String mapNodeName(String nodeName) {
String str = NODE_NAME_MAP.get(nodeName);
return str == null ? nodeName : str;
}
}

View File

@@ -1,16 +1,5 @@
package com.stardust.autojs;
import com.stardust.autojs.runtime.api.ui.XmlConverter;
import org.junit.Test;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.ParserConfigurationException;
/**
* Example local unit test, which will execute on the development machine (host).
*

View File

@@ -1,5 +1,7 @@
package com.stardust.autojs.runtime.api.ui;
import com.stardust.autojs.runtime.api.ui.xml.XmlConverter;
import org.junit.Test;
import org.xml.sax.SAXException;