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