• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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 Constants from '../../../common/constant';
17import { HiLog } from '../../../common/HiLog';
18import { rpc } from '@kit.IPCKit';
19import { emitter } from '@kit.BasicServicesKit';
20
21const TAG = 'ViewAbilityService';
22
23export default class ViewAbilityService {
24  private static instance: ViewAbilityService;
25  private _remoteProxy?: rpc.IRemoteObject | undefined;
26
27  static getInstance(): ViewAbilityService {
28    if (!ViewAbilityService.instance) {
29      ViewAbilityService.instance = new ViewAbilityService();
30    }
31    return ViewAbilityService.instance;
32  }
33
34  private constructor() {
35  }
36
37  public async setRemoteProxy(value: rpc.IRemoteObject) {
38    this._remoteProxy = value;
39    emitter.emit(Constants.CHECK_SHOW_DIALOG_STATE);
40  }
41
42  public async showDialog(showDialog: boolean, requestId?: string): Promise<boolean> {
43    HiLog.info(TAG, `showDialog ${showDialog}, requestId ${requestId}`);
44    if (!this._remoteProxy) {
45      HiLog.error(TAG, 'showDialog remoteProxy is invalid');
46      return false;
47    }
48    let option = new rpc.MessageOption();
49    let data = new rpc.MessageSequence();
50    let reply = new rpc.MessageSequence();
51    try {
52      data.writeInterfaceToken(Constants.DLP_MGR_VIEW_ABILITY_TOKEN);
53      data.writeBoolean(showDialog);
54      data.writeString(requestId ? requestId : '');
55      await this._remoteProxy.sendMessageRequest(Constants.COMMAND_SHOW_DIALOG, data, reply, option);
56      HiLog.info(TAG, 'showDialog sendmsg success.');
57    } catch (error) {
58      HiLog.wrapError(TAG, error, 'showDialog sendmsg error');
59      return false;
60    } finally {
61      data.reclaim();
62      reply.reclaim();
63    }
64    return true;
65  }
66
67  public async sendDisconnectMsgWithTimeout(): Promise<boolean> {
68    HiLog.info(TAG, 'ViewAbilityService sendDisconnectMsgWithTimeout');
69    if (!this._remoteProxy) {
70      HiLog.error(TAG, 'sendDisconnectMsgWithTimeout remoteProxy is invalid');
71      return false;
72    }
73    let option = new rpc.MessageOption();
74    let data = new rpc.MessageSequence();
75    let reply = new rpc.MessageSequence();
76    try {
77      data.writeInterfaceToken(Constants.DLP_MGR_VIEW_ABILITY_TOKEN);
78      await this._remoteProxy.sendMessageRequest(Constants.COMMAND_DISCONNECT_WITH_TIMEOUT, data, reply, option);
79      HiLog.info(TAG, 'sendDisconnectMsgWithTimeout sendmsg success.');
80    } catch (error) {
81      HiLog.wrapError(TAG, error, 'sendDisconnectMsgWithTimeout sendmsg error');
82      return false;
83    } finally {
84      data.reclaim();
85      reply.reclaim();
86    }
87    return true;
88  }
89
90  public async sendDisconnectMsg(): Promise<boolean> {
91    HiLog.info(TAG, 'ViewAbilityService sendDisconnectMsg');
92    if (!this._remoteProxy) {
93      HiLog.error(TAG, 'sendDisconnectMsg remoteProxy is invalid');
94      return false;
95    }
96    let option = new rpc.MessageOption();
97    let data = new rpc.MessageSequence();
98    let reply = new rpc.MessageSequence();
99    try {
100      data.writeInterfaceToken(Constants.DLP_MGR_VIEW_ABILITY_TOKEN);
101      await this._remoteProxy.sendMessageRequest(Constants.COMMAND_DISCONNECT, data, reply, option);
102      HiLog.info(TAG, 'sendDisconnectMsg sendmsg success.');
103    } catch (error) {
104      HiLog.wrapError(TAG, error, 'sendDisconnectMsg sendmsg error');
105      return false;
106    } finally {
107      data.reclaim();
108      reply.reclaim();
109    }
110    return true;
111  }
112}