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