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, Want } from '@kit.AbilityKit'; 18import { hilog } from '@kit.PerformanceAnalysisKit'; 19import { promptAction } from '@kit.ArkUI'; 20 21const TAG: string = '[UIExtensionContentSession]'; 22const DOMAIN: number = 0xFF00; 23 24@Entry 25@Component 26struct UIExtensionContentSession { 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.UIExtensionContentSession')) 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.UIExtensionAbilityContext_requestModalUIExtension')) 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('startUIExtensionAbility') 74 .onClick(() => { 75 ((): void => { 76 let want: Want = { 77 bundleName: 'com.samples.UIExtension', 78 moduleName: 'entry', 79 abilityName: 'SessionUIExtensionAbility', 80 type: 'sys/commonUI', 81 parameters: { 82 bundleName: 'com.samples.UIExtension', 83 'ability.want.params.uiExtensionType': 'sys/commonUI', 84 } 85 }; 86 87 this.context.requestModalUIExtension(want) 88 .then(() => { 89 let msg: string = `requestModalUIExtension succeed.`; 90 hilog.info(DOMAIN, TAG, msg); 91 promptAction.showToast({ message: msg, duration: this.promptDuration }); 92 }) 93 .catch((err: BusinessError) => { 94 let msg = `requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`; 95 hilog.info(DOMAIN, TAG, msg); 96 promptAction.showToast({ message: msg, duration: this.promptDuration }); 97 }) 98 })() 99 }) 100 } 101 .height('8.86%') 102 .backgroundColor($r('app.color.start_window_background')) 103 .borderRadius(24) 104 .margin({ top: 12, right: 12, left: 12 }) 105 106 } 107 .height('86%') 108 .backgroundColor($r('app.color.backGrounding')) 109 } 110 .height('100%') 111 .width('100%') 112 .backgroundColor($r('app.color.backGrounding')) 113 } 114}