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 { ThreadWorkerGlobalScope } from '@ohos.worker'; 17 18import { WorkerType } from './WorkFactory'; 19import WorkerWrapper from './base/WorkerWrapper'; 20import WorkerTask from './base/WorkerTask'; 21import common from '../data/commonData'; 22import ContactsModel from '../model/ContactsModel'; 23import ConversationModel from '../model/ConversationModel'; 24import ConversationListModel from '../model/ConversationListModel'; 25import HiLog from '../utils/HiLog'; 26 27const TAG = 'DataWorkerWrapper' 28 29export default class DataWorkerWrapper extends WorkerWrapper { 30 private static sInstance: DataWorkerWrapper = null; 31 32 private constructor() { 33 super() 34 } 35 36 static getInstance() { 37 HiLog.i(TAG, 'getInstance in.'); 38 if (DataWorkerWrapper.sInstance == null || DataWorkerWrapper.sInstance.mWorker == null) { 39 HiLog.i(TAG, 'make DataWorkerWrapper.'); 40 DataWorkerWrapper.sInstance = new DataWorkerWrapper(); 41 } 42 return DataWorkerWrapper.sInstance; 43 } 44 45 getWorkerType(): WorkerType { 46 return WorkerType.DataWorker; 47 } 48} 49 50export class DataWorkerTask extends WorkerTask { 51 private static sInstance: DataWorkerTask = null; 52 private mContactsModel: ContactsModel = new ContactsModel(); 53 private mConversationModel: ConversationModel = new ConversationModel(); 54 private mConversationListModel: ConversationListModel = new ConversationListModel(); 55 56 private constructor(workerPort: ThreadWorkerGlobalScope) { 57 super(workerPort); 58 } 59 60 static getInstance(workerPort: ThreadWorkerGlobalScope) { 61 HiLog.i(TAG, 'getInstance in.'); 62 if (DataWorkerTask.sInstance == null || DataWorkerTask.sInstance.workerPort == null) { 63 DataWorkerTask.sInstance = new DataWorkerTask(workerPort); 64 } 65 return DataWorkerTask.sInstance; 66 } 67 68 public runInWorker(request: string, callBack: (v?: any) => void, param?: any) { 69 HiLog.i(TAG, `runInWorker ${request}`); 70 switch (request) { 71 case common.RUN_IN_WORKER_METHOD.queryContactDataByCondition: 72 this.mContactsModel.queryContactDataByCondition(param.actionData, res => { 73 HiLog.i(TAG, `queryContactDataByCondition result: ${JSON.stringify(res).length}`); 74 if (callBack) { 75 callBack(res); 76 } 77 }, param.context); 78 break; 79 case common.RUN_IN_WORKER_METHOD.queryContactDataSizeByCondition: 80 this.mContactsModel.queryContactDataSizeByCondition(param.actionData, res => { 81 HiLog.i(TAG, `queryContactDataSizeByCondition result: ${JSON.stringify(res).length}`); 82 if (callBack) { 83 callBack(res); 84 } 85 }, param.context); 86 break; 87 case common.RUN_IN_WORKER_METHOD.queryContactByCondition: 88 this.mContactsModel.queryContactByCondition(param.actionData, res => { 89 HiLog.i(TAG, `queryContactByCondition result: ${JSON.stringify(res).length}`); 90 if (callBack) { 91 callBack(res); 92 } 93 }, param.context); 94 break; 95 case common.RUN_IN_WORKER_METHOD.queryContactSizeByCondition: 96 this.mContactsModel.queryContactSizeByCondition(param.actionData, res => { 97 HiLog.i(TAG, `queryContactSizeByCondition result: ${JSON.stringify(res).length}`); 98 if (callBack) { 99 callBack(res); 100 } 101 }, param.context); 102 break; 103 case common.RUN_IN_WORKER_METHOD.queryRawContactSizeByCondition: 104 this.mContactsModel.queryRawContactSizeByCondition(param.actionData, res => { 105 HiLog.i(TAG, `queryRawContactSizeByCondition result: ${JSON.stringify(res).length}`); 106 if (callBack) { 107 callBack(res); 108 } 109 }, param.context); 110 break; 111 case common.RUN_IN_WORKER_METHOD.queryContactViewByCondition: 112 this.mContactsModel.queryContactViewByCondition(param.actionData, res => { 113 HiLog.i(TAG, `queryContactViewByCondition result: ${JSON.stringify(res).length}`); 114 if (callBack) { 115 callBack(res); 116 } 117 }, param.context); 118 break; 119 default: 120 this.runInWorkerMore(request, callBack, param); 121 break; 122 } 123 } 124 125 public runInWorkerMore(request: string, callBack: (v?: any) => void, param?: any) { 126 HiLog.i(TAG, `runInWorkerMore ${request}`); 127 switch (request) { 128 case common.RUN_IN_WORKER_METHOD.insertSmsMmsInfo: 129 this.mConversationModel.insertSmsMmsInfo(param.valueBucket, res => { 130 HiLog.i(TAG, `insertSmsMmsInfo result: ${JSON.stringify(res).length}`); 131 if (callBack) { 132 callBack(res); 133 } 134 }, param.context); 135 break; 136 case common.RUN_IN_WORKER_METHOD.deleteSmsMmsInfoByCondition: 137 this.mConversationModel.deleteSmsMmsInfoByCondition(param.actionData, res => { 138 HiLog.i(TAG, `deleteSmsMmsInfoByCondition result: ${JSON.stringify(res).length}`); 139 if (callBack) { 140 callBack(res); 141 } 142 }, param.context); 143 break; 144 case common.RUN_IN_WORKER_METHOD.updateSmsMmsInfoByCondition: 145 this.mConversationModel.updateSmsMmsInfoByCondition(param.actionData, param.valueBucket, res => { 146 HiLog.i(TAG, `updateSmsMmsInfoByCondition result: ${JSON.stringify(res).length}`); 147 if (callBack) { 148 callBack(res); 149 } 150 }, param.context); 151 break; 152 case common.RUN_IN_WORKER_METHOD.querySmsMmsInfoByCondition: 153 this.mConversationModel.querySmsMmsInfoByCondition(param.actionData, res => { 154 HiLog.i(TAG, `querySmsMmsInfoByCondition result: ${JSON.stringify(res).length}`); 155 if (callBack) { 156 callBack(res); 157 } 158 }, param.context); 159 break; 160 case common.RUN_IN_WORKER_METHOD.querySmsMmsInfoSizeByCondition: 161 this.mConversationModel.querySmsMmsInfoSizeByCondition(param.actionData, res => { 162 HiLog.i(TAG, `querySmsMmsInfoSizeByCondition result: ${JSON.stringify(res).length}`); 163 if (callBack) { 164 callBack(res); 165 } 166 }, param.context); 167 break; 168 case common.RUN_IN_WORKER_METHOD.queryMaxGroupId: 169 this.mConversationModel.queryMaxGroupId(res => { 170 HiLog.i(TAG, `queryMaxGroupId result: ${JSON.stringify(res).length}`); 171 if (callBack) { 172 callBack(res); 173 } 174 }, param.context); 175 break; 176 case common.RUN_IN_WORKER_METHOD.statisticalData: 177 this.mConversationModel.statisticalData(res => { 178 HiLog.i(TAG, `statisticalData result: ${JSON.stringify(res).length}`); 179 if (callBack) { 180 callBack(res); 181 } 182 }, param.context); 183 break; 184 default: 185 this.runInWorkerMoreMore(request, callBack, param); 186 break; 187 } 188 } 189 190 public runInWorkerMoreMore(request: string, callBack: (v?: any) => void, param?: any) { 191 HiLog.i(TAG, `runInWorkerMoreMore ${request}`); 192 switch (request) { 193 case common.RUN_IN_WORKER_METHOD.insertSession: 194 this.mConversationListModel.insertSession(param.valueBucket, res => { 195 HiLog.i(TAG, `insertSession result: ${JSON.stringify(res).length}`); 196 if (callBack) { 197 callBack(res); 198 } 199 }, param.context); 200 break; 201 case common.RUN_IN_WORKER_METHOD.deleteSessionByCondition: 202 this.mConversationListModel.deleteSessionByCondition(param.actionData, res => { 203 HiLog.i(TAG, `deleteSessionByCondition result: ${JSON.stringify(res).length}`); 204 if (callBack) { 205 callBack(res); 206 } 207 }, param.context); 208 break; 209 case common.RUN_IN_WORKER_METHOD.updateSessionByCondition: 210 this.mConversationListModel.updateSessionByCondition(param.actionData, param.valueBucket, res => { 211 HiLog.i(TAG, `updateSessionByCondition result: ${JSON.stringify(res).length}`); 212 if (callBack) { 213 callBack(res); 214 } 215 }, param.context); 216 break; 217 case common.RUN_IN_WORKER_METHOD.querySessionByCondition: 218 this.mConversationListModel.querySessionByCondition(param.actionData, res => { 219 HiLog.i(TAG, `querySessionByCondition result: ${JSON.stringify(res).length}`); 220 if (callBack) { 221 callBack(res); 222 } 223 }, param.context); 224 break; 225 case common.RUN_IN_WORKER_METHOD.querySessionSizeByCondition: 226 this.mConversationListModel.querySessionSizeByCondition(param.actionData, res => { 227 HiLog.i(TAG, `querySessionSizeByCondition result: ${JSON.stringify(res).length}`); 228 if (callBack) { 229 callBack(res); 230 } 231 }, param.context); 232 break; 233 default: 234 HiLog.w(TAG, `${request} not allow!!!`); 235 break; 236 } 237 } 238}