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
16import process from '@ohos.process';
17import PluginMgr from '@ohos.pluginComponent';
18import pluginComponentManager from '@ohos.pluginComponent';
19import { AsyncCallback } from '@ohos.base';
20
21const TAG = 'InjectNotice';
22const PLUGIN_NAME = 'pages/capsuleIcon.js';
23
24export class CapsuleUtil {
25  public static readonly APP_NAME: string = 'com.ohos.powerdialog';
26  private static instance: CapsuleUtil;
27  private constructor() {}
28
29  public static getInstance(): CapsuleUtil {
30    if (!CapsuleUtil.instance) {
31      console.debug(TAG, `instance begin init`);
32      CapsuleUtil.instance = new CapsuleUtil();
33    }
34    console.debug(TAG, `instance return obj:${CapsuleUtil.instance}`);
35    return CapsuleUtil.instance;
36  }
37
38  /**
39   * peocess capsule visible or not
40   *
41   * @param isVisible visible status
42   */
43  private pushToPluginMgr(extraData: pluginComponentManager.KVObject, logInfo: AsyncCallback<void>): void {
44    console.debug(TAG, `pushToPluginMgr push begin `);
45    let pushData:PluginMgr.PushParameterForStage = {
46      owner: {
47        bundleName: CapsuleUtil.APP_NAME,
48      },
49      target: {
50        bundleName: CapsuleUtil.APP_NAME,
51      },
52      name: PLUGIN_NAME,
53      data: {},
54      extraData: extraData
55    }
56    console.debug(TAG, `pushToPluginMgr push content: ${JSON.stringify(pushData)} `);
57    PluginMgr.push(pushData, logInfo);
58  }
59
60  public processCapsule(isVisible: boolean): void {
61    this.pushToPluginMgr({
62      requestVisible: isVisible,
63      requestPid: process.pid
64    }, (err, data) => {
65      console.info(TAG, 'push complete isVisible: ' + isVisible);
66      console.log('push_callback:err:', JSON.stringify(err));
67      console.log('push_callback:data:', JSON.stringify(data));
68    });
69  }
70
71  /**
72   * close panel
73   */
74  public closePanel(): void {
75    this.pushToPluginMgr({
76      requestVisible: false,
77      requestCloseWindow: true,
78      requestPid: process.pid
79    }, (err, data) => {
80        console.info(TAG, 'push close panel complete');
81        console.log('push_callback:err:', JSON.stringify(err));
82        console.log('push_callback:data:', JSON.stringify(data));
83    });
84  }
85
86  /**
87   * close panel
88   */
89  public closeWindow(): void {
90    this.pushToPluginMgr({
91      requestCloseWindow: true
92    }, (err, data) => {
93        console.info(TAG, 'push close window complete');
94        console.log('push_callback:err:', JSON.stringify(err));
95        console.log('push_callback:data:', JSON.stringify(data));
96    });
97  }
98}