import { RequestTask, SyncOptions } from "./interface.uts"; import { log } from "./utils.uts"; export type AsyncOptions = { type : string; success : (res : string) => void; fail : (res : string) => void; complete : (res : string) => void; }; export type { SyncOptions } from "./interface.uts"; type SyntaxResult = { name : string }; type SyncResult = { msg : string } /** * 导出一个属性 */ export const MAX = 100; /** * 导出一个同步方法 * @returns */ export function testSync(msg : string) : SyncResult { console.log("log test"); log("log test1"); const res : SyncResult = { msg: `hello ${msg}` } return res // return { // msg: `hello ${msg}`, // }; } /** * 导出一个同步方法(触发了数组越界异常) */ export function testSyncError() { const arr : string[] = []; console.log(arr[1]); } /** * 导出一个带callback的同步方法 * @param opts */ export function testSyncWithCallback(opts : AsyncOptions) : SyntaxResult { if (opts.type == "success") { opts.success("success"); } else { opts.fail("fail"); } opts.complete("complete"); const res : SyntaxResult = { name: "testSyncWithCallback" } return res; // return { name: "testSyncWithCallback" }; } async function testAwaitPromise(res : SyntaxResult) : Promise { // #ifdef APP-ANDROID return await new Promise(function (resolve : (res : SyntaxResult) => void) { resolve(res) }) // #endif // #ifndef APP-ANDROID return res // #endif } /** * 导出一个异步方法 * @returns */ export async function testAsync(opts : AsyncOptions) : Promise { if (opts.type == "success") { opts.success("success"); } else { opts.fail("fail"); } opts.complete("complete"); const res : SyntaxResult = { name: "testAsync" } return await testAwaitPromise(res); // return { name: "testAsync" }; } export async function testAsyncParam3(id:number,name:string,opts : AsyncOptions) : Promise { console.log("id",id,"name",name) if (opts.type == "success") { opts.success("success"); } else { opts.fail("fail"); } opts.complete("complete"); const res : SyntaxResult = { name: "testUtsAsyncMulitParam" } return await testAwaitPromise(res); // return { name: "testAsync" }; } export type TestOptions = { name : string; callback : (res : string) => void; }; export class Test { id : number; name : string; static type : string = "Test"; constructor(id : number, options : TestOptions) { this.id = id; this.name = options.name; options.callback("Test.constructor"); } static testClassStaticSyncWithCallback(opts : AsyncOptions) : SyntaxResult { return testSyncWithCallback(opts); } static async testClassStaticAsync(opts : AsyncOptions) : Promise { const res = await testAsync(opts); return res; } testClassSyncWithCallback(opts : AsyncOptions) : SyntaxResult { return testSyncWithCallback(opts); } async testClassAsync(opts : AsyncOptions) : Promise { const res = await testAsync(opts); return res; } } class RequestTaskImpl implements RequestTask { url : string constructor(url : string) { this.url = url } abort() : RequestTask { return this } onCallback(callback : (res : string) => void) { callback("onCallback") } sync(options : SyncOptions) : string { options.success?.("success") options.complete?.("success") return "sync" } } export function request(url : string) : RequestTask | null { return new RequestTaskImpl(url) } // #ifdef UNI-APP-X import KeyEvent from 'android.view.KeyEvent'; import Configuration from 'android.content.res.Configuration'; import Bundle from 'android.os.Bundle'; import Menu from 'android.view.Menu'; import KeyboardShortcutGroup from 'android.view.KeyboardShortcutGroup'; import WindowManager from 'android.view.WindowManager'; import ActionMode from 'android.view.ActionMode'; // export let onCallBackChange: (event: string) => void = (res) => {}; let callback : (eventLog : string) => void = (res) => { }; let callbackIds : string[] = []; export function onCallbackChange(fn : (eventLog : string) => void) { callback = fn } export function registerCallbacks() { let reg1 = UTSAndroid.registerActivityCallback(new UTSAcvitiyLifeCycleCallback()) let reg2 = UTSAndroid.registerActivityCallback(new UTSActivityWindowCallback()) let reg3 = UTSAndroid.registerActivityCallback(new UTSAcvitiyKeyEventCallback()) let reg4 = UTSAndroid.registerActivityCallback(new UTSActivityCallback()) let reg5 = UTSAndroid.registerActivityCallback(new UTSActivityComponentCallback()) callbackIds.push(reg1) callbackIds.push(reg2) callbackIds.push(reg3) callbackIds.push(reg4) callbackIds.push(reg5) } export function unRegisterCallbacks() { callbackIds.forEach((value) => { console.log(value) UTSAndroid.unRegisterActivityCallback(value) }) } export class UTSAcvitiyLifeCycleCallback extends UniActivityLifeCycleCallback { constructor() { super() } override onCreate(params : UniActivityParams, savedInstanceState : Bundle | null) { console.log('UTSAcvitiyLifeCycle', 'onCreate', savedInstanceState) callback('onCreate') } override onResume(params : UniActivityParams) { console.log('UTSAcvitiyLifeCycle', 'onResume', params) callback('onResume') } override onPreResume(params : UniActivityParams) { console.log('UTSAcvitiyLifeCycle', 'onPreResume', params) callback('onPreResume') } override onStart(params : UniActivityParams) { console.log('UTSAcvitiyLifeCycle', 'onStart', params) callback('onStart') } override onPreStart(params : UniActivityParams) { console.log('UTSAcvitiyLifeCycle', 'onPreStart', params) callback('onPreStart') } } export class UTSAcvitiyKeyEventCallback extends UniActivityKeyEventCallback { constructor() { super() } override onKeyDown(params : UniActivityParams, keyCode : Int, event : KeyEvent | null) { console.log('UTSAcvitiyKeyEvent', 'onKeyDown', params, keyCode, '' + event) callback('onKeyDown') } override onPreKeyDown(params : UniActivityParams, keyCode : Int, event : KeyEvent | null) { console.log('UTSAcvitiyKeyEvent', 'onPreKeyDown', params, keyCode, '' + event) callback('onPreKeyDown') } override onKeyLongPress(params : UniActivityParams, keyCode : Int, event : KeyEvent | null) { console.log('UTSAcvitiyKeyEvent', 'onKeyLongPress', params, keyCode, '' + event) callback('onKeyLongPress') } override onPreKeyLongPress(params : UniActivityParams, keyCode : Int, event : KeyEvent | null) { console.log('UTSAcvitiyKeyEvent', 'onPreKeyLongPress', params, keyCode, '' + event) callback('onPreKeyLongPress') } } export class UTSActivityWindowCallback extends UniActivityWindowCallback { constructor() { super() } override dispatchPreKeyEvent(params : UniActivityParams, event : KeyEvent | null) { console.log('UTSActivityWindowCallback', 'dispatchPreKeyEvent', params, '' + event) callback('dispatchPreKeyEvent') } override dispatchKeyEvent(params : UniActivityParams, event : KeyEvent | null) { console.log('UTSActivityWindowCallback', 'dispatchKeyEvent', params, '' + event) callback('dispatchKeyEvent') } override onWindowAttributesChanged(params : UniActivityParams, attrs : WindowManager.LayoutParams) { console.log('UTSActivityWindowCallback', 'onWindowAttributesChanged', '' + attrs) callback('onWindowAttributesChanged') } override onAttachedToWindow(params : UniActivityParams) { console.log('UTSActivityWindowCallback', 'onAttachedToWindow', params) callback('onAttachedToWindow') } override onPanelClosed(params : UniActivityParams, featureId : Int, menu : Menu) { console.log('UTSActivityWindowCallback', 'onPanelClosed', featureId, menu) callback('onPanelClosed') } override onWindowStartingActionMode(params : UniActivityParams, callback : ActionMode.Callback | null) { console.log('UTSActivityWindowCallback', 'onWindowStartingActionMode', callback) callback('onWindowStartingActionMode') } override onProvideKeyboardShortcuts(params : UniActivityParams, data : MutableList | null, menu : Menu | null, deviceId : Int) { console.log('UTSActivityWindowCallback', 'onProvideKeyboardShortcuts', data, menu) callback('onProvideKeyboardShortcuts') } override onPreWindowAttributesChanged(params : UniActivityParams, attrs : WindowManager.LayoutParams) { console.log('UTSActivityWindowCallback', 'onPreWindowAttributesChanged', attrs) callback('onPreWindowAttributesChanged') } override onPrePanelClosed(params : UniActivityParams, featureId : Int, menu : Menu) { console.log('UTSActivityWindowCallback', 'onPrePanelClosed', featureId, menu) callback('onPrePanelClosed') } } export class UTSActivityCallback extends UniActivityCallback { constructor() { super() } override onBackPressed(params : UniActivityParams) { console.log('UTSActivityCallback', 'onBackPressed', params) callback('onBackPressed') } override onPreBackPressed(params : UniActivityParams) { console.log('UTSActivityCallback', 'onPreBackPressed', params) callback('onPreBackPressed') } override onRequestPermissionsResult(params : UniActivityParams, requestCode : Int, permissions : MutableList, grantResults : IntArray) { console.log('UTSActivityCallback', 'onRequestPermissionsResult', params) callback('onRequestPermissionsResult') } } export class UTSActivityComponentCallback extends UniActivityComponentCallback { constructor() { super() } override onConfigurationChanged(params : UniActivityParams, newConfig : Configuration) { console.log('UTSActivityComponentCallback', 'onConfigurationChanged', params, '' + newConfig) callback('onConfigurationChanged') } override onPreConfigurationChanged(params : UniActivityParams, newConfig : Configuration) { console.log('UTSActivityComponentCallback', 'onPreConfigurationChanged', params, '' + newConfig) callback('onPreConfigurationChanged') } } // #endif