• 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
16import { ArrayUtil } from '../../../../../../common/src/main/ets/util/ArrayUtil';
17import { HiLog } from '../../../../../../common/src/main/ets/util/HiLog';
18import { CallLogService, CallLogRepository } from '../../../../../../feature/call';
19import { ContactRepository } from '../../../../../../feature/contact/src/main/ets/repo/ContactRepository';
20import { StringUtil } from '../../../../../../common/src/main/ets/util/StringUtil';
21
22const TAG = 'CallLogModel';
23
24export default {
25  /**
26   * Obtains and caches all 2000 call records.
27   *
28   * @param {string} DAHelper Database path
29   * @param {string} mergeRule  Call Record Type
30   * @param {Object} callBack Call log data
31   */
32  getAllCalls: async function (actionData, mergeRule, callBack, context?) {
33    if (context) {
34      CallLogRepository.getInstance().init(context);
35    }
36    HiLog.i(TAG, 'getAllCalls in:' + JSON.stringify(actionData));
37    CallLogRepository.getInstance().findAll(actionData, result => {
38      let resultData = {
39        callLogList: [], missedList: []
40      };
41      if (ArrayUtil.isEmpty(result)) {
42        HiLog.i(TAG, 'getAllCalls logMessage callLog resultSet is empty!');
43        callBack(resultData);
44        return;
45      }
46      CallLogService.getInstance().init(context);
47      CallLogService.getInstance().setMergeRule(mergeRule)
48      resultData.callLogList = CallLogService.getInstance().mergeCallLogs(result);
49      resultData.missedList = CallLogService.getInstance().mergeMissedCalls(result);
50      let numberList = this.getNumberList(resultData);
51      this.queryContactsName(numberList, resultData, resultData => {
52        callBack(resultData);
53      }, context)
54    });
55  },
56
57  getNumberList(resultData) {
58    let numberList = new Set();
59    for (let callLog of resultData.callLogList) {
60      numberList.add(callLog.phoneNumber);
61    }
62    for (let missed of resultData.missedList) {
63      numberList.add(missed.phoneNumber);
64    }
65    return Array.from(numberList);
66  },
67
68  queryContactsName(numberList, resultData, callback, context?) {
69    if (numberList.length == 0) {
70      HiLog.w(TAG, "queryContactsName, has no number");
71      callback(resultData);
72      return;
73    }
74    ContactRepository.getInstance().init(context);
75    ContactRepository.getInstance().queryContactDataByNumber(numberList, contacts => {
76      // Convert the result to Map, key: mobile number, value: name
77      let numberMap = this.getNumberMap(contacts);
78      this.buildName(resultData, numberMap);
79      callback(resultData);
80    });
81  },
82
83  getNumberMap(contacts) {
84    let numberMap = new Map();
85    for (let item of contacts) {
86      if (!StringUtil.isEmpty(item.displayName)) {
87        numberMap.set(item.detailInfo, item.displayName);
88      }
89    }
90    return numberMap;
91  },
92
93  buildName(resultData, numberMap) {
94    // Match the result based on the mobile number.
95    for (let callLog of resultData.callLogList) {
96      if (numberMap.has(callLog.phoneNumber)) {
97        callLog.displayName = numberMap.get(callLog.phoneNumber);
98      } else {
99        callLog.displayName = "";
100      }
101    }
102    for (let missed of resultData.missedList) {
103      if (numberMap.has(missed.phoneNumber)) {
104        missed.displayName = numberMap.get(missed.phoneNumber);
105      } else {
106        missed.displayName = "";
107      }
108    }
109  }
110}