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 hilog from '@ohos.hilog'; 17import { common, OpenLinkOptions } from '@kit.AbilityKit'; 18import { BusinessError } from '@kit.BasicServicesKit'; 19import { promptAction } from '@kit.ArkUI'; 20 21const TAG: string = '[UIExtensionContext]'; 22const DOMAIN: number = 0xFF00; 23 24@Entry 25@Component 26struct UIExtensionContext { 27 private promptDuration: number = 2000; 28 private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 29 private uiExtContext = getContext(this) as common.UIExtensionContext; 30 31 build() { 32 Column() { 33 Row() { 34 Flex({ justifyContent: FlexAlign.Start, alignContent: FlexAlign.Center }) { 35 Text($r('app.string.UIExtensionContext')) 36 .fontSize(30) 37 .fontWeight(700) 38 .textAlign(TextAlign.Start) 39 .margin({ 40 top: 8, 41 bottom: 8, 42 left: 12 43 }) 44 } 45 } 46 .width('100%') 47 .height('14.36%') 48 .justifyContent(FlexAlign.Start) 49 .backgroundColor($r('app.color.backGrounding')) 50 51 List({ initialIndex: 0 }) { 52 ListItem() { 53 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 54 Text($r('app.string.UIExtensionAbilityContext_openLink')) 55 .textAlign(TextAlign.Start) 56 .fontWeight(500) 57 .margin({ 58 top: 17, 59 bottom: 17, 60 left: 12 61 }) 62 .fontSize(16) 63 .width('77.87%') 64 .height('39.29%') 65 .fontColor($r('app.color.text_color')) 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('openLink') 74 .onClick(() => { 75 ((): void => { 76 let link: string = 'http://docs.openharmony.cn'; 77 let openLinkOptions: OpenLinkOptions = { 78 appLinkingOnly: false 79 }; 80 try { 81 this.uiExtContext.openLink( 82 link, 83 openLinkOptions, 84 (err, result) => { 85 hilog.info(DOMAIN, TAG, 'openLink callback result:', JSON.stringify(result.resultCode)); 86 } 87 ).then(() => { 88 hilog.info(DOMAIN, TAG, 'open link success'); 89 }).catch((err: BusinessError) => { 90 hilog.info(DOMAIN, TAG, 'open link failed, errCode : %{public}s', JSON.stringify(err.code)); 91 }); 92 } catch (e) { 93 hilog.info(DOMAIN, TAG, 'exception occurred, errCode : %{public}s', JSON.stringify(e.code)); 94 } 95 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.UIExtensionAbilityContext_requestModalUIExtension')) 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 Row() { 120 Image($r('app.media.ic_arrow')) 121 .width(24) 122 .height(24) 123 .margin({ top: 16, bottom: 16, right: 6 }) 124 } 125 } 126 .id('startUIExtensionAbility') 127 .onClick(() => { 128 ((): void => { 129 130 let want: Want = { 131 bundleName: 'com.samples.UIExtension', 132 moduleName: 'entry', 133 abilityName: 'ContextUIExtensionAbility', 134 type: 'sys/commonUI', 135 parameters: { 136 bundleName: 'com.samples.UIExtension', 137 'ability.want.params.uiExtensionType': 'sys/commonUI', 138 } 139 }; 140 141 this.context.requestModalUIExtension(want) 142 .then(() => { 143 let msg: string = `requestModalUIExtension succeed.`; 144 hilog.info(DOMAIN, TAG, msg); 145 promptAction.showToast({ message: msg, duration: this.promptDuration }); 146 }) 147 .catch((err: BusinessError) => { 148 let msg = `requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`; 149 hilog.info(DOMAIN, TAG, msg); 150 promptAction.showToast({ message: msg, duration: this.promptDuration }); 151 }) 152 153 })() 154 }) 155 } 156 .height('8.45%') 157 .backgroundColor($r('app.color.start_window_background')) 158 .borderRadius(24) 159 .margin({ top: 12, right: 12, left: 12 }) 160 161 } 162 .height('86%') 163 .backgroundColor($r('app.color.backGrounding')) 164 } 165 .width('100%') 166 .height('100%') 167 .backgroundColor($r('app.color.backGrounding')) 168 } 169}