• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 common from "../data/commonData";
16import HiLog from "./HiLog";
17import call from "@ohos.telephony.call";
18
19const TAG = "TelephoneUtil";
20/**
21 *  log package tool class
22 */
23const infoMegTelephone = [
24    "1065796709202", "1065502043202", "1065902090202", "1069055999202",
25    "106579670915", "106550204315", "106590209015", "106905599915",
26    "106", "400", "111", "100", "118", "116", "12306", "12329", "12345",
27    "12122", "12321", "12580", "12520", "12583", "02512329", "053287003810",
28    "1258319559899", "1019", "12583110086", "000000", "95", "96",
29];
30
31export default {
32
33    /**
34     * Check whether the mobile number is notification information.
35     * @param telephone
36     * @return Yes or no
37     */
38    judgeIsInfoMsg(telephone) {
39        let result = false;
40        if (telephone == null || telephone == common.string.EMPTY_STR) {
41            return result;
42        }
43        for (let item of infoMegTelephone) {
44            if (telephone.indexOf(item) == 0) {
45                result = true;
46            }
47        }
48        return result;
49    },
50
51    formatTelephone(telephone) {
52        let formatTelephone: string = telephone;
53        if (telephone == null || telephone == common.string.EMPTY_STR) {
54            formatTelephone = common.string.EMPTY_STR;
55        }
56        if (formatTelephone.startsWith("+86")) {
57            formatTelephone = formatTelephone.substring(3);
58        } else if(formatTelephone.startsWith("86")) {
59            formatTelephone = formatTelephone.substring(2);
60        }
61        return formatTelephone;
62    },
63
64    dealSelectContactsSort(selectContacts) {
65        if (selectContacts.length == 0) {
66            return;
67        }
68        let result = [];
69        let telephone = common.string.EMPTY_STR;
70        let contactsMap = new Map();
71        for (let item of selectContacts) {
72            telephone = telephone + item.telephone + common.string.COMMA;
73            if (!contactsMap.has(item.telephone)) {
74                contactsMap.set(item.telephone, item);
75            }
76        }
77        telephone = this.dealTelephoneSort(telephone.substring(0, telephone.length - 1));
78        let telephones = telephone.split(common.string.COMMA);
79        for (let element of telephones) {
80            if (contactsMap.has(element)) {
81                result.push(contactsMap.get(element));
82            }
83        }
84        return result;
85    },
86
87    /**
88     * Mobile number, sorted in ascending order.
89     * @param telephone
90     * @return
91     */
92    dealTelephoneSort(telephone) {
93        if (telephone == null || telephone == common.string.EMPTY_STR) {
94            return common.string.EMPTY_STR;
95        }
96        let result = common.string.EMPTY_STR;
97        let telephones = telephone.split(common.string.COMMA);
98        // If there is only one mobile number, no sorting is required.
99        if (telephones.length == 1) {
100            return telephone;
101        }
102        let telephoneMap = new Map();
103        let indexs = [];
104        let count = 0;
105        // grouping
106        for (let item of telephones) {
107            if (telephoneMap.has(item.length)) {
108                let strings = telephoneMap.get(item.length);
109                strings.push(item);
110            } else {
111                let strings = [];
112                strings.push(item);
113                telephoneMap.set(item.length, strings);
114                indexs[count++] = item.length;
115            }
116        }
117        // Sort from Large to Small
118        this.bubbleSort(indexs, count);
119        for (let index of indexs) {
120            let arrs = telephoneMap.get(index);
121            this.bubbleSort(arrs, arrs.length);
122            for (let arr of arrs) {
123                result = result + arr + common.string.COMMA;
124            }
125        }
126        // Obtain the corresponding results and pair them.
127        return result.substring(0, result.length - 1);
128    },
129
130    /**
131     * Bubble sort, sorted in ascending order.
132     * @param arr
133     * @param length
134     * @return
135     */
136    bubbleSort(arr, length) {
137        // A minimum value is generated from the back to the front at a time, and the final position of a number
138        // in the sequence can be determined at a time.
139        for (let i = 0; i < length - 1; i++) {
140            // Improvement of bubbling so that the sequence is ordered if no reversal occurs in one pass
141            for (let j = length - 1; j > i; j--) {
142                // A minimum value pops up from the back at a time.
143                if (arr[j] < arr[j - 1]) {
144                    // Reverse order occurs, then swap
145                    let temple = arr[j];
146                    arr[j] = arr[j - 1];
147                    arr[j - 1] = temple;
148                }
149            }
150        }
151    },
152    async formatPhoneNumber(phoneNumber) {
153        if (phoneNumber == null || phoneNumber === common.string.EMPTY_STR) {
154            HiLog.w(TAG, "formatPhoneNumber, param is null");
155            return common.string.EMPTY_STR;
156        }
157        let promise = call.formatPhoneNumber(phoneNumber);
158        let formatPhoneNumber = common.string.EMPTY_STR;
159        promise.then((value) => {
160            formatPhoneNumber = value;
161        }).catch((err) => {
162            HiLog.e(TAG, "formatPhoneNumber, error: " + JSON.stringify(err.message));
163        });
164        await promise;
165        return formatPhoneNumber;
166    }
167};