重构 ExecutionConfig

This commit is contained in:
hyb1996
2018-12-10 10:35:16 +08:00
parent 8faa6cc990
commit c460425656
31 changed files with 320 additions and 320 deletions

View File

@@ -21,9 +21,9 @@ module.exports = function(__runtime__, scope){
var engine = Object.create(rtEngines.myEngine());
if(!execArgv){
execArgv = {};
var iter = engine.getTag("execution.config").getArguments().entrySet().iterator();
while(iter.hasNext()){
var entry = iter.next();
var iterator = engine.getTag("execution.config").arguments.entrySet().iterator();
while(iterator.hasNext()){
var entry = iterator.next();
execArgv[entry.getKey()] = entry.getValue();
}
}
@@ -43,18 +43,11 @@ module.exports = function(__runtime__, scope){
c = c || {};
c.path = c.path || files.cwd();
if(c.path){
if(Array.isArray(c.path)){
config.requirePath(c.path);
config.executePath(c.path[0]);
}else{
config.requirePath([c.path]);
config.executePath(c.path);
}
config.workingDirectory = c.path;
}
c.delay = c.delay || 0;
c.interval = c.interval || 0;
c.loopTimes = c.loopTimes || 1;
config.loop(c.delay, c.loopTimes, c.interval);
config.delay = c.delay || 0;
config.interval = c.interval || 0;
config.loopTimes = c.loopTimes || 1;
if(c.arguments){
var arguments = c.arguments;
for(var key in arguments){

View File

@@ -160,11 +160,7 @@ public class ScriptEngineService {
} else {
r = new RunnableScriptExecution(mScriptEngineManager, task);
}
if (task.getConfig().runInNewThread) {
new ThreadCompat(r).start();
} else {
r.run();
}
new ThreadCompat(r).start();
return r;
}

View File

@@ -7,6 +7,7 @@ import com.stardust.autojs.BuildConfig;
import com.stardust.autojs.core.ui.ViewExtras;
import com.stardust.autojs.rhino.NativeJavaObjectWithPrototype;
import com.stardust.autojs.rhino.RhinoAndroidHelper;
import com.stardust.autojs.rhino.TokenStream;
import com.stardust.autojs.rhino.TopLevelScope;
import com.stardust.autojs.runtime.ScriptRuntime;
import com.stardust.autojs.script.JavaScriptSource;

View File

@@ -25,7 +25,7 @@ public interface ScriptEngine<S extends ScriptSource> {
String TAG_ENV_PATH = "env_path";
String TAG_SOURCE = "source";
String TAG_EXECUTE_PATH = "execute_path";
String TAG_WORKING_DIRECTORY = "execute_path";
void put(String name, Object value);
@@ -103,7 +103,7 @@ public interface ScriptEngine<S extends ScriptSource> {
}
public String cwd() {
return (String) getTag(TAG_EXECUTE_PATH);
return (String) getTag(TAG_WORKING_DIRECTORY);
}
public void setOnDestroyListener(OnDestroyListener onDestroyListener) {

View File

@@ -1,84 +0,0 @@
package com.stardust.autojs.execution;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Stardust on 2017/2/1.
*/
public class ExecutionConfig implements Serializable {
public static final String TAG = "execution.config";
private List<String> mRequirePath = Collections.emptyList();
private String mExecutePath;
public long delay = 0;
public long interval = 0;
public int loopTimes = 1;
private Map<String, Object> mArguments = new HashMap<>();
private int mIntentFlags = 0;
public static ExecutionConfig getDefault() {
return new ExecutionConfig();
}
public boolean runInNewThread = true;
public ExecutionConfig runInNewThread(boolean runInNewThread) {
this.runInNewThread = runInNewThread;
return this;
}
public int getIntentFlags() {
return mIntentFlags;
}
public ExecutionConfig setIntentFlags(int intentFlags) {
mIntentFlags = intentFlags;
return this;
}
public ExecutionConfig requirePath(String... requirePath) {
mRequirePath = new ArrayList<>(Arrays.asList(requirePath));
if (mExecutePath != null) {
mRequirePath.add(mExecutePath);
}
return this;
}
public ExecutionConfig executePath(String executePath) {
mExecutePath = executePath;
return this;
}
public List<String> getRequirePath() {
return mRequirePath;
}
public String getExecutePath() {
return mExecutePath;
}
public void setArgument(String key, Object object){
mArguments.put(key, object);
}
public Object getArgument(String key){
return mArguments.get(key);
}
public Map<String, Object> getArguments() {
return mArguments;
}
public ExecutionConfig loop(long delay, int loopTimes, long interval) {
this.delay = delay;
this.loopTimes = loopTimes;
this.interval = interval;
return this;
}
}

View File

@@ -0,0 +1,97 @@
package com.stardust.autojs.execution
import android.os.Parcel
import android.os.Parcelable
import java.util.*
/**
* Created by Stardust on 2017/2/1.
*/
data class ExecutionConfig(var workingDirectory: String = "",
var path: Array<out String> = emptyArray(),
var intentFlags: Int = 0,
var delay: Long = 0,
var interval: Long = 0,
var loopTimes: Int = 1) : Parcelable {
private val mArguments = HashMap<String, Any>()
val arguments: Map<String, Any>
get() = mArguments
constructor(parcel: Parcel) : this(
parcel.readString().orEmpty(),
parcel.createStringArray().orEmpty(),
parcel.readInt(),
parcel.readLong(),
parcel.readLong(),
parcel.readInt())
fun setArgument(key: String, `object`: Any) {
mArguments[key] = `object`
}
fun getArgument(key: String): Any? {
return mArguments[key]
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as ExecutionConfig
if (workingDirectory != other.workingDirectory) return false
if (!path.contentEquals(other.path)) return false
if (intentFlags != other.intentFlags) return false
if (delay != other.delay) return false
if (interval != other.interval) return false
if (loopTimes != other.loopTimes) return false
if (mArguments != other.mArguments) return false
return true
}
override fun hashCode(): Int {
var result = workingDirectory.hashCode()
result = 31 * result + path.contentHashCode()
result = 31 * result + intentFlags
result = 31 * result + delay.hashCode()
result = 31 * result + interval.hashCode()
result = 31 * result + loopTimes
result = 31 * result + mArguments.hashCode()
return result
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(workingDirectory)
parcel.writeStringArray(path)
parcel.writeInt(intentFlags)
parcel.writeLong(delay)
parcel.writeLong(interval)
parcel.writeInt(loopTimes)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<ExecutionConfig> {
@JvmStatic
val tag = "execution.config"
@JvmStatic
val default: ExecutionConfig
get() = ExecutionConfig()
override fun createFromParcel(parcel: Parcel): ExecutionConfig {
return ExecutionConfig(parcel)
}
override fun newArray(size: Int): Array<ExecutionConfig?> {
return arrayOfNulls(size)
}
}
}

View File

@@ -20,12 +20,12 @@ public class LoopedBasedJavaScriptExecution extends RunnableScriptExecution {
protected Object doExecution(final ScriptEngine engine) {
engine.setTag(ScriptEngine.TAG_SOURCE, getSource());
getListener().onStart(this);
long delay = getConfig().delay;
long delay = getConfig().getDelay();
sleep(delay);
final LoopBasedJavaScriptEngine javaScriptEngine = (LoopBasedJavaScriptEngine) engine;
final long interval = getConfig().interval;
final long interval = getConfig().getInterval();
javaScriptEngine.getRuntime().loopers.setMainLooperQuitHandler(new Loopers.LooperQuitHandler() {
long times = getConfig().loopTimes == 0 ? Integer.MAX_VALUE : getConfig().loopTimes;
long times = getConfig().getLoopTimes() == 0 ? Integer.MAX_VALUE : getConfig().getLoopTimes();
@Override
public boolean shouldQuit() {

View File

@@ -32,7 +32,7 @@ public class RunnableScriptExecution extends ScriptExecution.AbstractScriptExecu
public Object execute() {
mScriptEngine = mScriptEngineManager.createEngineOfSourceOrThrow(getSource(), getId());
mScriptEngine.setTag(ExecutionConfig.TAG, getConfig());
mScriptEngine.setTag(ExecutionConfig.getTag(), getConfig());
return execute(mScriptEngine);
}
@@ -62,8 +62,8 @@ public class RunnableScriptExecution extends ScriptExecution.AbstractScriptExecu
}
private void prepare(ScriptEngine engine) {
engine.setTag(ScriptEngine.TAG_EXECUTE_PATH, getConfig().getExecutePath());
engine.setTag(ScriptEngine.TAG_ENV_PATH, getConfig().getRequirePath());
engine.setTag(ScriptEngine.TAG_WORKING_DIRECTORY, getConfig().getWorkingDirectory());
engine.setTag(ScriptEngine.TAG_ENV_PATH, getConfig().getPath());
engine.init();
}
@@ -71,12 +71,12 @@ public class RunnableScriptExecution extends ScriptExecution.AbstractScriptExecu
engine.setTag(ScriptEngine.TAG_SOURCE, getSource());
getListener().onStart(this);
Object result = null;
long delay = getConfig().delay;
int times = getConfig().loopTimes;
long delay = getConfig().getDelay();
int times = getConfig().getLoopTimes();
if (times == 0) {
times = Integer.MAX_VALUE;
}
long interval = getConfig().interval;
long interval = getConfig().getInterval();
sleep(delay);
ScriptSource source = getSource();
for (int i = 0; i < times; i++) {

View File

@@ -5,9 +5,7 @@ import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
@@ -120,8 +118,8 @@ public class ScriptExecuteActivity extends AppCompatActivity {
private void prepare() {
mScriptEngine.put("activity", this);
mScriptEngine.setTag("activity", this);
mScriptEngine.setTag(ScriptEngine.TAG_ENV_PATH, mScriptExecution.getConfig().getRequirePath());
mScriptEngine.setTag(ScriptEngine.TAG_EXECUTE_PATH, mScriptExecution.getConfig().getExecutePath());
mScriptEngine.setTag(ScriptEngine.TAG_ENV_PATH, mScriptExecution.getConfig().getWorkingDirectory());
mScriptEngine.setTag(ScriptEngine.TAG_WORKING_DIRECTORY, mScriptExecution.getConfig().getPath());
mScriptEngine.init();
}
@@ -250,7 +248,7 @@ public class ScriptExecuteActivity extends AppCompatActivity {
mScriptEngine.forceStop();
}
mScriptEngine = mScriptEngineManager.createEngineOfSourceOrThrow(getSource(), getId());
mScriptEngine.setTag(ExecutionConfig.TAG, getConfig());
mScriptEngine.setTag(ExecutionConfig.getTag(), getConfig());
return mScriptEngine;
}

View File

@@ -19,9 +19,9 @@ public class ProjectLauncher {
}
public void launch(ScriptEngineService service){
service.execute(new JavaScriptFileSource(mMainScriptFile), new ExecutionConfig()
.executePath(mProjectDir)
.requirePath(mProjectDir));
ExecutionConfig config = new ExecutionConfig();
config.setWorkingDirectory(mProjectDir);
service.execute(new JavaScriptFileSource(mMainScriptFile), config);
}
}

View File

@@ -24,8 +24,4 @@ class AutoJsContext(factory: ContextFactory?) : Context(factory) {
return mContinuations.isNotEmpty()
}
companion object {
private val EMPTY_RUNNABLE = Runnable { }
}
}