1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the 'License'); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an 'AS IS' BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { wantAgent } from '@kit.AbilityKit'; 17import { hilog } from '@kit.PerformanceAnalysisKit'; 18import { promptAction } from '@kit.ArkUI'; 19 20const TAG: string = 'WantAgent'; 21const DOMAIN: number = 0xFF00; 22 23@Entry 24@Component 25struct WantAgentClass { 26 private promptDuration: number = 2000; 27 private wantAgentInfo: wantAgent.WantAgentInfo = { 28 wants: [ 29 { 30 bundleName: 'com.samples.abilityruntime', 31 abilityName: 'EntryAbility', 32 } 33 ], 34 actionType: wantAgent.OperationType.START_ABILITY, 35 requestCode: 0, 36 actionFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 37 } 38 39 async getBundleNameCallback(): Promise<void> { 40 wantAgent.getWantAgent(this.wantAgentInfo).then((dataBase) => { 41 wantAgent.getBundleName(dataBase, (err, data) => { 42 hilog.info(DOMAIN, TAG, 43 `The result of getBundleName(callback) is: ${JSON.stringify(data)}`); 44 promptAction.showToast({ 45 message: `getBundleName(callback) success, data: ${JSON.stringify(data)}`, 46 duration: this.promptDuration 47 }) 48 }) 49 console.info(`${TAG} getWantAgent success: ${JSON.stringify(dataBase)}`) 50 }) 51 } 52 53 async getBundleNamePromise(): Promise<void> { 54 wantAgent.getWantAgent(this.wantAgentInfo).then((dataBase) => { 55 wantAgent.getBundleName(dataBase).then((data) => { 56 hilog.info(DOMAIN, TAG, 57 `The result of getBundleName(promise) is: ${JSON.stringify(data)}`); 58 promptAction.showToast({ 59 message: `getBundleName(promise) success, data: ${JSON.stringify(data)}`, 60 duration: this.promptDuration 61 }) 62 }) 63 console.info(`${TAG} getWantAgent success: ${JSON.stringify(dataBase)}`) 64 }) 65 } 66 67 async getUidCallback(): Promise<void> { 68 wantAgent.getWantAgent(this.wantAgentInfo).then((dataBase) => { 69 wantAgent.getUid(dataBase, (err, data) => { 70 hilog.info(DOMAIN, TAG, 71 `The result of getUid(callback) is: ${JSON.stringify(data)}`); 72 promptAction.showToast({ 73 message: `getUid(callback) success, data: ${JSON.stringify(data)}`, 74 duration: this.promptDuration 75 }) 76 }) 77 console.info(`${TAG} getWantAgent success: ${JSON.stringify(dataBase)}`) 78 }) 79 } 80 81 async getUidPromise(): Promise<void> { 82 wantAgent.getWantAgent(this.wantAgentInfo).then((dataBase) => { 83 wantAgent.getUid(dataBase).then((data) => { 84 hilog.info(DOMAIN, TAG, 85 `The result of getUid(promise) is: ${JSON.stringify(data)}`); 86 promptAction.showToast({ 87 message: `getUid(promise) success, data: ${JSON.stringify(data)}`, 88 duration: this.promptDuration 89 }) 90 }) 91 console.info(`${TAG} getWantAgent success: ${JSON.stringify(dataBase)}`) 92 }) 93 } 94 95 async cancelCallback(): Promise<void> { 96 wantAgent.getWantAgent(this.wantAgentInfo).then((dataBase) => { 97 wantAgent.cancel(dataBase, (err, data) => { 98 hilog.info(DOMAIN, TAG, 99 `The result of cancel(callback) is: ${JSON.stringify(data)}`); 100 promptAction.showToast({ 101 message: `cancel(callback) success`, 102 duration: this.promptDuration 103 }) 104 }) 105 console.info(`${TAG} getWantAgent success: ${JSON.stringify(dataBase)}`) 106 }) 107 } 108 109 async cancelPromise(): Promise<void> { 110 wantAgent.getWantAgent(this.wantAgentInfo).then((dataBase) => { 111 wantAgent.cancel(dataBase).then((data) => { 112 hilog.info(DOMAIN, TAG, 113 `The result of cancel(promise) is: ${JSON.stringify(data)}`); 114 promptAction.showToast({ 115 message: `cancel(promise) success`, 116 duration: this.promptDuration 117 }) 118 }) 119 console.info(`${TAG} getWantAgent success: ${JSON.stringify(dataBase)}`) 120 }) 121 } 122 123 async equalCallback(): Promise<void> { 124 wantAgent.getWantAgent(this.wantAgentInfo).then((dataBase) => { 125 wantAgent.equal(dataBase, dataBase, (err, data) => { 126 hilog.info(DOMAIN, TAG, 127 `The result of equal(callback) is: ${JSON.stringify(data)}`); 128 promptAction.showToast({ 129 message: `equal(callback) success, data: ${JSON.stringify(data)}`, 130 duration: this.promptDuration 131 }) 132 }) 133 console.info(`${TAG} getWantAgent success: ${JSON.stringify(dataBase)}`) 134 }) 135 } 136 137 async equalPromise(): Promise<void> { 138 wantAgent.getWantAgent(this.wantAgentInfo).then((dataBase) => { 139 wantAgent.equal(dataBase, dataBase).then((data) => { 140 hilog.info(DOMAIN, TAG, 141 `The result of equal(promise) is: ${JSON.stringify(data)}`); 142 promptAction.showToast({ 143 message: `equal(promise) success, data: ${JSON.stringify(data)}`, 144 duration: this.promptDuration 145 }) 146 }) 147 console.info(`${TAG} getWantAgent success: ${JSON.stringify(dataBase)}`) 148 }) 149 } 150 151 async getOperationTypeCallback(): Promise<void> { 152 wantAgent.getWantAgent(this.wantAgentInfo).then((dataBase) => { 153 wantAgent.getOperationType(dataBase, (err, data) => { 154 hilog.info(DOMAIN, TAG, 155 `The result of getOperationType(callback) is: ${JSON.stringify(data)}`); 156 promptAction.showToast({ 157 message: `getOperationType(callback) success, data: ${JSON.stringify(data)}`, 158 duration: this.promptDuration 159 }) 160 }) 161 console.info(`${TAG} getWantAgent success: ${JSON.stringify(dataBase)}`) 162 }) 163 } 164 165 async getOperationTypePromise(): Promise<void> { 166 wantAgent.getWantAgent(this.wantAgentInfo).then((dataBase) => { 167 wantAgent.getOperationType(dataBase).then((data) => { 168 hilog.info(DOMAIN, TAG, 169 `The result of getOperationType(promise) is: ${JSON.stringify(data)}`); 170 promptAction.showToast({ 171 message: `getOperationType(promise) success, data: ${JSON.stringify(data)}`, 172 duration: this.promptDuration 173 }) 174 }) 175 console.info(`${TAG} getWantAgent success: ${JSON.stringify(dataBase)}`) 176 }) 177 } 178 179 async trigger(): Promise<void> { 180 let triggerInfo: wantAgent.TriggerInfo = { 181 code: 0, 182 want: { 183 bundleName: 'com.samples.abilityruntime', 184 abilityName: 'EntryAbility', 185 }, 186 permission: '', 187 extraInfo: { 188 test: 'this is a test value' 189 } 190 } 191 192 wantAgent.getWantAgent(this.wantAgentInfo).then((dataBase) => { 193 wantAgent.trigger(dataBase, triggerInfo, (err, data) => { 194 hilog.info(DOMAIN, TAG, 195 `The result of trigger is: ${JSON.stringify(data)}`); 196 promptAction.showToast({ 197 message: `trigger success, data: ${JSON.stringify(data)}`, 198 duration: this.promptDuration 199 }) 200 }) 201 console.info(`${TAG} getWantAgent success: ${JSON.stringify(dataBase)}`) 202 }) 203 } 204 205 build() { 206 Column() { 207 Row() { 208 Flex({ justifyContent: FlexAlign.Start, alignContent: FlexAlign.Center }) { 209 Text($r('app.string.WantAgent')) 210 .fontSize(30) 211 .fontWeight(700) 212 .textAlign(TextAlign.Start) 213 .margin({ 214 top: 8, 215 bottom: 8, 216 left: 12 217 }) 218 } 219 } 220 .width('100%') 221 .height('14.36%') 222 .justifyContent(FlexAlign.Start) 223 .backgroundColor($r('app.color.backGrounding')) 224 225 List({ initialIndex: 0 }) { 226 ListItem() { 227 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 228 Text($r(`app.string.getBundleNameCallback`)) 229 .textAlign(TextAlign.Start) 230 .fontWeight(500) 231 .margin({ 232 top: 17, 233 bottom: 17, 234 left: 12 235 }) 236 .fontSize(16) 237 .width('77.87%') 238 .height('39.29%') 239 .fontColor($r('app.color.text_color')) 240 } 241 .id('getBundleNameCallback') 242 .onClick(() => { 243 this.getBundleNameCallback(); 244 }) 245 } 246 .height('8.45%') 247 .backgroundColor($r('app.color.start_window_background')) 248 .borderRadius(24) 249 .margin({ top: 12, right: 12, left: 12 }) 250 251 ListItem() { 252 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 253 Text($r(`app.string.getBundleNamePromise`)) 254 .textAlign(TextAlign.Start) 255 .fontWeight(500) 256 .margin({ 257 top: 17, 258 bottom: 17, 259 left: 12 260 }) 261 .fontSize(16) 262 .width('77.87%') 263 .height('39.29%') 264 .fontColor($r('app.color.text_color')) 265 } 266 .id('getBundleNamePromise') 267 .onClick(() => { 268 this.getBundleNamePromise(); 269 }) 270 } 271 .height('8.45%') 272 .backgroundColor($r('app.color.start_window_background')) 273 .borderRadius(24) 274 .margin({ top: 12, right: 12, left: 12 }) 275 276 ListItem() { 277 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 278 Text($r(`app.string.getUidCallback`)) 279 .textAlign(TextAlign.Start) 280 .fontWeight(500) 281 .margin({ 282 top: 17, 283 bottom: 17, 284 left: 12 285 }) 286 .fontSize(16) 287 .width('77.87%') 288 .height('39.29%') 289 .fontColor($r('app.color.text_color')) 290 } 291 .id('getUidCallback') 292 .onClick(() => { 293 this.getUidCallback(); 294 }) 295 } 296 .height('8.45%') 297 .backgroundColor($r('app.color.start_window_background')) 298 .borderRadius(24) 299 .margin({ top: 12, right: 12, left: 12 }) 300 301 ListItem() { 302 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 303 Text($r(`app.string.getUidPromise`)) 304 .textAlign(TextAlign.Start) 305 .fontWeight(500) 306 .margin({ 307 top: 17, 308 bottom: 17, 309 left: 12 310 }) 311 .fontSize(16) 312 .width('77.87%') 313 .height('39.29%') 314 .fontColor($r('app.color.text_color')) 315 } 316 .id('getUidPromise') 317 .onClick(() => { 318 this.getUidPromise(); 319 }) 320 } 321 .height('8.45%') 322 .backgroundColor($r('app.color.start_window_background')) 323 .borderRadius(24) 324 .margin({ top: 12, right: 12, left: 12 }) 325 326 ListItem() { 327 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 328 Text($r(`app.string.cancelCallback`)) 329 .textAlign(TextAlign.Start) 330 .fontWeight(500) 331 .margin({ 332 top: 17, 333 bottom: 17, 334 left: 12 335 }) 336 .fontSize(16) 337 .width('77.87%') 338 .height('39.29%') 339 .fontColor($r('app.color.text_color')) 340 } 341 .id('cancelCallback') 342 .onClick(() => { 343 this.cancelCallback(); 344 }) 345 } 346 .height('8.45%') 347 .backgroundColor($r('app.color.start_window_background')) 348 .borderRadius(24) 349 .margin({ top: 12, right: 12, left: 12 }) 350 351 ListItem() { 352 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 353 Text($r(`app.string.cancelPromise`)) 354 .textAlign(TextAlign.Start) 355 .fontWeight(500) 356 .margin({ 357 top: 17, 358 bottom: 17, 359 left: 12 360 }) 361 .fontSize(16) 362 .width('77.87%') 363 .height('39.29%') 364 .fontColor($r('app.color.text_color')) 365 } 366 .id('cancelPromise') 367 .onClick(() => { 368 this.cancelPromise(); 369 }) 370 } 371 .height('8.45%') 372 .backgroundColor($r('app.color.start_window_background')) 373 .borderRadius(24) 374 .margin({ top: 12, right: 12, left: 12 }) 375 376 ListItem() { 377 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 378 Text($r(`app.string.equalCallback`)) 379 .textAlign(TextAlign.Start) 380 .fontWeight(500) 381 .margin({ 382 top: 17, 383 bottom: 17, 384 left: 12 385 }) 386 .fontSize(16) 387 .width('77.87%') 388 .height('39.29%') 389 .fontColor($r('app.color.text_color')) 390 } 391 .id('equalCallback') 392 .onClick(() => { 393 this.equalCallback(); 394 }) 395 } 396 .height('8.45%') 397 .backgroundColor($r('app.color.start_window_background')) 398 .borderRadius(24) 399 .margin({ top: 12, right: 12, left: 12 }) 400 401 ListItem() { 402 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 403 Text($r(`app.string.equalPromise`)) 404 .textAlign(TextAlign.Start) 405 .fontWeight(500) 406 .margin({ 407 top: 17, 408 bottom: 17, 409 left: 12 410 }) 411 .fontSize(16) 412 .width('77.87%') 413 .height('39.29%') 414 .fontColor($r('app.color.text_color')) 415 } 416 .id('equalPromise') 417 .onClick(() => { 418 this.equalPromise(); 419 }) 420 } 421 .height('8.45%') 422 .backgroundColor($r('app.color.start_window_background')) 423 .borderRadius(24) 424 .margin({ top: 12, right: 12, left: 12 }) 425 426 ListItem() { 427 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 428 Text($r(`app.string.getOperationTypeCallback`)) 429 .textAlign(TextAlign.Start) 430 .fontWeight(500) 431 .margin({ 432 top: 17, 433 bottom: 17, 434 left: 12 435 }) 436 .fontSize(16) 437 .width('77.87%') 438 .height('39.29%') 439 .fontColor($r('app.color.text_color')) 440 } 441 .id('getOperationTypeCallback') 442 .onClick(() => { 443 this.getOperationTypeCallback(); 444 }) 445 } 446 .height('8.45%') 447 .backgroundColor($r('app.color.start_window_background')) 448 .borderRadius(24) 449 .margin({ top: 12, right: 12, left: 12 }) 450 451 ListItem() { 452 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 453 Text($r(`app.string.getOperationTypePromise`)) 454 .textAlign(TextAlign.Start) 455 .fontWeight(500) 456 .margin({ 457 top: 17, 458 bottom: 17, 459 left: 12 460 }) 461 .fontSize(16) 462 .width('77.87%') 463 .height('39.29%') 464 .fontColor($r('app.color.text_color')) 465 } 466 .id('getOperationTypePromise') 467 .onClick(() => { 468 this.getOperationTypePromise(); 469 }) 470 } 471 .height('8.45%') 472 .backgroundColor($r('app.color.start_window_background')) 473 .borderRadius(24) 474 .margin({ top: 12, right: 12, left: 12 }) 475 476 ListItem() { 477 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 478 Text($r(`app.string.trigger`)) 479 .textAlign(TextAlign.Start) 480 .fontWeight(500) 481 .margin({ 482 top: 17, 483 bottom: 17, 484 left: 12 485 }) 486 .fontSize(16) 487 .width('77.87%') 488 .height('39.29%') 489 .fontColor($r('app.color.text_color')) 490 } 491 .id('trigger') 492 .onClick(() => { 493 this.trigger(); 494 }) 495 } 496 .height('8.45%') 497 .backgroundColor($r('app.color.start_window_background')) 498 .borderRadius(24) 499 .margin({ top: 12, right: 12, left: 12 }) 500 } 501 .height('86%') 502 .backgroundColor($r('app.color.backGrounding')) 503 } 504 .width('100%') 505 } 506}