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