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 */ 15import hilog from '@ohos.hilog'; 16import abilityManager from '@ohos.app.ability.abilityManager'; 17import common from '@ohos.app.ability.common'; 18import { Callback } from '@ohos.base'; 19import AtomicServiceOptions from '@ohos.app.ability.AtomicServiceOptions'; 20import commonEventManager from '@ohos.commonEventManager'; 21import Base from '@ohos.base'; 22 23@Component 24export struct FullScreenLaunchComponent { 25 @BuilderParam content: Callback<void> = this.doNothingBuilder; 26 context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 27 appId: string = ""; 28 options?: AtomicServiceOptions 29 @State private isShow: boolean = false; 30 private subscriber: commonEventManager.CommonEventSubscriber | null = null; 31 aboutToAppear() { 32 let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { 33 events: [commonEventManager.Support.COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOUT], 34 } 35 36 commonEventManager.createSubscriber(subscribeInfo, 37 (err: Base.BusinessError, data: commonEventManager.CommonEventSubscriber) => { 38 if (err) { 39 hilog.error(0x3900, 'FullScreenLaunchComponent', 40 'Failed to create subscriber, err: %{public}s.', JSON.stringify(err)) 41 return; 42 } 43 44 if (data == null || data == undefined) { 45 hilog.error(0x3900, 'FullScreenLaunchComponent', 'Failed to create subscriber, data is null.') 46 return; 47 } 48 49 this.subscriber = data; 50 commonEventManager.subscribe(this.subscriber, 51 (err: Base.BusinessError, data: commonEventManager.CommonEventData) => { 52 if (err) { 53 hilog.error(0x3900, 'FullScreenLaunchComponent', 54 'Failed to subscribe common event, err: %{public}s.', JSON.stringify(err)) 55 return; 56 } 57 58 hilog.info(0x3900, 'FullScreenLaunchComponent', 'Received account logout event.') 59 this.isShow = false; 60 }) 61 }) 62 } 63 64 aboutToDisappear() { 65 if (this.subscriber !== null) { 66 commonEventManager.unsubscribe(this.subscriber, (err) => { 67 if (err) { 68 hilog.error(0x3900, 'FullScreenLaunchComponent', 69 'UnsubscribeCallBack, err: %{public}s.', JSON.stringify(err)) 70 } else { 71 hilog.info(0x3900, 'FullScreenLaunchComponent', 'Unsubscribe.') 72 this.subscriber = null 73 } 74 }) 75 } 76 } 77 78 @Builder 79 doNothingBuilder() { 80 }; 81 82 resetOptions() { 83 if (this.options?.parameters) { 84 this.options.parameters['ohos.extra.param.key.showMode'] = 1; 85 this.options.parameters['ability.want.params.IsNotifyOccupiedAreaChange'] = true; 86 this.options.parameters['ability.want.params.IsModal'] = true; 87 hilog.info(0x3900, 'FullScreenLaunchComponent', 'replaced options is %{public}s !', JSON.stringify(this.options)) 88 } else { 89 this.options = { 90 parameters: { 91 'ohos.extra.param.key.showMode': 1, 92 'ability.want.params.IsNotifyOccupiedAreaChange': true, 93 'ability.want.params.IsModal': true 94 } 95 } 96 } 97 } 98 99 async checkAbility() { 100 this.resetOptions() 101 try { 102 const res: boolean = await abilityManager.isEmbeddedOpenAllowed(this.context, this.appId) 103 if (res) { 104 this.isShow = true; 105 hilog.info(0x3900, 'FullScreenLaunchComponent', ' EmbeddedOpen is Allowed!') 106 } else { 107 this.popUp() 108 } 109 } catch (e) { 110 hilog.error(0x3900, 'FullScreenLaunchComponent', 'isEmbeddedOpenAllowed called error!%{public}s', e.message) 111 } 112 } 113 114 async popUp() { 115 this.isShow = false; 116 try { 117 const ability = await this.context.openAtomicService(this.appId, this.options) 118 hilog.info(0x3900, 'FullScreenLaunchComponent', '%{public}s open service success!', ability.want) 119 } catch (e) { 120 hilog.error(0x3900, 'FullScreenLaunchComponent', '%{public}s open service error!', e.message) 121 } 122 } 123 124 build() { 125 Row() { 126 this.content(); 127 }.justifyContent(FlexAlign.Center) 128 .onClick( 129 () => { 130 this.checkAbility(); 131 } 132 ).bindContentCover($$this.isShow, this.uiExtensionBuilder()); 133 134 } 135 136 @Builder 137 uiExtensionBuilder() { 138 UIExtensionComponent({ 139 bundleName: `com.atomicservice.${this.appId}`, 140 flags: this.options?.flags, 141 parameters: this.options?.parameters 142 }) 143 .height('100%') 144 .width('100%') 145 .onRelease( 146 () => { 147 this.isShow = false; 148 } 149 ).onError( 150 err => { 151 this.isShow = false; 152 hilog.error(0x3900, 'FullScreenLaunchComponent', 'call up UIExtension error!%{public}s', err.message) 153 this.getUIContext().showAlertDialog({ 154 message: err.message 155 }) 156 } 157 ) 158 } 159}