• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
17import Want from '@ohos.app.ability.Want';
18import { logger } from '../util/Logger';
19
20export interface ListItemModel {
21  text: Resource,
22  callback?: () => void
23}
24
25const TAG: string = 'UIExtensionAbility';
26let storage: LocalStorage = LocalStorage.getShared();
27
28@Entry(storage)
29@Component
30struct Ext {
31  private session: (UIExtensionContentSession | undefined) = storage.get<UIExtensionContentSession>('session');
32  private want: (Want | undefined) = storage.get<Want>('want');
33  @State list: ListItemModel[] = [
34    {
35      text: $r('app.string.extStartAbilityByType'),
36      callback: () => {
37        if (this.session === undefined) {
38          logger.error(`${TAG} session undefined`);
39          return;
40        }
41        this.session.startAbilityByType("share", {}, {
42          onError: (code: number, name: string, message: string) => {
43            logger.error(`${TAG} code:${code} name:${name} message:${message}`);
44          }
45        }, (err) => {
46          logger.error(`${TAG} startAbilityByType fail, err:${JSON.stringify(err)}`);
47        });
48      }
49    },
50    {
51      text: $r('app.string.extStartAbilityAsCaller'),
52      callback: () => {
53        if (this.session === undefined) {
54          logger.error(`${TAG} session undefined`);
55          return;
56        }
57        if (this.want === undefined) {
58          logger.error(`${TAG} want undefined`);
59          return;
60        }
61        let localWant: Want = this.want;
62        localWant.bundleName = 'com.samples.abilityFeatureSystem';
63        localWant.moduleName = 'entry';
64        localWant.abilityName = 'NewAbility';
65        // 使用启动方的Caller身份信息启动新Ability
66        this.session.startAbilityAsCaller(localWant, (err, data) => {
67          logger.info(`${TAG} startAbilityAsCaller error:${JSON.stringify(err)}  data:${JSON.stringify(data)}`);
68        })
69      },
70    }
71  ];
72
73  build() {
74    Column() {
75      Column() {
76        Row() {
77          Image($r('app.media.ic_back'))
78            .size({ width: 20, height: 18 })
79            .margin({ right: 18 })
80            .onClick(() => {
81              if (this.session === undefined) {
82                return;
83              }
84              this.session.sendData({ back: true });
85            })
86          Text($r('app.string.UIExtAbility_label'))
87            .fontSize(20)
88            .fontWeight(FontWeight.Bold)
89        }.height(56)
90        .padding({ left: 24, right: 24 })
91        .width('100%')
92      }
93
94      Column() {
95        List({ space: 0, initialIndex: 0 }) {
96          ForEach(this.list, (item: ListItemModel, index: number) => {
97            ListItemGroup() {
98              ListItem() {
99                Column() {
100                  Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
101                    Text(item.text) {
102                    }.fontSize(16)
103                    .fontColor($r('sys.color.ohos_id_color_toolbar_sub_bg_dark'))
104                    .height(48)
105                  }.width('100%')
106                }
107              }
108              .align(Alignment.Start)
109              .onClick(item.callback)
110              .width('100%')
111              .padding({ left: 12, right: 12 })
112            }
113            .padding({ top: 4, bottom: 4 })
114            .borderRadius(24)
115            .width('100%')
116            .backgroundColor($r('sys.color.ohos_id_color_foreground_contrary'))
117            .margin({ top: 10, bottom: 10 })
118          })
119        }
120        .scrollBar(BarState.Off)
121        .listDirection(Axis.Vertical) // 排列方向
122        .friction(0.6)
123        .width('100%')
124        .height('100%')
125        .padding({ left: 12, right: 12 })
126        .edgeEffect(EdgeEffect.Spring) // 边缘效果设置为Spring
127      }.width('100%')
128    }.width('100%')
129    .height('100%')
130    .backgroundColor($r('sys.color.ohos_id_color_text_field_sub_bg'))
131    .padding({ top: 36, bottom: 106 })
132  }
133}