• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
27  private optionsSearchUser: common.ConnectOptions;
28  private optionsGetAccount: common.ConnectOptions;
29  private cloudPhone: string = '';
30
31  constructor(context: common.UIExtensionContext) {
32    this.context = context;
33    this.optionsSearchUser = {
34      onConnect: (elementName, remote) => {
35        HiLog.info(TAG, `onConnect success search account ${JSON.stringify(elementName)}`);
36        this.service.searchUserInfo(remote, this.cloudPhone);
37      },
38      onDisconnect: () => {
39        HiLog.info(TAG, `onDisconnect: SearchUser`);
40      },
41      onFailed: () => {
42        HiLog.info(TAG, `onFailed: SearchUser`);
43      }
44    }
45    this.optionsGetAccount = {
46      onConnect: (elementName, remote) => {
47        HiLog.info(TAG, `onConnect success get account ${JSON.stringify(elementName)}`);
48        this.service.getLocalAccountInfo(remote);
49      },
50      onDisconnect: () => {
51        HiLog.info(TAG, `onDisconnect: GetAccount`);
52      },
53      onFailed: () => {
54        HiLog.info(TAG, `onFailed: GetAccount`);
55      }
56    }
57  }
58
59  setCloudPhone(cloudPhone: string) {
60    this.cloudPhone = cloudPhone;
61  }
62
63  connectServiceShareAbility(code: number) {
64    HiLog.info(TAG, `connectServiceShareAbility start`);
65    let want: Want = {
66      bundleName: Constants.DLP_CREDMGR_BUNDLE_NAME,
67      abilityName: Constants.DLP_CREDMGR_DATA_ABILITY_NAME,
68    };
69    let connectionId: number | undefined;
70    try {
71      switch (code) {
72        case Constants.COMMAND_SEARCH_USER_INFO: {
73          connectionId = this.context.connectServiceExtensionAbility(want, this.optionsSearchUser);
74          AppStorage.setOrCreate(`connection_${Constants.COMMAND_SEARCH_USER_INFO}`, connectionId);
75          break;
76        }
77        case Constants.COMMAND_GET_ACCOUNT_INFO: {
78          connectionId = this.context.connectServiceExtensionAbility(want, this.optionsGetAccount);
79          AppStorage.setOrCreate(`connection_${Constants.COMMAND_GET_ACCOUNT_INFO}`, connectionId);
80          break;
81        }
82        default: {
83          HiLog.error(TAG, `code is not exist ${code}`);
84        }
85      }
86      if (connectionId !== undefined) {
87        HiLog.info(TAG, `connectServiceShareAbility result: ${connectionId}`);
88      }
89    } catch (err) {
90      HiLog.error(TAG, `connectServiceShareAbility failed: ${JSON.stringify(err)}`);
91    }
92  }
93}