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