• 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 */
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                if (this.isShow) {
154                    hilog.error(0x3900, 'InnerFullScreenLaunchComponent', ' EmbeddedAbility already shows');
155                    this.isShow = false;
156                    return;
157                }
158                this.isShow = true;
159                hilog.info(0x3900, 'InnerFullScreenLaunchComponent', ' EmbeddedOpen is Allowed!');
160            }
161            else {
162                hilog.info(0x3900, 'InnerFullScreenLaunchComponent', ' EmbeddedOpen is not Allowed!');
163                this.popUp();
164            }
165        }
166        catch (n) {
167            hilog.error(0x3900, 'InnerFullScreenLaunchComponent', 'isEmbeddedOpenAllowed called error!%{public}s', n.message);
168        }
169    }
170    async popUp() {
171        this.isShow = false;
172        try {
173            const m = await this.context.openAtomicService(this.appId, this.options);
174            hilog.info(0x3900, 'InnerFullScreenLaunchComponent', '%{public}s open service success!', m.want);
175        }
176        catch (l) {
177            hilog.error(0x3900, 'InnerFullScreenLaunchComponent', '%{public}s open service error!', l.message);
178        }
179    }
180    initialRender() {
181        this.observeComponentCreation2((i, j) => {
182            Row.create();
183            Row.justifyContent(FlexAlign.Center);
184            Row.bindContentCover({ value: this.isShow, changeEvent: k => { this.isShow = k; } }, { builder: () => {
185                    this.uiExtensionBuilder.call(this);
186                } }, { modalTransition: ModalTransition.DEFAULT });
187        }, Row);
188        this.content.bind(this)(this);
189        Row.pop();
190    }
191    uiExtensionBuilder(a = null) {
192        this.observeComponentCreation2((c, d) => {
193            UIExtensionComponent.create({
194                bundleName: `com.atomicservice.${this.appId}`,
195                flags: this.options?.flags,
196                parameters: this.options?.parameters
197            });
198            UIExtensionComponent.backgroundColor({ 'id': -1, 'type': 10001, params: ['sys.color.ohos_id_color_titlebar_bg'], 'bundleName': '__harDefaultBundleName__', 'moduleName': '__harDefaultModuleName__' });
199            UIExtensionComponent.defaultFocus(true);
200            UIExtensionComponent.height('100%');
201            UIExtensionComponent.width('100%');
202            UIExtensionComponent.onRelease(() => {
203                hilog.error(0x3900, 'InnerFullScreenLaunchComponent', 'onRelease');
204                this.isShow = false;
205            });
206            UIExtensionComponent.onError(g => {
207                this.isShow = false;
208                hilog.error(0x3900, 'InnerFullScreenLaunchComponent', 'call up UIExtension error!%{public}s', g.message);
209                this.getUIContext().showAlertDialog({
210                    message: g.message
211                });
212            });
213        }, UIExtensionComponent);
214    }
215    rerender() {
216        this.updateDirtyElements();
217    }
218}
219
220
221export default { InnerFullScreenLaunchComponent, LaunchController};