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