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