• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { BusinessError } from '@kit.BasicServicesKit';
17import { common } from '@kit.AbilityKit';
18import { hilog } from '@kit.PerformanceAnalysisKit';
19import { promptAction } from '@kit.ArkUI';
20
21const TAG: string = '[AbilityStartCallback]';
22const DOMAIN: number = 0xFF00;
23
24@Entry
25@Component
26struct AbilityStartCallback {
27  private promptDuration: number = 2000;
28  private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
29
30  build() {
31    Column() {
32      Row() {
33        Flex({ justifyContent: FlexAlign.Start, alignContent: FlexAlign.Center }) {
34          Text($r('app.string.AbilityStartCallback'))
35            .fontSize(30)
36            .fontWeight(700)
37            .textAlign(TextAlign.Start)
38            .margin({
39              top: 8,
40              bottom: 8,
41              left: 12
42            })
43        }
44      }
45      .width('100%')
46      .height('14.36%')
47      .justifyContent(FlexAlign.Start)
48      .backgroundColor($r('app.color.backGrounding'))
49
50      List({ initialIndex: 0 }) {
51        ListItem() {
52          Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
53            Text($r('app.string.UIAbilityContext_startAbilityByType_onResult'))
54              .textAlign(TextAlign.Start)
55              .fontWeight(500)
56              .margin({
57                top: 17,
58                bottom: 17,
59                left: 12
60              })
61              .fontSize(16)
62              .width('77.87%')
63              .height('39.29%')
64              .fontColor($r('app.color.text_color'))
65
66            Row() {
67              Image($r('app.media.ic_arrow'))
68                .width(24)
69                .height(24)
70                .margin({ top: 16, bottom: 16, right: 6 })
71            }
72          }
73          .id('onResult')
74          .onClick(() => {
75            let wantParam: Record<string, Object> = {};
76            let abilityStartCallback: common.AbilityStartCallback = {
77              onError: (code: number, name: string, message: string) => {
78                let msg = `startAbilityByType onError, code:${code} name:${name} message:${message}`;
79                hilog.info(DOMAIN, TAG, msg);
80                promptAction.showToast({ message: msg, duration: this.promptDuration, bottom: '11%' });
81              },
82              onResult: (abilityResult: common.AbilityResult) => {
83                hilog.info(DOMAIN, TAG, `resultCode:` + abilityResult.resultCode + `bundleName:` +
84                  abilityResult.want?.bundleName);
85                let msg = `startAbilityByType onResult,
86                    resultCode:${abilityResult.resultCode} bundleName:${abilityResult.want?.bundleName}`;
87                hilog.info(DOMAIN, TAG, msg);
88                promptAction.showToast({ message: msg, duration: this.promptDuration, bottom: '11%' });
89              }
90            };
91
92            this.context.startAbilityByType('audioPicker', wantParam, abilityStartCallback, (err: BusinessError) => {
93              let msg = `startAbilityByType fail, err: ${JSON.stringify(err)}`;
94              hilog.info(DOMAIN, TAG, msg);
95              promptAction.showToast({ message: msg, duration: this.promptDuration, bottom: '11%' });
96            });
97          })
98        }
99        .height('8.45%')
100        .backgroundColor($r('app.color.start_window_background'))
101        .borderRadius(24)
102        .margin({ top: 12, right: 12, left: 12 })
103
104        ListItem() {
105          Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
106            Text($r('app.string.UIAbilityContext_startAbilityByType_onError'))
107              .textAlign(TextAlign.Start)
108              .fontWeight(500)
109              .margin({
110                top: 17,
111                bottom: 17,
112                left: 12
113              })
114              .fontSize(16)
115              .width('77.87%')
116              .height('39.29%')
117              .fontColor($r('app.color.text_color'))
118          }
119          .id('onError')
120          .onClick(() => {
121            let wantParam: Record<string, Object> = {};
122            let abilityStartCallback: common.AbilityStartCallback = {
123              onError: (code: number, name: string, message: string) => {
124                let msg = `startAbilityByType onError, code:${code} name:${name} message:${message}`;
125                hilog.info(DOMAIN, TAG, msg);
126                promptAction.showToast({ message: msg, duration: this.promptDuration, bottom: '11%' });
127              },
128              onResult: (abilityResult: common.AbilityResult) => {
129                hilog.info(DOMAIN, TAG, `resultCode:` + abilityResult.resultCode + `bundleName:` +
130                  abilityResult.want?.bundleName);
131                let msg = `startAbilityByType onResult,
132                    resultCode:${abilityResult.resultCode} bundleName:${abilityResult.want?.bundleName}`;
133                hilog.info(DOMAIN, TAG, msg);
134                promptAction.showToast({ message: msg, duration: this.promptDuration, bottom: '11%' });
135              }
136            };
137
138            this.context.startAbilityByType('error', wantParam, abilityStartCallback, (err: BusinessError) => {
139              let msg = `startAbilityByType fail, err: ${JSON.stringify(err)}`;
140              hilog.info(DOMAIN, TAG, msg);
141              promptAction.showToast({ message: msg, duration: this.promptDuration, bottom: '11%' });
142            });
143          })
144        }
145        .height('8.45%')
146        .backgroundColor($r('app.color.start_window_background'))
147        .borderRadius(24)
148        .margin({ top: 12, right: 12, left: 12 })
149      }
150      .height('86%')
151      .backgroundColor($r('app.color.backGrounding'))
152    }
153    .height('100%')
154    .width('100%')
155    .backgroundColor($r('app.color.backGrounding'))
156  }
157}