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
16if (!("finalizeConstruction" in ViewPU.prototype)) {
17    Reflect.set(ViewPU.prototype, "finalizeConstruction", () => { });
18}
19const hilog = requireNapi('hilog');
20const abilityManager = requireNapi('app.ability.abilityManager');
21const commonEventManager = requireNapi('commonEventManager');
22export class LaunchController {
23    constructor() {
24        this.launchAtomicService = (n1, o1) => { };
25    }
26}
27
28const EMBEDDED_FULL_MODE = 1;
29export class InnerFullScreenLaunchComponent extends ViewPU {
30    constructor(d1, e1, f1, g1 = -1, h1 = undefined, i1) {
31        super(d1, f1, g1, i1);
32        if (typeof h1 === "function") {
33            this.paramsGenerator_ = h1;
34        }
35        this.content = this.doNothingBuilder;
36        this.context = getContext(this);
37        this.controller = new LaunchController();
38        this.appId = '';
39        this.options = undefined;
40        this.__isShow = new ObservedPropertySimplePU(false, this, "isShow");
41        this.subscriber = null;
42        this.launchAtomicService = (k1, l1) => {
43            hilog.info(0x3900, 'InnerFullScreenLaunchComponent', 'launchAtomicService, appId: %{public}s.', k1);
44            this.appId = k1;
45            this.options = l1;
46            this.checkAbility();
47        };
48        this.setInitiallyProvidedValue(e1);
49        this.finalizeConstruction();
50    }
51    setInitiallyProvidedValue(c1) {
52        if (c1.content !== undefined) {
53            this.content = c1.content;
54        }
55        if (c1.context !== undefined) {
56            this.context = c1.context;
57        }
58        if (c1.controller !== undefined) {
59            this.controller = c1.controller;
60        }
61        if (c1.appId !== undefined) {
62            this.appId = c1.appId;
63        }
64        if (c1.options !== undefined) {
65            this.options = c1.options;
66        }
67        if (c1.isShow !== undefined) {
68            this.isShow = c1.isShow;
69        }
70        if (c1.subscriber !== undefined) {
71            this.subscriber = c1.subscriber;
72        }
73        if (c1.launchAtomicService !== undefined) {
74            this.launchAtomicService = c1.launchAtomicService;
75        }
76    }
77    updateStateVars(b1) {
78    }
79    purgeVariableDependenciesOnElmtId(a1) {
80        this.__isShow.purgeDependencyOnElmtId(a1);
81    }
82    aboutToBeDeleted() {
83        this.__isShow.aboutToBeDeleted();
84        SubscriberManager.Get().delete(this.id__());
85        this.aboutToBeDeletedInternal();
86    }
87    get isShow() {
88        return this.__isShow.get();
89    }
90    set isShow(z) {
91        this.__isShow.set(z);
92    }
93    aboutToAppear() {
94        let s = {
95            events: [commonEventManager.Support.COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOUT],
96        };
97        commonEventManager.createSubscriber(s, (u, v) => {
98            if (u) {
99                hilog.error(0x3900, 'InnerFullScreenLaunchComponent', 'Failed to create subscriber, err: %{public}s.', u.message);
100                return;
101            }
102            if (v == null || v == undefined) {
103                hilog.error(0x3900, 'InnerFullScreenLaunchComponent', 'Failed to create subscriber, data is null.');
104                return;
105            }
106            this.subscriber = v;
107            commonEventManager.subscribe(this.subscriber, (x, y) => {
108                if (x) {
109                    hilog.error(0x3900, 'InnerFullScreenLaunchComponent', 'Failed to subscribe common event, err: %{public}s.', x.message);
110                    return;
111                }
112                hilog.info(0x3900, 'InnerFullScreenLaunchComponent', 'Received account logout event.');
113                this.isShow = false;
114            });
115        });
116        this.controller.launchAtomicService = this.launchAtomicService;
117    }
118    aboutToDisappear() {
119        if (this.subscriber !== null) {
120            commonEventManager.unsubscribe(this.subscriber, (r) => {
121                if (r) {
122                    hilog.error(0x3900, 'InnerFullScreenLaunchComponent', 'UnsubscribeCallBack, err: %{public}s.', r.message);
123                }
124                else {
125                    hilog.info(0x3900, 'InnerFullScreenLaunchComponent', 'Unsubscribe.');
126                    this.subscriber = null;
127                }
128            });
129        }
130    }
131    doNothingBuilder(p = null) {
132    }
133    resetOptions() {
134        if (this.options?.parameters) {
135            this.options.parameters['ohos.extra.param.key.showMode'] = EMBEDDED_FULL_MODE;
136            this.options.parameters['ability.want.params.IsNotifyOccupiedAreaChange'] = true;
137            hilog.info(0x3900, 'InnerFullScreenLaunchComponent', 'replaced options is %{public}s !', JSON.stringify(this.options));
138        }
139        else {
140            this.options = {
141                parameters: {
142                    'ohos.extra.param.key.showMode': EMBEDDED_FULL_MODE,
143                    'ability.want.params.IsNotifyOccupiedAreaChange': true,
144                }
145            };
146        }
147    }
148    async checkAbility() {
149        this.resetOptions();
150        try {
151            const o = await abilityManager.isEmbeddedOpenAllowed(this.context, this.appId);
152            if (o) {
153                this.isShow = true;
154                hilog.info(0x3900, 'InnerFullScreenLaunchComponent', ' EmbeddedOpen is Allowed!');
155            }
156            else {
157                hilog.info(0x3900, 'InnerFullScreenLaunchComponent', ' EmbeddedOpen is not Allowed!');
158                this.popUp();
159            }
160        }
161        catch (n) {
162            hilog.error(0x3900, 'InnerFullScreenLaunchComponent', 'isEmbeddedOpenAllowed called error!%{public}s', n.message);
163        }
164    }
165    async popUp() {
166        this.isShow = false;
167        try {
168            const m = await this.context.openAtomicService(this.appId, this.options);
169            hilog.info(0x3900, 'InnerFullScreenLaunchComponent', '%{public}s open service success!', m.want);
170        }
171        catch (l) {
172            hilog.error(0x3900, 'InnerFullScreenLaunchComponent', '%{public}s open service error!', l.message);
173        }
174    }
175    initialRender() {
176        this.observeComponentCreation2((i, j) => {
177            Row.create();
178            Row.justifyContent(FlexAlign.Center);
179            Row.bindContentCover({ value: this.isShow, changeEvent: k => { this.isShow = k; } }, { builder: () => {
180                    this.uiExtensionBuilder.call(this);
181                } }, { modalTransition: ModalTransition.DEFAULT });
182        }, Row);
183        this.content.bind(this)(this);
184        Row.pop();
185    }
186    uiExtensionBuilder(a = null) {
187        this.observeComponentCreation2((c, d) => {
188            UIExtensionComponent.create({
189                bundleName: `com.atomicservice.${this.appId}`,
190                flags: this.options?.flags,
191                parameters: this.options?.parameters
192            });
193            UIExtensionComponent.backgroundColor({ "id": -1, "type": 10001, params: ['sys.color.ohos_id_color_titlebar_bg'], "bundleName": "__harDefaultBundleName__", "moduleName": "__harDefaultModuleName__" });
194            UIExtensionComponent.defaultFocus(true);
195            UIExtensionComponent.height('100%');
196            UIExtensionComponent.width('100%');
197            UIExtensionComponent.onRelease(() => {
198                hilog.error(0x3900, 'InnerFullScreenLaunchComponent', 'onRelease');
199                this.isShow = false;
200            });
201            UIExtensionComponent.onError(g => {
202                this.isShow = false;
203                hilog.error(0x3900, 'InnerFullScreenLaunchComponent', 'call up UIExtension error!%{public}s', g.message);
204                this.getUIContext().showAlertDialog({
205                    message: g.message
206                });
207            });
208        }, UIExtensionComponent);
209    }
210    rerender() {
211        this.updateDirtyElements();
212    }
213}
214
215
216export default { InnerFullScreenLaunchComponent, LaunchController};