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 common from '@ohos.app.ability.common'; 17import Want from '@ohos.app.ability.Want'; 18import { BusinessError } from '@ohos.base'; 19import rpc from '@ohos.rpc'; 20import CredCallbackStub from './CredCallback'; 21import Constants from '../constant'; 22import { HiLog } from '../HiLog'; 23 24const TAG = 'ConnectService'; 25 26export default class ConnectService { 27 private context: ESObject; 28 private optionsSearchUser: common.ConnectOptions; 29 private optionsGetAccount: common.ConnectOptions; 30 31 constructor(context: ESObject) { 32 let that = this; 33 this.context = context; 34 this.optionsSearchUser = { 35 onConnect: (elementName, remote) => { 36 HiLog.info(TAG, `onConnect success search account ${JSON.stringify(elementName)}`); 37 that.searchUserInfo(remote); 38 }, 39 onDisconnect: () => { 40 HiLog.info(TAG, `onDisconnect: SearchUser`); 41 }, 42 onFailed: () => { 43 HiLog.info(TAG, `onFailed: SearchUser`); 44 } 45 } 46 this.optionsGetAccount = { 47 onConnect: (elementName, remote) => { 48 HiLog.info(TAG, `onConnect success get account ${JSON.stringify(elementName)}`); 49 that.getAccountInfo(remote); 50 }, 51 onDisconnect: () => { 52 HiLog.info(TAG, `onDisconnect: GetAccount`); 53 }, 54 onFailed: () => { 55 HiLog.info(TAG, `onFailed: GetAccount`); 56 } 57 } 58 } 59 60 connectServiceShareAbility(code: number) { 61 HiLog.info(TAG, `connectServiceShareAbility start`); 62 let want: Want = { 63 bundleName: 'com.huawei.hmos.dlpcredmgr', 64 abilityName: 'DlpCredDataExtAbility', 65 }; 66 let connectionId: number | undefined; 67 try { 68 switch (code) { 69 case Constants.COMMAND_SEARCH_USER_INFO: { 70 connectionId = this.context.connectServiceExtensionAbility(want, this.optionsSearchUser); 71 AppStorage.setOrCreate(`connection_${Constants.COMMAND_SEARCH_USER_INFO}`, connectionId); 72 break; 73 } 74 case Constants.COMMAND_GET_ACCOUNT_INFO: { 75 connectionId = this.context.connectServiceExtensionAbility(want, this.optionsGetAccount); 76 AppStorage.setOrCreate(`connection_${Constants.COMMAND_GET_ACCOUNT_INFO}`, connectionId); 77 break; 78 } 79 default: { 80 HiLog.error(TAG, `code is not exist ${code}`); 81 } 82 } 83 if (connectionId !== undefined) { 84 HiLog.info(TAG, `connectServiceExtAbility result: ${connectionId}`); 85 } 86 } catch (err) { 87 HiLog.error(TAG, `connectServiceExtAbility failed: ${JSON.stringify(err)}`); 88 } 89 } 90 91 searchUserInfo(remote: rpc.IRemoteObject) { 92 let cloudPhone: string | undefined = AppStorage.get('cloudPhone'); 93 HiLog.info(TAG, `searchUserInfo start`); 94 let option = new rpc.MessageOption(); 95 let data = new rpc.MessageSequence(); 96 let reply = new rpc.MessageSequence(); 97 data.writeInterfaceToken('OHOS.HapDlpCredAbilityServiceStub'); 98 let callback: CredCallbackStub = new CredCallbackStub('CredCallbackStub'); 99 data.writeRemoteObject(callback.asObject()); 100 data.writeString(JSON.stringify({'phone': cloudPhone})); 101 if (remote === null) { 102 HiLog.info(TAG, `onConnect remote is null.`); 103 return; 104 } 105 remote.sendMessageRequest(Constants.COMMAND_SEARCH_USER_INFO, data, reply, option).then((result) => { 106 HiLog.info(TAG, `searchUserInfo success.`); 107 }).catch((e: string) => { 108 HiLog.info(TAG, `searchUserInfo error: ${e}`); 109 }); 110 } 111 112 getAccountInfo(remote: rpc.IRemoteObject) { 113 HiLog.info(TAG, `getAccountInfo start`); 114 let option = new rpc.MessageOption(); 115 let data = new rpc.MessageSequence(); 116 let reply = new rpc.MessageSequence(); 117 data.writeInterfaceToken('OHOS.HapDlpCredAbilityServiceStub'); 118 let callback: CredCallbackStub = new CredCallbackStub('CredCallbackStub'); 119 data.writeRemoteObject(callback.asObject()); 120 if (remote === null) { 121 HiLog.info(TAG, `onConnect remote is null.`); 122 return; 123 } 124 remote.sendMessageRequest(Constants.COMMAND_GET_ACCOUNT_INFO, data, reply, option).then((result) => { 125 HiLog.info(TAG, `getAccountInfo success.`); 126 }).catch((e: string) => { 127 HiLog.info(TAG, `getAccountInfo error: ${e}`); 128 }); 129 } 130 131 disconnectServiceShareAbility(connectionKey: string) { 132 let connectionId: number | undefined = AppStorage.get(connectionKey); 133 HiLog.info(TAG, `disconnectServiceShareAbility: ${connectionId}`); 134 try { 135 this.context.disconnectServiceExtensionAbility(connectionId); 136 HiLog.info(TAG, `disconnectServiceExtensionAbility success.`); 137 } catch (error) { 138 HiLog.error(TAG, `disconnectServiceExtensionAbility failed. Error: ${JSON.stringify(error)}`); 139 } 140 }; 141}