Files
youle_app_ohos/feature_capabilities/src/main/ets/providers/VibrateProvider.ets
T
lanterngamescn 59b000ce7a M3(批1): 占位桩 + 震动 + 剪贴板 + 系统能力 Provider
T-M3-15 RoomStubProvider/PayStubProvider:createRoom/exitRoom/getVideoinfo/DragViewvideoIsshow、
        paybrowser/getGameplay 全 void 空实现+日志,绝不触发出站(§6.5 安全网)
T-M3-04 VibrateProvider:vibrator/repeatvibrator/canclevibrator(@ohos.vibrator,重复用递归 setTimeout)
T-M3-05 ClipboardProvider:gameCopytext/gamepastetext(@ohos.pasteboard,读取经 responseCallback 回执)
T-M3-01 AppSystemProvider:orientation(window.setPreferredOrientation 横/竖)、browser(隐式 want)、
        finsh(terminateSelf)、openApplyDownloadpath(按包名启动失败回退浏览器)、notification(空)
组装根 buildCapabilities 装配上述 + 桩;module.json5 增 VIBRATE 权限

devecocli build 通过。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-25 19:15:21 +08:00

82 lines
2.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 震动能力(契约 §8.4,T-M3-04)。@kit.SensorServiceKit vibrator。
* - vibrator:data=毫秒裸串,单次震动。
* - repeatvibrator:data="1" 重复 / 其他停止重复。
* - canclevibrator(拼写原样):取消全部震动。
* 需 module.json5 声明 ohos.permission.VIBRATE。
*/
import { vibrator } from '@kit.SensorServiceKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { BridgeController } from 'feature_bridge';
import { InboundHandlers } from 'contracts';
import { Logger } from 'common';
import { CapabilityContext, CapabilityProvider } from '../core/CapabilityProvider';
const DEFAULT_DURATION: number = 1000;
export class VibrateProvider implements CapabilityProvider {
readonly name: string = 'vibrate';
private readonly log: Logger = Logger.tag('VibrateProvider');
private lastDuration: number = DEFAULT_DURATION;
private repeating: boolean = false;
private repeatTimer: number = -1;
register(bridge: BridgeController, _ctx: CapabilityContext): void {
bridge.registerHandler(InboundHandlers.Vibrator, (data: string, _cb: (resp: string) => void) => {
this.vibrateOnce(this.parseMs(data));
});
bridge.registerHandler(InboundHandlers.RepeatVibrator, (data: string, _cb: (resp: string) => void) => {
if (data === '1') {
this.startRepeat();
} else {
this.stopRepeat();
}
});
bridge.registerHandler(InboundHandlers.CancleVibrator, (_d: string, _cb: (resp: string) => void) => {
this.cancel();
});
}
onDestroy(): void {
this.cancel();
}
private parseMs(data: string): number {
const n: number = parseInt(data, 10);
return isNaN(n) || n <= 0 ? DEFAULT_DURATION : n;
}
private vibrateOnce(ms: number): void {
this.lastDuration = ms;
vibrator.startVibration({ type: 'time', duration: ms }, { usage: 'notification' })
.catch((e: BusinessError) => this.log.w(`startVibration failed: ${e.code} ${e.message}`));
}
private startRepeat(): void {
this.repeating = true;
this.loop();
}
private loop(): void {
if (!this.repeating) {
return;
}
this.vibrateOnce(this.lastDuration);
// 当次震动结束后再触发下一次(间隔取等长),形成重复,直到取消
this.repeatTimer = setTimeout(() => this.loop(), this.lastDuration * 2);
}
private stopRepeat(): void {
this.repeating = false;
if (this.repeatTimer !== -1) {
clearTimeout(this.repeatTimer);
this.repeatTimer = -1;
}
}
private cancel(): void {
this.stopRepeat();
vibrator.stopVibration().catch((e: BusinessError) => this.log.w(`stopVibration failed: ${e.code} ${e.message}`));
}
}