• 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 { common, Want } from '@kit.AbilityKit';
17import { hilog } from '@kit.PerformanceAnalysisKit';
18import { promptAction } from '@kit.ArkUI';
19
20const TAG: string = 'UIExtensionContent';
21const DOMAIN: number = 0xFF00;
22let storage: LocalStorage = LocalStorage.getShared();
23
24@Entry
25@Component
26struct UIExtensionContextPage {
27  private context = getContext(this) as common.UIExtensionContext;
28  private want: (Want | undefined) = storage.get<Want>('want');
29  private promptDuration: number = 2000;
30
31  build() {
32    Column() {
33      Row() {
34        Flex({ justifyContent: FlexAlign.Start, alignContent: FlexAlign.Center }) {
35          Text($r('app.string.UIExtensionAbilityContext'))
36            .fontSize(30)
37            .fontWeight(700)
38            .textAlign(TextAlign.Start)
39            .margin({
40              top: '12%',
41              left: '2%'
42            })
43        }
44      }
45      .width('100%')
46      .height('18%')
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.UIExtensionContent_reportDrawnCompleted'))
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          .id('reportDrawnCompleted')
67          .onClick(() => {
68            ((): void => {
69              try {
70                this.context.reportDrawnCompleted((err) => {
71                  if (err.code) {
72                    // 处理业务逻辑错误
73                    let msg =
74                      `UIExtensionContent.reportDrawnCompleted failed, code is ${err.code}, message is ${err.message}`;
75                    hilog.info(DOMAIN, TAG, msg);
76                    promptAction.showToast({ message: msg, duration: this.promptDuration });
77                    return;
78                  }
79                  // 执行正常业务
80                  let msg = `UIExtensionContent.reportDrawnCompleted succeed.`;
81                  hilog.info(DOMAIN, TAG, msg);
82                  promptAction.showToast({ message: msg, duration: this.promptDuration });
83                });
84              } catch (err) {
85                // 捕获同步的参数错误
86                let msg = `UIExtensionContent.reportDrawnCompleted failed,err is ${JSON.stringify(err)}`;
87                hilog.info(DOMAIN, TAG, msg);
88                promptAction.showToast({ message: msg, duration: this.promptDuration });
89              }
90            })()
91          })
92        }
93        .height('8.45%')
94        .backgroundColor($r('app.color.start_window_background'))
95        .borderRadius(24)
96        .margin({ top: 12, right: 12, left: 12 })
97
98        ListItem() {
99          Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
100            Text($r('app.string.UIExtensionContent_terminateSelfCallback'))
101              .textAlign(TextAlign.Start)
102              .fontWeight(500)
103              .margin({
104                top: 17,
105                bottom: 17,
106                left: 12
107              })
108              .fontSize(16)
109              .width('77.87%')
110              .height('39.29%')
111              .fontColor($r('app.color.text_color'))
112          }
113          .id('terminateSelfCallback')
114          .onClick(() => {
115            ((): void => {
116              this.context?.terminateSelf(() => {
117                hilog.info(DOMAIN, TAG, 'UIExtensionContent_terminateSelfCallback(callback) called');
118              })
119            })()
120          })
121        }
122        .height('8.45%')
123        .backgroundColor($r('app.color.start_window_background'))
124        .borderRadius(24)
125        .margin({ top: 12, right: 12, left: 12 })
126
127        ListItem() {
128          Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
129            Text($r('app.string.UIExtensionContent_terminateSelfPromise'))
130              .textAlign(TextAlign.Start)
131              .fontWeight(500)
132              .margin({
133                top: 17,
134                bottom: 17,
135                left: 12
136              })
137              .fontSize(16)
138              .width('77.87%')
139              .height('39.29%')
140              .fontColor($r('app.color.text_color'))
141          }
142          .id('terminateSelfPromise')
143          .onClick(() => {
144            ((): void => {
145              this.context?.terminateSelf().then(() => {
146                hilog.info(DOMAIN, TAG, 'UIExtensionContent_terminateSelfPromise(promise) called');
147              })
148            })()
149          })
150        }
151        .height('8.45%')
152        .backgroundColor($r('app.color.start_window_background'))
153        .borderRadius(24)
154        .margin({ top: 12, right: 12, left: 12 })
155
156        ListItem() {
157          Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
158            Text($r('app.string.UIExtensionContent_terminateSelfWithResultCallback'))
159              .textAlign(TextAlign.Start)
160              .fontWeight(500)
161              .margin({
162                top: 17,
163                bottom: 17,
164                left: 12
165              })
166              .fontSize(16)
167              .width('77.87%')
168              .height('39.29%')
169              .fontColor($r('app.color.text_color'))
170          }
171          .id('terminateSelfWithResultCallback')
172          .onClick(() => {
173            ((): void => {
174              this.context?.terminateSelfWithResult({ resultCode: 99, want: this.want }, () => {
175                hilog.info(DOMAIN, TAG, 'UIExtensionContent_terminateSelfWithResultCallback(callback) called');
176              })
177            })()
178          })
179        }
180        .height('8.45%')
181        .backgroundColor($r('app.color.start_window_background'))
182        .borderRadius(24)
183        .margin({ top: 12, right: 12, left: 12 })
184
185        ListItem() {
186          Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
187            Text($r('app.string.UIExtensionContent_terminateSelfWithResultPromise'))
188              .textAlign(TextAlign.Start)
189              .fontWeight(500)
190              .margin({
191                top: 17,
192                bottom: 17,
193                left: 12
194              })
195              .fontSize(16)
196              .width('77.87%')
197              .height('39.29%')
198              .fontColor($r('app.color.text_color'))
199          }
200          .id('terminateSelfWithResultPromise')
201          .onClick(() => {
202            ((): void => {
203              this.context?.terminateSelfWithResult({ resultCode: 100, want: this.want }).then(() => {
204                hilog.info(DOMAIN, TAG, 'UIExtensionContent.terminateSelfWithResult(promise) called');
205              })
206            })()
207          })
208        }
209        .height('8.45%')
210        .backgroundColor($r('app.color.start_window_background'))
211        .borderRadius(24)
212        .margin({ top: 12, right: 12, left: 12 })
213
214      }
215      .height('86%')
216      .backgroundColor($r('app.color.backGrounding'))
217    }
218    .height('100%')
219    .width('100%')
220    .backgroundColor($r('app.color.backGrounding'))
221  }
222}