18779efd5Sopenharmony_ci/** 28779efd5Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 38779efd5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48779efd5Sopenharmony_ci * you may not use this file except in compliance with the License. 58779efd5Sopenharmony_ci * You may obtain a copy of the License at 68779efd5Sopenharmony_ci * 78779efd5Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88779efd5Sopenharmony_ci * 98779efd5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108779efd5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118779efd5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128779efd5Sopenharmony_ci * See the License for the specific language governing permissions and 138779efd5Sopenharmony_ci * limitations under the License. 148779efd5Sopenharmony_ci */ 158779efd5Sopenharmony_ciimport { HiLog } from '../../../../../common/src/main/ets/util/HiLog'; 168779efd5Sopenharmony_ciimport { StringUtil } from '../../../../../common/src/main/ets/util/StringUtil'; 178779efd5Sopenharmony_ciimport i18n from '@ohos.i18n'; 188779efd5Sopenharmony_ciimport MmsService from './MmsService'; 198779efd5Sopenharmony_ciimport TelecomService from './TelecomService'; 208779efd5Sopenharmony_ciimport call from '@ohos.telephony.call'; 218779efd5Sopenharmony_ci 228779efd5Sopenharmony_ciconst TAG = 'PhoneNumber'; 238779efd5Sopenharmony_ci 248779efd5Sopenharmony_ci/** 258779efd5Sopenharmony_ci * Number object, which provides number-related services. 268779efd5Sopenharmony_ci */ 278779efd5Sopenharmony_ciexport class PhoneNumber { 288779efd5Sopenharmony_ci readonly number: string; 298779efd5Sopenharmony_ci constructor(number: string) { 308779efd5Sopenharmony_ci this.number = number; 318779efd5Sopenharmony_ci } 328779efd5Sopenharmony_ci 338779efd5Sopenharmony_ci static fromString(number: string): PhoneNumber { 348779efd5Sopenharmony_ci return new PhoneNumber(StringUtil.removeSpace(number)); 358779efd5Sopenharmony_ci } 368779efd5Sopenharmony_ci 378779efd5Sopenharmony_ci isDialEmergencyNum(): Promise<boolean> { 388779efd5Sopenharmony_ci let phoneNumber = this.number; 398779efd5Sopenharmony_ci return new Promise<boolean>(function (resolve, reject) { 408779efd5Sopenharmony_ci call.isEmergencyPhoneNumber(phoneNumber, (err, data) => { 418779efd5Sopenharmony_ci if (err) { 428779efd5Sopenharmony_ci HiLog.e(TAG, 'isEmergencyPhoneNumber error: ' + JSON.stringify(err)); 438779efd5Sopenharmony_ci reject(err); 448779efd5Sopenharmony_ci } else { 458779efd5Sopenharmony_ci HiLog.i(TAG, 'isEmergencyPhoneNumber data: ' + JSON.stringify(data)); 468779efd5Sopenharmony_ci resolve(data); 478779efd5Sopenharmony_ci } 488779efd5Sopenharmony_ci }); 498779efd5Sopenharmony_ci }); 508779efd5Sopenharmony_ci } 518779efd5Sopenharmony_ci 528779efd5Sopenharmony_ci dial(options?: any): Promise<boolean> { 538779efd5Sopenharmony_ci HiLog.i(TAG, 'dial phone.'); 548779efd5Sopenharmony_ci let phoneNumber = this.number; 558779efd5Sopenharmony_ci if (!options && AppStorage.Has('defaultSlot')) { 568779efd5Sopenharmony_ci options = { 578779efd5Sopenharmony_ci accountId: AppStorage.Get<number>('defaultSlot'), 588779efd5Sopenharmony_ci } 598779efd5Sopenharmony_ci } 608779efd5Sopenharmony_ci return new Promise<boolean>(function (resolve, reject) { 618779efd5Sopenharmony_ci TelecomService.getInstance().dial(phoneNumber, options, (data) => { 628779efd5Sopenharmony_ci HiLog.i(TAG, ` dial data:${JSON.stringify(data)}`); 638779efd5Sopenharmony_ci if (!!data) { 648779efd5Sopenharmony_ci resolve(data); 658779efd5Sopenharmony_ci } else { 668779efd5Sopenharmony_ci reject({code: -1}); 678779efd5Sopenharmony_ci } 688779efd5Sopenharmony_ci }); 698779efd5Sopenharmony_ci }); 708779efd5Sopenharmony_ci } 718779efd5Sopenharmony_ci 728779efd5Sopenharmony_ci dialerSpecialCode() { 738779efd5Sopenharmony_ci if (this.number.length === 0) { 748779efd5Sopenharmony_ci return; 758779efd5Sopenharmony_ci } 768779efd5Sopenharmony_ci let phoneNumber = this.number; 778779efd5Sopenharmony_ci call.inputDialerSpecialCode(phoneNumber, (err) => { 788779efd5Sopenharmony_ci if (err) { 798779efd5Sopenharmony_ci HiLog.e(TAG, 'inputDialerSpecialCode error: ' + JSON.stringify(err)); 808779efd5Sopenharmony_ci } else { 818779efd5Sopenharmony_ci HiLog.e(TAG, 'inputDialerSpecialCode success'); 828779efd5Sopenharmony_ci } 838779efd5Sopenharmony_ci }); 848779efd5Sopenharmony_ci } 858779efd5Sopenharmony_ci 868779efd5Sopenharmony_ci sendMessage(formatnum?:string, name?: string) { 878779efd5Sopenharmony_ci HiLog.i(TAG, 'send message.'); 888779efd5Sopenharmony_ci MmsService.sendMessage(this.number, formatnum, name); 898779efd5Sopenharmony_ci } 908779efd5Sopenharmony_ci 918779efd5Sopenharmony_ci getNumber() { 928779efd5Sopenharmony_ci let phoneNumber = this.number; 938779efd5Sopenharmony_ci return phoneNumber; 948779efd5Sopenharmony_ci } 958779efd5Sopenharmony_ci 968779efd5Sopenharmony_ci format() { 978779efd5Sopenharmony_ci let phoneNumber = this.number; 988779efd5Sopenharmony_ci let countryId = i18n.getSystemRegion(); 998779efd5Sopenharmony_ci let phoneNumberFormat= new i18n.PhoneNumberFormat(countryId); 1008779efd5Sopenharmony_ci let isNumberValid:boolean = phoneNumberFormat.isValidNumber(phoneNumber); 1018779efd5Sopenharmony_ci let formatNumber = isNumberValid ? phoneNumberFormat.format(phoneNumber) : phoneNumber; 1028779efd5Sopenharmony_ci return formatNumber; 1038779efd5Sopenharmony_ci } 1048779efd5Sopenharmony_ci}