1048147e0Sopenharmony_ci/** 2048147e0Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3048147e0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4048147e0Sopenharmony_ci * you may not use this file except in compliance with the License. 5048147e0Sopenharmony_ci * You may obtain a copy of the License at 6048147e0Sopenharmony_ci * 7048147e0Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8048147e0Sopenharmony_ci * 9048147e0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10048147e0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11048147e0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12048147e0Sopenharmony_ci * See the License for the specific language governing permissions and 13048147e0Sopenharmony_ci * limitations under the License. 14048147e0Sopenharmony_ci */ 15048147e0Sopenharmony_ciimport commonEvent from '@ohos.commonEventManager'; 16048147e0Sopenharmony_ciimport telSim from '@ohos.telephony.sms'; 17048147e0Sopenharmony_ciimport http from '@ohos.net.http'; 18048147e0Sopenharmony_ci 19048147e0Sopenharmony_ciimport common from '../data/commonData'; 20048147e0Sopenharmony_ciimport telephoneUtils from '../utils/TelephoneUtil'; 21048147e0Sopenharmony_ciimport HiLog from '../utils/HiLog'; 22048147e0Sopenharmony_ciimport commonService from '../service/CommonService'; 23048147e0Sopenharmony_ciimport ConversationService from '../service/ConversationService'; 24048147e0Sopenharmony_ciimport NotificationService from '../service/NotificationService'; 25048147e0Sopenharmony_ciimport LooseObject from '../data/LooseObject' 26048147e0Sopenharmony_ciimport ConversationListService from '../service/ConversationListService'; 27048147e0Sopenharmony_ciimport ContactService from '../service/ContactsService'; 28048147e0Sopenharmony_ci 29048147e0Sopenharmony_ciconst TAG: string = 'MmsStaticSubscriber' 30048147e0Sopenharmony_ci 31048147e0Sopenharmony_civar StaticSubscriberExtensionAbility = globalThis.requireNapi('application.StaticSubscriberExtensionAbility'); 32048147e0Sopenharmony_ci 33048147e0Sopenharmony_ciexport default class MmsStaticSubscriber extends StaticSubscriberExtensionAbility { 34048147e0Sopenharmony_ci 35048147e0Sopenharmony_ci public onReceiveEvent(data): void { 36048147e0Sopenharmony_ci HiLog.i(TAG, 'onReceiveEvent, event:' ); 37048147e0Sopenharmony_ci if (data.event === common.string.SUBSCRIBER_EVENT) { 38048147e0Sopenharmony_ci this.dealSmsReceiveData(data, this.context); 39048147e0Sopenharmony_ci } else { 40048147e0Sopenharmony_ci this.dealMmsReceiveData(data, this.context); 41048147e0Sopenharmony_ci } 42048147e0Sopenharmony_ci } 43048147e0Sopenharmony_ci 44048147e0Sopenharmony_ci public async dealSmsReceiveData(data, context): Promise<void> { 45048147e0Sopenharmony_ci let netType: string = data.parameters.isCdma ? '3gpp2' : '3gpp'; 46048147e0Sopenharmony_ci // Synchronize wait operation 47048147e0Sopenharmony_ci let promisesAll = []; 48048147e0Sopenharmony_ci data.parameters.pdus.forEach(pdu => { 49048147e0Sopenharmony_ci let promise = telSim.createMessage(this.convertStrArray(pdu), netType); 50048147e0Sopenharmony_ci promisesAll.push(promise); 51048147e0Sopenharmony_ci }); 52048147e0Sopenharmony_ci let result: LooseObject = {}; 53048147e0Sopenharmony_ci let createMessagePromise = Promise.all(promisesAll); 54048147e0Sopenharmony_ci createMessagePromise.then(shortMsgList => { 55048147e0Sopenharmony_ci result.code = common.int.SUCCESS; 56048147e0Sopenharmony_ci result.telephone = telephoneUtils.formatTelephone(shortMsgList[0].visibleRawAddress); 57048147e0Sopenharmony_ci result.content = common.string.EMPTY_STR; 58048147e0Sopenharmony_ci shortMsgList.forEach(shortMessage => { 59048147e0Sopenharmony_ci result.content += shortMessage.visibleMessageBody; 60048147e0Sopenharmony_ci }); 61048147e0Sopenharmony_ci }).catch(error => { 62048147e0Sopenharmony_ci HiLog.e(TAG, 'dealSmsReceiveData, error: ' + JSON.stringify(error)); 63048147e0Sopenharmony_ci result.code = common.int.FAILURE; 64048147e0Sopenharmony_ci }); 65048147e0Sopenharmony_ci await createMessagePromise; 66048147e0Sopenharmony_ci let actionData: LooseObject = {}; 67048147e0Sopenharmony_ci actionData.slotId = data.parameters.slotId; 68048147e0Sopenharmony_ci actionData.telephone = result.telephone; 69048147e0Sopenharmony_ci actionData.content = result.content; 70048147e0Sopenharmony_ci actionData.isMms = false; 71048147e0Sopenharmony_ci actionData.mmsSource = []; 72048147e0Sopenharmony_ci this.insertMessageDetailBy(actionData, res => { 73048147e0Sopenharmony_ci this.sendNotification(result.telephone, res.initDatas[0].id, result.content, context); 74048147e0Sopenharmony_ci this.publishData(result.telephone, result.content); 75048147e0Sopenharmony_ci }, context); 76048147e0Sopenharmony_ci } 77048147e0Sopenharmony_ci 78048147e0Sopenharmony_ci public dealMmsReceiveData(data, context): void { 79048147e0Sopenharmony_ci let result = JSON.parse(data.data); 80048147e0Sopenharmony_ci this.saveAttachment(result.mmsSource); 81048147e0Sopenharmony_ci let content: string = commonService.getMmsContent(result.mmsSource); 82048147e0Sopenharmony_ci let actionData: LooseObject = {}; 83048147e0Sopenharmony_ci actionData.telephone = result.telephone; 84048147e0Sopenharmony_ci actionData.content = content; 85048147e0Sopenharmony_ci actionData.isMms = true; 86048147e0Sopenharmony_ci actionData.mmsSource = result.mmsSource; 87048147e0Sopenharmony_ci actionData.slotId = data.parameters.slotId; 88048147e0Sopenharmony_ci this.insertMessageDetailBy(actionData, res => { 89048147e0Sopenharmony_ci let notificationContent = this.getNotificationContent(result.mmsSource, content); 90048147e0Sopenharmony_ci this.sendNotification(result.telephone, res.initDatas[0].id, notificationContent, context); 91048147e0Sopenharmony_ci this.publishData(result.telephone, result.content); 92048147e0Sopenharmony_ci }, context); 93048147e0Sopenharmony_ci } 94048147e0Sopenharmony_ci 95048147e0Sopenharmony_ci public saveAttachment(mmsSource): void { 96048147e0Sopenharmony_ci for (let item of mmsSource) { 97048147e0Sopenharmony_ci let baseUrl = item.msgUriPath; 98048147e0Sopenharmony_ci let httpRequest = http.createHttp(); 99048147e0Sopenharmony_ci httpRequest.request(common.string.MMS_URL, 100048147e0Sopenharmony_ci { 101048147e0Sopenharmony_ci method: http.RequestMethod.GET, 102048147e0Sopenharmony_ci header: { 103048147e0Sopenharmony_ci 'Content-Type': 'application/json', 104048147e0Sopenharmony_ci }, 105048147e0Sopenharmony_ci extraData: baseUrl, 106048147e0Sopenharmony_ci readTimeout: 50000, 107048147e0Sopenharmony_ci connectTimeout: 50000 108048147e0Sopenharmony_ci }, (err, data) => { 109048147e0Sopenharmony_ci HiLog.i(TAG, 'saveAttachment, err: ' + JSON.stringify(err.message)); 110048147e0Sopenharmony_ci } 111048147e0Sopenharmony_ci ); 112048147e0Sopenharmony_ci } 113048147e0Sopenharmony_ci } 114048147e0Sopenharmony_ci 115048147e0Sopenharmony_ci public getNotificationContent(mmsSource, themeContent): string { 116048147e0Sopenharmony_ci let content: string = common.string.EMPTY_STR; 117048147e0Sopenharmony_ci if (mmsSource.length === 1) { 118048147e0Sopenharmony_ci let item = mmsSource[0]; 119048147e0Sopenharmony_ci switch (item.msgType) { 120048147e0Sopenharmony_ci // Subject 121048147e0Sopenharmony_ci case 0: 122048147e0Sopenharmony_ci content = themeContent; 123048147e0Sopenharmony_ci break; 124048147e0Sopenharmony_ci // Pictures 125048147e0Sopenharmony_ci case 1: 126048147e0Sopenharmony_ci content = '(picture)' + themeContent; 127048147e0Sopenharmony_ci break; 128048147e0Sopenharmony_ci // Video 129048147e0Sopenharmony_ci case 2: 130048147e0Sopenharmony_ci content = '(video)' + themeContent; 131048147e0Sopenharmony_ci break; 132048147e0Sopenharmony_ci // Audio 133048147e0Sopenharmony_ci case 3: 134048147e0Sopenharmony_ci content = '(audio)' + themeContent; 135048147e0Sopenharmony_ci break; 136048147e0Sopenharmony_ci } 137048147e0Sopenharmony_ci } else { 138048147e0Sopenharmony_ci content = '(slide)' + mmsSource[0].content; 139048147e0Sopenharmony_ci } 140048147e0Sopenharmony_ci return content; 141048147e0Sopenharmony_ci } 142048147e0Sopenharmony_ci 143048147e0Sopenharmony_ci public insertMessageDetailBy(param, callback, context): void { 144048147e0Sopenharmony_ci let sendResults: Array<LooseObject> = []; 145048147e0Sopenharmony_ci let sendResult: LooseObject = {}; 146048147e0Sopenharmony_ci sendResult.slotId = param.slotId; 147048147e0Sopenharmony_ci sendResult.telephone = param.telephone; 148048147e0Sopenharmony_ci sendResult.content = param.content; 149048147e0Sopenharmony_ci sendResult.sendStatus = common.int.SEND_MESSAGE_SUCCESS; 150048147e0Sopenharmony_ci sendResults.push(sendResult); 151048147e0Sopenharmony_ci 152048147e0Sopenharmony_ci let hasAttachment: boolean = commonService.judgeIsAttachment(param.mmsSource); 153048147e0Sopenharmony_ci let actionData: LooseObject = {}; 154048147e0Sopenharmony_ci actionData.slotId = param.slotId; 155048147e0Sopenharmony_ci actionData.sendResults = sendResults; 156048147e0Sopenharmony_ci actionData.isReceive = true; 157048147e0Sopenharmony_ci actionData.ownNumber = common.string.EMPTY_STR; 158048147e0Sopenharmony_ci actionData.isSender = true; 159048147e0Sopenharmony_ci actionData.isMms = param.isMms; 160048147e0Sopenharmony_ci actionData.mmsSource = param.mmsSource; 161048147e0Sopenharmony_ci actionData.hasAttachment = hasAttachment; 162048147e0Sopenharmony_ci ConversationService.getInstance().insertSessionAndDetail(actionData, callback, context); 163048147e0Sopenharmony_ci } 164048147e0Sopenharmony_ci 165048147e0Sopenharmony_ci public convertStrArray(sourceStr): Array<number> { 166048147e0Sopenharmony_ci let wby: string = sourceStr; 167048147e0Sopenharmony_ci let length: number = wby.length; 168048147e0Sopenharmony_ci let isDouble: boolean = (length % 2) == 0; 169048147e0Sopenharmony_ci let halfSize: number = parseInt('' + length / 2); 170048147e0Sopenharmony_ci HiLog.i(TAG, 'convertStrArray, length=' + length + ', isDouble=' + isDouble); 171048147e0Sopenharmony_ci if (isDouble) { 172048147e0Sopenharmony_ci let number0xArray = new Array(halfSize); 173048147e0Sopenharmony_ci for (let i = 0;i < halfSize; i++) { 174048147e0Sopenharmony_ci number0xArray[i] = '0x' + wby.substr(i * 2, 2); 175048147e0Sopenharmony_ci } 176048147e0Sopenharmony_ci let numberArray = new Array(halfSize); 177048147e0Sopenharmony_ci for (let i = 0;i < halfSize; i++) { 178048147e0Sopenharmony_ci numberArray[i] = parseInt(number0xArray[i], 16); 179048147e0Sopenharmony_ci } 180048147e0Sopenharmony_ci return numberArray; 181048147e0Sopenharmony_ci } else { 182048147e0Sopenharmony_ci let number0xArray = new Array(halfSize + 1); 183048147e0Sopenharmony_ci for (let i = 0;i < halfSize; i++) { 184048147e0Sopenharmony_ci number0xArray[i] = '0x' + wby.substr(i * 2, 2); 185048147e0Sopenharmony_ci } 186048147e0Sopenharmony_ci number0xArray[halfSize] = '0x' + wby.substr((halfSize * 2) + 1, 1); 187048147e0Sopenharmony_ci let numberArray = new Array(halfSize + 1); 188048147e0Sopenharmony_ci for (let i = 0;i < halfSize; i++) { 189048147e0Sopenharmony_ci numberArray[i] = parseInt(number0xArray[i], 16); 190048147e0Sopenharmony_ci } 191048147e0Sopenharmony_ci let last0x = '0x' + wby.substr(wby.length - 1, 1); 192048147e0Sopenharmony_ci numberArray[halfSize] = parseInt(last0x); 193048147e0Sopenharmony_ci return numberArray; 194048147e0Sopenharmony_ci } 195048147e0Sopenharmony_ci } 196048147e0Sopenharmony_ci 197048147e0Sopenharmony_ci public publishData(telephone, content): void { 198048147e0Sopenharmony_ci HiLog.i(TAG, 'publishData, start'); 199048147e0Sopenharmony_ci let actionData = { 200048147e0Sopenharmony_ci telephone: telephone, 201048147e0Sopenharmony_ci content: content 202048147e0Sopenharmony_ci }; 203048147e0Sopenharmony_ci commonEvent.publish(common.string.RECEIVE_TRANSMIT_EVENT, { 204048147e0Sopenharmony_ci bundleName: common.string.BUNDLE_NAME, 205048147e0Sopenharmony_ci subscriberPermissions: ['ohos.permission.RECEIVE_SMS'], 206048147e0Sopenharmony_ci isOrdered: false, 207048147e0Sopenharmony_ci data: JSON.stringify(actionData) 208048147e0Sopenharmony_ci }, (res) => { 209048147e0Sopenharmony_ci }); 210048147e0Sopenharmony_ci } 211048147e0Sopenharmony_ci 212048147e0Sopenharmony_ci public sendNotification(telephone, msgId, content, context): void { 213048147e0Sopenharmony_ci let condition: LooseObject = {}; 214048147e0Sopenharmony_ci condition.telephones = [telephone]; 215048147e0Sopenharmony_ci ContactService.getInstance().queryContactDataByCondition(condition, res => { 216048147e0Sopenharmony_ci HiLog.i(TAG, 'sendNotification, callback'); 217048147e0Sopenharmony_ci if (res.code == common.int.FAILURE) { 218048147e0Sopenharmony_ci return; 219048147e0Sopenharmony_ci } 220048147e0Sopenharmony_ci let contacts: Array<LooseObject> = res.abilityResult; 221048147e0Sopenharmony_ci let actionData: LooseObject = this.dealContactParams(contacts, telephone); 222048147e0Sopenharmony_ci if (content.length > 15) { 223048147e0Sopenharmony_ci content = content.substring(0, 15) + '...'; 224048147e0Sopenharmony_ci } 225048147e0Sopenharmony_ci let title: string = telephone; 226048147e0Sopenharmony_ci if(contacts.length > 0) { 227048147e0Sopenharmony_ci title = contacts[0].displayName 228048147e0Sopenharmony_ci } 229048147e0Sopenharmony_ci let message: LooseObject = { 230048147e0Sopenharmony_ci 'title': title, 231048147e0Sopenharmony_ci 'text': content, 232048147e0Sopenharmony_ci }; 233048147e0Sopenharmony_ci actionData.message = message; 234048147e0Sopenharmony_ci actionData.msgId = msgId; 235048147e0Sopenharmony_ci actionData.unreadTotal = 0; 236048147e0Sopenharmony_ci NotificationService.getInstance().sendNotify(actionData); 237048147e0Sopenharmony_ci ConversationListService.getInstance().statisticalData(res => { 238048147e0Sopenharmony_ci if (res.code == common.int.SUCCESS) { 239048147e0Sopenharmony_ci NotificationService.getInstance().setBadgeNumber(Number(res.response.totalListCount)); 240048147e0Sopenharmony_ci } 241048147e0Sopenharmony_ci }, context); 242048147e0Sopenharmony_ci }, context); 243048147e0Sopenharmony_ci } 244048147e0Sopenharmony_ci 245048147e0Sopenharmony_ci public dealContactParams(contacts, telephone): LooseObject { 246048147e0Sopenharmony_ci let actionData: LooseObject = {}; 247048147e0Sopenharmony_ci let params: Array<LooseObject> = []; 248048147e0Sopenharmony_ci if (contacts.length == 0) { 249048147e0Sopenharmony_ci params.push({ 250048147e0Sopenharmony_ci telephone: telephone, 251048147e0Sopenharmony_ci }); 252048147e0Sopenharmony_ci } else { 253048147e0Sopenharmony_ci let contact: LooseObject = contacts[0]; 254048147e0Sopenharmony_ci params.push({ 255048147e0Sopenharmony_ci contactsName: contact.displayName, 256048147e0Sopenharmony_ci telephone: telephone, 257048147e0Sopenharmony_ci telephoneFormat: contact.detailInfo, 258048147e0Sopenharmony_ci }); 259048147e0Sopenharmony_ci } 260048147e0Sopenharmony_ci actionData.contactObjects = JSON.stringify(params); 261048147e0Sopenharmony_ci return actionData; 262048147e0Sopenharmony_ci } 263048147e0Sopenharmony_ci} 264