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 CredCallbackStub from './CredCallbackStub'; 19import Constants from '../common/constant'; 20import { HiLog } from '../common/HiLog'; 21 22const TAG = 'CredConnectService'; 23 24export default class CredConnectService { 25 private service: CredCallbackStub = new CredCallbackStub(); 26 private context: common.UIExtensionContext | common.ServiceExtensionContext; 27 private optionsSearchUser: common.ConnectOptions; 28 private optionsGetAccount: common.ConnectOptions; 29 private optionsBatchRefresh: common.ConnectOptions; 30 private cloudPhone: string = ''; 31 32 constructor(context: common.UIExtensionContext | common.ServiceExtensionContext) { 33 this.context = context; 34 this.optionsSearchUser = { 35 onConnect: (elementName, remote) => { 36 HiLog.info(TAG, `onConnect success search account ${JSON.stringify(elementName)}`); 37 this.service.searchUserInfo(remote, this.cloudPhone); 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 this.service.getLocalAccountInfo(remote); 50 }, 51 onDisconnect: () => { 52 HiLog.info(TAG, `onDisconnect: GetAccount`); 53 }, 54 onFailed: () => { 55 HiLog.info(TAG, `onFailed: GetAccount`); 56 } 57 } 58 this.optionsBatchRefresh = { 59 onConnect: (elementName, remote) => { 60 HiLog.info(TAG, `onConnect success batch refresht ${JSON.stringify(elementName)}`); 61 this.service.batchRefresh(remote); 62 }, 63 onDisconnect: () => { 64 HiLog.info(TAG, `onDisconnect: BatchRefresh`); 65 }, 66 onFailed: () => { 67 HiLog.info(TAG, `onFailed: BatchRefresh`); 68 } 69 } 70 } 71 72 setCloudPhone(cloudPhone: string) { 73 this.cloudPhone = cloudPhone; 74 } 75 76 connectServiceShareAbility(code: number) { 77 HiLog.info(TAG, 'connectServiceShareAbility start'); 78 let want: Want = { 79 bundleName: Constants.DLP_CREDMGR_BUNDLE_NAME, 80 abilityName: Constants.DLP_CREDMGR_DATA_ABILITY_NAME, 81 }; 82 let connectionId: number | undefined; 83 try { 84 switch (code) { 85 case Constants.COMMAND_SEARCH_USER_INFO: { 86 connectionId = this.context.connectServiceExtensionAbility(want, this.optionsSearchUser); 87 AppStorage.setOrCreate(`connection_${Constants.COMMAND_SEARCH_USER_INFO}`, connectionId); 88 break; 89 } 90 case Constants.COMMAND_GET_ACCOUNT_INFO: { 91 connectionId = this.context.connectServiceExtensionAbility(want, this.optionsGetAccount); 92 AppStorage.setOrCreate(`connection_${Constants.COMMAND_GET_ACCOUNT_INFO}`, connectionId); 93 break; 94 } 95 case Constants.COMMAND_BATCH_REFRESH: { 96 connectionId = this.context.connectServiceExtensionAbility(want, this.optionsBatchRefresh); 97 AppStorage.setOrCreate(`connection_${Constants.COMMAND_BATCH_REFRESH}`, connectionId); 98 break; 99 } 100 default: { 101 HiLog.error(TAG, `code is not exist ${code}`); 102 } 103 } 104 if (connectionId !== undefined) { 105 HiLog.info(TAG, `connectServiceShareAbility result: ${connectionId}`); 106 } 107 } catch (err) { 108 HiLog.error(TAG, `connectServiceShareAbility failed: ${JSON.stringify(err)}`); 109 } 110 } 111}