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