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 Base from '@ohos.base';
17import rpc from '@ohos.rpc';
18import deviceInfo from '@ohos.DeviceInfo';
19import {NOTICE_ID, injectNoticeUtil } from './InjectNoticeUtil';
20import {CapsuleUtil} from './CapsuleUtil';
21
22const TAG = 'InjectNotice';
23const getConnectId = (...args): string => {
24    return args.join('-');
25};
26
27export enum CmdCode {
28    OPEN_NOTICE = 0,
29    CLOSE_NOTICE_BY_REQUST = 1,
30};
31
32export class InjectNoticeStub extends rpc.RemoteObject {
33    constructor(des) {
34        console.debug(TAG, `InjectNoticeStub constructor start`);
35        if (typeof des === 'string') {
36            console.debug(TAG, `InjectNoticeStub constructor typeof string`);
37            super(des);
38        } else {
39            console.debug(TAG, `InjectNoticeStub constructor typeof not string`);
40            return;
41        }
42    }
43
44    onRemoteRequest(code, data, reply, option): boolean {
45        console.debug(TAG, `onRemoteRequest start deviceInfo.deviceType:${deviceInfo.deviceType}`);
46        const connectId = getConnectId(rpc.IPCSkeleton.getCallingPid(), rpc.IPCSkeleton.getCallingTokenId());
47        console.info(TAG, `onRemoteRequest start ${connectId}`);
48        if (deviceInfo.deviceType === '2in1') {
49            return this.handlePC(code, data, reply, option);
50        }
51        switch (code) {
52            case CmdCode.OPEN_NOTICE: {
53                console.debug(TAG, `RpcServer:open notice is called`);
54                let pid = data.readInt();
55                console.debug(TAG, `code:${code} pid: ${pid}`);
56                let ret: number = 0;
57                let retStr: string = 'success';
58                try {
59                    injectNoticeUtil.sendNotice();
60                    console.debug(TAG, `SendNotice() code:${code} pid: ${pid}`);
61                } catch (e) {
62                    ret = -1;
63                    console.error(TAG, `send notice failed:${e}`);
64                    retStr = 'failed';
65                }
66                reply.writeInt(ret);
67                reply.writeString(retStr);
68            }
69            break;
70            case CmdCode.CLOSE_NOTICE_BY_REQUST: {
71                console.debug(TAG, `RpcServer:close notice is called`);
72                let pid = data.readInt();
73                console.debug(TAG, `code:${code} pid: ${pid}`);
74                injectNoticeUtil.cancelNotificationById(NOTICE_ID);
75                reply.writeInt(0);
76                reply.writeString('success');
77            }
78            break;
79            default:
80                reply.writeInt(-1);
81                reply.writeString('not support');
82        }
83        console.debug(TAG, `onRemoteRequest end`);
84        return true;
85    }
86
87    handlePC(code, data, reply, option): boolean {
88
89        switch (code) {
90            case CmdCode.OPEN_NOTICE: {
91                console.debug(TAG, `RpcServer:open notice is called`);
92                try {
93                    console.debug(TAG, ` CapsuleUtil.getInstance beign:${deviceInfo.deviceType}`);
94                    let instance: CapsuleUtil = CapsuleUtil.getInstance();
95                    console.debug(TAG, ` processCapsulebeign:${deviceInfo.deviceType}`);
96                    instance.processCapsule(true);
97                } catch (err: Base.BusinessError) {
98                    console.error(TAG, `CapsuleUtil.getInstance() err:${JSON.stringify(err)}`);
99                }
100                reply.writeInt(0);
101                reply.writeString('success');
102                return true;
103            }
104            break;
105            case CmdCode.CLOSE_NOTICE_BY_REQUST: {
106                console.debug(TAG, `RpcServer:close notice is called`);
107                try {
108                    console.debug(TAG, ` CapsuleUtil.getInstance beign close:${deviceInfo.deviceType}`);
109                    let instance: CapsuleUtil = CapsuleUtil.getInstance();
110                    console.debug(TAG, ` processCapsulebeign close:${deviceInfo.deviceType}`);
111                    instance.processCapsule(false);
112                    instance.closePanel();
113
114                } catch (err: Base.BusinessError) {
115                    console.error(TAG, `CapsuleUtil.getInstance()  close err:${JSON.stringify(err)}`);
116                }
117                reply.writeInt(0);
118                reply.writeString('success');
119                return true;
120            }
121            break;
122            default:
123                reply.writeInt(-1);
124                reply.writeString('not support');
125        }
126        console.debug(TAG, `onRemoteRequest end`);
127        return true;
128    }
129};