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