chore
This commit is contained in:
@@ -24,12 +24,18 @@ module.exports = function(__runtime__, scope){
|
||||
}
|
||||
}
|
||||
if (i.action) {
|
||||
if(i.action.indexOf(".") == -1){
|
||||
i.action = "android.intent.action." + i.action;
|
||||
}
|
||||
intent.setAction(i.action);
|
||||
}
|
||||
if (i.type) {
|
||||
intent.setType(i.type);
|
||||
}
|
||||
if (i.data) {
|
||||
if(i.data){
|
||||
intent.setDataAndType(android.net.Uri.parse(i.data), i.type);
|
||||
}else{
|
||||
intent.setType(i.type);
|
||||
}
|
||||
}else if (i.data) {
|
||||
intent.setData(android.net.Uri.parse(i.data));
|
||||
}
|
||||
return intent;
|
||||
|
||||
@@ -10,6 +10,8 @@ import com.stardust.autojs.runtime.api.Timers;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.lang.ThreadCompat;
|
||||
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/7/29.
|
||||
*/
|
||||
@@ -24,6 +26,7 @@ public class Loopers implements MessageQueue.IdleHandler {
|
||||
};
|
||||
|
||||
private volatile ThreadLocal<Boolean> waitWhenIdle = new ThreadLocal<>();
|
||||
private volatile ThreadLocal<CopyOnWriteArrayList<LooperQuitHandler>> looperQuitHanders = new ThreadLocal<>();
|
||||
private volatile Looper mServantLooper;
|
||||
private Timers mTimers;
|
||||
private ScriptRuntime mScriptRuntime;
|
||||
@@ -48,6 +51,20 @@ public class Loopers implements MessageQueue.IdleHandler {
|
||||
return mMainLooper;
|
||||
}
|
||||
|
||||
public void addLooperQuiteHandler(LooperQuitHandler handler) {
|
||||
CopyOnWriteArrayList<LooperQuitHandler> handlers = looperQuitHanders.get();
|
||||
if (handlers == null) {
|
||||
handlers = new CopyOnWriteArrayList<>();
|
||||
looperQuitHanders.set(handlers);
|
||||
}
|
||||
handlers.add(handler);
|
||||
}
|
||||
|
||||
public boolean removeLooperQuiteHandler(LooperQuitHandler handler) {
|
||||
CopyOnWriteArrayList<LooperQuitHandler> handlers = looperQuitHanders.get();
|
||||
return handlers != null && handlers.remove(handler);
|
||||
}
|
||||
|
||||
private boolean shouldQuitLooper() {
|
||||
if (Thread.currentThread().isInterrupted()) {
|
||||
return true;
|
||||
@@ -55,7 +72,19 @@ public class Loopers implements MessageQueue.IdleHandler {
|
||||
if (mTimers.hasPendingCallbacks()) {
|
||||
return false;
|
||||
}
|
||||
return !waitWhenIdle.get();
|
||||
if (waitWhenIdle.get()) {
|
||||
return false;
|
||||
}
|
||||
CopyOnWriteArrayList<LooperQuitHandler> handlers = looperQuitHanders.get();
|
||||
if (handlers == null) {
|
||||
return true;
|
||||
}
|
||||
for (LooperQuitHandler handler : handlers) {
|
||||
if (!handler.shouldQuit()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -173,7 +173,7 @@ public class ScriptRuntime {
|
||||
public final Files files;
|
||||
|
||||
@ScriptVariable
|
||||
public final Sensors sensors;
|
||||
public Sensors sensors;
|
||||
|
||||
|
||||
private Images images;
|
||||
@@ -205,7 +205,6 @@ public class ScriptRuntime {
|
||||
device = new Device(context);
|
||||
floaty = new Floaty(uiHandler, ui, this);
|
||||
files = new Files(this);
|
||||
sensors = new Sensors(context, bridges);
|
||||
}
|
||||
|
||||
public void init() {
|
||||
@@ -216,6 +215,7 @@ public class ScriptRuntime {
|
||||
loopers = new Loopers(this);
|
||||
events = new Events(uiHandler.getContext(), accessibilityBridge, this);
|
||||
mThread = Thread.currentThread();
|
||||
sensors = new Sensors(uiHandler.getContext(), this);
|
||||
}
|
||||
|
||||
public static void setApplicationContext(Context context) {
|
||||
|
||||
@@ -1,19 +1,12 @@
|
||||
package com.stardust.autojs.runtime.api;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
|
||||
import com.stardust.autojs.engine.ScriptEngine;
|
||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||
import com.stardust.autojs.script.JavaScriptSource;
|
||||
import com.stardust.pio.PFileInterface;
|
||||
import com.stardust.pio.PFiles;
|
||||
import com.stardust.util.Func1;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/1/23.
|
||||
@@ -93,6 +86,10 @@ public class Files {
|
||||
return PFiles.read(path(path));
|
||||
}
|
||||
|
||||
public byte[] readBytes(String path){
|
||||
return PFiles.readBytes(path(path));
|
||||
}
|
||||
|
||||
public void write(String path, String text) {
|
||||
PFiles.write(path(path), text);
|
||||
}
|
||||
|
||||
@@ -8,9 +8,12 @@ import android.hardware.SensorManager;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.stardust.autojs.core.eventloop.EventEmitter;
|
||||
import com.stardust.autojs.core.looper.Loopers;
|
||||
import com.stardust.autojs.runtime.ScriptBridges;
|
||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||
import com.stardust.util.MapEntries;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -20,7 +23,8 @@ import java.util.concurrent.CopyOnWriteArraySet;
|
||||
* Created by Stardust on 2018/2/5.
|
||||
*/
|
||||
|
||||
public class Sensors extends EventEmitter {
|
||||
public class Sensors extends EventEmitter implements Loopers.LooperQuitHandler {
|
||||
|
||||
|
||||
public class SensorEventEmitter extends EventEmitter implements SensorEventListener {
|
||||
|
||||
@@ -79,13 +83,16 @@ public class Sensors extends EventEmitter {
|
||||
private final SensorManager mSensorManager;
|
||||
private final ScriptBridges mScriptBridges;
|
||||
private final SensorEventEmitter mNoOpSensorEventEmitter;
|
||||
private final ScriptRuntime mScriptRuntime;
|
||||
|
||||
|
||||
public Sensors(Context context, ScriptBridges bridges) {
|
||||
super(bridges);
|
||||
public Sensors(Context context, ScriptRuntime runtime) {
|
||||
super(runtime.bridges);
|
||||
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
|
||||
mScriptBridges = bridges;
|
||||
mNoOpSensorEventEmitter = new SensorEventEmitter(bridges);
|
||||
mScriptBridges = runtime.bridges;
|
||||
mNoOpSensorEventEmitter = new SensorEventEmitter(runtime.bridges);
|
||||
mScriptRuntime = runtime;
|
||||
runtime.loopers.addLooperQuiteHandler(this);
|
||||
}
|
||||
|
||||
public SensorEventEmitter register(String sensorName) {
|
||||
@@ -107,23 +114,6 @@ public class Sensors extends EventEmitter {
|
||||
return register(sensor, delay);
|
||||
}
|
||||
|
||||
public SensorEventEmitter registerByType(int sensorType, int delay) {
|
||||
Sensor sensor = mSensorManager.getDefaultSensor(sensorType);
|
||||
if (sensor == null) {
|
||||
if (ignoresUnsupportedSensor) {
|
||||
emit("unsupported_sensor", null, sensorType);
|
||||
return mNoOpSensorEventEmitter;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return register(sensor, delay);
|
||||
}
|
||||
|
||||
public SensorEventEmitter registerByType(int sensorType) {
|
||||
return registerByType(sensorType, delay.normal);
|
||||
}
|
||||
|
||||
private SensorEventEmitter register(@NonNull Sensor sensor, int delay) {
|
||||
SensorEventEmitter emitter = new SensorEventEmitter(mScriptBridges);
|
||||
mSensorManager.registerListener(emitter, sensor, delay);
|
||||
@@ -134,13 +124,33 @@ public class Sensors extends EventEmitter {
|
||||
}
|
||||
|
||||
|
||||
private Sensor getSensor(String sensorName) {
|
||||
@Override
|
||||
public boolean shouldQuit() {
|
||||
if (mSensorEventEmitters.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Sensor getSensor(String sensorName) {
|
||||
Integer type = SENSORS.get(sensorName.toUpperCase());
|
||||
if (type == null)
|
||||
type = getSensorTypeByReflect(sensorName);
|
||||
if (type == null)
|
||||
return null;
|
||||
return mSensorManager.getDefaultSensor(type);
|
||||
}
|
||||
|
||||
private Integer getSensorTypeByReflect(String sensorName) {
|
||||
sensorName = sensorName.toUpperCase();
|
||||
try {
|
||||
Field field = Sensor.class.getField("TYPE_" + sensorName);
|
||||
return (Integer) field.get(null);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void unregister(SensorEventEmitter emitter) {
|
||||
if (emitter == null)
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user