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 (error) { 98 let err = error as Base.BusinessError; 99 console.error(TAG, `CapsuleUtil.getInstance() err:${JSON.stringify(err)}`); 100 } 101 reply.writeInt(0); 102 reply.writeString('success'); 103 return true; 104 } 105 break; 106 case CmdCode.CLOSE_NOTICE_BY_REQUST: { 107 console.debug(TAG, `RpcServer:close notice is called`); 108 try { 109 console.debug(TAG, `capsuleUtil.getInstance beign close:${deviceInfo.deviceType}`); 110 let instance: CapsuleUtil = CapsuleUtil.getInstance(); 111 instance.processCapsule(false); 112 instance.closePanel(); 113 console.debug(TAG, `capsuleUtil cancelAuthorization begin:${deviceInfo.deviceType}`); 114 instance.cancelAuthorization(); 115 console.debug(TAG, `capsuleUtil cancelAuthorization end:${deviceInfo.deviceType}`); 116 } catch (error) { 117 let err = error as Base.BusinessError; 118 console.error(TAG, `CapsuleUtil.getInstance() close err:${JSON.stringify(err)}`); 119 } 120 reply.writeInt(0); 121 reply.writeString('success'); 122 return true; 123 } 124 break; 125 default: 126 reply.writeInt(-1); 127 reply.writeString('not support'); 128 } 129 console.debug(TAG, `onRemoteRequest end`); 130 return true; 131 } 132};