外观
全能类型 (Omnipotent Types)
Rhino 2.0 运行时合同
全能类型是 Monkey King 当前版本 文档层的“可接受输入集合”,不是 Rhino 2.0 的真实类。API 会根据实参形态依次尝试标准化;例如 OmniColor 可以是颜色字符串、整数或颜色对象,但最终仍转换为目标 Android API 所需的具体值。
- 参数、默认值与合法值:以每个 Omni 类型下列出的成员类型和调用 API 的选项说明为准;未列出的形态不应视为隐式支持。
- 返回值与异常:Omni 类型只描述输入,返回类型由目标 API 决定;无法匹配任一成员类型时同步抛出参数或转换异常。
- 权限与线程:聚合与标准化本身通常同步且无需权限;若输入是文件、URI 或 Android 对象,仍继承目标 API 的权限与线程限制。
- 生命周期与副作用:类型定义本身没有生命周期和副作用;标准化可能创建临时 Java / Rhino 包装对象,但不转移外部资源所有权。
- 旧版本:无法从实现合同恢复首次版本的既有类型按 ≤ v6.6.4(旧文档未记录精确版本) 记录。
js
// Rhino 2.0:三种 OmniColor 输入最终可得到同一个 ColorInt。
const a = s13n.color('red');
const b = s13n.color('#ff0000');
const c = s13n.color(Color('red'));
console.log(a === b && b === c); // true全能类型是一种聚合类型.
Monkey King 模块中, 一个参数往往接受多种不同的类型, 这些类型均可体现这个参数的含义. 这样的类型成为全能类型.
如对于 颜色 (color), 有 ColorHex 和 ColorName 等多种类型可以表示, 它们都可以作为实参传入方法中:
js
/* 需要为 console 悬浮窗设置一个浅蓝色标题. */
/* 以下 4 种方法分别传入不同类型的参数, 但实现了同样的效果. */
console.setTitleTextColor('light-blue'); /* ColorName 类型. */
console.setTitleTextColor('#ADD8E6'); /* ColorHex 类型. */
console.setTitleTextColor(Color('#ADD8E6').toInt()); /* ColorInt 类型. */
console.setTitleTextColor(Color('#ADD8E6')); /* Color 类型. */
console.show(); /* 显示控制台悬浮窗. */上述示例中 console.setTitleTextColor 的方法签名可以写成以下形式:
text
console.setTitleTextColor(color: OmniColor): void其中使用 OmniColor 这个全能类型代表了颜色聚合类型.
OmniColor
颜色聚合类型.
| 类型 | 简述 | 示例 |
|---|---|---|
| ColorHex | 颜色代码 | #663399 |
| ColorInt | 颜色整数 | -10079335 |
| ColorName | 颜色名称 | 'rebecca-purple' |
| Color | 颜色类 | Color('#663399') |
| ThemeColor | 主题颜色类 | monkeyking.themeColor |
OmniColor 不仅可用于参数传入方法中, 还可以作为 XML 元素的属性值:
js
'ui';
ui.layout(<vertical>
<button text="click to start" color="#006400"/>
</vertical>);上述示例设置按钮布局的文字颜色为深绿色, 使用了 ColorHex 作为颜色值.
使用 ColorName 作为颜色值也可达到相同的效果:
js
'ui';
ui.layout(<vertical>
<button text="click to start" color="dark-green"/>
</vertical>);使用 ColorInt 作为颜色值同样可以达到相同的效果:
js
'ui';
ui.layout(<vertical>
<button text="click to start" color="-16751616"/>
</vertical>);如需使用主题色作为颜色值, 需要使用一对花括号以及绑定全局作用域的表达式:
js
'ui';
ui.layout(<vertical>
<button text="click to start" color="{{monkeyking.themeColor}}"/>
</vertical>);OmniIntent
Intent 聚合类型.
| 类型 | 简述 | 示例 |
|---|---|---|
| Intent | 意图类 | new Intent().setAction( ... ) / app.intent({ ... }) |
| IntentOptions | 意图选项 | { action: ... , className: ... , data: ... } |
| IntentShortFormForActivity | 意图活动简称 | docs / home / settings / console / about |
| IntentUriString | 意图 URI 字符串 | 'https://msn.com' / 'msn.com' |
OmniVibrationPattern
振动模式聚合类型.
| 类型 | 简述 | 示例 |
|---|---|---|
| number[] | 传统振动模式 (按数字代表的启停间隔振动) | [ 0, 200, 0, 200, 0, 200 ] |
| string | 文本 (按文本对应的摩斯电码振动) | 'hello' |
用于模拟 SOS (紧急求救信号) 的示例:
js
device.vibrate([ 100, 100, 100, 100, 100, 300, 300, 100, 300, 100, 300, 300, 100, 100, 100, 100, 100 ], 0);
device.vibrate('SOS'); /* 效果同上. */OmniRegion
区域聚合类型.
| 类型 | 简述 | 示例 |
|---|---|---|
| number[] | 数字数组, [ X 坐标, Y 坐标, 宽, 高 ] | [ 0, 0, 200, 400 ] |
| OpenCVRect | org.opencv.core.Rect 类型 | 1. images.buildRegion(img, [ 0, 0, 200, 400 ])2. new org.opencv.core.Rect(x, y, w, h) |
| AndroidRect | android.graphics.Rect 类型 | 1. pickup(/\w+/, 'bounds')2. new android.graphics.Rect(left, top, right, bottom) |
将一个 500 × 500 的图片裁剪其中心区域 300 × 300 的示例:
js
let img = images.read('...');
let imgWidth = img.getWidth(); // 500
let imgHeight = img.getHeight(); // 500
let clipWidth = 300;
let clipHeight = 300;
let clipX = (imgWidth - clipWidth) / 2;
let clipY = (imgHeight - clipHeight) / 2;
/* 使用 number[] 作为区域. */
images.clip(img, [ clipX, clipY, clipWidth, clipHeight ]);
/* 使用 OpenCVRect 作为区域. */
images.clip(img, new org.opencv.core.Rect(clipX, clipY, clipWidth, clipHeight));
/* 使用 AndroidRect 作为区域. */
let left = clipX;
let top = clipY;
let right = clipX + clipWidth;
let bottom = clipY + clipHeight;
images.clip(img, new android.graphics.Rect(left, top, right, bottom));
/* AndroidRect 结合控件的应用. */
/* 假设屏幕的活动窗口中存在一个控件, id 为 aim, 它的控件矩形区域恰好为所需区域. */
let bounds = pickup({ id: 'aim' }, 'bounds');
images.clip(img, bounds); /* bounds 是一个 AndroidRect 实例. */OmniThrowable
可抛异常聚合类型.
| 类型 | 简述 | 示例 |
|---|---|---|
| Error | Monkey King 内置错误类型 | 1. Error('error')2. TypeError('error') |
| java.lang.Throwable | Throwable 及其所有子类型 | 1. new java.lang.Exception('error')2. try { a++ } catch(e) { e.rhinoException } |
| string | 字符串, 表示异常消息, 将被包装为 java.lang.Exception 实例 | 'An error has occurred' |
使用 console.printAllStackTrace 打印详细栈追踪的示例:
js
try {
a++;
} catch (e) {
console.printAllStackTrace(e); /* Error 实例. */
console.printAllStackTrace(e.rhinoException); /* java.lang.Throwable 实例. */
console.printAllStackTrace(e.message); /* 字符串变量. */
}