api: module sensors

index: all.json
This commit is contained in:
hyb1996
2018-02-06 12:41:23 +08:00
parent 464c2498db
commit 76fd77f1c7
9 changed files with 883 additions and 89 deletions

View File

@@ -79,7 +79,8 @@ require("__globals__")(__runtime__, this);
(function(scope){
var modules = ['app', 'automator', 'console', 'dialogs', 'io', 'selector', 'shell', 'web', 'ui',
"images", "timers", "threads", "events", "engines", "RootAutomator", "http", "storages", "floaty"];
"images", "timers", "threads", "events", "engines", "RootAutomator", "http", "storages", "floaty",
"sensors"];
var len = modules.length;
for(var i = 0; i < len; i++) {
var m = modules[i];

View File

@@ -0,0 +1,4 @@
module.exports = function(runtime, global){
var sensors = Object.create(runtime.sensors);
return sensors;
};

View File

@@ -21,6 +21,7 @@ import com.stardust.autojs.runtime.api.Events;
import com.stardust.autojs.runtime.api.Files;
import com.stardust.autojs.runtime.api.Floaty;
import com.stardust.autojs.core.looper.Loopers;
import com.stardust.autojs.runtime.api.Sensors;
import com.stardust.autojs.runtime.api.Threads;
import com.stardust.autojs.runtime.api.Timers;
import com.stardust.autojs.core.accessibility.UiSelector;
@@ -171,6 +172,10 @@ public class ScriptRuntime {
@ScriptVariable
public final Files files;
@ScriptVariable
public final Sensors sensors;
private Images images;
private static WeakReference<Context> applicationContext;
@@ -182,24 +187,25 @@ public class ScriptRuntime {
protected ScriptRuntime(Builder builder) {
app = builder.mAppUtils;
uiHandler = builder.mUiHandler;
Context context = uiHandler.getContext();
app = builder.mAppUtils;
console = builder.mConsole;
accessibilityBridge = builder.mAccessibilityBridge;
mShellSupplier = builder.mShellSupplier;
ui = new UI(uiHandler.getContext(), this);
ui = new UI(context, this);
this.automator = new SimpleActionAutomator(accessibilityBridge, this);
automator.setScreenMetrics(mScreenMetrics);
this.info = accessibilityBridge.getInfoProvider();
Context context = uiHandler.getContext();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
images = new Images(context, this, builder.mScreenCaptureRequester);
}
engines = new Engines(builder.mEngineService, this);
dialogs = new Dialogs(app, uiHandler, bridges);
device = new Device(uiHandler.getContext());
device = new Device(context);
floaty = new Floaty(uiHandler, ui, this);
files = new Files(this);
sensors = new Sensors(context, bridges);
}
public void init() {
@@ -369,6 +375,7 @@ public class ScriptRuntime {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
ignoresException(images::releaseScreenCapturer);
}
ignoresException(sensors::unregisterAll);
}
private void ignoresException(Runnable r) {

View File

@@ -0,0 +1,161 @@
package com.stardust.autojs.runtime.api;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.annotation.NonNull;
import com.stardust.autojs.core.eventloop.EventEmitter;
import com.stardust.autojs.runtime.ScriptBridges;
import com.stardust.util.MapEntries;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* Created by Stardust on 2018/2/5.
*/
public class Sensors extends EventEmitter {
public class SensorEventEmitter extends EventEmitter implements SensorEventListener {
public SensorEventEmitter(ScriptBridges bridges) {
super(bridges);
}
@Override
public void onSensorChanged(SensorEvent event) {
Object[] args = new Object[event.values.length + 1];
args[0] = event;
for (int i = 1; i < args.length; i++) {
args[i] = event.values[i - 1];
}
emit("change", args);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
emit("accuracy_change", accuracy);
}
public void unregister() {
Sensors.this.unregister(this);
}
}
public static class Delay {
public static final int normal = SensorManager.SENSOR_DELAY_NORMAL;
public static final int ui = SensorManager.SENSOR_DELAY_UI;
public static final int game = SensorManager.SENSOR_DELAY_GAME;
public static final int fastest = SensorManager.SENSOR_DELAY_FASTEST;
}
private static final Map<String, Integer> SENSORS = new MapEntries<String, Integer>()
.entry("ACCELEROMETER", Sensor.TYPE_ACCELEROMETER)
.entry("MAGNETIC_FIELD", Sensor.TYPE_MAGNETIC_FIELD)
.entry("ORIENTATION", Sensor.TYPE_ORIENTATION)
.entry("GYROSCOPE", Sensor.TYPE_GYROSCOPE)
.entry("LIGHT", Sensor.TYPE_LIGHT)
.entry("TEMPERATURE", Sensor.TYPE_TEMPERATURE)
.entry("PRESSURE", Sensor.TYPE_PRESSURE)
.entry("AMBIENT_TEMPERATURE", Sensor.TYPE_AMBIENT_TEMPERATURE)
.entry("PROXIMITY", Sensor.TYPE_PROXIMITY)
.entry("GRAVITY", Sensor.TYPE_GRAVITY)
.entry("LINEAR_ACCELERATION", Sensor.TYPE_LINEAR_ACCELERATION)
.entry("RELATIVE_HUMIDITY", Sensor.TYPE_RELATIVE_HUMIDITY)
.entry("AMBIENT_TEMPERATURE", Sensor.TYPE_AMBIENT_TEMPERATURE)
.map();
public boolean ignoresUnsupportedSensor = false;
public final Delay delay = new Delay();
private final Set<SensorEventEmitter> mSensorEventEmitters = new HashSet<>();
private final SensorManager mSensorManager;
private final ScriptBridges mScriptBridges;
private final SensorEventEmitter mNoOpSensorEventEmitter;
public Sensors(Context context, ScriptBridges bridges) {
super(bridges);
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mScriptBridges = bridges;
mNoOpSensorEventEmitter = new SensorEventEmitter(bridges);
}
public SensorEventEmitter register(String sensorName) {
return register(sensorName, delay.normal);
}
public SensorEventEmitter register(String sensorName, int delay) {
if (sensorName == null)
throw new NullPointerException("sensorName = null");
Sensor sensor = getSensor(sensorName);
if (sensor == null) {
if (ignoresUnsupportedSensor) {
emit("unsupported_sensor", sensorName);
return mNoOpSensorEventEmitter;
} else {
return null;
}
}
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);
synchronized (mSensorEventEmitters) {
mSensorEventEmitters.add(emitter);
}
return emitter;
}
private Sensor getSensor(String sensorName) {
Integer type = SENSORS.get(sensorName.toUpperCase());
if (type == null)
return null;
return mSensorManager.getDefaultSensor(type);
}
public void unregister(SensorEventEmitter emitter) {
if (emitter == null)
return;
synchronized (mSensorEventEmitters) {
mSensorEventEmitters.remove(emitter);
}
mSensorManager.unregisterListener(emitter);
}
public void unregisterAll() {
synchronized (mSensorEventEmitters) {
for (SensorEventEmitter emitter : mSensorEventEmitters) {
mSensorManager.unregisterListener(emitter);
}
mSensorEventEmitters.clear();
}
}
}