mirror of
https://github.com/TonyJiangWJ/Auto.js.git
synced 2026-06-24 21:33:16 +08:00
重构 ExecutionConfig
This commit is contained in:
parent
8faa6cc990
commit
c460425656
@ -40,7 +40,10 @@ public class ScriptIntents {
|
||||
long delay = intent.getLongExtra(EXTRA_KEY_DELAY, 0);
|
||||
long interval = intent.getLongExtra(EXTRA_KEY_LOOP_INTERVAL, 0);
|
||||
ScriptSource source = null;
|
||||
ExecutionConfig config = new ExecutionConfig().loop(delay, loopTimes, interval);
|
||||
ExecutionConfig config = new ExecutionConfig();
|
||||
config.setDelay(delay);
|
||||
config.setLoopTimes(loopTimes);
|
||||
config.setInterval(interval);
|
||||
config.setArgument("intent", intent);
|
||||
if (path == null && script != null) {
|
||||
source = new StringScriptSource(script);
|
||||
@ -51,9 +54,9 @@ public class ScriptIntents {
|
||||
} else {
|
||||
source = fileScriptSource;
|
||||
}
|
||||
config.executePath(new File(path).getParent());
|
||||
config.setWorkingDirectory(new File(path).getParent());
|
||||
} else {
|
||||
config.executePath(Pref.getScriptDirPath());
|
||||
config.setWorkingDirectory(Pref.getScriptDirPath());
|
||||
}
|
||||
if (source == null) {
|
||||
return false;
|
||||
|
||||
@ -38,7 +38,7 @@ public class RunIntentActivity extends Activity {
|
||||
Uri uri = intent.getData();
|
||||
if (uri != null && "content".equals(uri.getScheme())) {
|
||||
InputStream stream = getContentResolver().openInputStream(uri);
|
||||
Scripts.run(new StringScriptSource(PFiles.read(stream)));
|
||||
Scripts.INSTANCE.run(new StringScriptSource(PFiles.read(stream)));
|
||||
} else {
|
||||
ScriptIntents.handleIntent(this, intent);
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ public class BaseBroadcastReceiver extends BroadcastReceiver {
|
||||
ScriptFile file = new ScriptFile(task.getScriptPath());
|
||||
ExecutionConfig config = new ExecutionConfig();
|
||||
config.setArgument("intent", intent.clone());
|
||||
config.executePath(file.getParent());
|
||||
config.setWorkingDirectory(file.getParent());
|
||||
try {
|
||||
AutoJs.getInstance().getScriptEngineService().execute(file.toSource(), config);
|
||||
} catch (Exception e) {
|
||||
|
||||
@ -26,7 +26,7 @@ public class ShortcutActivity extends Activity {
|
||||
|
||||
private void runScriptFile(String path) {
|
||||
try {
|
||||
Scripts.run(new ScriptFile(path));
|
||||
Scripts.INSTANCE.run(new ScriptFile(path));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
|
||||
@ -42,7 +42,7 @@ public class ScriptWidgetSettingsActivity extends BaseActivity {
|
||||
|
||||
|
||||
private void initScriptListRecyclerView() {
|
||||
mExplorer = new Explorer(new ExplorerFileProvider(Scripts.FILE_FILTER), 0);
|
||||
mExplorer = new Explorer(new ExplorerFileProvider(Scripts.INSTANCE.getFILE_FILTER()), 0);
|
||||
ExplorerView explorerView = findViewById(R.id.script_list);
|
||||
explorerView.setExplorer(mExplorer, ExplorerDirPage.createRoot(Environment.getExternalStorageDirectory()));
|
||||
explorerView.setOnItemClickListener((view, file) -> {
|
||||
|
||||
@ -1,159 +0,0 @@
|
||||
package org.autojs.autojs.model.script;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import androidx.annotation.Nullable;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.stardust.app.GlobalAppContext;
|
||||
import com.stardust.autojs.execution.ExecutionConfig;
|
||||
import com.stardust.autojs.execution.ScriptExecution;
|
||||
import com.stardust.autojs.execution.ScriptExecutionListener;
|
||||
import com.stardust.autojs.execution.SimpleScriptExecutionListener;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.autojs.script.ScriptSource;
|
||||
import com.stardust.util.IntentUtil;
|
||||
|
||||
import org.autojs.autojs.Pref;
|
||||
import org.autojs.autojs.R;
|
||||
import org.autojs.autojs.autojs.AutoJs;
|
||||
import org.autojs.autojs.external.ScriptIntents;
|
||||
import org.autojs.autojs.external.fileprovider.AppFileProvider;
|
||||
import org.autojs.autojs.external.shortcut.Shortcut;
|
||||
import org.autojs.autojs.external.shortcut.ShortcutActivity;
|
||||
import org.autojs.autojs.ui.edit.EditActivity;
|
||||
|
||||
import org.mozilla.javascript.RhinoException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/3.
|
||||
*/
|
||||
|
||||
public class Scripts {
|
||||
|
||||
public static final String ACTION_ON_EXECUTION_FINISHED = "ACTION_ON_EXECUTION_FINISHED";
|
||||
public static final String EXTRA_EXCEPTION_MESSAGE = "message";
|
||||
public static final String EXTRA_EXCEPTION_LINE_NUMBER = "lineNumber";
|
||||
public static final String EXTRA_EXCEPTION_COLUMN_NUMBER = "columnNumber";
|
||||
|
||||
public static final FileFilter FILE_FILTER = file ->
|
||||
file.isDirectory() || file.getName().endsWith(".js") || file.getName().endsWith(".auto");
|
||||
|
||||
private static final ScriptExecutionListener BROADCAST_SENDER_SCRIPT_EXECUTION_LISTENER = new SimpleScriptExecutionListener() {
|
||||
|
||||
@Override
|
||||
public void onSuccess(ScriptExecution execution, Object result) {
|
||||
GlobalAppContext.get().sendBroadcast(new Intent(ACTION_ON_EXECUTION_FINISHED));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onException(ScriptExecution execution, Throwable e) {
|
||||
RhinoException rhinoException = getRhinoException(e);
|
||||
int line = -1, col = 0;
|
||||
if (rhinoException != null) {
|
||||
line = rhinoException.lineNumber();
|
||||
col = rhinoException.columnNumber();
|
||||
}
|
||||
if (ScriptInterruptedException.causedByInterrupted(e)) {
|
||||
GlobalAppContext.get().sendBroadcast(new Intent(ACTION_ON_EXECUTION_FINISHED)
|
||||
.putExtra(EXTRA_EXCEPTION_LINE_NUMBER, line)
|
||||
.putExtra(EXTRA_EXCEPTION_COLUMN_NUMBER, col));
|
||||
} else {
|
||||
GlobalAppContext.get().sendBroadcast(new Intent(ACTION_ON_EXECUTION_FINISHED)
|
||||
.putExtra(EXTRA_EXCEPTION_MESSAGE, e.getMessage())
|
||||
.putExtra(EXTRA_EXCEPTION_LINE_NUMBER, line)
|
||||
.putExtra(EXTRA_EXCEPTION_COLUMN_NUMBER, col));
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
public static void openByOtherApps(Uri uri) {
|
||||
IntentUtil.viewFile(GlobalAppContext.get(), uri, "text/plain", AppFileProvider.AUTHORITY);
|
||||
}
|
||||
|
||||
public static void openByOtherApps(File file) {
|
||||
openByOtherApps(Uri.fromFile(file));
|
||||
}
|
||||
|
||||
public static void createShortcut(ScriptFile scriptFile) {
|
||||
new Shortcut(GlobalAppContext.get()).name(scriptFile.getSimplifiedName())
|
||||
.targetClass(ShortcutActivity.class)
|
||||
.iconRes(R.drawable.ic_node_js_black)
|
||||
.extras(new Intent().putExtra(ScriptIntents.EXTRA_KEY_PATH, scriptFile.getPath()))
|
||||
.send();
|
||||
}
|
||||
|
||||
|
||||
public static void edit(ScriptFile file) {
|
||||
EditActivity.editFile(GlobalAppContext.get(), file.getSimplifiedName(), file.getPath());
|
||||
}
|
||||
|
||||
public static void edit(String path) {
|
||||
edit(new ScriptFile(path));
|
||||
}
|
||||
|
||||
public static ScriptExecution run(ScriptFile file) {
|
||||
try {
|
||||
return AutoJs.getInstance().getScriptEngineService().execute(file.toSource(), new ExecutionConfig()
|
||||
.executePath(file.getParent()));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Toast.makeText(GlobalAppContext.get(), e.getMessage(), Toast.LENGTH_LONG).show();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static ScriptExecution run(ScriptSource source) {
|
||||
try {
|
||||
return AutoJs.getInstance().getScriptEngineService().execute(source, new ExecutionConfig()
|
||||
.executePath(Pref.getScriptDirPath())
|
||||
.requirePath(Pref.getScriptDirPath()));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Toast.makeText(GlobalAppContext.get(), e.getMessage(), Toast.LENGTH_LONG).show();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ScriptExecution runWithBroadcastSender(File file) {
|
||||
return AutoJs.getInstance().getScriptEngineService().execute(new ScriptFile(file).toSource(), BROADCAST_SENDER_SCRIPT_EXECUTION_LISTENER,
|
||||
new ExecutionConfig().executePath(file.getParent()));
|
||||
}
|
||||
|
||||
|
||||
public static ScriptExecution runRepeatedly(ScriptFile scriptFile, int loopTimes, long delay, long interval) {
|
||||
ScriptSource source = scriptFile.toSource();
|
||||
String directoryPath = scriptFile.getParent();
|
||||
return AutoJs.getInstance().getScriptEngineService().execute(source, new ExecutionConfig()
|
||||
.executePath(directoryPath)
|
||||
.loop(delay, loopTimes, interval));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static RhinoException getRhinoException(Throwable e) {
|
||||
while (e != null) {
|
||||
if (e instanceof RhinoException) {
|
||||
return (RhinoException) e;
|
||||
}
|
||||
e = e.getCause();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void send(ScriptFile file) {
|
||||
Context context = GlobalAppContext.get();
|
||||
context.startActivity(Intent.createChooser(new Intent(Intent.ACTION_SEND)
|
||||
.setType("text/plain")
|
||||
.putExtra(Intent.EXTRA_STREAM, IntentUtil.getUriOfFile(context, file.getPath(), AppFileProvider.AUTHORITY)),
|
||||
GlobalAppContext.getString(R.string.text_send)
|
||||
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
|
||||
|
||||
}
|
||||
}
|
||||
160
app/src/main/java/org/autojs/autojs/model/script/Scripts.kt
Normal file
160
app/src/main/java/org/autojs/autojs/model/script/Scripts.kt
Normal file
@ -0,0 +1,160 @@
|
||||
package org.autojs.autojs.model.script
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import androidx.annotation.Nullable
|
||||
import android.widget.Toast
|
||||
|
||||
import com.stardust.app.GlobalAppContext
|
||||
import com.stardust.autojs.execution.ExecutionConfig
|
||||
import com.stardust.autojs.execution.ScriptExecution
|
||||
import com.stardust.autojs.execution.ScriptExecutionListener
|
||||
import com.stardust.autojs.execution.SimpleScriptExecutionListener
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException
|
||||
import com.stardust.autojs.script.ScriptSource
|
||||
import com.stardust.util.IntentUtil
|
||||
|
||||
import org.autojs.autojs.Pref
|
||||
import org.autojs.autojs.R
|
||||
import org.autojs.autojs.autojs.AutoJs
|
||||
import org.autojs.autojs.external.ScriptIntents
|
||||
import org.autojs.autojs.external.fileprovider.AppFileProvider
|
||||
import org.autojs.autojs.external.shortcut.Shortcut
|
||||
import org.autojs.autojs.external.shortcut.ShortcutActivity
|
||||
import org.autojs.autojs.ui.edit.EditActivity
|
||||
|
||||
import org.mozilla.javascript.RhinoException
|
||||
|
||||
import java.io.File
|
||||
import java.io.FileFilter
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/3.
|
||||
*/
|
||||
|
||||
object Scripts {
|
||||
|
||||
const val ACTION_ON_EXECUTION_FINISHED = "ACTION_ON_EXECUTION_FINISHED"
|
||||
const val EXTRA_EXCEPTION_MESSAGE = "message"
|
||||
const val EXTRA_EXCEPTION_LINE_NUMBER = "lineNumber"
|
||||
const val EXTRA_EXCEPTION_COLUMN_NUMBER = "columnNumber"
|
||||
|
||||
val FILE_FILTER = FileFilter { file ->
|
||||
file.isDirectory || file.name.endsWith(".js")
|
||||
|| file.name.endsWith(".auto")
|
||||
}
|
||||
|
||||
private val BROADCAST_SENDER_SCRIPT_EXECUTION_LISTENER = object : SimpleScriptExecutionListener() {
|
||||
|
||||
override fun onSuccess(execution: ScriptExecution, result: Any?) {
|
||||
GlobalAppContext.get().sendBroadcast(Intent(ACTION_ON_EXECUTION_FINISHED))
|
||||
}
|
||||
|
||||
override fun onException(execution: ScriptExecution, e: Throwable) {
|
||||
val rhinoException = getRhinoException(e)
|
||||
var line = -1
|
||||
var col = 0
|
||||
if (rhinoException != null) {
|
||||
line = rhinoException.lineNumber()
|
||||
col = rhinoException.columnNumber()
|
||||
}
|
||||
if (ScriptInterruptedException.causedByInterrupted(e)) {
|
||||
GlobalAppContext.get().sendBroadcast(Intent(ACTION_ON_EXECUTION_FINISHED)
|
||||
.putExtra(EXTRA_EXCEPTION_LINE_NUMBER, line)
|
||||
.putExtra(EXTRA_EXCEPTION_COLUMN_NUMBER, col))
|
||||
} else {
|
||||
GlobalAppContext.get().sendBroadcast(Intent(ACTION_ON_EXECUTION_FINISHED)
|
||||
.putExtra(EXTRA_EXCEPTION_MESSAGE, e.message)
|
||||
.putExtra(EXTRA_EXCEPTION_LINE_NUMBER, line)
|
||||
.putExtra(EXTRA_EXCEPTION_COLUMN_NUMBER, col))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
fun openByOtherApps(uri: Uri) {
|
||||
IntentUtil.viewFile(GlobalAppContext.get(), uri, "text/plain", AppFileProvider.AUTHORITY)
|
||||
}
|
||||
|
||||
fun openByOtherApps(file: File) {
|
||||
openByOtherApps(Uri.fromFile(file))
|
||||
}
|
||||
|
||||
fun createShortcut(scriptFile: ScriptFile) {
|
||||
Shortcut(GlobalAppContext.get()).name(scriptFile.simplifiedName)
|
||||
.targetClass(ShortcutActivity::class.java)
|
||||
.iconRes(R.drawable.ic_node_js_black)
|
||||
.extras(Intent().putExtra(ScriptIntents.EXTRA_KEY_PATH, scriptFile.path))
|
||||
.send()
|
||||
}
|
||||
|
||||
|
||||
fun edit(file: ScriptFile) {
|
||||
EditActivity.editFile(GlobalAppContext.get(), file.simplifiedName, file.path)
|
||||
}
|
||||
|
||||
fun edit(path: String) {
|
||||
edit(ScriptFile(path))
|
||||
}
|
||||
|
||||
fun run(file: ScriptFile): ScriptExecution? {
|
||||
try {
|
||||
return AutoJs.getInstance().scriptEngineService.execute(file.toSource(),
|
||||
ExecutionConfig(workingDirectory = file.parent))
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
Toast.makeText(GlobalAppContext.get(), e.message, Toast.LENGTH_LONG).show()
|
||||
return null
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
fun run(source: ScriptSource): ScriptExecution? {
|
||||
return try {
|
||||
AutoJs.getInstance().scriptEngineService.execute(source, ExecutionConfig(workingDirectory = Pref.getScriptDirPath()))
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
Toast.makeText(GlobalAppContext.get(), e.message, Toast.LENGTH_LONG).show()
|
||||
null
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun runWithBroadcastSender(file: File): ScriptExecution {
|
||||
return AutoJs.getInstance().scriptEngineService.execute(ScriptFile(file).toSource(), BROADCAST_SENDER_SCRIPT_EXECUTION_LISTENER,
|
||||
ExecutionConfig(workingDirectory = file.parent))
|
||||
}
|
||||
|
||||
|
||||
fun runRepeatedly(scriptFile: ScriptFile, loopTimes: Int, delay: Long, interval: Long): ScriptExecution {
|
||||
val source = scriptFile.toSource()
|
||||
val directoryPath = scriptFile.parent
|
||||
return AutoJs.getInstance().scriptEngineService.execute(source, ExecutionConfig(workingDirectory = directoryPath,
|
||||
delay = delay, loopTimes = loopTimes, interval = interval))
|
||||
}
|
||||
|
||||
@Nullable
|
||||
fun getRhinoException(e: Throwable?): RhinoException? {
|
||||
var e = e
|
||||
while (e != null) {
|
||||
if (e is RhinoException) {
|
||||
return e
|
||||
}
|
||||
e = e.cause
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun send(file: ScriptFile) {
|
||||
val context = GlobalAppContext.get()
|
||||
context.startActivity(Intent.createChooser(Intent(Intent.ACTION_SEND)
|
||||
.setType("text/plain")
|
||||
.putExtra(Intent.EXTRA_STREAM, IntentUtil.getUriOfFile(context, file.path, AppFileProvider.AUTHORITY)),
|
||||
GlobalAppContext.getString(R.string.text_send)
|
||||
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
|
||||
|
||||
}
|
||||
}
|
||||
@ -118,7 +118,7 @@ public class DevPluginResponseHandler implements Handler {
|
||||
} else {
|
||||
name = PFiles.getNameWithoutExtension(name);
|
||||
}
|
||||
mScriptExecutions.put(viewId, Scripts.run(new StringScriptSource("[remote]" + name, script)));
|
||||
mScriptExecutions.put(viewId, Scripts.INSTANCE.run(new StringScriptSource("[remote]" + name, script)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -53,9 +53,9 @@ public class TimedTask extends BaseModel {
|
||||
mMillis = millis;
|
||||
mTimeFlag = timeFlag;
|
||||
mScriptPath = scriptPath;
|
||||
mDelay = config.delay;
|
||||
mLoopTimes = config.loopTimes;
|
||||
mInterval = config.interval;
|
||||
mDelay = config.getDelay();
|
||||
mLoopTimes = config.getLoopTimes();
|
||||
mInterval = config.getInterval();
|
||||
}
|
||||
|
||||
public boolean isDisposable() {
|
||||
|
||||
@ -54,7 +54,7 @@ public class ScriptLoopDialog {
|
||||
int loopTimes = Integer.parseInt(mLoopTimes.getText().toString());
|
||||
float loopInterval = Float.parseFloat(mLoopInterval.getText().toString());
|
||||
float loopDelay = Float.parseFloat(mLoopDelay.getText().toString());
|
||||
Scripts.runRepeatedly(mScriptFile, loopTimes, (long) (1000L * loopDelay), (long) (loopInterval * 1000L));
|
||||
Scripts.INSTANCE.runRepeatedly(mScriptFile, loopTimes, (long) (1000L * loopDelay), (long) (loopInterval * 1000L));
|
||||
} catch (NumberFormatException e) {
|
||||
GlobalAppContext.toast(R.string.text_number_format_error);
|
||||
}
|
||||
|
||||
@ -114,7 +114,7 @@ public class ScriptOperations {
|
||||
}
|
||||
notifyFileCreated(mCurrentDirectory, new ScriptFile(path));
|
||||
if (edit)
|
||||
Scripts.edit(path);
|
||||
Scripts.INSTANCE.edit(path);
|
||||
} else {
|
||||
showMessage(R.string.text_create_fail);
|
||||
}
|
||||
|
||||
@ -80,6 +80,9 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
import static org.autojs.autojs.model.script.Scripts.ACTION_ON_EXECUTION_FINISHED;
|
||||
import static org.autojs.autojs.model.script.Scripts.EXTRA_EXCEPTION_COLUMN_NUMBER;
|
||||
import static org.autojs.autojs.model.script.Scripts.EXTRA_EXCEPTION_LINE_NUMBER;
|
||||
import static org.autojs.autojs.model.script.Scripts.EXTRA_EXCEPTION_MESSAGE;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/9/28.
|
||||
@ -137,9 +140,9 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
exitDebugging();
|
||||
}
|
||||
setMenuItemStatus(R.id.run, true);
|
||||
String msg = intent.getStringExtra(Scripts.EXTRA_EXCEPTION_MESSAGE);
|
||||
int line = intent.getIntExtra(Scripts.EXTRA_EXCEPTION_LINE_NUMBER, -1);
|
||||
int col = intent.getIntExtra(Scripts.EXTRA_EXCEPTION_COLUMN_NUMBER, 0);
|
||||
String msg = intent.getStringExtra(EXTRA_EXCEPTION_MESSAGE);
|
||||
int line = intent.getIntExtra(EXTRA_EXCEPTION_LINE_NUMBER, -1);
|
||||
int col = intent.getIntExtra(EXTRA_EXCEPTION_COLUMN_NUMBER, 0);
|
||||
if (line >= 1) {
|
||||
mEditor.jumpTo(line - 1, col);
|
||||
}
|
||||
@ -405,7 +408,7 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
Snackbar.make(this, R.string.text_start_running, Snackbar.LENGTH_SHORT).show();
|
||||
}
|
||||
// TODO: 2018/10/24
|
||||
ScriptExecution execution = Scripts.runWithBroadcastSender(new File(mUri.getPath()));
|
||||
ScriptExecution execution = Scripts.INSTANCE.runWithBroadcastSender(new File(mUri.getPath()));
|
||||
if (execution == null) {
|
||||
return null;
|
||||
}
|
||||
@ -502,7 +505,7 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
|
||||
public void openByOtherApps() {
|
||||
if (mUri != null) {
|
||||
Scripts.openByOtherApps(mUri);
|
||||
Scripts.INSTANCE.openByOtherApps(mUri);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -70,7 +70,6 @@ public class ViewSampleActivity extends AppCompatActivity implements OnActivityR
|
||||
setContentView(mView);
|
||||
handleIntent(getIntent());
|
||||
setUpUI();
|
||||
setUpEditor();
|
||||
registerReceiver(mOnRunFinishedReceiver, new IntentFilter(ACTION_ON_EXECUTION_FINISHED));
|
||||
}
|
||||
|
||||
@ -85,10 +84,6 @@ public class ViewSampleActivity extends AppCompatActivity implements OnActivityR
|
||||
ButterKnife.bind(this);
|
||||
}
|
||||
|
||||
private void setUpEditor() {
|
||||
|
||||
}
|
||||
|
||||
private void setUpToolbar() {
|
||||
BaseActivity.setToolbarAsBack(this, R.id.toolbar, mSample.getSimplifiedName());
|
||||
}
|
||||
|
||||
@ -339,11 +339,11 @@ public class ExplorerView extends ThemeColorSwipeRefreshLayout implements SwipeR
|
||||
.createShortcut(mSelectedItem.toScriptFile());
|
||||
break;
|
||||
case R.id.open_by_other_apps:
|
||||
Scripts.openByOtherApps(mSelectedItem.toScriptFile());
|
||||
Scripts.INSTANCE.openByOtherApps(mSelectedItem.toScriptFile());
|
||||
notifyOperated();
|
||||
break;
|
||||
case R.id.send:
|
||||
Scripts.send(mSelectedItem.toScriptFile());
|
||||
Scripts.INSTANCE.send(mSelectedItem.toScriptFile());
|
||||
notifyOperated();
|
||||
break;
|
||||
case R.id.timed_task:
|
||||
@ -551,13 +551,13 @@ public class ExplorerView extends ThemeColorSwipeRefreshLayout implements SwipeR
|
||||
|
||||
@OnClick(R.id.run)
|
||||
void run() {
|
||||
Scripts.run(new ScriptFile(mExplorerItem.getPath()));
|
||||
Scripts.INSTANCE.run(new ScriptFile(mExplorerItem.getPath()));
|
||||
notifyOperated();
|
||||
}
|
||||
|
||||
@OnClick(R.id.edit)
|
||||
void edit() {
|
||||
Scripts.edit(new ScriptFile(mExplorerItem.getPath()));
|
||||
Scripts.INSTANCE.edit(new ScriptFile(mExplorerItem.getPath()));
|
||||
notifyOperated();
|
||||
}
|
||||
|
||||
|
||||
@ -75,7 +75,7 @@ public class FileChooserDialogBuilder extends ThemeColorMaterialDialogBuilder {
|
||||
}
|
||||
|
||||
public FileChooserDialogBuilder justScriptFile() {
|
||||
mFileFilter = Scripts.FILE_FILTER;
|
||||
mFileFilter = Scripts.INSTANCE.getFILE_FILTER();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@ -144,7 +144,7 @@ public class CircularMenu implements Recorder.OnStateChangedListener, LayoutInsp
|
||||
.positiveText(R.string.cancel)
|
||||
.build();
|
||||
explorerView.setOnItemOperatedListener(file -> dialog.dismiss());
|
||||
explorerView.setOnItemClickListener((view, item) -> Scripts.run(item.toScriptFile()));
|
||||
explorerView.setOnItemClickListener((view, item) -> Scripts.INSTANCE.run(item.toScriptFile()));
|
||||
DialogUtils.showDialog(dialog);
|
||||
|
||||
}
|
||||
|
||||
@ -75,7 +75,7 @@ public class CommunityWebView extends EWebView {
|
||||
.subscribe(file ->
|
||||
Snackbar.make(CommunityWebView.this, getResources().getString(R.string.format_file_downloaded, file.getPath())
|
||||
, Snackbar.LENGTH_LONG)
|
||||
.setAction(R.string.text_open, v -> Scripts.edit(file))
|
||||
.setAction(R.string.text_open, v -> Scripts.INSTANCE.edit(file))
|
||||
.show(),
|
||||
error -> {
|
||||
error.printStackTrace();
|
||||
@ -93,7 +93,7 @@ public class CommunityWebView extends EWebView {
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(file -> {
|
||||
Snackbar.make(CommunityWebView.this, R.string.text_start_running, Snackbar.LENGTH_SHORT).show();
|
||||
Scripts.run(file);
|
||||
Scripts.INSTANCE.run(file);
|
||||
}, error -> {
|
||||
error.printStackTrace();
|
||||
Snackbar.make(CommunityWebView.this, R.string.text_download_failed, Toast.LENGTH_SHORT).show();
|
||||
|
||||
@ -62,7 +62,7 @@ public class MyScriptListFragment extends ViewPagerFragment implements FloatingA
|
||||
mExplorerView.setExplorer(Explorers.workspace(), ExplorerDirPage.createRoot(Pref.getScriptDirPath()));
|
||||
mExplorerView.setOnItemClickListener((view, item) -> {
|
||||
if (item.isEditable()) {
|
||||
Scripts.edit(item.toScriptFile());
|
||||
Scripts.INSTANCE.edit(item.toScriptFile());
|
||||
} else {
|
||||
IntentUtil.viewFile(GlobalAppContext.get(), item.getPath(), AppFileProvider.AUTHORITY);
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package org.autojs.autojs.ui.timing;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.DatePickerDialog;
|
||||
import android.app.TimePickerDialog;
|
||||
import android.content.ActivityNotFoundException;
|
||||
@ -344,6 +345,7 @@ public class TimedTaskSettingActivity extends BaseActivity {
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressLint("BatteryLife")
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (item.getItemId() == R.id.action_done) {
|
||||
|
||||
@ -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){
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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() {
|
||||
|
||||
@ -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++) {
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -24,8 +24,4 @@ class AutoJsContext(factory: ContextFactory?) : Context(factory) {
|
||||
return mContinuations.isNotEmpty()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val EMPTY_RUNNABLE = Runnable { }
|
||||
}
|
||||
|
||||
}
|
||||
@ -67,10 +67,9 @@ open class AssetsProjectLauncher(private val mAssetsProjectDir: String, private
|
||||
}
|
||||
try {
|
||||
val source = JavaScriptFileSource("main", mMainScriptFile)
|
||||
val config = ExecutionConfig()
|
||||
.executePath(mProjectDir)
|
||||
val config = ExecutionConfig(workingDirectory = mProjectDir)
|
||||
if (source.executionMode and JavaScriptSource.EXECUTION_MODE_UI != 0) {
|
||||
config.intentFlags = Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
config.intentFlags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
|
||||
} else {
|
||||
activity?.finish()
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user