index.uts 10.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import { RequestTask, SyncOptions } from "./interface.uts";
import { log } from "./utils.uts";
Y
yurj26 已提交
3
export type AsyncOptions = {
fxy060608's avatar
fxy060608 已提交
4 5 6 7 8 9
    type : string;
    success : (res : string) => void;
    fail : (res : string) => void;
    complete : (res : string) => void;
};

Y
yurj26 已提交
10
export type {
Q
qiang 已提交
11
    SyncOptions
Y
yurj26 已提交
12 13
} from "./interface.uts";

fxy060608's avatar
fxy060608 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
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" };
}
Q
qiang 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80



async function testAwaitPromise(res : SyntaxResult) : Promise<SyntaxResult> {
    // #ifdef APP-ANDROID
    return await new Promise(function (resolve : (res : SyntaxResult) => void) {
        resolve(res)
    })
    // #endif
    // #ifndef APP-ANDROID
    return res
    // #endif
}
fxy060608's avatar
fxy060608 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/**
 * 导出一个异步方法
 * @returns
 */
export async function testAsync(opts : AsyncOptions) : Promise<SyntaxResult> {
    if (opts.type == "success") {
        opts.success("success");
    } else {
        opts.fail("fail");
    }
    opts.complete("complete");

    const res : SyntaxResult = {
        name: "testAsync"
    }
Q
qiang 已提交
96
    return await testAwaitPromise(res);
fxy060608's avatar
fxy060608 已提交
97
    // return { name: "testAsync" };
杜庆泉's avatar
杜庆泉 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
}


export async function testAsyncParam3(id:number,name:string,opts : AsyncOptions) : Promise<SyntaxResult> {
    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" };
}

fxy060608's avatar
fxy060608 已提交
117

Y
yurj26 已提交
118
export type TestOptions = {
fxy060608's avatar
fxy060608 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    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<SyntaxResult> {
        const res = await testAsync(opts);
        return res;
    }
    testClassSyncWithCallback(opts : AsyncOptions) : SyntaxResult {
        return testSyncWithCallback(opts);
    }
    async testClassAsync(opts : AsyncOptions) : Promise<SyntaxResult> {
        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"
    }
}

166
export function request(url : string) : RequestTask | null {
fxy060608's avatar
fxy060608 已提交
167
    return new RequestTaskImpl(url)
M
mahaifeng 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
}


 // #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<KeyboardShortcutGroup> | 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<String>, 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