api: threads, files
This commit is contained in:
1 parent
25e9c90bf4
commit
729958d419
26 files changed
+652
-339
No files matched your search
@@ -0,0 +1 @@
|
|||||||
|
var i = threads.atomic();
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
var running = true;
|
||||||
|
|
||||||
|
threads.start(function(){
|
||||||
|
while(running){
|
||||||
|
log("running = true");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
sleep(2000);
|
||||||
|
running = false;
|
||||||
|
console.info("running = false");
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
//启动一个线程
|
||||||
|
threads.start(function(){
|
||||||
|
//在线程中每隔1秒打印"线程1"
|
||||||
|
while(true){
|
||||||
|
log("线程1");
|
||||||
|
sleep(1000);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//启动另一个线程
|
||||||
|
threads.start(function(){
|
||||||
|
//在线程中每隔2秒打印"线程1"
|
||||||
|
while(true){
|
||||||
|
log("线程2");
|
||||||
|
sleep(2000);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//在主线程中每隔3秒打印"主线程"
|
||||||
|
for(var i = 0; i < 10; i++){
|
||||||
|
log("主线程");
|
||||||
|
sleep(3000);
|
||||||
|
}
|
||||||
|
//打印100次后退出所有线程
|
||||||
|
threads.shutDownAll();
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
//启动一个无限循环的线程
|
||||||
|
var thread = threads.start(function(){
|
||||||
|
while(true){
|
||||||
|
log("子线程运行中...");
|
||||||
|
sleep(1000);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//5秒后关闭线程
|
||||||
|
sleep(5000);
|
||||||
|
thread.interrupt();
|
||||||
|
|
||||||
@@ -20,6 +20,7 @@ import com.stardust.scriptdroid.R;
|
|||||||
import com.stardust.scriptdroid.accessibility.AccessibilityService;
|
import com.stardust.scriptdroid.accessibility.AccessibilityService;
|
||||||
import com.stardust.scriptdroid.autojs.AutoJs;
|
import com.stardust.scriptdroid.autojs.AutoJs;
|
||||||
import com.stardust.scriptdroid.autojs.record.GlobalActionRecorder;
|
import com.stardust.scriptdroid.autojs.record.GlobalActionRecorder;
|
||||||
|
import com.stardust.scriptdroid.model.script.Scripts;
|
||||||
import com.stardust.scriptdroid.storage.file.StorageFileProvider;
|
import com.stardust.scriptdroid.storage.file.StorageFileProvider;
|
||||||
import com.stardust.scriptdroid.tool.AccessibilityServiceTool;
|
import com.stardust.scriptdroid.tool.AccessibilityServiceTool;
|
||||||
import com.stardust.scriptdroid.ui.common.NotAskAgainDialog;
|
import com.stardust.scriptdroid.ui.common.NotAskAgainDialog;
|
||||||
@@ -132,6 +133,7 @@ public class CircularMenu implements Recorder.OnStateChangedListener {
|
|||||||
.positiveText(R.string.cancel)
|
.positiveText(R.string.cancel)
|
||||||
.build();
|
.build();
|
||||||
listView.setOnItemOperatedListener(file -> dialog.dismiss());
|
listView.setOnItemOperatedListener(file -> dialog.dismiss());
|
||||||
|
listView.setOnScriptFileClickListener((view, file) -> Scripts.run(file));
|
||||||
DialogUtils.showDialog(dialog);
|
DialogUtils.showDialog(dialog);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ import org.androidannotations.annotations.ViewById;
|
|||||||
import org.greenrobot.eventbus.EventBus;
|
import org.greenrobot.eventbus.EventBus;
|
||||||
import org.greenrobot.eventbus.Subscribe;
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
@EActivity(R.layout.activity_main)
|
@EActivity(R.layout.activity_main)
|
||||||
|
|||||||
@@ -1,7 +1,18 @@
|
|||||||
|
|
||||||
module.exports = function(__runtime__, scope){
|
module.exports = function(__runtime__, scope){
|
||||||
|
importClass(android.view.KeyEvent);
|
||||||
var events = Object.create(__runtime__.events);
|
var events = Object.create(__runtime__.events);
|
||||||
|
|
||||||
|
var keys = {
|
||||||
|
"home": KeyEvent.KEYCODE_HOME,
|
||||||
|
"menu": KeyEvent.KEYCODE_MENU,
|
||||||
|
"back": KeyEvent.KEYCODE_BACK,
|
||||||
|
"volume_up": KeyEvent.KEYCODE_VOLUME_UP,
|
||||||
|
"volume_down": KeyEvent.KEYCODE_VOLUME_DOWN
|
||||||
|
}
|
||||||
|
|
||||||
|
scope.keys = keys;
|
||||||
|
|
||||||
return events;
|
return events;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
|
|
||||||
module.exports = function(__runtime__, scope){
|
module.exports = function(__runtime__, scope){
|
||||||
var files = com.stardust.pio.PFiles;
|
var files = Object.create(com.stardust.pio.PFiles);
|
||||||
|
files.cwd = function(){
|
||||||
|
return scope.engines.myEngine().cwd();
|
||||||
|
}
|
||||||
scope.files = files;
|
scope.files = files;
|
||||||
scope.open = function(path, mode, encoding, bufferSize){
|
scope.open = function(path, mode, encoding, bufferSize){
|
||||||
if(arguments.length == 1){
|
if(arguments.length == 1){
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Stardust on 2017/5/2.
|
* Created by Stardust on 2017/5/2.
|
||||||
|
*
|
||||||
|
* TODO: 优化为无锁形式
|
||||||
*/
|
*/
|
||||||
public class ConsoleView extends FrameLayout implements StardustConsole.LogListener {
|
public class ConsoleView extends FrameLayout implements StardustConsole.LogListener {
|
||||||
|
|
||||||
@@ -77,12 +79,9 @@ public class ConsoleView extends FrameLayout implements StardustConsole.LogListe
|
|||||||
|
|
||||||
private void initSubmitButton() {
|
private void initSubmitButton() {
|
||||||
final Button submit = (Button) findViewById(R.id.submit);
|
final Button submit = (Button) findViewById(R.id.submit);
|
||||||
submit.setOnClickListener(new OnClickListener() {
|
submit.setOnClickListener(v -> {
|
||||||
@Override
|
CharSequence input = mEditText.getText();
|
||||||
public void onClick(View v) {
|
submitInput(input);
|
||||||
CharSequence input = mEditText.getText();
|
|
||||||
submitInput(input);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,13 +98,10 @@ public class ConsoleView extends FrameLayout implements StardustConsole.LogListe
|
|||||||
mEditText = (EditText) findViewById(R.id.input);
|
mEditText = (EditText) findViewById(R.id.input);
|
||||||
mEditText.setFocusableInTouchMode(true);
|
mEditText.setFocusableInTouchMode(true);
|
||||||
mInputContainer = (LinearLayout) findViewById(R.id.input_container);
|
mInputContainer = (LinearLayout) findViewById(R.id.input_container);
|
||||||
OnClickListener listener = new OnClickListener() {
|
OnClickListener listener = v -> {
|
||||||
@Override
|
if (mWindow != null) {
|
||||||
public void onClick(View v) {
|
mWindow.requestWindowFocus();
|
||||||
if (mWindow != null) {
|
mEditText.requestFocus();
|
||||||
mWindow.requestWindowFocus();
|
|
||||||
mEditText.requestFocus();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
mEditText.setOnClickListener(listener);
|
mEditText.setOnClickListener(listener);
|
||||||
@@ -146,12 +142,9 @@ public class ConsoleView extends FrameLayout implements StardustConsole.LogListe
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLogClear() {
|
public void onLogClear() {
|
||||||
post(new Runnable() {
|
post(() -> {
|
||||||
@Override
|
mLogs.clear();
|
||||||
public void run() {
|
mLogListRecyclerView.getAdapter().notifyDataSetChanged();
|
||||||
mLogs.clear();
|
|
||||||
mLogListRecyclerView.getAdapter().notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,22 +153,24 @@ public class ConsoleView extends FrameLayout implements StardustConsole.LogListe
|
|||||||
return;
|
return;
|
||||||
int oldSize = mLogs.size();
|
int oldSize = mLogs.size();
|
||||||
ArrayList<StardustConsole.Log> logs = mConsole.getAllLogs();
|
ArrayList<StardustConsole.Log> logs = mConsole.getAllLogs();
|
||||||
final int size = logs.size();
|
synchronized (logs) {
|
||||||
if (size == 0) {
|
final int size = logs.size();
|
||||||
return;
|
if (size == 0) {
|
||||||
}
|
return;
|
||||||
if (oldSize >= size) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (oldSize == 0) {
|
|
||||||
mLogs.addAll(logs);
|
|
||||||
} else {
|
|
||||||
for (int i = oldSize; i < size; i++) {
|
|
||||||
mLogs.add(logs.get(i));
|
|
||||||
}
|
}
|
||||||
|
if (oldSize >= size) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (oldSize == 0) {
|
||||||
|
mLogs.addAll(logs);
|
||||||
|
} else {
|
||||||
|
for (int i = oldSize; i < size; i++) {
|
||||||
|
mLogs.add(logs.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mLogListRecyclerView.getAdapter().notifyItemRangeInserted(oldSize, size - 1);
|
||||||
|
mLogListRecyclerView.scrollToPosition(size - 1);
|
||||||
}
|
}
|
||||||
mLogListRecyclerView.getAdapter().notifyItemRangeInserted(oldSize, size - 1);
|
|
||||||
mLogListRecyclerView.scrollToPosition(size - 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setWindow(ResizableExpandableFloatyWindow window) {
|
public void setWindow(ResizableExpandableFloatyWindow window) {
|
||||||
@@ -183,13 +178,10 @@ public class ConsoleView extends FrameLayout implements StardustConsole.LogListe
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void showEditText() {
|
public void showEditText() {
|
||||||
post(new Runnable() {
|
post(() -> {
|
||||||
@Override
|
mWindow.requestWindowFocus();
|
||||||
public void run() {
|
mInputContainer.setVisibility(VISIBLE);
|
||||||
mWindow.requestWindowFocus();
|
mEditText.requestFocus();
|
||||||
mInputContainer.setVisibility(VISIBLE);
|
|
||||||
mEditText.requestFocus();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -122,7 +122,9 @@ public class StardustConsole extends AbstractConsole {
|
|||||||
@Override
|
@Override
|
||||||
public String println(int level, CharSequence charSequence) {
|
public String println(int level, CharSequence charSequence) {
|
||||||
Log log = new Log(mIdCounter.getAndIncrement(), level, charSequence, true);
|
Log log = new Log(mIdCounter.getAndIncrement(), level, charSequence, true);
|
||||||
mLogs.add(log);
|
synchronized (mLogs) {
|
||||||
|
mLogs.add(log);
|
||||||
|
}
|
||||||
if (mGlobalConsole != null) {
|
if (mGlobalConsole != null) {
|
||||||
mGlobalConsole.println(level, charSequence);
|
mGlobalConsole.println(level, charSequence);
|
||||||
}
|
}
|
||||||
@@ -135,20 +137,15 @@ public class StardustConsole extends AbstractConsole {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(int level, CharSequence charSequence) {
|
public void write(int level, CharSequence charSequence) {
|
||||||
Log log = new Log(mIdCounter.getAndIncrement(), level, charSequence);
|
println(level, charSequence);
|
||||||
mLogs.add(log);
|
|
||||||
if (mGlobalConsole != null) {
|
|
||||||
mGlobalConsole.print(level, charSequence);
|
|
||||||
}
|
|
||||||
if (mLogListener != null && mLogListener.get() != null) {
|
|
||||||
mLogListener.get().onNewLog(log);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clear() {
|
public void clear() {
|
||||||
mLogs.clear();
|
synchronized (mLogs) {
|
||||||
|
mLogs.clear();
|
||||||
|
}
|
||||||
if (mLogListener != null && mLogListener.get() != null) {
|
if (mLogListener != null && mLogListener.get() != null) {
|
||||||
mLogListener.get().onLogClear();
|
mLogListener.get().onLogClear();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.stardust.autojs.core.looper;
|
||||||
|
|
||||||
|
import android.os.Looper;
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Stardust on 2017/12/27.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class LooperHelper {
|
||||||
|
|
||||||
|
private static volatile ConcurrentHashMap<Thread, Looper> sLoopers = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
public static void prepare() {
|
||||||
|
if (Looper.myLooper() == Looper.getMainLooper())
|
||||||
|
return;
|
||||||
|
if (Looper.myLooper() == null)
|
||||||
|
Looper.prepare();
|
||||||
|
Looper l = Looper.myLooper();
|
||||||
|
if (l != null)
|
||||||
|
sLoopers.put(Thread.currentThread(), l);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void quitForThread(Thread thread) {
|
||||||
|
Looper looper = sLoopers.remove(thread);
|
||||||
|
if (looper != null)
|
||||||
|
looper.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
package com.stardust.autojs.core.looper;
|
||||||
|
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
|
|
||||||
|
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||||
|
import com.stardust.autojs.runtime.api.Threads;
|
||||||
|
import com.stardust.autojs.runtime.api.Timers;
|
||||||
|
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||||
|
import com.stardust.lang.ThreadCompat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Stardust on 2017/7/29.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Loopers {
|
||||||
|
|
||||||
|
public interface LooperQuitHandler {
|
||||||
|
boolean shouldQuit();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Runnable EMPTY_RUNNABLE = () -> {
|
||||||
|
};
|
||||||
|
|
||||||
|
private volatile ThreadLocal<Boolean> waitWhenIdle = new ThreadLocal<>();
|
||||||
|
private volatile Looper mServantLooper;
|
||||||
|
private Timers mTimers;
|
||||||
|
private ScriptRuntime mScriptRuntime;
|
||||||
|
private LooperQuitHandler mMainLooperQuitHandler;
|
||||||
|
private Handler mMainHandler;
|
||||||
|
private Looper mMainLooper;
|
||||||
|
private Threads mThreads;
|
||||||
|
|
||||||
|
public Loopers(ScriptRuntime runtime) {
|
||||||
|
mTimers = runtime.timers;
|
||||||
|
mThreads = runtime.threads;
|
||||||
|
mScriptRuntime = runtime;
|
||||||
|
prepare();
|
||||||
|
mMainLooper = Looper.myLooper();
|
||||||
|
mMainHandler = new Handler();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Looper getMainLooper() {
|
||||||
|
return mMainLooper;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean shouldQuitLooper() {
|
||||||
|
if (Thread.currentThread().isInterrupted()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (mTimers.hasPendingCallbacks()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return !waitWhenIdle.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void initServantThread() {
|
||||||
|
new ThreadCompat(() -> {
|
||||||
|
Looper.prepare();
|
||||||
|
final Object lock = Loopers.this;
|
||||||
|
mServantLooper = Looper.myLooper();
|
||||||
|
synchronized (lock) {
|
||||||
|
lock.notifyAll();
|
||||||
|
}
|
||||||
|
Looper.loop();
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Looper getServantLooper() {
|
||||||
|
if (mServantLooper == null) {
|
||||||
|
initServantThread();
|
||||||
|
synchronized (this) {
|
||||||
|
try {
|
||||||
|
this.wait();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new ScriptInterruptedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mServantLooper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void quitServantLooper() {
|
||||||
|
if (mServantLooper == null)
|
||||||
|
return;
|
||||||
|
mServantLooper.quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void waitWhenIdle(boolean b) {
|
||||||
|
waitWhenIdle.set(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void quitAll() {
|
||||||
|
quitServantLooper();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMainLooperQuitHandler(LooperQuitHandler mainLooperQuitHandler) {
|
||||||
|
mMainLooperQuitHandler = mainLooperQuitHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void prepare() {
|
||||||
|
if (Looper.myLooper() == null)
|
||||||
|
Looper.prepare();
|
||||||
|
Looper.myQueue().addIdleHandler(() -> {
|
||||||
|
Looper l = Looper.myLooper();
|
||||||
|
if (l == null)
|
||||||
|
return true;
|
||||||
|
if (l == mMainLooper) {
|
||||||
|
if (shouldQuitLooper() && !mThreads.hasRunningThreads() &&
|
||||||
|
mMainLooperQuitHandler != null && mMainLooperQuitHandler.shouldQuit()) {
|
||||||
|
l.quit();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (shouldQuitLooper()) {
|
||||||
|
l.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
waitWhenIdle.set(Looper.myLooper() == Looper.getMainLooper());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void notifyThreadExit(TimerThread thread) {
|
||||||
|
//当子线程退成时,主线程需要检查自身是否退出(主线程在所有子线程执行完成后才能退出,如果主线程已经执行完任务仍然要等待所有子线程),
|
||||||
|
//此时通过向主线程发送一个空的Runnable,主线程执行完这个Runnable后会触发IdleHandler,从而检查自身是否退出
|
||||||
|
mMainHandler.post(EMPTY_RUNNABLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
package com.stardust.autojs.core.looper;
|
||||||
|
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.SystemClock;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.util.SparseArray;
|
||||||
|
|
||||||
|
import com.stardust.autojs.runtime.ScriptBridges;
|
||||||
|
import com.stardust.concurrent.VolatileBox;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Stardust on 2017/12/27.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Timer {
|
||||||
|
|
||||||
|
private static final String LOG_TAG = "Timer";
|
||||||
|
|
||||||
|
private SparseArray<Runnable> mHandlerCallbacks = new SparseArray<>();
|
||||||
|
private int mCallbackMaxId = 0;
|
||||||
|
private ScriptBridges mBridges;
|
||||||
|
private Handler mHandler;
|
||||||
|
private long mMaxCallbackUptimeMillis = 0;
|
||||||
|
private final VolatileBox<Long> mMaxCallbackMillisForAllThread;
|
||||||
|
|
||||||
|
public Timer(ScriptBridges bridges, VolatileBox<Long> maxCallbackMillisForAllThread) {
|
||||||
|
mBridges = bridges;
|
||||||
|
mMaxCallbackMillisForAllThread = maxCallbackMillisForAllThread;
|
||||||
|
mHandler = new Handler();
|
||||||
|
Log.d(LOG_TAG, "Timer: handler = " + mHandler + ", thread = " + Thread.currentThread());
|
||||||
|
}
|
||||||
|
|
||||||
|
public int setTimeout(final Object callback, final long delay, final Object... args) {
|
||||||
|
Log.d(LOG_TAG, "setTimeout: handler = " + mHandler);
|
||||||
|
mCallbackMaxId++;
|
||||||
|
final int id = mCallbackMaxId;
|
||||||
|
Runnable r = () -> {
|
||||||
|
Log.d(LOG_TAG, "callFunction: handler = " + mHandler);
|
||||||
|
mBridges.callFunction(callback, null, args);
|
||||||
|
mHandlerCallbacks.remove(id);
|
||||||
|
};
|
||||||
|
mHandlerCallbacks.put(id, r);
|
||||||
|
postDelayed(r, delay);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean clearTimeout(int id) {
|
||||||
|
return clearCallback(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int setInterval(final Object listener, final long interval, final Object... args) {
|
||||||
|
mCallbackMaxId++;
|
||||||
|
final int id = mCallbackMaxId;
|
||||||
|
final Runnable r = new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (mHandlerCallbacks.get(id) == null)
|
||||||
|
return;
|
||||||
|
mBridges.callFunction(listener, null, args);
|
||||||
|
postDelayed(this, interval);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
mHandlerCallbacks.put(id, r);
|
||||||
|
postDelayed(r, interval);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void postDelayed(Runnable r, long interval) {
|
||||||
|
long uptime = SystemClock.uptimeMillis() + interval;
|
||||||
|
mHandler.postAtTime(r, uptime);
|
||||||
|
mMaxCallbackUptimeMillis = Math.max(mMaxCallbackUptimeMillis, uptime);
|
||||||
|
synchronized (mMaxCallbackMillisForAllThread) {
|
||||||
|
mMaxCallbackMillisForAllThread.set(Math.max(mMaxCallbackMillisForAllThread.get(), uptime));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean clearInterval(int id) {
|
||||||
|
return clearCallback(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int setImmediate(final Object listener, final Object... args) {
|
||||||
|
mCallbackMaxId++;
|
||||||
|
final int id = mCallbackMaxId;
|
||||||
|
Runnable r = () -> {
|
||||||
|
mBridges.callFunction(listener, null, args);
|
||||||
|
mHandlerCallbacks.remove(id);
|
||||||
|
};
|
||||||
|
mHandlerCallbacks.put(id, r);
|
||||||
|
postDelayed(r, 0);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean clearImmediate(int id) {
|
||||||
|
return clearCallback(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean clearCallback(int id) {
|
||||||
|
Runnable callback = mHandlerCallbacks.get(id);
|
||||||
|
if (callback != null) {
|
||||||
|
mHandler.removeCallbacks(callback);
|
||||||
|
mHandlerCallbacks.remove(id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasPendingCallbacks() {
|
||||||
|
Log.d(LOG_TAG, "[thread]hasPendingCallbacks:" + (mMaxCallbackUptimeMillis > SystemClock.uptimeMillis()));
|
||||||
|
Log.d(LOG_TAG, "mMaxCallbackUptimeMillisForAllThreads:" + mMaxCallbackUptimeMillis);
|
||||||
|
return mMaxCallbackUptimeMillis > SystemClock.uptimeMillis();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package com.stardust.autojs.core.looper;
|
||||||
|
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
|
import android.support.annotation.CallSuper;
|
||||||
|
|
||||||
|
import com.stardust.autojs.engine.RhinoJavaScriptEngine;
|
||||||
|
import com.stardust.autojs.runtime.ScriptBridges;
|
||||||
|
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||||
|
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||||
|
import com.stardust.concurrent.VolatileBox;
|
||||||
|
import com.stardust.lang.ThreadCompat;
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Stardust on 2017/12/27.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class TimerThread extends ThreadCompat {
|
||||||
|
|
||||||
|
private static ConcurrentHashMap<Thread, Timer> sTimerMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private Timer mTimer;
|
||||||
|
private final VolatileBox<Long> mMaxCallbackUptimeMillisForAllThreads;
|
||||||
|
private final ScriptRuntime mRuntime;
|
||||||
|
private Runnable mTarget;
|
||||||
|
|
||||||
|
public TimerThread(ScriptRuntime runtime, VolatileBox<Long> maxCallbackUptimeMillisForAllThreads, Runnable target) {
|
||||||
|
super(target);
|
||||||
|
mRuntime = runtime;
|
||||||
|
mTarget = target;
|
||||||
|
mMaxCallbackUptimeMillisForAllThreads = maxCallbackUptimeMillisForAllThreads;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
mRuntime.loopers.prepare();
|
||||||
|
mTimer = new Timer(mRuntime.bridges, mMaxCallbackUptimeMillisForAllThreads);
|
||||||
|
sTimerMap.put(Thread.currentThread(), mTimer);
|
||||||
|
new Handler().post(mTarget);
|
||||||
|
try {
|
||||||
|
Looper.loop();
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (!ScriptInterruptedException.causedByInterrupted(e)) {
|
||||||
|
mRuntime.console.error(Thread.currentThread().toString() + ": " + e);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
onExit();
|
||||||
|
sTimerMap.remove(Thread.currentThread(), mTimer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@CallSuper
|
||||||
|
protected void onExit() {
|
||||||
|
mRuntime.loopers.notifyThreadExit(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Timer getTimerForThread(Thread thread) {
|
||||||
|
return sTimerMap.get(thread);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Timer getTimerForCurrentThread() {
|
||||||
|
return getTimerForThread(Thread.currentThread());
|
||||||
|
}
|
||||||
|
|
||||||
|
public int setTimeout(Object callback, long delay, Object... args) {
|
||||||
|
return mTimer.setTimeout(callback, delay, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean clearTimeout(int id) {
|
||||||
|
return mTimer.clearTimeout(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int setInterval(Object listener, long interval, Object... args) {
|
||||||
|
return mTimer.setInterval(listener, interval, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean clearInterval(int id) {
|
||||||
|
return mTimer.clearInterval(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int setImmediate(Object listener, Object... args) {
|
||||||
|
return mTimer.setImmediate(listener, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean clearImmediate(int id) {
|
||||||
|
return mTimer.clearImmediate(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ import android.os.Handler;
|
|||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.os.MessageQueue;
|
import android.os.MessageQueue;
|
||||||
|
|
||||||
import com.stardust.autojs.runtime.api.Loopers;
|
import com.stardust.autojs.core.looper.LooperHelper;
|
||||||
import com.stardust.autojs.script.JavaScriptSource;
|
import com.stardust.autojs.script.JavaScriptSource;
|
||||||
import com.stardust.autojs.script.ScriptSource;
|
import com.stardust.autojs.script.ScriptSource;
|
||||||
import com.stardust.util.Callback;
|
import com.stardust.util.Callback;
|
||||||
@@ -62,7 +62,7 @@ public class LoopBasedJavaScriptEngine extends RhinoJavaScriptEngine {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void forceStop() {
|
public void forceStop() {
|
||||||
Loopers.quitForThread(getThread());
|
LooperHelper.quitForThread(getThread());
|
||||||
super.forceStop();
|
super.forceStop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,13 +70,13 @@ public class LoopBasedJavaScriptEngine extends RhinoJavaScriptEngine {
|
|||||||
public synchronized void destroy() {
|
public synchronized void destroy() {
|
||||||
Thread thread = getThread();
|
Thread thread = getThread();
|
||||||
if (thread != null)
|
if (thread != null)
|
||||||
Loopers.quitForThread(thread);
|
LooperHelper.quitForThread(thread);
|
||||||
super.destroy();
|
super.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
Loopers.prepare();
|
LooperHelper.prepare();
|
||||||
mHandler = new Handler();
|
mHandler = new Handler();
|
||||||
super.init();
|
super.init();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,6 @@ public interface ScriptEngine<S extends ScriptSource> {
|
|||||||
|
|
||||||
Object getTag(String key);
|
Object getTag(String key);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
@@ -87,6 +86,10 @@ public interface ScriptEngine<S extends ScriptSource> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String cwd() {
|
||||||
|
return (String) getTag(TAG_PATH);
|
||||||
|
}
|
||||||
|
|
||||||
public void setOnDestroyListener(OnDestroyListener onDestroyListener) {
|
public void setOnDestroyListener(OnDestroyListener onDestroyListener) {
|
||||||
if (mOnDestroyListener != null)
|
if (mOnDestroyListener != null)
|
||||||
throw new SecurityException("setOnDestroyListener can be called only once");
|
throw new SecurityException("setOnDestroyListener can be called only once");
|
||||||
|
|||||||
+3
-7
@@ -1,14 +1,10 @@
|
|||||||
package com.stardust.autojs.execution;
|
package com.stardust.autojs.execution;
|
||||||
|
|
||||||
import android.os.MessageQueue;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import com.stardust.autojs.engine.LoopBasedJavaScriptEngine;
|
import com.stardust.autojs.engine.LoopBasedJavaScriptEngine;
|
||||||
import com.stardust.autojs.engine.ScriptEngine;
|
import com.stardust.autojs.engine.ScriptEngine;
|
||||||
import com.stardust.autojs.engine.ScriptEngineManager;
|
import com.stardust.autojs.engine.ScriptEngineManager;
|
||||||
import com.stardust.autojs.runtime.api.Loopers;
|
import com.stardust.autojs.core.looper.Loopers;
|
||||||
import com.stardust.autojs.script.JavaScriptSource;
|
import com.stardust.autojs.script.JavaScriptSource;
|
||||||
import com.stardust.autojs.script.ScriptSource;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Stardust on 2017/10/27.
|
* Created by Stardust on 2017/10/27.
|
||||||
@@ -28,7 +24,7 @@ public class LoopedBasedJavaScriptExecution extends RunnableScriptExecution {
|
|||||||
sleep(delay);
|
sleep(delay);
|
||||||
final LoopBasedJavaScriptEngine javaScriptEngine = (LoopBasedJavaScriptEngine) engine;
|
final LoopBasedJavaScriptEngine javaScriptEngine = (LoopBasedJavaScriptEngine) engine;
|
||||||
final long interval = getConfig().interval;
|
final long interval = getConfig().interval;
|
||||||
javaScriptEngine.getRuntime().loopers.setLooperQuitHandler(new Loopers.LooperQuitHandler() {
|
javaScriptEngine.getRuntime().loopers.setMainLooperQuitHandler(new Loopers.LooperQuitHandler() {
|
||||||
long times = getConfig().loopTimes == 0 ? Integer.MAX_VALUE : getConfig().loopTimes;
|
long times = getConfig().loopTimes == 0 ? Integer.MAX_VALUE : getConfig().loopTimes;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -39,7 +35,7 @@ public class LoopedBasedJavaScriptExecution extends RunnableScriptExecution {
|
|||||||
javaScriptEngine.execute(getSource());
|
javaScriptEngine.execute(getSource());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
javaScriptEngine.getRuntime().loopers.setLooperQuitHandler(null);
|
javaScriptEngine.getRuntime().loopers.setMainLooperQuitHandler(null);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import com.stardust.autojs.runtime.api.Device;
|
|||||||
import com.stardust.autojs.runtime.api.Engines;
|
import com.stardust.autojs.runtime.api.Engines;
|
||||||
import com.stardust.autojs.runtime.api.Events;
|
import com.stardust.autojs.runtime.api.Events;
|
||||||
import com.stardust.autojs.runtime.api.Floaty;
|
import com.stardust.autojs.runtime.api.Floaty;
|
||||||
import com.stardust.autojs.runtime.api.Loopers;
|
import com.stardust.autojs.core.looper.Loopers;
|
||||||
import com.stardust.autojs.runtime.api.Threads;
|
import com.stardust.autojs.runtime.api.Threads;
|
||||||
import com.stardust.autojs.runtime.api.Timers;
|
import com.stardust.autojs.runtime.api.Timers;
|
||||||
import com.stardust.autojs.core.accessibility.UiSelector;
|
import com.stardust.autojs.core.accessibility.UiSelector;
|
||||||
@@ -168,6 +168,7 @@ public class ScriptRuntime {
|
|||||||
private AbstractShell mRootShell;
|
private AbstractShell mRootShell;
|
||||||
private Supplier<AbstractShell> mShellSupplier;
|
private Supplier<AbstractShell> mShellSupplier;
|
||||||
private ScreenMetrics mScreenMetrics = new ScreenMetrics();
|
private ScreenMetrics mScreenMetrics = new ScreenMetrics();
|
||||||
|
private Thread mThread;
|
||||||
|
|
||||||
|
|
||||||
protected ScriptRuntime(Builder builder) {
|
protected ScriptRuntime(Builder builder) {
|
||||||
@@ -197,6 +198,7 @@ public class ScriptRuntime {
|
|||||||
timers = new Timers(bridges);
|
timers = new Timers(bridges);
|
||||||
loopers = new Loopers(this);
|
loopers = new Loopers(this);
|
||||||
events = new Events(uiHandler.getContext(), accessibilityBridge, this);
|
events = new Events(uiHandler.getContext(), accessibilityBridge, this);
|
||||||
|
mThread = Thread.currentThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setApplicationContext(Context context) {
|
public static void setApplicationContext(Context context) {
|
||||||
@@ -248,12 +250,7 @@ public class ScriptRuntime {
|
|||||||
return ClipboardUtil.getClipOrEmpty(uiHandler.getContext()).toString();
|
return ClipboardUtil.getClipOrEmpty(uiHandler.getContext()).toString();
|
||||||
}
|
}
|
||||||
final VolatileDispose<String> clip = new VolatileDispose<>();
|
final VolatileDispose<String> clip = new VolatileDispose<>();
|
||||||
uiHandler.post(new Runnable() {
|
uiHandler.post(() -> clip.setAndNotify(ClipboardUtil.getClipOrEmpty(uiHandler.getContext()).toString()));
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
clip.setAndNotify(ClipboardUtil.getClipOrEmpty(uiHandler.getContext()).toString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return clip.blockedGetOrThrow(ScriptInterruptedException.class);
|
return clip.blockedGetOrThrow(ScriptInterruptedException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,7 +294,7 @@ public class ScriptRuntime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void exit() {
|
public void exit() {
|
||||||
Thread.currentThread().interrupt();
|
mThread.interrupt();
|
||||||
throw new ScriptInterruptedException();
|
throw new ScriptInterruptedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -320,6 +317,8 @@ public class ScriptRuntime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onExit() {
|
public void onExit() {
|
||||||
|
//清除interrupt状态
|
||||||
|
Thread.interrupted();
|
||||||
//悬浮窗需要第一时间关闭以免出现恶意脚本全屏悬浮窗屏蔽屏幕并且在exit中写死循环的问题
|
//悬浮窗需要第一时间关闭以免出现恶意脚本全屏悬浮窗屏蔽屏幕并且在exit中写死循环的问题
|
||||||
ignoresException(floaty::closeAll);
|
ignoresException(floaty::closeAll);
|
||||||
try {
|
try {
|
||||||
@@ -335,11 +334,9 @@ public class ScriptRuntime {
|
|||||||
mRootShell = null;
|
mRootShell = null;
|
||||||
mShellSupplier = null;
|
mShellSupplier = null;
|
||||||
});
|
});
|
||||||
ignoresException(() -> {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
ignoresException(images::releaseScreenCapturer);
|
||||||
images.releaseScreenCapturer();
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ignoresException(Runnable r) {
|
private void ignoresException(Runnable r) {
|
||||||
|
|||||||
@@ -2,19 +2,21 @@ package com.stardust.autojs.runtime.api;
|
|||||||
|
|
||||||
import android.accessibilityservice.AccessibilityServiceInfo;
|
import android.accessibilityservice.AccessibilityServiceInfo;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
import android.graphics.Point;
|
import android.graphics.Point;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
import android.provider.Settings;
|
||||||
|
import android.support.annotation.RequiresApi;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
import android.view.accessibility.AccessibilityEvent;
|
|
||||||
|
|
||||||
import com.stardust.autojs.R;
|
import com.stardust.autojs.R;
|
||||||
import com.stardust.autojs.core.accessibility.AccessibilityBridge;
|
import com.stardust.autojs.core.accessibility.AccessibilityBridge;
|
||||||
import com.stardust.autojs.core.eventloop.EventEmitter;
|
import com.stardust.autojs.core.eventloop.EventEmitter;
|
||||||
|
import com.stardust.autojs.core.looper.Loopers;
|
||||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||||
import com.stardust.notification.Notification;
|
import com.stardust.notification.Notification;
|
||||||
import com.stardust.notification.NotificationListenerService;
|
import com.stardust.notification.NotificationListenerService;
|
||||||
import com.stardust.autojs.runtime.ScriptBridges;
|
|
||||||
import com.stardust.autojs.runtime.exception.ScriptException;
|
import com.stardust.autojs.runtime.exception.ScriptException;
|
||||||
import com.stardust.autojs.core.inputevent.InputEventObserver;
|
import com.stardust.autojs.core.inputevent.InputEventObserver;
|
||||||
import com.stardust.autojs.core.inputevent.TouchObserver;
|
import com.stardust.autojs.core.inputevent.TouchObserver;
|
||||||
@@ -41,6 +43,7 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
|
|||||||
private Loopers mLoopers;
|
private Loopers mLoopers;
|
||||||
private Handler mHandler;
|
private Handler mHandler;
|
||||||
private boolean mListeningNotification = false;
|
private boolean mListeningNotification = false;
|
||||||
|
private boolean mListeningToast = false;
|
||||||
private ScriptRuntime mScriptRuntime;
|
private ScriptRuntime mScriptRuntime;
|
||||||
|
|
||||||
public Events(Context context, AccessibilityBridge accessibilityBridge, ScriptRuntime runtime) {
|
public Events(Context context, AccessibilityBridge accessibilityBridge, ScriptRuntime runtime) {
|
||||||
@@ -136,20 +139,30 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
|
|||||||
mTouchEventTimeout = touchEventTimeout;
|
mTouchEventTimeout = touchEventTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
|
||||||
public void observeNotification() {
|
public void observeNotification() {
|
||||||
|
mScriptRuntime.requiresApi(18);
|
||||||
if (mListeningNotification)
|
if (mListeningNotification)
|
||||||
return;
|
return;
|
||||||
mAccessibilityBridge.ensureServiceEnabled();
|
|
||||||
mListeningNotification = true;
|
mListeningNotification = true;
|
||||||
ensureHandler();
|
ensureHandler();
|
||||||
mLoopers.waitWhenIdle(true);
|
mLoopers.waitWhenIdle(true);
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
|
if (NotificationListenerService.getInstance() == null) {
|
||||||
&& NotificationListenerService.getInstance() != null) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
|
||||||
NotificationListenerService.getInstance().addListener(this);
|
mContext.startActivity(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS));
|
||||||
} else {
|
}
|
||||||
mAccessibilityBridge.getNotificationObserver().addNotificationListener(this);
|
throw new ScriptException(mContext.getString(R.string.exception_notification_service_disabled));
|
||||||
}
|
}
|
||||||
|
NotificationListenerService.getInstance().addListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void observeToast() {
|
||||||
|
if (mListeningToast)
|
||||||
|
return;
|
||||||
|
mAccessibilityBridge.ensureServiceEnabled();
|
||||||
|
mListeningToast = true;
|
||||||
|
ensureHandler();
|
||||||
|
mLoopers.waitWhenIdle(true);
|
||||||
mAccessibilityBridge.getNotificationObserver().addToastListener(this);
|
mAccessibilityBridge.getNotificationObserver().addToastListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,20 +200,17 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onKeyEvent(final int keyCode, final KeyEvent event) {
|
public void onKeyEvent(final int keyCode, final KeyEvent event) {
|
||||||
mHandler.post(new Runnable() {
|
mHandler.post(() -> {
|
||||||
@Override
|
String keyName = KeyEvent.keyCodeToString(keyCode).substring(8).toLowerCase();
|
||||||
public void run() {
|
emit(keyName, event);
|
||||||
String keyName = KeyEvent.keyCodeToString(keyCode).substring(8).toLowerCase();
|
if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
||||||
emit(keyName, event);
|
emit(PREFIX_KEY_DOWN + keyName, event);
|
||||||
if (event.getAction() == KeyEvent.ACTION_DOWN) {
|
emit("key_down", keyCode, event);
|
||||||
emit(PREFIX_KEY_DOWN + keyName, event);
|
} else if (event.getAction() == KeyEvent.ACTION_UP) {
|
||||||
emit("key_down", keyCode, event);
|
emit(PREFIX_KEY_UP + keyName, event);
|
||||||
} else if (event.getAction() == KeyEvent.ACTION_UP) {
|
emit("key_up", keyCode, event);
|
||||||
emit(PREFIX_KEY_UP + keyName, event);
|
|
||||||
emit("key_up", keyCode, event);
|
|
||||||
}
|
|
||||||
emit("key", keyCode, event);
|
|
||||||
}
|
}
|
||||||
|
emit("key", keyCode, event);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,31 +220,16 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mLastTouchEventMillis = System.currentTimeMillis();
|
mLastTouchEventMillis = System.currentTimeMillis();
|
||||||
mHandler.post(new Runnable() {
|
mHandler.post(() -> emit("touch", new Point(x, y)));
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
emit("touch", new Point(x, y));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onNotification(final Notification notification) {
|
public void onNotification(final Notification notification) {
|
||||||
mHandler.post(new Runnable() {
|
mHandler.post(() -> emit("notification", notification));
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
emit("notification", notification);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onToast(final AccessibilityNotificationObserver.Toast toast) {
|
public void onToast(final AccessibilityNotificationObserver.Toast toast) {
|
||||||
mHandler.post(new Runnable() {
|
mHandler.post(() -> emit("toast", toast));
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
emit("toast", toast);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,119 +0,0 @@
|
|||||||
package com.stardust.autojs.runtime.api;
|
|
||||||
|
|
||||||
import android.os.Looper;
|
|
||||||
import android.os.MessageQueue;
|
|
||||||
|
|
||||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
|
||||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
|
||||||
import com.stardust.lang.ThreadCompat;
|
|
||||||
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by Stardust on 2017/7/29.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class Loopers {
|
|
||||||
|
|
||||||
public interface LooperQuitHandler {
|
|
||||||
boolean shouldQuit();
|
|
||||||
}
|
|
||||||
|
|
||||||
public volatile boolean waitWhenIdle = false;
|
|
||||||
private volatile Looper mServantLooper;
|
|
||||||
private static volatile ConcurrentHashMap<Thread, Looper> sLoopers = new ConcurrentHashMap<>();
|
|
||||||
private Timers mTimers;
|
|
||||||
private ScriptRuntime mScriptRuntime;
|
|
||||||
private LooperQuitHandler mLooperQuitHandler;
|
|
||||||
|
|
||||||
public Loopers(ScriptRuntime runtime) {
|
|
||||||
mTimers = runtime.timers;
|
|
||||||
mScriptRuntime = runtime;
|
|
||||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
|
||||||
waitWhenIdle = true;
|
|
||||||
}
|
|
||||||
Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
|
|
||||||
@Override
|
|
||||||
public boolean queueIdle() {
|
|
||||||
Looper l = Looper.myLooper();
|
|
||||||
if (l != null && shouldQuitLooper()) {
|
|
||||||
if (mLooperQuitHandler != null && mLooperQuitHandler.shouldQuit()) {
|
|
||||||
l.quit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean shouldQuitLooper() {
|
|
||||||
if (mTimers.hasPendingCallback()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return !waitWhenIdle;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void initServantThread() {
|
|
||||||
new ThreadCompat(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
Looper.prepare();
|
|
||||||
final Object lock = Loopers.this;
|
|
||||||
mServantLooper = Looper.myLooper();
|
|
||||||
synchronized (lock) {
|
|
||||||
lock.notifyAll();
|
|
||||||
}
|
|
||||||
Looper.loop();
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Looper getServantLooper() {
|
|
||||||
if (mServantLooper == null) {
|
|
||||||
initServantThread();
|
|
||||||
synchronized (this) {
|
|
||||||
try {
|
|
||||||
this.wait();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new ScriptInterruptedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return mServantLooper;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void quitServantLooper() {
|
|
||||||
if (mServantLooper == null)
|
|
||||||
return;
|
|
||||||
mServantLooper.quit();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void waitWhenIdle(boolean b) {
|
|
||||||
waitWhenIdle = b;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void quitAll() {
|
|
||||||
quitServantLooper();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void prepare() {
|
|
||||||
if (Looper.myLooper() == Looper.getMainLooper())
|
|
||||||
return;
|
|
||||||
if (Looper.myLooper() == null)
|
|
||||||
Looper.prepare();
|
|
||||||
Looper l = Looper.myLooper();
|
|
||||||
if (l != null)
|
|
||||||
sLoopers.put(Thread.currentThread(), l);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void quitForThread(Thread thread) {
|
|
||||||
Looper looper = sLoopers.remove(thread);
|
|
||||||
if (looper != null)
|
|
||||||
looper.quit();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLooperQuitHandler(LooperQuitHandler looperQuitHandler) {
|
|
||||||
mLooperQuitHandler = looperQuitHandler;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,21 +1,23 @@
|
|||||||
package com.stardust.autojs.runtime.api;
|
package com.stardust.autojs.runtime.api;
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import com.stardust.autojs.core.looper.TimerThread;
|
||||||
import com.stardust.autojs.engine.RhinoJavaScriptEngine;
|
import com.stardust.autojs.engine.RhinoJavaScriptEngine;
|
||||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
|
||||||
import com.stardust.concurrent.VolatileBox;
|
import com.stardust.concurrent.VolatileBox;
|
||||||
import com.stardust.concurrent.VolatileDispose;
|
import com.stardust.concurrent.VolatileDispose;
|
||||||
import com.stardust.lang.ThreadCompat;
|
|
||||||
import com.stardust.pio.PFile;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentSkipListSet;
|
import java.util.concurrent.ConcurrentSkipListSet;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Stardust on 2017/12/3.
|
* Created by Stardust on 2017/12/3.
|
||||||
@@ -23,30 +25,38 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
|
|
||||||
public class Threads {
|
public class Threads {
|
||||||
|
|
||||||
private List<ThreadCompat> mThreads = new ArrayList<>();
|
private final HashSet<Thread> mThreads = new HashSet<>();
|
||||||
private ScriptRuntime mScriptRuntime;
|
private ScriptRuntime mRuntime;
|
||||||
|
|
||||||
public Threads(ScriptRuntime scriptRuntime) {
|
public Threads(ScriptRuntime runtime) {
|
||||||
mScriptRuntime = scriptRuntime;
|
mRuntime = runtime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start(Runnable runnable) {
|
public TimerThread start(Runnable runnable) {
|
||||||
ThreadCompat threadCompat = new ThreadCompat(() -> {
|
TimerThread thread = startThread(runnable);
|
||||||
try {
|
synchronized (mThreads) {
|
||||||
((RhinoJavaScriptEngine) mScriptRuntime.engines.myEngine()).createContext();
|
mThreads.add(thread);
|
||||||
runnable.run();
|
}
|
||||||
} catch (Exception e) {
|
thread.start();
|
||||||
if (!ScriptInterruptedException.causedByInterrupted(e)) {
|
return thread;
|
||||||
mScriptRuntime.console.error(e);
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private TimerThread startThread(Runnable runnable) {
|
||||||
|
return new TimerThread(mRuntime, mRuntime.timers.getMaxCallbackUptimeMillisForAllThreads(),
|
||||||
|
() -> {
|
||||||
|
((RhinoJavaScriptEngine) mRuntime.engines.myEngine()).createContext();
|
||||||
|
runnable.run();
|
||||||
}
|
}
|
||||||
|
) {
|
||||||
|
@Override
|
||||||
|
protected void onExit() {
|
||||||
|
synchronized (mThreads) {
|
||||||
|
mThreads.remove(Thread.currentThread());
|
||||||
|
}
|
||||||
|
super.onExit();
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
mThreads.add(threadCompat);
|
|
||||||
threadCompat.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
public VolatileBox variable() {
|
|
||||||
return new VolatileBox();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public VolatileDispose disposable() {
|
public VolatileDispose disposable() {
|
||||||
@@ -65,16 +75,27 @@ public class Threads {
|
|||||||
return new ConcurrentHashMap();
|
return new ConcurrentHashMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
public AtomicInteger atomicInt() {
|
public AtomicLong atomic() {
|
||||||
return new AtomicInteger();
|
return new AtomicLong();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AtomicLong atomic(long value) {
|
||||||
|
return new AtomicLong(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void shutDownAll() {
|
public void shutDownAll() {
|
||||||
for (ThreadCompat threadCompat : mThreads) {
|
synchronized (mThreads) {
|
||||||
threadCompat.interrupt();
|
for (Thread thread : mThreads) {
|
||||||
|
thread.interrupt();
|
||||||
|
}
|
||||||
|
mThreads.clear();
|
||||||
}
|
}
|
||||||
mThreads.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean hasRunningThreads() {
|
||||||
|
synchronized (mThreads) {
|
||||||
|
return !mThreads.isEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,10 @@ import android.os.SystemClock;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.SparseArray;
|
import android.util.SparseArray;
|
||||||
|
|
||||||
|
import com.stardust.autojs.core.looper.Timer;
|
||||||
|
import com.stardust.autojs.core.looper.TimerThread;
|
||||||
import com.stardust.autojs.runtime.ScriptBridges;
|
import com.stardust.autojs.runtime.ScriptBridges;
|
||||||
|
import com.stardust.concurrent.VolatileBox;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Stardust on 2017/7/21.
|
* Created by Stardust on 2017/7/21.
|
||||||
@@ -13,99 +16,61 @@ import com.stardust.autojs.runtime.ScriptBridges;
|
|||||||
|
|
||||||
public class Timers {
|
public class Timers {
|
||||||
|
|
||||||
private SparseArray<Runnable> mHandlerCallbacks = new SparseArray<>();
|
private static final String LOG_TAG = "Timers";
|
||||||
private int mCallbackMaxId = 0;
|
|
||||||
private ScriptBridges mBridges;
|
private VolatileBox<Long> mMaxCallbackUptimeMillisForAllThreads = new VolatileBox<>(0L);
|
||||||
private ThreadLocal<Handler> mHandler = new ThreadLocal<>();
|
private Thread mMainThread;
|
||||||
private long mFutureCallbackUptimeMillis = 0;
|
private Timer mMainTimer;
|
||||||
|
|
||||||
public Timers(ScriptBridges bridges) {
|
public Timers(ScriptBridges bridges) {
|
||||||
mBridges = bridges;
|
mMainThread = Thread.currentThread();
|
||||||
|
mMainTimer = new Timer(bridges, mMaxCallbackUptimeMillisForAllThreads);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureHandler() {
|
public VolatileBox<Long> getMaxCallbackUptimeMillisForAllThreads() {
|
||||||
if (mHandler.get() == null) {
|
return mMaxCallbackUptimeMillisForAllThreads;
|
||||||
mHandler.set(new Handler());
|
}
|
||||||
|
|
||||||
|
private Timer getTimerForCurrentThread() {
|
||||||
|
if (Thread.currentThread() == mMainThread) {
|
||||||
|
return mMainTimer;
|
||||||
}
|
}
|
||||||
|
return TimerThread.getTimerForCurrentThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int setTimeout(final Object callback, final long delay, final Object... args) {
|
public int setTimeout(Object callback, long delay, Object... args) {
|
||||||
ensureHandler();
|
return getTimerForCurrentThread().setTimeout(callback, delay, args);
|
||||||
mCallbackMaxId++;
|
|
||||||
final int id = mCallbackMaxId;
|
|
||||||
Runnable r = () -> {
|
|
||||||
mBridges.callFunction(callback, null, args);
|
|
||||||
mHandlerCallbacks.remove(id);
|
|
||||||
};
|
|
||||||
mHandlerCallbacks.put(id, r);
|
|
||||||
postDelayed(r, delay);
|
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean clearTimeout(int id) {
|
public boolean clearTimeout(int id) {
|
||||||
return clearCallback(id);
|
return getTimerForCurrentThread().clearTimeout(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int setInterval(final Object listener, final long interval, final Object... args) {
|
public int setInterval(Object listener, long interval, Object... args) {
|
||||||
ensureHandler();
|
return getTimerForCurrentThread().setInterval(listener, interval, args);
|
||||||
mCallbackMaxId++;
|
|
||||||
final int id = mCallbackMaxId;
|
|
||||||
final Runnable r = new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (mHandlerCallbacks.get(id) == null)
|
|
||||||
return;
|
|
||||||
mBridges.callFunction(listener, null, args);
|
|
||||||
postDelayed(this, interval);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
mHandlerCallbacks.put(id, r);
|
|
||||||
postDelayed(r, interval);
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void postDelayed(Runnable r, long interval) {
|
|
||||||
long uptime = SystemClock.uptimeMillis() + interval;
|
|
||||||
mHandler.get().postAtTime(r, uptime);
|
|
||||||
mFutureCallbackUptimeMillis = Math.max(mFutureCallbackUptimeMillis, uptime);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean clearInterval(int id) {
|
public boolean clearInterval(int id) {
|
||||||
return clearCallback(id);
|
return getTimerForCurrentThread().clearInterval(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int setImmediate(final Object listener, final Object... args) {
|
public int setImmediate(Object listener, Object... args) {
|
||||||
ensureHandler();
|
return getTimerForCurrentThread().setImmediate(listener, args);
|
||||||
mCallbackMaxId++;
|
|
||||||
final int id = mCallbackMaxId;
|
|
||||||
Runnable r = new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
mBridges.callFunction(listener, null, args);
|
|
||||||
mHandlerCallbacks.remove(id);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
mHandlerCallbacks.put(id, r);
|
|
||||||
postDelayed(r, 0);
|
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean clearImmediate(int id) {
|
public boolean clearImmediate(int id) {
|
||||||
return clearCallback(id);
|
return getTimerForCurrentThread().clearImmediate(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean clearCallback(int id) {
|
public boolean hasPendingCallbacks() {
|
||||||
Runnable callback = mHandlerCallbacks.get(id);
|
//如果是脚本主线程,则检查所有子线程中的定时回调。mFutureCallbackUptimeMillis用来记录所有子线程中定时最久的一个。
|
||||||
if (callback != null) {
|
if (mMainThread == Thread.currentThread()) {
|
||||||
mHandler.get().removeCallbacks(callback);
|
Log.d(LOG_TAG, "[main thread]hasPendingCallbacks:" + (mMaxCallbackUptimeMillisForAllThreads.get() > SystemClock.uptimeMillis()));
|
||||||
mHandlerCallbacks.remove(id);
|
Log.d(LOG_TAG, "mMaxCallbackUptimeMillisForAllThreads:" + mMaxCallbackUptimeMillisForAllThreads.get());
|
||||||
return true;
|
return mMaxCallbackUptimeMillisForAllThreads.get() > SystemClock.uptimeMillis();
|
||||||
}
|
}
|
||||||
return false;
|
//否则检查当前线程的定时回调
|
||||||
|
return getTimerForCurrentThread().hasPendingCallbacks();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean hasPendingCallback() {
|
|
||||||
return mFutureCallbackUptimeMillis > SystemClock.uptimeMillis();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
<string name="_app_name">AutoJs</string>
|
<string name="_app_name">AutoJs</string>
|
||||||
<string name="text_should_enable_key_observing">按键监听未启用,请在软件设置中开启</string>
|
<string name="text_should_enable_key_observing">按键监听未启用,请在软件设置中开启</string>
|
||||||
<string name="no_write_settings_permissin">沒有修改系統设置权限</string>
|
<string name="no_write_settings_permissin">沒有修改系統设置权限</string>
|
||||||
|
<string name="exception_notification_service_disabled">通知服务未运行,请重新启用通知权限</string>
|
||||||
|
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.stardust.concurrent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Stardust on 2017/12/27.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Value<T> {
|
||||||
|
|
||||||
|
private T mValue;
|
||||||
|
|
||||||
|
public Value(T value) {
|
||||||
|
mValue = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Value() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public T get() {
|
||||||
|
return mValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(T value) {
|
||||||
|
mValue = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -10,14 +10,14 @@ import java.util.WeakHashMap;
|
|||||||
|
|
||||||
public class ThreadCompat extends Thread {
|
public class ThreadCompat extends Thread {
|
||||||
|
|
||||||
private static volatile WeakHashMap<Thread, Boolean> interruptStatus = new WeakHashMap<>();
|
// private static volatile WeakHashMap<Thread, Boolean> interruptStatus = new WeakHashMap<>();
|
||||||
|
|
||||||
public ThreadCompat() {
|
public ThreadCompat() {
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init() {
|
private void init() {
|
||||||
interruptStatus.put(this, false);
|
// interruptStatus.put(this, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ThreadCompat(Runnable target) {
|
public ThreadCompat(Runnable target) {
|
||||||
@@ -59,23 +59,23 @@ public class ThreadCompat extends Thread {
|
|||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
super.run();
|
super.run();
|
||||||
interruptStatus.remove(this);
|
// interruptStatus.remove(this);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
interruptStatus.remove(this);
|
// interruptStatus.remove(this);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isInterrupted() {
|
public boolean isInterrupted() {
|
||||||
Boolean isInterrupted = interruptStatus.get(this);
|
//Boolean isInterrupted = interruptStatus.get(this);
|
||||||
return super.isInterrupted() || isInterrupted == null || isInterrupted;
|
return super.isInterrupted();// || isInterrupted == null || isInterrupted;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void interrupt() {
|
public void interrupt() {
|
||||||
super.interrupt();
|
super.interrupt();
|
||||||
interruptStatus.remove(this);
|
//interruptStatus.put(this, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -76,6 +76,10 @@ public class PFiles {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean createWithDirs(String path) {
|
||||||
|
return createIfNotExists(path);
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean exists(String path) {
|
public static boolean exists(String path) {
|
||||||
return new File(path).exists();
|
return new File(path).exists();
|
||||||
}
|
}
|
||||||
@@ -212,6 +216,7 @@ public class PFiles {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void append(String path, String text) {
|
public static void append(String path, String text) {
|
||||||
|
create(path);
|
||||||
try {
|
try {
|
||||||
write(new FileOutputStream(path, true), text);
|
write(new FileOutputStream(path, true), text);
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
@@ -221,6 +226,7 @@ public class PFiles {
|
|||||||
|
|
||||||
|
|
||||||
public static void append(String path, String text, String encoding) {
|
public static void append(String path, String text, String encoding) {
|
||||||
|
create(path);
|
||||||
try {
|
try {
|
||||||
write(new FileOutputStream(path, true), text, encoding);
|
write(new FileOutputStream(path, true), text, encoding);
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
@@ -238,6 +244,7 @@ public class PFiles {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void appendBytes(String path, byte[] bytes) {
|
public static void appendBytes(String path, byte[] bytes) {
|
||||||
|
create(path);
|
||||||
try {
|
try {
|
||||||
writeBytes(new FileOutputStream(path, true), bytes);
|
writeBytes(new FileOutputStream(path, true), bytes);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
Reference in new issue
Block a user