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 common from '../data/commonData';
16048147e0Sopenharmony_ciimport HiLog from './HiLog';
17048147e0Sopenharmony_ciimport call from '@ohos.telephony.call';
18048147e0Sopenharmony_ci
19048147e0Sopenharmony_ciconst TAG = 'TelephoneUtil';
20048147e0Sopenharmony_ci/**
21048147e0Sopenharmony_ci *  log package tool class
22048147e0Sopenharmony_ci */
23048147e0Sopenharmony_ciconst infoMegTelephone = [
24048147e0Sopenharmony_ci    '1065796709202', '1065502043202', '1065902090202', '1069055999202',
25048147e0Sopenharmony_ci    '106579670915', '106550204315', '106590209015', '106905599915',
26048147e0Sopenharmony_ci    '106', '400', '111', '100', '118', '116', '12306', '12329', '12345',
27048147e0Sopenharmony_ci    '12122', '12321', '12580', '12520', '12583', '02512329', '053287003810',
28048147e0Sopenharmony_ci    '1258319559899', '1019', '12583110086', '000000', '95', '96',
29048147e0Sopenharmony_ci];
30048147e0Sopenharmony_ci
31048147e0Sopenharmony_ciexport default {
32048147e0Sopenharmony_ci
33048147e0Sopenharmony_ci    /**
34048147e0Sopenharmony_ci     * Check whether the mobile number is notification information.
35048147e0Sopenharmony_ci     * @param telephone
36048147e0Sopenharmony_ci     * @return Yes or no
37048147e0Sopenharmony_ci     */
38048147e0Sopenharmony_ci    judgeIsInfoMsg(telephone: string): boolean {
39048147e0Sopenharmony_ci        let result: boolean = false;
40048147e0Sopenharmony_ci        if (telephone == null || telephone == common.string.EMPTY_STR) {
41048147e0Sopenharmony_ci            return result;
42048147e0Sopenharmony_ci        }
43048147e0Sopenharmony_ci        for (let item of infoMegTelephone) {
44048147e0Sopenharmony_ci            if (telephone.indexOf(item) == 0) {
45048147e0Sopenharmony_ci                result = true;
46048147e0Sopenharmony_ci                break;
47048147e0Sopenharmony_ci            }
48048147e0Sopenharmony_ci        }
49048147e0Sopenharmony_ci        return result;
50048147e0Sopenharmony_ci    },
51048147e0Sopenharmony_ci
52048147e0Sopenharmony_ci    formatTelephone(telephone) {
53048147e0Sopenharmony_ci        let formatTelephone: string = telephone;
54048147e0Sopenharmony_ci        if (telephone == null || telephone == common.string.EMPTY_STR) {
55048147e0Sopenharmony_ci            formatTelephone = common.string.EMPTY_STR;
56048147e0Sopenharmony_ci        }
57048147e0Sopenharmony_ci        if (formatTelephone.startsWith('+86')) {
58048147e0Sopenharmony_ci            formatTelephone = formatTelephone.substring(3);
59048147e0Sopenharmony_ci        } else if(formatTelephone.startsWith('86')) {
60048147e0Sopenharmony_ci            formatTelephone = formatTelephone.substring(2);
61048147e0Sopenharmony_ci        }
62048147e0Sopenharmony_ci        return formatTelephone;
63048147e0Sopenharmony_ci    },
64048147e0Sopenharmony_ci
65048147e0Sopenharmony_ci    dealSelectContactsSort(selectContacts) {
66048147e0Sopenharmony_ci        if (selectContacts.length == 0) {
67048147e0Sopenharmony_ci            return;
68048147e0Sopenharmony_ci        }
69048147e0Sopenharmony_ci        let result = [];
70048147e0Sopenharmony_ci        let telephone = common.string.EMPTY_STR;
71048147e0Sopenharmony_ci        let contactsMap = new Map();
72048147e0Sopenharmony_ci        for (let item of selectContacts) {
73048147e0Sopenharmony_ci            telephone = telephone + item.telephone + common.string.COMMA;
74048147e0Sopenharmony_ci            if (!contactsMap.has(item.telephone)) {
75048147e0Sopenharmony_ci                contactsMap.set(item.telephone, item);
76048147e0Sopenharmony_ci            }
77048147e0Sopenharmony_ci        }
78048147e0Sopenharmony_ci        telephone = this.dealTelephoneSort(telephone.substring(0, telephone.length - 1));
79048147e0Sopenharmony_ci        let telephones = telephone.split(common.string.COMMA);
80048147e0Sopenharmony_ci        for (let element of telephones) {
81048147e0Sopenharmony_ci            if (contactsMap.has(element)) {
82048147e0Sopenharmony_ci                result.push(contactsMap.get(element));
83048147e0Sopenharmony_ci            }
84048147e0Sopenharmony_ci        }
85048147e0Sopenharmony_ci        return result;
86048147e0Sopenharmony_ci    },
87048147e0Sopenharmony_ci
88048147e0Sopenharmony_ci    /**
89048147e0Sopenharmony_ci     * Mobile number, sorted in ascending order.
90048147e0Sopenharmony_ci     * @param telephone
91048147e0Sopenharmony_ci     * @return
92048147e0Sopenharmony_ci     */
93048147e0Sopenharmony_ci    dealTelephoneSort(telephone) {
94048147e0Sopenharmony_ci        if (telephone == null || telephone == common.string.EMPTY_STR) {
95048147e0Sopenharmony_ci            return common.string.EMPTY_STR;
96048147e0Sopenharmony_ci        }
97048147e0Sopenharmony_ci        let result = common.string.EMPTY_STR;
98048147e0Sopenharmony_ci        let telephones = telephone.split(common.string.COMMA);
99048147e0Sopenharmony_ci        // If there is only one mobile number, no sorting is required.
100048147e0Sopenharmony_ci        if (telephones.length == 1) {
101048147e0Sopenharmony_ci            return telephone;
102048147e0Sopenharmony_ci        }
103048147e0Sopenharmony_ci        let telephoneMap = new Map();
104048147e0Sopenharmony_ci        let indexs = [];
105048147e0Sopenharmony_ci        let count = 0;
106048147e0Sopenharmony_ci        // grouping
107048147e0Sopenharmony_ci        for (let item of telephones) {
108048147e0Sopenharmony_ci            if (telephoneMap.has(item.length)) {
109048147e0Sopenharmony_ci                let strings = telephoneMap.get(item.length);
110048147e0Sopenharmony_ci                strings.push(item);
111048147e0Sopenharmony_ci            } else {
112048147e0Sopenharmony_ci                let strings = [];
113048147e0Sopenharmony_ci                strings.push(item);
114048147e0Sopenharmony_ci                telephoneMap.set(item.length, strings);
115048147e0Sopenharmony_ci                indexs[count++] = item.length;
116048147e0Sopenharmony_ci            }
117048147e0Sopenharmony_ci        }
118048147e0Sopenharmony_ci        // Sort from Large to Small
119048147e0Sopenharmony_ci        this.bubbleSort(indexs, count);
120048147e0Sopenharmony_ci        for (let index of indexs) {
121048147e0Sopenharmony_ci            let arrs = telephoneMap.get(index);
122048147e0Sopenharmony_ci            this.bubbleSort(arrs, arrs.length);
123048147e0Sopenharmony_ci            for (let arr of arrs) {
124048147e0Sopenharmony_ci                result = result + arr + common.string.COMMA;
125048147e0Sopenharmony_ci            }
126048147e0Sopenharmony_ci        }
127048147e0Sopenharmony_ci        // Obtain the corresponding results and pair them.
128048147e0Sopenharmony_ci        return result.substring(0, result.length - 1);
129048147e0Sopenharmony_ci    },
130048147e0Sopenharmony_ci
131048147e0Sopenharmony_ci    /**
132048147e0Sopenharmony_ci     * Bubble sort, sorted in ascending order.
133048147e0Sopenharmony_ci     * @param arr
134048147e0Sopenharmony_ci     * @param length
135048147e0Sopenharmony_ci     * @return
136048147e0Sopenharmony_ci     */
137048147e0Sopenharmony_ci    bubbleSort(arr, length) {
138048147e0Sopenharmony_ci        // A minimum value is generated from the back to the front at a time, and the final position of a number
139048147e0Sopenharmony_ci        // in the sequence can be determined at a time.
140048147e0Sopenharmony_ci        for (let i = 0; i < length - 1; i++) {
141048147e0Sopenharmony_ci            // Improvement of bubbling so that the sequence is ordered if no reversal occurs in one pass
142048147e0Sopenharmony_ci            for (let j = length - 1; j > i; j--) {
143048147e0Sopenharmony_ci                // A minimum value pops up from the back at a time.
144048147e0Sopenharmony_ci                if (arr[j] < arr[j - 1]) {
145048147e0Sopenharmony_ci                    // Reverse order occurs, then swap
146048147e0Sopenharmony_ci                    let temple = arr[j];
147048147e0Sopenharmony_ci                    arr[j] = arr[j - 1];
148048147e0Sopenharmony_ci                    arr[j - 1] = temple;
149048147e0Sopenharmony_ci                }
150048147e0Sopenharmony_ci            }
151048147e0Sopenharmony_ci        }
152048147e0Sopenharmony_ci    },
153048147e0Sopenharmony_ci    async formatPhoneNumber(phoneNumber) {
154048147e0Sopenharmony_ci        if (phoneNumber == null || phoneNumber === common.string.EMPTY_STR) {
155048147e0Sopenharmony_ci            HiLog.w(TAG, 'formatPhoneNumber, param is null');
156048147e0Sopenharmony_ci            return common.string.EMPTY_STR;
157048147e0Sopenharmony_ci        }
158048147e0Sopenharmony_ci        let promise = call.formatPhoneNumber(phoneNumber);
159048147e0Sopenharmony_ci        let formatPhoneNumber = common.string.EMPTY_STR;
160048147e0Sopenharmony_ci        promise.then((value) => {
161048147e0Sopenharmony_ci            formatPhoneNumber = value;
162048147e0Sopenharmony_ci        }).catch((err) => {
163048147e0Sopenharmony_ci            HiLog.e(TAG, 'formatPhoneNumber, error: ' + JSON.stringify(err.message));
164048147e0Sopenharmony_ci        });
165048147e0Sopenharmony_ci        await promise;
166048147e0Sopenharmony_ci        return formatPhoneNumber;
167048147e0Sopenharmony_ci    }
168048147e0Sopenharmony_ci};