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

@ -31,34 +31,6 @@
</value>
</option>
<option name="RIGHT_MARGIN" value="100" />
<AndroidXmlCodeStyleSettings>
<option name="USE_CUSTOM_SETTINGS" value="true" />
</AndroidXmlCodeStyleSettings>
<Objective-C-extensions>
<file>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
</file>
<class>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
</class>
<extensions>
<pair source="cpp" header="h" />
<pair source="c" header="h" />
</extensions>
</Objective-C-extensions>
<XML>
<option name="XML_KEEP_LINE_BREAKS" value="false" />
<option name="XML_ALIGN_ATTRIBUTES" value="false" />

View File

@ -8,8 +8,8 @@ android {
applicationId "com.stardust.scriptdroid"
minSdkVersion 17
targetSdkVersion 23
versionCode 248
versionName "3.0.0 Beta2"
versionCode 249
versionName "3.0.0 Beta3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
ndk {

View File

@ -662,121 +662,151 @@
},
{
"name": "files",
"url": "files.html",
"summary": "文件",
"properties": [
{
"key": "open",
"url": "",
"summary": "打开文件",
"global": true
},
{
"key": "isFile",
"url": "files.html#files_files_isfile_path",
"summary": "文件判断"
"summary": "文件判断",
"url": "files.html#files_files_isfile_path"
},
{
"key": "isDir",
"url": "files.html#files_files_isdir_path",
"summary": "文件夹判断"
"summary": "文件夹判断",
"url": "files.html#files_files_isdir_path"
},
{
"key": "isEmptyDir",
"url": "files.html#files_files_isemptydir_path",
"summary": "空文件夹判断"
"summary": "空文件夹判断",
"url": "files.html#files_files_isemptydir_path"
},
{
"key": "join",
"url": "files.html#files_files_join_parent_child",
"summary": "连接路径"
"summary": "连接路径",
"url": "files.html#files_files_join_parent_child"
},
{
"key": "create",
"url": "files.html#files_files_create_path",
"summary": "创建文件"
"summary": "创建文件",
"url": "files.html#files_files_create_path"
},
{
"key": "createWithDirs",
"url": "files.html#files_files_createwithdirs_path",
"summary": "创建文件"
"summary": "创建文件",
"url": "files.html#files_files_createwithdirs_path"
},
{
"key": "exists",
"url": "files.html#files_files_exists_path",
"summary": "判断文件存在"
"summary": "判断文件存在",
"url": "files.html#files_files_exists_path"
},
{
"key": "ensureDir",
"url": "files.html#files_files_ensuredir_path",
"summary": "确保文件夹"
"summary": "确保文件夹",
"url": "files.html#files_files_ensuredir_path"
},
{
"key": "read",
"url": "files.html#files_files_read_path_encoding_utf_8",
"summary": "读取文本"
"summary": "读取文本",
"url": "files.html#files_files_read_path_encoding_utf_8"
},
{
"key": "readBytes",
"summary": "读取字节",
"url": "files.html#files_files_readbytes_path"
},
{
"key": "write",
"url": "files.html#files_files_write_path_text_encoding_utf_8",
"summary": "写入文本"
"summary": "写入文本",
"url": "files.html#files_files_write_path_text_encoding_utf_8"
},
{
"key": "writeBytes",
"summary": "写入字节",
"url": "files.html#files_files_writebytes_path_bytes"
},
{
"key": "append",
"summary": "附加文本",
"url": "files.html#files_files_append_path_text_encoding_utf_8"
},
{
"key": "appendBytes",
"summary": "附加字节",
"url": "files.html#files_files_appendbytes_path_text_encoding_utf_8"
},
{
"key": "copy",
"url": "files.html#files_files_copy_frompath_topath",
"summary": "复制"
"summary": "复制",
"url": "files.html#files_files_copy_frompath_topath"
},
{
"key": "move",
"url": "files.html#files_files_move_frompath_topath",
"summary": "移动"
"summary": "移动",
"url": "files.html#files_files_move_frompath_topath"
},
{
"key": "rename",
"url": "files.html#files_files_rename_path_newname",
"summary": "重命名"
"summary": "重命名",
"url": "files.html#files_files_rename_path_newname"
},
{
"key": "renameWithoutExtension",
"url": "files.html#files_files_renamewithoutextension_path_newname",
"summary": "重命名"
"summary": "重命名",
"url": "files.html#files_files_renamewithoutextension_path_newname"
},
{
"key": "getName",
"url": "files.html#files_files_getname_path",
"summary": "获取文件名"
},
{
"key": "getExtension",
"url": "files.html#files_files_getextension_path",
"summary": "获取拓展名"
"summary": "获取文件名",
"url": "files.html#files_files_getname_path"
},
{
"key": "getNameWithoutExtension",
"url": "files.html#files_files_getnamewithoutextension_path",
"summary": "获取文件名"
"summary": "获取文件名",
"url": "files.html#files_files_getnamewithoutextension_path"
},
{
"key": "getExtension",
"summary": "获取拓展名",
"url": "files.html#files_files_getextension_path"
},
{
"key": "remove",
"url": "files.html#files_files_remove_path",
"summary": "删除"
"summary": "删除",
"url": "files.html#files_files_remove_path"
},
{
"key": "removeDir",
"url": "files.html#files_files_removedir_path",
"summary": "删除文件夹"
"summary": "删除文件夹",
"url": "files.html#files_files_removedir_path"
},
{
"key": "getSdcardPath",
"url": "files.html#files_files_getsdcardpath",
"summary": "SD卡路径"
"summary": "SD卡路径",
"url": "files.html#files_files_getsdcardpath"
},
{
"key": "cwd",
"summary": "当前文件夹",
"url": "files.html#files_files_cwd"
},
{
"key": "path",
"summary": "解析路径",
"url": "files.html#files_files_path_relativepath"
},
{
"key": "listDir",
"url": "files.html#files_files_listdir_path_filter",
"summary": "列表文件夹"
"summary": "列表文件夹",
"url": "files.html#files_files_listdir_path_filter"
},
{
"global": true,
"key": "open",
"summary": "打开文件",
"url": "files.html#files_open_path_mode_r_encoding_utf_8_buffersize_8192"
}
]
],
"summary": "文件",
"url": "files.html"
},
{
"name": "timers",
@ -1205,5 +1235,496 @@
"global": true
}
]
},
{
"name": "floaty",
"properties": [
{
"key": "window",
"summary": "",
"url": "floaty.html#floaty_floaty_window_layout"
},
{
"key": "closeAll",
"summary": "",
"url": "floaty.html#floaty_floaty_closeall"
}
],
"url": "floaty.html",
"summary": "悬浮窗"
},
{
"name": "device",
"properties": [
{
"key": "width",
"summary": "屏幕宽度",
"url": "device.html#device_device_width",
"variable": true
},
{
"key": "height",
"summary": "屏幕高度",
"url": "device.html#device_device_height",
"variable": true
},
{
"key": "buildId",
"summary": "修订版本号",
"url": "device.html#device_device_buildid",
"variable": true
},
{
"key": "broad",
"summary": "",
"url": "device.html#device_device_broad",
"variable": true
},
{
"key": "brand",
"summary": "品牌",
"url": "device.html#device_device_brand",
"variable": true
},
{
"key": "device",
"summary": "",
"url": "device.html#device_device_device",
"variable": true
},
{
"key": "model",
"summary": "设备型号",
"url": "device.html#device_deivce_model",
"variable": true
},
{
"key": "product",
"summary": "",
"url": "device.html#device_device_product",
"variable": true
},
{
"key": "bootloader",
"summary": "",
"url": "device.html#device_device_bootloader",
"variable": true
},
{
"key": "hardware",
"summary": "",
"url": "device.html#device_device_hardware",
"variable": true
},
{
"key": "fingerprint",
"summary": "",
"url": "device.html#device_device_fingerprint",
"variable": true
},
{
"key": "serial",
"summary": "",
"url": "device.html#device_device_serial",
"variable": true
},
{
"key": "sdkInt",
"summary": "安卓API版本",
"url": "device.html#device_device_sdkint",
"variable": true
},
{
"key": "incremental",
"summary": "",
"url": "device.html#device_device_incremental",
"variable": true
},
{
"key": "release",
"summary": "安卓版本",
"url": "device.html#device_device_release",
"variable": true
},
{
"key": "baseOS",
"summary": "",
"url": "device.html#device_device_baseos",
"variable": true
},
{
"key": "securityPatch",
"summary": "",
"url": "device.html#device_device_securitypatch",
"variable": true
},
{
"key": "codename",
"summary": "",
"url": "device.html#device_device_codename",
"variable": true
},
{
"key": "getIMEI",
"summary": "IMEI",
"url": "device.html#device_device_getimei"
},
{
"key": "getAndroidId",
"summary": "AndroidID",
"url": "device.html#device_device_getandroidid"
},
{
"key": "getMacAddress",
"summary": "MAC地址",
"url": "device.html#device_device_getmacaddress"
},
{
"key": "getBrightness",
"summary": "屏幕亮度",
"url": "device.html#device_device_getbrightness"
},
{
"key": "getBrightnessMode",
"summary": "亮度模式",
"url": "device.html#device_device_getbrightnessmode"
},
{
"key": "setBrightness",
"summary": "设置亮度",
"url": "device.html#device_device_setbrightness_b"
},
{
"key": "setBrightnessMode",
"summary": "设亮度模式",
"url": "device.html#device_device_setbrightnessmode_mode"
},
{
"key": "getMusicVolume",
"summary": "媒体音量",
"url": "device.html#device_device_getmusicvolume"
},
{
"key": "getNotificationVolume",
"summary": "通知音量",
"url": "device.html#device_device_getnotificationvolume"
},
{
"key": "getAlarmVolume",
"summary": "闹钟音量",
"url": "device.html#device_device_getalarmvolume"
},
{
"key": "getMusicMaxVolume",
"summary": "媒体最大音量",
"url": "device.html#device_device_getmusicmaxvolume"
},
{
"key": "getNotificationMaxVolume",
"summary": "通知最大音量",
"url": "device.html#device_device_getnotificationmaxvolume"
},
{
"key": "getAlarmMaxVolume",
"summary": "闹钟最大音量",
"url": "device.html#device_device_getalarmmaxvolume"
},
{
"key": "setMusicVolume",
"summary": "设媒体音量",
"url": "device.html#device_device_setmusicvolume_volume"
},
{
"key": "setNotificationVolume",
"summary": "设通知音量",
"url": "device.html#device_device_setnotificationvolume_volume"
},
{
"key": "setAlarmVolume",
"summary": "设闹钟音量",
"url": "device.html#device_device_setalarmvolume_volume"
},
{
"key": "getBattery",
"summary": "获取电量",
"url": "device.html#device_device_getbattery"
},
{
"key": "isCharging",
"summary": "是否充电",
"url": "device.html#device_device_ischarging"
},
{
"key": "getTotalMem",
"summary": "全部内存",
"url": "device.html#device_device_gettotalmem"
},
{
"key": "getAvailMem",
"summary": "可用内存",
"url": "device.html#device_device_getavailmem"
},
{
"key": "isScreenOn",
"summary": "屏幕是否亮",
"url": "device.html#device_device_isscreenon"
},
{
"key": "wakeUp",
"summary": "唤醒设备",
"url": "device.html#device_device_wakeup"
},
{
"key": "wakeUpIfNeeded",
"summary": "唤醒",
"url": "device.html#device_device_wakeupifneeded"
},
{
"key": "keepScreenOn",
"summary": "屏幕常亮",
"url": "device.html#device_device_keepscreenon_timeout"
},
{
"key": "keepScreenDim",
"summary": "屏幕常半亮",
"url": "device.html#device_device_keepscreendim_timeout"
},
{
"key": "cancelKeepingAwake",
"summary": "取消保持唤醒",
"url": "device.html#device_device_cancelkeepingawake"
},
{
"key": "vibrate",
"summary": "震动",
"url": "device.html#device_device_vibrate_millis"
},
{
"key": "cancelVibration",
"summary": "取消震动",
"url": "device.html#device_device_cancelvibration"
}
],
"url": "device.html",
"summary": "设备"
},
{
"name": "http",
"properties": [
{
"key": "get",
"summary": "GET请求",
"url": "http.html#http_http_get_url_options_callback"
},
{
"key": "post",
"summary": "POST请求",
"url": "http.html#http_http_post_url_data_options_callback"
},
{
"key": "postJson",
"summary": "",
"url": "http.html#http_http_postjson_url_data_options_callback"
},
{
"key": "postMultipart",
"summary": "",
"url": "http.html#http_http_postmultipart_url_files_options_callback"
},
{
"key": "request",
"summary": "",
"url": "http.html#http_http_request_url_options_callback"
}
],
"url": "http.html",
"summary": "HTTP"
},
{
"name": "keys",
"properties": [
{
"global": true,
"key": "back",
"summary": "返回",
"url": "keys.html#keys_back"
},
{
"global": true,
"key": "home",
"summary": "HOME键",
"url": "keys.html#keys_home"
},
{
"global": true,
"key": "powerDialog",
"summary": "电源菜单",
"url": "keys.html#keys_powerdialog"
},
{
"global": true,
"key": "notifications",
"summary": "拉通知栏",
"url": "keys.html#keys_notifications"
},
{
"global": true,
"key": "quickSettings",
"summary": "长拉通知栏",
"url": "keys.html#keys_quicksettings"
},
{
"global": true,
"key": "recents",
"summary": "最近任务",
"url": "keys.html#keys_recents"
},
{
"global": true,
"key": "splitScreen",
"summary": "分屏",
"url": "keys.html#keys_splitscreen"
},
{
"global": true,
"key": "Home",
"summary": "HOME键",
"url": "keys.html#keys_home_1"
},
{
"global": true,
"key": "Back",
"summary": "返回键",
"url": "keys.html#keys_back_1"
},
{
"global": true,
"key": "Power",
"summary": "电源键",
"url": "keys.html#keys_power"
},
{
"global": true,
"key": "Menu",
"summary": "菜单键",
"url": "keys.html#keys_menu"
},
{
"global": true,
"key": "VolumeUp",
"summary": "音量上键",
"url": "keys.html#keys_volumeup"
},
{
"global": true,
"key": "VolumeDown",
"summary": "音量下键",
"url": "keys.html#keys_volumedown"
},
{
"global": true,
"key": "Camera",
"summary": "照相键",
"url": "keys.html#keys_camera"
},
{
"global": true,
"key": "Up",
"summary": "上键",
"url": "keys.html#keys_up"
},
{
"global": true,
"key": "Down",
"summary": "下键",
"url": "keys.html#keys_down"
},
{
"global": true,
"key": "Left",
"summary": "左键",
"url": "keys.html#keys_left"
},
{
"global": true,
"key": "Right",
"summary": "右键",
"url": "keys.html#keys_right"
},
{
"global": true,
"key": "OK",
"summary": "确定键",
"url": "keys.html#keys_ok"
},
{
"global": true,
"key": "Text",
"summary": "输入文本",
"url": "keys.html#keys_text_text"
},
{
"global": true,
"key": "KeyCode",
"summary": "键值模拟",
"url": "keys.html#keys_keycode_code"
}
],
"url": "keys.html",
"summary": "按键"
},
{
"name": "storages",
"properties": [
{
"key": "create",
"summary": "创建",
"url": "storages.html#storages_storages_create_name"
},
{
"key": "remove",
"summary": "移除",
"url": "storages.html#storages_storages_remove_name"
}
],
"url": "storages.html",
"summary": "本地存储"
},
{
"name": "threads",
"properties": [
{
"key": "start",
"summary": "新线程",
"url": "threads.html#threads_threads_start_action"
},
{
"key": "shutDownAll",
"summary": "关闭所有线程",
"url": "threads.html#threads_threads_shutdownall"
},
{
"key": "currentThread",
"summary": "当前线程",
"url": "threads.html#threads_threads_currentthread"
},
{
"key": "disposable",
"summary": "",
"url": "threads.html#threads_threads_disposable"
},
{
"key": "atomic",
"summary": "原子整数",
"url": "threads.html#threads_threads_atomic_initialvalue"
},
{
"key": "lock",
"summary": "创建锁",
"url": "threads.html#threads_threads_lock"
}
],
"url": "threads.html",
"summary": "多线程"
}
]

View File

@ -0,0 +1,54 @@
//忽略不支持的传感器,即使有传感器不支持也不抛出异常
sensors.ignoresUnsupportedSensor = true;
sensors.on("unsupported_sensor", function(sensorName, sensorType){
log("不支持的传感器: %s 类型: %d", sensorName, sensorType);
});
//加速度传感器
sensors.register("accelerometer").on("change", (event, ax, ay, az)=>{
log("x方向加速度: %d\ny方向加速度: %d\nz方向加速度: %d", ax, ay, az);
});
//方向传感器
sensors.register("orientation").on("change", (event, dx, dy, dz)=>{
log("绕x轴转过角度: %d\n绕y轴转过角度: %d\n绕z轴转过角度: %d", dx, dy, dz);
});
//陀螺仪传感器
sensors.register("gyroscope").on("change", (event, wx, wy, wz)=>{
log("绕x轴角速度: %d\n绕y轴角速度: %d\n绕z轴角速度: %d", wx, wy, wz);
});
//磁场传感器
sensors.register("magnetic_field").on("change", (event, bx, by, bz)=>{
log("x方向磁场强度: %d\ny方向磁场强度: %d\nz方向磁场强度: %d", bx, by, bz);
});
//重力传感器
sensors.register("magnetic_field").on("change", (event, gx, gy, gz)=>{
log("x方向重力: %d\ny方向重力: %d\nz方向重力: %d", gx, gy, gz);
});
//线性加速度传感器
sensors.register("linear_acceleration").on("change", (event, ax, ay, az)=>{
log("x方向线性加速度: %d\ny方向线性加速度: %d\nz方向线性加速度: %d", ax, ay, az);
});
//温度传感器
sensors.register("ambient_temperature").on("change", (event, t)=>{
log("当前温度: %d", t);
});
//光线传感器
sensors.register("light").on("change", (event, l)=>{
log("当前光的强度: %d", l);
});
//压力传感器
sensors.register("pressure").on("change", (event, p)=>{
log("当前压力: %d", p);
});
//距离传感器
sensors.register("proximity").on("change", (event, d)=>{
log("当前距离: %d", d);
});
//湿度传感器
sensors.register("relative_humidity").on("change", (event, rh)=>{
log("当前相对湿度: %d", rh);
});
//30秒后退出程序
setTimeout(exit, 30 * 1000);

View File

@ -0,0 +1,74 @@
"ui";
ui.layout(
<scroll>
<vertical>
<text id="accelerometer" margin="12dp" textSize="16sp" textColor="#000000"/>
<text id="orientation" margin="12dp" textSize="16sp" textColor="#000000"/>
<text id="gyroscope" margin="12dp" textSize="16sp" textColor="#000000"/>
<text id="magnetic_field" margin="12dp" textSize="16sp" textColor="#000000"/>
<text id="gravity" margin="12dp" textSize="16sp" textColor="#000000"/>
<text id="linear_acceleration" margin="12dp" textSize="16sp" textColor="#000000"/>
<text id="ambient_temperature" margin="12dp" textSize="16sp" textColor="#000000"/>
<text id="light" margin="12dp" textSize="16sp" textColor="#000000"/>
<text id="pressure" margin="12dp" textSize="16sp" textColor="#000000"/>
<text id="proximity" margin="12dp" textSize="16sp" textColor="#000000"/>
<text id="relative_humidity" margin="12dp" textSize="16sp" textColor="#000000"/>
</vertical>
</scroll>
);
//忽略不支持的传感器,即使有传感器不支持也不抛出异常
sensors.ignoresUnsupportedSensor = true;
sensors.on("unsupported_sensor", function(sensorName, sensorType){
log(util.format("不支持的传感器: %s 类型: %d", sensorName, sensorType));
});
//加速度传感器
sensors.register("accelerometer", sensors.delay.ui).on("change", (event, ax, ay, az)=>{
ui.accelerometer.setText(util.format("x方向加速度: %d\ny方向加速度: %d\nz方向加速度: %d", ax, ay, az));
});
//方向传感器
sensors.register("orientation", sensors.delay.ui).on("change", (event, dx, dy, dz)=>{
ui.orientation.setText(util.format("绕x轴转过角度: %d\n绕y轴转过角度: %d\n绕z轴转过角度: %d", dx, dy, dz));
});
//陀螺仪传感器
sensors.register("gyroscope", sensors.delay.ui).on("change", (event, wx, wy, wz)=>{
ui.gyroscope.setText(util.format("绕x轴角速度: %d\n绕y轴角速度: %d\n绕z轴角速度: %d", wx, wy, wz));
});
//磁场传感器
sensors.register("magnetic_field", sensors.delay.ui).on("change", (event, bx, by, bz)=>{
ui.magnetic_field.setText(util.format("x方向磁场强度: %d\ny方向磁场强度: %d\nz方向磁场强度: %d", bx, by, bz));
});
//重力传感器
sensors.register("gravity", sensors.delay.ui).on("change", (event, gx, gy, gz)=>{
ui.gravity.setText(util.format("x方向重力: %d\ny方向重力: %d\nz方向重力: %d", gx, gy, gz));
});
//线性加速度传感器
sensors.register("linear_acceleration", sensors.delay.ui).on("change", (event, ax, ay, az)=>{
ui.linear_acceleration.setText(util.format("x方向线性加速度: %d\ny方向线性加速度: %d\nz方向线性加速度: %d", ax, ay, az));
});
//温度传感器
sensors.register("ambient_temperature", sensors.delay.ui).on("change", (event, t)=>{
ui.ambient_temperature.setText(util.format("当前温度: %d", t));
});
//光线传感器
sensors.register("light", sensors.delay.ui).on("change", (event, l)=>{
ui.light.setText(util.format("当前光的强度: %d", l));
});
//压力传感器
sensors.register("pressure", sensors.delay.ui).on("change", (event, p)=>{
ui.pressure.setText(util.format("当前压力: %d", p));
});
//距离传感器
sensors.register("proximity", sensors.delay.ui).on("change", (event, d)=>{
ui.proximity.setText(util.format("当前距离: %d", d));
});
//湿度传感器
sensors.register("relative_humidity", sensors.delay.ui).on("change", (event, rh)=>{
ui.relative_humidity.setText(util.format("当前相对湿度: %d", rh));
});
//30秒后退出程序
setTimeout(exit, 30 * 1000);

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();
}
}
}