1/** 2 * Copyright (c) 2023 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 */ 15import { CallLogRepository } from '../../../../../feature/call'; 16import { ContactRepository } from '../../../../../feature/contact'; 17import { HiLog, ArrayUtil } from '../../../../../common'; 18import CallLogSetting from '../../../../../feature/call/src/main/ets/CallLogSetting' 19import WorkerWrapper from "../workers/base/WorkerWrapper"; 20import { DataWorkerConstant } from "../workers/DataWorkerWrapper"; 21import emitter from '@ohos.events.emitter' 22import Constants from '../../../../../common/src/main/ets/Constants'; 23 24const TAG = "CallsService" 25 26export default class CallsService { 27 page: number = 0; 28 limit: number = 50; 29 callLogList: Array<any> = []; 30 missedList: Array<any> = []; 31 context: Context; 32 worker: WorkerWrapper; 33 innerEvent = { 34 eventId: Constants.Event.CALLS_CHANGE, 35 priority: emitter.EventPriority.HIGH 36 }; 37 onContactsChange = () => { 38 HiLog.i(TAG, 'onContactsChange refresh'); 39 this.requestItem(); 40 } 41 onCallLogsChange = () => { 42 HiLog.i(TAG, 'refresh'); 43 this.requestItem(); 44 } 45 46 constructor(context: Context, worker: WorkerWrapper) { 47 this.context = context; 48 this.worker = worker; 49 this.requestItem(); 50 CallLogRepository.getInstance().registerDataChangeObserver(this.onCallLogsChange); 51 ContactRepository.getInstance().registerDataChangeObserver(this.onContactsChange); 52 emitter.on({ 53 eventId: Constants.Event.CALLS_MERGE, 54 priority: emitter.EventPriority.HIGH 55 }, () => { 56 this.requestItem(); 57 }) 58 } 59 60 onDestroy() { 61 ContactRepository.getInstance().unRegisterDataChangeObserver(this.onContactsChange); 62 CallLogRepository.getInstance().unRegisterDataChangeObserver(this.onCallLogsChange); 63 emitter.off(Constants.Event.CALLS_MERGE); 64 } 65 66 requestItem() { 67 HiLog.i(TAG, 'CallLogs requestItem!'); 68 if (this.page == 0) { 69 this.callLogList = []; 70 this.missedList = []; 71 this.page++; 72 this.refresh(); 73 } else { 74 HiLog.i(TAG, 'isLoading'); 75 } 76 } 77 78 refresh() { 79 if (this.page == 1) { 80 this.limit = 50; 81 } else { 82 this.limit = 500; 83 } 84 let actionData: any = {}; 85 actionData.page = this.page; 86 actionData.limit = this.limit; 87 this.worker.sendRequest(DataWorkerConstant[DataWorkerConstant.getAllCalls], { 88 context: this.context, 89 mergeRule: CallLogSetting.getInstance().getMergeRule(), 90 actionData: actionData 91 }, (data) => { 92 HiLog.i(TAG, 'getAllCalls and refresh, length is ' + JSON.stringify(data.callLogList.length)); 93 const dateLength = data.callLogList.length; 94 if (!ArrayUtil.isEmpty(data.callLogList)) { 95 this.callLogList = this.callLogList.concat(data.callLogList); 96 } 97 if (!ArrayUtil.isEmpty(data.missedList)) { 98 this.missedList = this.missedList.concat(data.missedList); 99 } 100 HiLog.i(TAG, 'getAllCalls and refresh end, length is ' + JSON.stringify(this.callLogList.length)); 101 102 AppStorage.SetOrCreate("callLogList", this.callLogList) 103 AppStorage.SetOrCreate("missedList", this.missedList) 104 emitter.emit(this.innerEvent); 105 if (dateLength < this.limit) { 106 this.page = 0; 107 HiLog.i(TAG, 'CallLogs load completed: ' + JSON.stringify(this.callLogList.length)); 108 } else { 109 this.page++; 110 setTimeout(() => { 111 this.refresh(); 112 }, this.page == 2 ? 700 : 1); 113 } 114 }) 115 } 116}