• 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 (param, 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(param.favorite, actionData, result => {
38      let resultData = {
39        callLogList: [], missedList: [], callLogTotal: 0
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      resultData.callLogTotal = result.length;
51      let numberList = this.getNumberList(resultData);
52      this.queryContactsName(numberList, resultData, resultData => {
53        callBack(resultData);
54      }, context)
55    });
56  },
57
58  getNumberList(resultData) {
59    let numberList = new Set();
60    for (let callLog of resultData.callLogList) {
61      numberList.add(callLog.phoneNumber);
62    }
63    for (let missed of resultData.missedList) {
64      numberList.add(missed.phoneNumber);
65    }
66    return Array.from(numberList);
67  },
68
69  queryContactsName(numberList, resultData, callback, context?) {
70    if (numberList.length == 0) {
71      HiLog.w(TAG, "queryContactsName, has no number");
72      callback(resultData);
73      return;
74    }
75    ContactRepository.getInstance().init(context);
76    ContactRepository.getInstance().queryContactDataByNumber(numberList, contacts => {
77      // Convert the result to Map, key: mobile number, value: name
78      let numberMap = this.getNumberMap(contacts);
79      this.buildName(resultData, numberMap);
80      callback(resultData);
81    });
82  },
83
84  getNumberMap(contacts) {
85    let numberMap = new Map();
86    for (let item of contacts) {
87      if (!StringUtil.isEmpty(item.displayName)) {
88        numberMap.set(item.detailInfo, item.displayName);
89      }
90    }
91    return numberMap;
92  },
93
94  buildName(resultData, numberMap) {
95    // Match the result based on the mobile number.
96    for (let callLog of resultData.callLogList) {
97      if (numberMap.has(callLog.phoneNumber)) {
98        callLog.displayName = numberMap.get(callLog.phoneNumber);
99      } else {
100        callLog.displayName = "";
101      }
102    }
103    for (let missed of resultData.missedList) {
104      if (numberMap.has(missed.phoneNumber)) {
105        missed.displayName = numberMap.get(missed.phoneNumber);
106      } else {
107        missed.displayName = "";
108      }
109    }
110  },
111
112  getCallHistorySearch: async function (actionData, mergeRule, callBack, context?) {
113    if (context) {
114      CallLogRepository.getInstance().init(context);
115    }
116    CallLogRepository.getInstance().findSearch(actionData, result => {
117      HiLog.i(TAG, 'getCallHistorySearch resultSet.rowCount :' + JSON.stringify(result.rowCount));
118      let resultData = {
119        callLogList: [], missedList: []
120      };
121      if (ArrayUtil.isEmpty(result)) {
122        HiLog.i(TAG, 'getCallHistorySearch logMessage callLog resultSet is empty!');
123        callBack(resultData);
124        return;
125      }
126      CallLogService.getInstance().init(context);
127      CallLogService.getInstance().setMergeRule(mergeRule)
128      resultData.callLogList = CallLogService.getInstance().mergeCallLogs(result);
129      resultData.missedList = CallLogService.getInstance().mergeMissedCalls(result);
130      let numberList = this.getNumberList(resultData);
131      this.queryContactsName(numberList, resultData, resultData => {
132        callBack(resultData);
133      }, context)
134    });
135  }
136}