1/* 2 * Copyright (c) 2022 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 rpc from "@ohos.rpc"; 17 18let CODE_CONNECT_REMOTE = 1; 19let CODE_CONNECT_LOCAL = 2; 20let CODE_CONTROL_FA = 3; 21 22class FirstServiceAbilityStub extends rpc.RemoteObject { 23 mainAbilityStub; 24 25 constructor(des) { 26 if (typeof des === 'string') { 27 super(des); 28 } else { 29 return null; 30 } 31 } 32 33 async sendMessageToFa(x: number, y: number, touchType: number): Promise<void> { 34 let option = new rpc.MessageOption(); 35 let data = new rpc.MessageParcel(); 36 let reply = new rpc.MessageParcel(); 37 data.writeInt(x); 38 data.writeInt(y); 39 data.writeInt(touchType); 40 await this.mainAbilityStub.sendRequest(CODE_CONTROL_FA, data, reply, option); 41 } 42 43 onRemoteRequest(code: number, data: any, reply: any, options: any): boolean { 44 if (code === CODE_CONNECT_REMOTE) { 45 let x = data.readInt(); 46 let y = data.readInt(); 47 let touchType = data.readInt(); 48 if (this.mainAbilityStub == null) { 49 console.info('ServiceAbility mainAbilityStub_ is null'); 50 return; 51 } 52 this.sendMessageToFa(x, y, touchType); 53 } else if (code === CODE_CONNECT_LOCAL) { 54 this.mainAbilityStub = data.readRemoteObject(); 55 } 56 else { 57 console.log("ServiceAbility unknown request code"); 58 return false; 59 } 60 return true; 61 } 62} 63 64export default { 65 onStart() { 66 console.info('ServiceAbility onStart'); 67 }, 68 onStop() { 69 console.info('ServiceAbility onStop'); 70 }, 71 onConnect(want) { 72 console.log("ServiceAbility onConnect"); 73 try { 74 let value = JSON.stringify(want); 75 } catch (error) { 76 console.log("ServiceAbility error:" + error); 77 } 78 return new FirstServiceAbilityStub("first ts service stub"); 79 }, 80 onDisconnect(want) { 81 console.log("ServiceAbility onDisconnect"); 82 let value = JSON.stringify(want); 83 }, 84 onCommand(want, startId) { 85 console.info('ServiceAbility onCommand'); 86 let value = JSON.stringify(want); 87 } 88};