• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16/**
17 * @file: Contact management
18 */
19import dataShare from '@ohos.data.dataShare';
20import dataSharePredicates from '@ohos.data.dataSharePredicates';
21import LogUtils from '../common/utils/LogUtils';
22import CallManager from '../model/CallManager';
23
24const TAG = 'ContactManager';
25const DB_BASE_URI = 'datashare:///com.ohos.contactsdataability';
26const DB_URI = DB_BASE_URI + '/contacts/contact_data';
27
28/**
29 * class ContactManager
30 */
31export default class ContactManager {
32  /**
33   * get contact info
34   *
35   * @param { Object } callData -Object
36   */
37  async getContactInfo(callData): Promise<void> {
38    if (callData.contactName) {
39      return;
40    }
41    try {
42      const columns = ['id', 'display_name', 'detail_info'];
43      const predicates = new dataSharePredicates.DataSharePredicates();
44      predicates.equalTo('detail_info', callData.accountNumber);
45      predicates.equalTo('is_deleted', 0);
46      let context = globalThis.calluiAbilityContext;
47      const dataAbilityHelper = await dataShare.createDataShareHelper(context, DB_BASE_URI);
48      const resSet = await dataAbilityHelper.query(DB_URI, predicates, columns);
49      LogUtils.i(TAG, 'getContactInfo resSet : ' + JSON.stringify(resSet.rowCount));
50      if (resSet.rowCount > 0) {
51        resSet.goToFirstRow();
52        callData.contactName = resSet.getString(resSet.getColumnIndex('display_name'));
53      } else {
54        callData.contactName = '';
55      }
56      CallManager.getInstance().update(callData);
57    } catch (err) {
58      LogUtils.i(TAG, 'getContactInfo catch err : ' + JSON.stringify(err));
59    } finally {
60      if (dataAbilityHelper != undefined) {
61        dataAbilityHelper.close();
62      }
63      if (resSet != undefined) {
64        resSet.close();
65      }
66    }
67  }
68}