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 */ 15import { HiLog } from '../../../../../common/src/main/ets/util/HiLog'; 16import { StringUtil } from '../../../../../common/src/main/ets/util/StringUtil'; 17import i18n from '@ohos.i18n'; 18import MmsService from './MmsService'; 19import TelecomService from './TelecomService'; 20import call from '@ohos.telephony.call'; 21 22const TAG = 'PhoneNumber'; 23 24/** 25 * Number object, which provides number-related services. 26 */ 27export class PhoneNumber { 28 readonly number: string; 29 constructor(number: string) { 30 this.number = number; 31 } 32 33 static fromString(number: string): PhoneNumber { 34 return new PhoneNumber(StringUtil.removeSpace(number)); 35 } 36 37 isDialEmergencyNum(): Promise<boolean> { 38 let phoneNumber = this.number; 39 return new Promise<boolean>(function (resolve, reject) { 40 call.isEmergencyPhoneNumber(phoneNumber, (err, data) => { 41 if (err) { 42 HiLog.e(TAG, 'isEmergencyPhoneNumber error: ' + JSON.stringify(err)); 43 reject(err); 44 } else { 45 HiLog.i(TAG, 'isEmergencyPhoneNumber data: ' + JSON.stringify(data)); 46 resolve(data); 47 } 48 }); 49 }); 50 } 51 52 dial(options?: any): Promise<boolean> { 53 HiLog.i(TAG, 'dial phone.'); 54 let phoneNumber = this.number; 55 if (!options && AppStorage.Has('defaultSlot')) { 56 options = { 57 accountId: AppStorage.Get<number>('defaultSlot'), 58 } 59 } 60 return new Promise<boolean>(function (resolve, reject) { 61 TelecomService.getInstance().dial(phoneNumber, options, (data) => { 62 HiLog.i(TAG, ` dial data:${JSON.stringify(data)}`); 63 if (!!data) { 64 resolve(data); 65 } else { 66 reject({code: -1}); 67 } 68 }); 69 }); 70 } 71 72 dialerSpecialCode() { 73 if (this.number.length === 0) { 74 return; 75 } 76 let phoneNumber = this.number; 77 call.inputDialerSpecialCode(phoneNumber, (err) => { 78 if (err) { 79 HiLog.e(TAG, 'inputDialerSpecialCode error: ' + JSON.stringify(err)); 80 } else { 81 HiLog.e(TAG, 'inputDialerSpecialCode success'); 82 } 83 }); 84 } 85 86 sendMessage(formatnum?:string, name?: string) { 87 HiLog.i(TAG, 'send message.'); 88 MmsService.sendMessage(this.number, formatnum, name); 89 } 90 91 getNumber() { 92 let phoneNumber = this.number; 93 return phoneNumber; 94 } 95 96 format() { 97 let phoneNumber = this.number; 98 let countryId = i18n.getSystemRegion(); 99 let phoneNumberFormat= new i18n.PhoneNumberFormat(countryId); 100 let isNumberValid:boolean = phoneNumberFormat.isValidNumber(phoneNumber); 101 let formatNumber = isNumberValid ? phoneNumberFormat.format(phoneNumber) : phoneNumber; 102 return formatNumber; 103 } 104}