• 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.extStartAbilityAsCaller'),
36      callback: () => {
37        if (this.session === undefined) {
38          logger.error(`${TAG} session undefined`);
39          return;
40        }
41        if (this.want === undefined) {
42          logger.error(`${TAG} want undefined`);
43          return;
44        }
45        let localWant: Want = this.want;
46        localWant.bundleName = 'com.samples.abilityFeatureSystem';
47        localWant.moduleName = 'entry';
48        localWant.abilityName = 'NewAbility';
49        // 使用启动方的Caller身份信息启动新Ability
50        this.session.startAbilityAsCaller(localWant, (err, data) => {
51          logger.info(`${TAG} startAbilityAsCaller error:${JSON.stringify(err)}  data:${JSON.stringify(data)}`);
52        })
53      },
54    }
55  ];
56
57  build() {
58    Column() {
59      Column() {
60        Row() {
61          Image($r('app.media.ic_back'))
62            .size({ width: 20, height: 18 })
63            .margin({ right: 18 })
64            .onClick(() => {
65              if (this.session === undefined) {
66                return;
67              }
68              this.session.sendData({ back: true });
69            })
70          Text($r('app.string.UIExtAbility_label'))
71            .fontSize(20)
72            .fontWeight(FontWeight.Bold)
73        }.height(56)
74        .padding({ left: 24, right: 24 })
75        .width('100%')
76      }
77
78      Column() {
79        List({ space: 0, initialIndex: 0 }) {
80          ForEach(this.list, (item: ListItemModel, index: number) => {
81            ListItemGroup() {
82              ListItem() {
83                Column() {
84                  Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
85                    Text(item.text) {
86                    }.fontSize(16)
87                    .fontColor($r('sys.color.ohos_id_color_toolbar_sub_bg_dark'))
88                    .height(48)
89                  }.width('100%')
90                }
91              }
92              .align(Alignment.Start)
93              .onClick(item.callback)
94              .width('100%')
95              .padding({ left: 12, right: 12 })
96            }
97            .padding({ top: 4, bottom: 4 })
98            .borderRadius(24)
99            .width('100%')
100            .backgroundColor($r('sys.color.ohos_id_color_foreground_contrary'))
101            .margin({ top: 10, bottom: 10 })
102          })
103        }
104        .scrollBar(BarState.Off)
105        .listDirection(Axis.Vertical) // 排列方向
106        .friction(0.6)
107        .width('100%')
108        .height('100%')
109        .padding({ left: 12, right: 12 })
110        .edgeEffect(EdgeEffect.Spring) // 边缘效果设置为Spring
111      }.width('100%')
112    }.width('100%')
113    .height('100%')
114    .backgroundColor($r('sys.color.ohos_id_color_text_field_sub_bg'))
115    .padding({ top: 36, bottom: 106 })
116  }
117}