6.3.4 Alpha2 - 新增 Paddle Lite / WebSocket; 修复屏幕旋转浮动按钮异常; 优化日志复制与导出

This commit is contained in:
SuperMonster003
2023-10-03 18:08:36 +08:00
parent 4674f94520
commit 4481f539f9
164 changed files with 3149 additions and 1428 deletions

View File

@@ -3,7 +3,7 @@
let { util, plugins } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Arrayx}
*/

View File

@@ -3,7 +3,7 @@
let { plugins, Arrayx, Numberx } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Mathx}
*/
@@ -14,7 +14,7 @@ module.exports = (scriptRuntime, scope) => {
* @implements Internal.Mathx
*/
const MathxCtor = function () {
// Empty interface body.
/* Empty body. */
};
MathxCtor.prototype = {

View File

@@ -3,7 +3,7 @@
let { util, plugins } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Numberx}
*/

View File

@@ -3,7 +3,7 @@
let { autojs } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.RootAutomator}
*/

View File

@@ -5,7 +5,7 @@
let { autojs, shell, files, util } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.App}
*/
@@ -30,7 +30,7 @@ module.exports = function (scriptRuntime, scope) {
* @extends Internal.App
*/
const App = function () {
// Empty interface body.
/* Empty body. */
};
App.prototype = {

View File

@@ -5,7 +5,7 @@
let { util } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Autojs}
*/
@@ -24,7 +24,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Autojs
*/
const Autojs = function () {
// Empty interface body.
/* Empty body. */
};
Autojs.prototype = {
@@ -54,6 +54,18 @@ module.exports = function (scriptRuntime, scope) {
},
R: global.R,
name: context.getString(R.strings.app_name),
get rotation() {
return ScreenMetrics.getRotation();
},
get orientation() {
return ScreenMetrics.getOrientation();
},
isScreenPortrait() {
return ScreenMetrics.isScreenPortrait();
},
isScreenLandscape() {
return ScreenMetrics.isScreenLandscape();
},
isRootAvailable() {
return RootUtils.isRootAvailable();
},

View File

@@ -5,7 +5,7 @@
let { util } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Automator}
*/
@@ -136,8 +136,8 @@ module.exports = function (scriptRuntime, scope) {
gestureAsync(duration, points) {
return rtAutomator.gestureAsync.apply(rtAutomator, [ 0 ].concat(Array.from(arguments)));
},
swipe(x1, y1, x2, y2, delay) {
return rtAutomator.swipe(x1, y1, x2, y2, delay);
swipe(x1, y1, x2, y2, duration) {
return rtAutomator.swipe(x1, y1, x2, y2, duration);
},
isServiceRunning() {
return rtAutomator.isServiceRunning();

View File

@@ -1,5 +1,5 @@
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Base64}
*/
@@ -11,7 +11,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Base64
*/
const Base64Ctor = function () {
// Empty interface body.
/* Empty body. */
};
Base64Ctor.prototype = {

View File

@@ -5,7 +5,7 @@
let { util } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Colors}
*/
@@ -63,10 +63,10 @@ module.exports = function (scriptRuntime, scope) {
Color.prototype = {
constructor: Color,
toString() {
return this.digest();
return this.summary();
},
digest() {
return colors.digest.apply(colors, [ this.color ].concat(Array.from(arguments)));
summary() {
return colors.summary.apply(colors, [ this.color ].concat(Array.from(arguments)));
},
toHex() {
return colors.toHex.apply(colors, [ this.color ].concat(Array.from(arguments)));
@@ -250,7 +250,7 @@ module.exports = function (scriptRuntime, scope) {
* @extends Internal.Colors
*/
const Colors = function () {
// Empty class body.
/* Empty body. */
};
Colors.prototype = {
@@ -666,7 +666,7 @@ module.exports = function (scriptRuntime, scope) {
let args = [ _.Color ].concat(Array.from(arguments));
return new (Function.prototype.bind.apply(_.Color, args));
},
digest(color) {
summary(color) {
let [ r, g, b, a ] = this.toRgba(color);
let niceA = _.toDoubleComponent(a).toFixed(2).replace(/0$/, '');
return `hex(${this.toHex(color)}), rgba(${r},${g},${b}/${niceA}), int(${this.toInt(color)})`;

View File

@@ -4,7 +4,7 @@
let { files, util, s13n } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Console}
*/
@@ -27,7 +27,7 @@ module.exports = function (scriptRuntime, scope) {
* @extends Internal.Console
*/
const Console = function () {
// Empty interface body.
/* Empty body. */
};
Console.prototype = {

View File

@@ -3,7 +3,7 @@
let { engines, global: _global } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Continuation}
*/
@@ -41,7 +41,13 @@ module.exports = function (scriptRuntime, scope) {
*/
let result = this.cont.suspend();
if (result.error !== null) {
throw result.error;
// throw result.error;
if (typeof result.error.message === 'string') {
ScriptRuntime.popException(result.error.message);
} else {
ScriptRuntime.popException(String(result.error));
}
return result.error;
}
return result.result;
},
@@ -64,7 +70,7 @@ module.exports = function (scriptRuntime, scope) {
*/
const Continuation = function () {
return Object.assign(function () {
// Empty interface body.
/* Empty body. */
}, Continuation.prototype);
};
@@ -97,7 +103,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Continuation.PromiseExtension
*/
const PromiseExtension = function () {
// Empty interface body.
/* Empty body. */
};
Object.assign(PromiseExtension.prototype, {

View File

@@ -3,12 +3,11 @@
let { util } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Device}
*/
module.exports = function (scriptRuntime, scope) {
const ScreenMetrics = org.autojs.autojs.runtime.api.ScreenMetrics;
const NetworkUtils = org.autojs.autojs.util.NetworkUtils;
const DeviceUtils = org.autojs.autojs.util.DeviceUtils;
@@ -20,7 +19,7 @@ module.exports = function (scriptRuntime, scope) {
* @extends Internal.Device
*/
const Device = function () {
// Empty interface body.
/* Empty body. */
};
Device.prototype = {
@@ -32,7 +31,10 @@ module.exports = function (scriptRuntime, scope) {
return ScreenMetrics.getDeviceScreenHeight();
},
get rotation() {
return ScreenMetrics.getRotation();
return rtDevice.getRotation();
},
get orientation() {
return rtDevice.getOrientation();
},
get density() {
return ScreenMetrics.getDeviceScreenDensity();
@@ -63,6 +65,12 @@ module.exports = function (scriptRuntime, scope) {
isScreenOff() {
return !rtDevice.isScreenOn();
},
isScreenPortrait() {
return rtDevice.isScreenPortrait();
},
isScreenLandscape() {
return rtDevice.isScreenLandscape();
},
getIpAddress(useIPv4) {
return useIPv4 === undefined
? NetworkUtils.getIpAddress()

View File

@@ -3,7 +3,7 @@
let { colors, threads, ui } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Dialogs}
*/
@@ -19,7 +19,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Dialogs
*/
const Dialogs = function () {
// Empty interface body.
/* Empty body. */
};
Dialogs.prototype = {

View File

@@ -3,7 +3,7 @@
let { files } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Engines}
*/
@@ -18,7 +18,7 @@ module.exports = function (scriptRuntime, scope) {
* @extends Internal.Engines
*/
const Engines = function () {
// Empty class body.
/* Empty body. */
};
Engines.prototype = {

View File

@@ -1,7 +1,7 @@
// noinspection JSUnusedGlobalSymbols
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Events}
*/
@@ -12,7 +12,7 @@ module.exports = function (scriptRuntime, scope) {
* @extends Internal.Events
*/
const Events = function () {
// Empty class body.
/* Empty body. */
};
Events.prototype = {
@@ -37,7 +37,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Keys
*/
const Keys = function () {
// Empty interface body.
/* Empty body. */
};
Keys.prototype = {

View File

@@ -1,5 +1,5 @@
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Files}
*/
@@ -12,7 +12,7 @@ module.exports = function (scriptRuntime, scope) {
* @extends Internal.Files
*/
const Files = function () {
// Empty class body.
/* Empty body. */
};
Files.prototype = {

View File

@@ -1,5 +1,7 @@
// noinspection UnnecessaryLocalVariableJS
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Floaty}
*/
@@ -12,7 +14,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Floaty
*/
const Floaty = function () {
// Empty interface body.
/* Empty body. */
};
Floaty.prototype = {
@@ -28,6 +30,7 @@ module.exports = function (scriptRuntime, scope) {
},
hasPermission: () => rtFloaty.hasPermission(),
requestPermission: () => rtFloaty.requestPermission(),
ensurePermission: () => rtFloaty.ensurePermission(),
};
return Floaty;

View File

@@ -7,8 +7,6 @@ let { autojs, console, device, pickup, ui, s13n, Numberx } = global;
/* Here, importClass() is not recommended for intelligent code completion in IDE like WebStorm. */
/* The same is true of destructuring assignment syntax (like `let {Uri} = android.net`). */
let ScriptRuntime = org.autojs.autojs.runtime.ScriptRuntime;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} runtime
* @param {org.mozilla.javascript.Scriptable | global} scope

View File

@@ -5,7 +5,7 @@
let { ui } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Http}
*/
@@ -26,7 +26,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Http
*/
const Http = function () {
// Empty interface body.
/* Empty body. */
};
Http.prototype = {

View File

@@ -5,7 +5,7 @@
let { files } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.I18n}
*/

View File

@@ -5,7 +5,7 @@
let { files, util, ui, threads } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Images}
*/
@@ -42,7 +42,7 @@ module.exports = function (scriptRuntime, scope) {
*/
const Images = function () {
return Object.assign(function () {
// Empty interface body.
/* Empty body. */
}, Images.prototype);
};
@@ -577,19 +577,23 @@ module.exports = function (scriptRuntime, scope) {
};
/**
* @class
* @extends Images.MatchingResult
* @param {Images.TemplateMatch[] | java.lang.Iterable<Images.TemplateMatch>} list
*/
const MatchingResult = function (list) {
Object.defineProperties(this, {
matches: {
value: Array.isArray(list) ? list : scriptRuntime.bridges.getBridges().toArray(list),
enumerable: true,
},
points: {
get() {
return this.matches.map(m => m.point);
},
enumerable: false,
Object.assign(this, {
matches: (() => {
if (Array.isArray(list)) {
return list;
}
// noinspection UnnecessaryLocalVariableJS
/** @type Images.TemplateMatch[] */
let results = scriptRuntime.bridges.getBridges().toArray(list);
return results;
})(),
get points() {
return this.matches.map(m => m.point);
},
});
};
@@ -606,6 +610,9 @@ module.exports = function (scriptRuntime, scope) {
if (!this.matches.length) {
return null;
}
/**
* @type Images.TemplateMatch
*/
let target = this.matches[0];
this.matches.forEach(m => target = compareFn(target, m) > 0 ? m : target);
return target;

View File

@@ -1,5 +1,5 @@
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Media}
*/
@@ -10,7 +10,7 @@ module.exports = function (scriptRuntime, scope) {
* @extends Internal.Media
*/
const Media = function () {
// Empty interface body.
/* Empty body. */
};
Media.prototype = {

View File

@@ -1,7 +1,7 @@
// noinspection JSUnusedLocalSymbols,UnnecessaryLocalVariableJS
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Notice}
*/
@@ -47,7 +47,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements {Notice.Config}
*/
const Config = function () {
// Empty interface body.
/* Empty body. */
};
Config.prototype = {
@@ -86,7 +86,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements {Notice.Channel}
*/
const Channel = function () {
// Empty interface body.
/* Empty body. */
};
Channel.prototype = {

View File

@@ -1,95 +1,228 @@
// noinspection UnnecessaryLocalVariableJS,JSUnusedLocalSymbols
// noinspection JSUnusedLocalSymbols
/* Overwritten protection. */
let { images, files } = global;
let { images } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Ocr}
*/
module.exports = function (scriptRuntime, scope) {
const rtOcr = scriptRuntime.mlKitOCR;
let _ = {
OcrCtor: (/* @IIFE */ () => {
/**
* @type {{[modeName in Internal.Ocr.ModeName]: Internal.Ocr.ModeName}}
*/
const modeName = {
mlkit: 'mlkit',
paddle: 'paddle',
unknown: 'unknown',
};
/**
* @type {Internal.Ocr.ModeName}
*/
let _mode = modeName.unknown;
const defaultPrivateDescriptor = {
configurable: false,
writable: false,
enumerable: false,
};
const defaultProtectedDescriptor = {
configurable: false,
writable: false,
enumerable: true, /* Emphasis. */
};
const instanceFunc = function (img, options) {
if (typeof arguments[0] === 'string') {
let img = images.read(/* path = */ arguments[0]);
if (img === null) {
throw TypeError(`Invalid image of path "${arguments[0]}" for ocr(img, options?)`);
}
return OcrCtor.prototype.recognizeText(img.oneShot(), options);
}
return OcrCtor.prototype.recognizeText.apply(OcrCtor.prototype, arguments);
};
/**
* @implements Internal.Ocr
*/
const OcrCtor = function () {
/** @global */
const ocr = function (img, options) {
if (typeof arguments[0] === 'string') {
let img = images.read(/* path = */ arguments[0]);
if (img === null) {
throw TypeError(`Invalid image of path "${arguments[0]}" for ocr(img, options?)`);
}
return OcrCtor.prototype.recognizeText(img.oneShot(), options);
}
return OcrCtor.prototype.recognizeText.apply(OcrCtor.prototype, arguments);
};
return Object.assign(ocr, OcrCtor.prototype);
let o = Object.assign(instanceFunc, OcrCtor.prototype);
Object.defineProperties(instanceFunc, Object.getOwnPropertyDescriptors(OcrCtor.prototype));
return o;
};
OcrCtor.prototype = {
constructor: OcrCtor,
recognizeText(img, options) {
_modeName: modeName,
_forcibleModeArgIndex: 10,
mlkit: require('ocr-mlkit')(scriptRuntime, scope),
paddle: require('ocr-paddle')(scriptRuntime, scope),
get mode() {
return _mode;
},
set mode(mode) {
this.tap(mode);
},
tap(mode) {
if (mode === this.mlkit || String(mode).toLowerCase() === modeName.mlkit) {
_mode = modeName.mlkit;
} else if (mode === this.paddle || String(mode).toLowerCase() === modeName.paddle) {
_mode = modeName.paddle;
} else {
_mode = modeName.unknown;
throw TypeError(`Unknown mode (value: ${mode}, species: ${species(mode)}) for ocr.tap(mode)`);
}
},
recognizeText() {
if (typeof arguments[0] === 'string') {
// @Signature
// recognizeText(imgPath: string, options?: DetectOptionsMLKit | DetectOptionsPaddle): string[];
// recognizeText(imgPath: string, region: Images.Options.Region): string[];
let img = images.read(/* path = */ arguments[0]);
if (img === null) {
throw TypeError(`Invalid image of path "${arguments[0]}" for ocr.recognizeText(img, options?)`);
}
return this.recognizeText(img.oneShot(), options);
// @Overload
// recognizeText(img: ImageWrapper, options?: DetectOptionsMLKit | DetectOptionsPaddle): string[];
// recognizeText(img: ImageWrapper, region: Images.Options.Region): string[];
return this.recognizeText(img.oneShot(), arguments[1]);
}
if (_.shouldTakenAsRegion(arguments[1])) {
// @Signature recognizeText(img: ImageWrapper, region: Images.Options.Region): string[];
// @Overload recognizeText(img: ImageWrapper, options: DetectOptionsMLKit | DetectOptionsPaddle): string[];
return this.recognizeText(img, { region: arguments[1] });
}
let opt = options || {};
// @Signature recognizeText(img: ImageWrapper, options?: DetectOptionsMLKit | DetectOptionsPaddle): string[];
/** @type {Internal.Ocr.DetectOptions} */
let opt = /* options */ arguments[1] || {};
let region = opt.region;
if (region === null) {
return [];
}
if (region === undefined) {
return Array.from(rtOcr.recognizeText(img));
/** @type Internal.Ocr.RecognizeTextMethod */
let recognizeTextMethod;
switch (arguments[OcrCtor.prototype._forcibleModeArgIndex] || _mode) {
case modeName.mlkit:
recognizeTextMethod = this.mlkit.recognizeTextMethodCreator(opt);
break;
case modeName.paddle:
recognizeTextMethod = this.paddle.recognizeTextMethodCreator(opt);
break;
default:
throw TypeError(`Can't call ocr.recognizeText with an unknown mode (value: ${_mode}, species: ${species(_mode)})`);
}
let results = Array.from(rtOcr.recognizeText(images.clip(img, region).oneShot()));
let img = arguments[0];
if (region === undefined) {
return recognizeTextMethod(img);
}
let results = recognizeTextMethod(
images.clip(img, region).oneShot(),
);
img.shoot();
return results;
},
detect(img, options) {
detect() {
if (typeof arguments[0] === 'string') {
// @Signature
// detect(imgPath: string, options?: DetectOptionsMLKit | DetectOptionsPaddle): org.autojs.autojs.runtime.api.OcrResult[];
// detect(imgPath: string, region: Images.Options.Region): org.autojs.autojs.runtime.api.OcrResult[];
let img = images.read(/* path = */ arguments[0]);
if (img === null) {
throw TypeError(`Invalid image of path "${arguments[0]}" for ocr.detect(img, options?)`);
}
return this.detect(img.oneShot(), options);
// @Overload
// detect(img: ImageWrapper, options?: DetectOptionsMLKit | DetectOptionsPaddle): org.autojs.autojs.runtime.api.OcrResult[];
// detect(img: ImageWrapper, region: Images.Options.Region): org.autojs.autojs.runtime.api.OcrResult[];
return this.detect(img.oneShot(), arguments[1]);
}
if (_.shouldTakenAsRegion(arguments[1])) {
// @Signature detect(img: ImageWrapper, region: Images.Options.Region): org.autojs.autojs.runtime.api.OcrResult[];
// @Overload detect(img: ImageWrapper, options: DetectOptionsMLKit | DetectOptionsPaddle): org.autojs.autojs.runtime.api.OcrResult[];
return this.detect(img, { region: arguments[1] });
}
let opt = options || {};
// @Signature detect(img: ImageWrapper, options?: DetectOptionsMLKit | DetectOptionsPaddle): org.autojs.autojs.runtime.api.OcrResult[];
let opt = /* options */ arguments[1] || {};
let region = opt.region;
if (region === null) {
return [];
}
/**
* @type {org.autojs.autojs.runtime.api.OcrResult[]}
*/
let resultList = rtOcr.detect(region !== undefined ? images.clip(img, region).oneShot() : img).toArray();
if (!isNullish(region)) {
resultList.forEach((result) => {
let rect = images.buildRegion(img, region);
result.bounds.offset(rect.x, rect.y);
});
/** type {Internal.Ocr.DetectMethod} */
let detectMethod;
switch (_mode) {
case modeName.mlkit:
detectMethod = this.mlkit.detectMethodCreator(opt);
break;
case modeName.paddle:
detectMethod = this.paddle.detectMethodCreator(opt);
break;
default:
throw TypeError(`Can't call ocr.detect with an unknown mode (value: ${_mode}, species: ${species(_mode)})`);
}
return Array.from(resultList);
let img = arguments[0];
if (region === undefined) {
return detectMethod(img);
}
let results = detectMethod(images.clip(img, region).oneShot());
results.forEach((result) => {
let rect = images.buildRegion(img, region);
result.bounds.offset(rect.x, rect.y);
});
img.shoot();
return results;
},
summary() {
return [
`[ OCR summary ]`,
`Current mode: ${this.mode}`,
`Available modes: [ ${Object.values(modeName).filter(n => n !== modeName.unknown).join(', ')} ]`,
].join('\n');
},
toString() {
return this.summary();
},
};
/* Set prototype descriptors. */
Object.defineProperties(OcrCtor.prototype, {
_modeName: defaultPrivateDescriptor,
_forcibleModeArgIndex: defaultPrivateDescriptor,
mode: { configurable: false },
constructor: defaultProtectedDescriptor,
mlkit: defaultProtectedDescriptor,
paddle: defaultProtectedDescriptor,
tap: defaultProtectedDescriptor,
summary: defaultProtectedDescriptor,
toString: defaultProtectedDescriptor,
});
return OcrCtor;
})(),
shouldTakenAsRegion(o) {
@@ -104,5 +237,8 @@ module.exports = function (scriptRuntime, scope) {
*/
const ocr = new _.OcrCtor();
/* Init OCR mode with MLKit. */
ocr.mode = 'mlkit';
return ocr;
};

View File

@@ -1,120 +0,0 @@
// noinspection UnnecessaryLocalVariableJS,JSUnusedLocalSymbols
/* Overwritten protection. */
let { images } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Ocr}
*/
module.exports = function (scriptRuntime, scope) {
const rtOcr = scriptRuntime.paddleOCR;
let _ = {
OcrCtor: (/* @IIFE */ () => {
/**
* @implements Internal.Ocr
*/
const OcrCtor = function () {
/** @global */
const ocr = function (img, options) {
if (typeof arguments[0] === 'string') {
let img = images.read(/* path = */ arguments[0]);
if (img === null) {
throw TypeError(`Invalid image of path "${arguments[0]}" for ocr(img, options?)`);
}
return OcrCtor.prototype.recognizeText(img.oneShot(), options);
}
return OcrCtor.prototype.recognizeText.apply(OcrCtor.prototype, arguments);
};
return Object.assign(ocr, OcrCtor.prototype);
};
OcrCtor.prototype = {
constructor: OcrCtor,
recognizeText(img, options) {
if (typeof arguments[0] === 'string') {
let img = images.read(/* path = */ arguments[0]);
if (img === null) {
throw TypeError(`Invalid image of path "${arguments[0]}" for ocr.recognizeText(img, options?)`);
}
return this.recognizeText(img.oneShot(), options);
}
if (_.shouldTakenAsRegion(arguments[1])) {
return this.recognizeText(img, { region: arguments[1] });
}
let opt = options || {};
let region = opt.region;
if (region === null) {
return [];
}
let cpuThreadNum = opt.cpuThreadNum || 4
let useSlim = opt.useSlim
if (useSlim === undefined) {
// 默认使用轻量化模型
useSlim = true
}
if (region === undefined) {
return Array.from(rtOcr.recognizeText(img, cpuThreadNum, useSlim));
}
let results = Array.from(rtOcr.recognizeText(images.clip(img, region).oneShot()));
img.shoot();
return results;
},
detect(img, options) {
if (typeof arguments[0] === 'string') {
let img = images.read(/* path = */ arguments[0]);
if (img === null) {
throw TypeError(`Invalid image of path "${arguments[0]}" for ocr.detect(img, options?)`);
}
return this.detect(img.oneShot(), options);
}
if (_.shouldTakenAsRegion(arguments[1])) {
return this.detect(img, { region: arguments[1] });
}
let opt = options || {};
let region = opt.region;
if (region === null) {
return [];
}
let cpuThreadNum = opt.cpuThreadNum || 4
let useSlim = opt.useSlim
if (useSlim === undefined) {
// 默认使用轻量化模型
useSlim = true
}
/**
* @type {org.autojs.autojs.runtime.api.OcrResult[]}
*/
let resultList = rtOcr.detect(region !== undefined ? images.clip(img, region).oneShot() : img, cpuThreadNum, useSlim).toArray();
if (!isNullish(region)) {
resultList.forEach((result) => {
let rect = images.buildRegion(img, region);
result.bounds.offset(rect.x, rect.y);
});
}
return Array.from(resultList);
},
};
return OcrCtor;
})(),
shouldTakenAsRegion(o) {
return o instanceof org.opencv.core.Rect
|| o instanceof android.graphics.Rect
|| Array.isArray(o);
},
};
/**
* @type {Internal.Ocr}
*/
const ocr = new _.OcrCtor();
return ocr;
};

View File

@@ -1,5 +1,5 @@
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Plugins}
*/
@@ -10,7 +10,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Plugins
*/
const Plugins = function () {
// Empty interface body.
/* Empty body. */
};
Plugins.prototype = {

View File

@@ -1,7 +1,7 @@
// noinspection JSUnusedGlobalSymbols
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Recorder}
*/

View File

@@ -1,7 +1,7 @@
// noinspection JSUnusedLocalSymbols,UnnecessaryLocalVariableJS
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.S13n}
*/
@@ -12,7 +12,7 @@ module.exports = function (scriptRuntime, scope) {
* @extends Internal.S13n
*/
const S13n = function () {
// Empty interface body.
/* Empty body. */
};
S13n.prototype = {

View File

@@ -5,7 +5,7 @@
let { i18n } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {() => Internal.Selector}
*/

View File

@@ -1,5 +1,5 @@
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Sensors}
*/
@@ -10,7 +10,7 @@ module.exports = function (scriptRuntime, scope) {
* @extends Internal.Sensors
*/
const Sensors = function () {
// Empty interface body.
/* Empty body. */
};
Sensors.prototype = {

View File

@@ -1,5 +1,5 @@
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Shell}
*/

View File

@@ -1,5 +1,5 @@
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Storages}
*/
@@ -47,7 +47,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Storages
*/
const Storage = function () {
// Empty interface body.
/* Empty body. */
};
Storage.prototype = {

View File

@@ -5,7 +5,7 @@
let { ui, threads, files } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Tasks}
*/
@@ -22,7 +22,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Tasks
*/
let Task = function () {
// Empty interface body.
/* Empty body. */
};
Task.prototype = {

View File

@@ -1,7 +1,7 @@
// noinspection NpmUsedModulesInstalled
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Threads}
*/
@@ -18,7 +18,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Threads
*/
const Threads = function () {
// Empty interface body.
/* Empty body. */
};
Threads.prototype = {
@@ -73,7 +73,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Threads.PromiseExtension
*/
const PromiseExtension = function () {
// Empty interface body.
/* Empty body. */
};
Object.assign(PromiseExtension.prototype, {

View File

@@ -1,5 +1,5 @@
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Timers}
*/
@@ -11,7 +11,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Timers
*/
const Timers = function () {
// Empty interface body.
/* Empty body. */
};
Timers.prototype = {

View File

@@ -1,86 +1,26 @@
// noinspection UnnecessaryLocalVariableJS,JSUnusedLocalSymbols
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Toast}
*/
module.exports = function (scriptRuntime, scope) {
let rtToast = scriptRuntime.toast;
let _ = {
uiHandler: runtime.getUiHandler(),
toasts: {
/**
* @type {Set<android.widget.Toast>}
*/
pool: new Set(),
lock: new ReentrantLock(),
add(t) {
if (t instanceof Toast) {
this.lock.lock();
this.pool.add(t);
this.addCallback(t);
this.lock.unlock();
}
},
/**
* @param {android.widget.Toast} t
*/
addCallback(t) {
_.uiHandler.postDelayed(new java.lang.Runnable({
run: () => _.toasts.remove(t),
}), this.getDuration(t) + 1e3 /* As toast may show with some delay. */);
},
remove(t) {
if (this.pool.has(t)) {
this.lock.lock();
this.pool.delete(t);
this.lock.unlock();
}
},
dismissAll() {
if (this.pool.size > 0) {
this.lock.lock();
this.pool.forEach((t) => t.cancel());
this.pool.clear();
this.lock.unlock();
}
},
getDuration(t) {
let du = {
SHORT_DELAY: 2e3,
LONG_DELAY: 3.5e3,
};
switch (t.getDuration()) {
case Toast.LENGTH_SHORT:
return du.SHORT_DELAY;
case Toast.LENGTH_LONG:
return du.LONG_DELAY;
default:
return Math.max.apply(null, Object.values(du));
}
},
},
ToastCtor: (/* @IIFE */ () => {
/**
* @implements Internal.Toast
*/
const ToastCtor = function () {
// @Caution by SuperMonster003 on Oct 11, 2022.
// ! android.widget.Toast.makeText() doesn't work well on Android API Level 28 (Android 9) [P].
// ! There hasn't been a solution for this so far.
// ! Tested devices:
// ! 1. SONY XPERIA XZ1 Compact (G8441)
// ! 2. Android Studio AVD (Android 9.0 x86)
const Ctor = function () {
/** @global */
const toast = function (msg, isLong, isForcible) {
const instance = function (msg, isLong, isForcible) {
let $ = {
rex: {
badge: {
long: /^l(ong)?$/i,
short: /^s(hort)?$/i,
forcible: /^f(orcible)?$/i,
},
toast() {
this.init(arguments);
this.show();
},
init() {
this.message = isNullish(msg) ? '' : String(msg);
this.isForcible = this.parseIsForcible(isForcible);
@@ -94,13 +34,13 @@ module.exports = function (scriptRuntime, scope) {
return Boolean(isLong);
}
if (typeof isLong === 'string') {
if (this.rex.long.test(isLong)) {
if (this.badge.long.test(isLong)) {
return true;
}
if (this.rex.short.test(isLong)) {
if (this.badge.short.test(isLong)) {
return false;
}
if (this.rex.forcible.test(isLong)) {
if (this.badge.forcible.test(isLong)) {
this.isForcible = true;
return false;
}
@@ -116,42 +56,31 @@ module.exports = function (scriptRuntime, scope) {
return Boolean(isForcible);
}
if (typeof isForcible === 'string') {
if (this.rex.forcible.test(isForcible)) {
if (this.badge.forcible.test(isForcible)) {
return true;
}
throw Error(`Invalid param: {name: isForcible, value: ${isForcible}, type: ${species(isForcible)}.`);
}
return false;
},
show() {
_.uiHandler.post(() => {
if ($.isForcible) {
_.toasts.dismissAll();
}
let len = $.isLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
let o = Toast.makeText(_.uiHandler.getContext(), $.message, len);
_.toasts.add(o);
o.show();
});
makeToast() {
rtToast.makeToast(this.message, this.isLong, this.isForcible);
},
};
$.toast();
$.init();
$.makeToast();
};
return Object.assign(toast, ToastCtor.prototype);
return Object.assign(instance, Ctor.prototype);
};
ToastCtor.prototype = {
constructor: ToastCtor,
dismissAll() {
_.uiHandler.post(() => {
_.toasts.dismissAll();
});
},
Ctor.prototype = {
constructor: Ctor,
dismissAll: () => rtToast.dismissAll(),
};
return ToastCtor;
return Ctor;
})(),
};

View File

@@ -5,7 +5,7 @@
let { files } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.UI}
*/
@@ -54,7 +54,7 @@ module.exports = function (scriptRuntime, scope) {
* @extends Internal.UI
*/
const UI = function () {
// Empty class body.
/* Empty body. */
};
UI.prototype = {
@@ -67,7 +67,7 @@ module.exports = function (scriptRuntime, scope) {
let Widget = function () {
this.__attrs__ = {};
return Object.assign(function () {
// Empty interface body.
/* Empty body. */
}, Widget.prototype);
};

View File

@@ -22,7 +22,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Util}
*/
@@ -38,7 +38,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Util
*/
const Util = function () {
// Empty interface body.
/* Empty body. */
};
Util.prototype = {

View File

@@ -5,7 +5,7 @@
let { http } = global;
/**
* @param {org.autojs.autojs.runtime.ScriptRuntime} scriptRuntime
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.Web}
*/
@@ -20,7 +20,7 @@ module.exports = function (scriptRuntime, scope) {
* @implements Internal.Web
*/
const Web = function () {
// Empty interface body.
/* Empty body. */
};
Web.prototype = {
@@ -43,7 +43,7 @@ module.exports = function (scriptRuntime, scope) {
return new InjectableWebClient(Context.getCurrentContext(), scope);
},
newWebSocket(url) {
return new org.autojs.autojs.core.web.WebSocket(http.__okhttp__, url);
return new scope.WebSocket(url);
},
};
@@ -55,6 +55,33 @@ module.exports = function (scriptRuntime, scope) {
*/
let methods = [ 'newInjectableWebView', 'newInjectableWebClient', 'newWebSocket' ];
__asGlobal__(web, methods, scope);
const WebSocket = function () {
if (!(this instanceof WebSocket)) {
throw TypeError('WebSocket must be called as constructor');
}
if (arguments[0] instanceof MutableOkHttp) {
this.client = arguments[0];
this.url = arguments[1];
} else {
this.client = http.__okhttp__;
this.url = arguments[0];
}
if (typeof this.url !== 'string') {
throw TypeError(`Invalid url (value: ${this.url}, species: ${species(this.url)}) for WebSocket`);
}
return new org.autojs.autojs.core.web.WebSocket(this.client, this.url);
};
/* Append all primitive static fields. */
Object.keys(org.autojs.autojs.core.web.WebSocket).forEach((key) => {
let value = org.autojs.autojs.core.web.WebSocket[key]
if (isPrimitive(value)) {
WebSocket[key] = value;
}
});
scope.WebSocket = WebSocket;
},
};

View File

@@ -0,0 +1,50 @@
// noinspection UnnecessaryLocalVariableJS,JSUnusedLocalSymbols
/**
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.OcrMLKit}
*/
module.exports = function (scriptRuntime, scope) {
const rtOcrMLKit = scriptRuntime.ocrMLKit;
let _ = {
OcrMLKitCtor: (/* @IIFE */ () => {
/**
* @implements Internal.OcrMLKit
*/
const OcrMLKitCtor = function () {
return Object.assign(function () {
let argArray = Array.from(arguments);
/** @type {Internal.Ocr.ModeName} */
argArray[ocr['_forcibleModeArgIndex']] = ocr['_modeName']['mlkit'];
return ocr.recognizeText.apply(ocr, argArray);
}, OcrMLKitCtor.prototype);
};
OcrMLKitCtor.prototype = {
constructor: OcrMLKitCtor,
recognizeTextMethodCreator(options) {
return function (img) {
return Array.from(rtOcrMLKit.recognizeText(img));
};
},
detectMethodCreator(options) {
return function (img) {
return Array.from(rtOcrMLKit.detect(img).toArray());
};
},
};
return OcrMLKitCtor;
})(),
};
/**
* @type {Internal.OcrMLKit}
*/
const ocrMLKit = new _.OcrMLKitCtor();
return ocrMLKit;
};

View File

@@ -0,0 +1,68 @@
// noinspection UnnecessaryLocalVariableJS,JSUnusedLocalSymbols
/**
* @param {ScriptRuntime} scriptRuntime
* @param {org.mozilla.javascript.Scriptable | global} scope
* @return {Internal.OcrPaddle}
*/
module.exports = function (scriptRuntime, scope) {
const rtOcrPaddle = scriptRuntime.ocrPaddle;
let _ = {
OcrPaddleCtor: (/* @IIFE */ () => {
/**
* @implements Internal.OcrPaddle
*/
const OcrPaddleCtor = function () {
return Object.assign(function () {
let argArray = Array.from(arguments);
/** @type {Internal.Ocr.ModeName} */
argArray[ocr['_forcibleModeArgIndex']] = ocr['_modeName']['paddle'];
return ocr.recognizeText.apply(ocr, argArray);
}, OcrPaddleCtor.prototype);
};
OcrPaddleCtor.prototype = {
constructor: OcrPaddleCtor,
recognizeTextMethodCreator(options) {
return function (img) {
const { cpuThreadNum, useSlim } = options;
let niceUseSlim = useSlim === undefined ? true : Boolean(useSlim);
let niceCpuThreadNum = cpuThreadNum || 4;
return Array.from(rtOcrPaddle.recognizeText(
img,
niceCpuThreadNum,
niceUseSlim,
));
};
},
detectMethodCreator(options) {
return function (img) {
const { cpuThreadNum, useSlim } = options;
let niceUseSlim = useSlim === undefined ? true : Boolean(useSlim);
let niceCpuThreadNum = cpuThreadNum || 4;
return Array.from(rtOcrPaddle.detect(
img,
niceCpuThreadNum,
niceUseSlim,
).toArray());
};
},
};
return OcrPaddleCtor;
})(),
};
/**
* @type {Internal.OcrPaddle}
*/
const ocrPaddle = new _.OcrPaddleCtor();
return ocrPaddle;
};