~rsalveti/telepathy-ofono/porting-pulse-droid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Original source code available at: http://androidxref.com/4.0.4/xref/frameworks/base/telephony/java/android/telephony/PhoneNumberUtils.java
 */

#ifndef PHONENUMBERUTILS_H
#define PHONENUMBERUTILS_H

namespace PhoneNumberUtils 
{

/** True if c is ISO-LATIN characters 0-9, *, # , +, WILD, WAIT, PAUSE   */
bool isNonSeparator(char c) 
{
    return (c >= '0' && c <= '9') || c == '*' || c == '#' || c == '+'
            || c == 'N' || c == ';' || c == ',';
}

/** True if c is ISO-LATIN characters 0-9, *, # , +, WILD  */
bool isDialable(char c) 
{
    return (c >= '0' && c <= '9') || c == '*' || c == '#' || c == '+' || c == 'N';
}

/** True if c is ISO-LATIN characters 0-9 */
bool isISODigit (char c) {
    return c >= '0' && c <= '9';
}

/** or -1 if both are negative */
int minPositive (int a, int b) 
{
    if (a >= 0 && b >= 0) {
        return (a < b) ? a : b;
    } else if (a >= 0) { /* && b < 0 */
        return a;
    } else if (b >= 0) { /* && a < 0 */
        return b;
    } else { /* a < 0 && b < 0 */
        return -1;
    }
}

/** index of the last character of the network portion
 *  (eg anything after is a post-dial string)
 */
int indexOfLastNetworkChar(const QString &a) 
{
    int pIndex, wIndex;
    int origLength;
    int trimIndex;

    origLength = a.length();

    pIndex = a.indexOf(',');
    wIndex = a.indexOf(';');

    trimIndex = minPositive(pIndex, wIndex);

    if (trimIndex < 0) {
        return origLength - 1;
    } else {
        return trimIndex - 1;
    }
}

/** all of a up to len must be an international prefix or
 *  separators/non-dialing digits
 */
bool matchIntlPrefix(const QString &a, int len) 
{
    /* '([^0-9*#+pwn]\+[^0-9*#+pwn] | [^0-9*#+pwn]0(0|11)[^0-9*#+pwn] )$' */
    /*        0       1                           2 3 45               */

    int state = 0;
    for (int i = 0 ; i < len ; i++) {
        char c = a.at(i).toLatin1();

        switch (state) {
            case 0:
                if      (c == '+') state = 1;
                else if (c == '0') state = 2;
                else if (isNonSeparator(c)) return false;
            break;

            case 2:
                if      (c == '0') state = 3;
                else if (c == '1') state = 4;
                else if (isNonSeparator(c)) return false;
            break;

            case 4:
                if      (c == '1') state = 5;
                else if (isNonSeparator(c)) return false;
            break;

            default:
                if (isNonSeparator(c)) return false;
            break;

        }
    }

    return state == 1 || state == 3 || state == 5;
}

/** all of 'a' up to len must match non-US trunk prefix ('0') */
bool matchTrunkPrefix(const QString &a, int len) {
    bool found;

    found = false;

    for (int i = 0 ; i < len ; i++) {
        char c = a.at(i).toLatin1();

        if (c == '0' && !found) {
            found = true;
        } else if (isNonSeparator(c)) {
            return false;
        }
    }

    return found;
}

/** all of 'a' up to len must be a (+|00|011)country code)
 *  We're fast and loose with the country code. Any \d{1,3} matches */
bool matchIntlPrefixAndCC(const QString &a, int len) {
    /*  [^0-9*#+pwn]*(\+|0(0|11)\d\d?\d? [^0-9*#+pwn] $ */
    /*      0          1 2 3 45  6 7  8                 */

    int state = 0;
    for (int i = 0 ; i < len ; i++ ) {
        char c = a.at(i).toLatin1();

        switch (state) {
            case 0:
                if      (c == '+') state = 1;
                else if (c == '0') state = 2;
                else if (isNonSeparator(c)) return false;
            break;

            case 2:
                if      (c == '0') state = 3;
                else if (c == '1') state = 4;
                else if (isNonSeparator(c)) return false;
            break;

            case 4:
                if      (c == '1') state = 5;
                else if (isNonSeparator(c)) return false;
            break;

            case 1:
            case 3:
            case 5:
                if      (isISODigit(c)) state = 6;
                else if (isNonSeparator(c)) return false;
            break;

            case 6:
            case 7:
                if      (isISODigit(c)) state++;
                else if (isNonSeparator(c)) return false;
            break;

            default:
                if (isNonSeparator(c)) return false;
        }
    }

    return state == 6 || state == 7 || state == 8;
}


/**
 * Compare phone numbers a and b, return true if they're identical
 * enough for caller ID purposes.
 *
 * - Compares from right to left
 * - requires MIN_MATCH (7) characters to match
 * - handles common trunk prefixes and international prefixes
 *   (basically, everything except the Russian trunk prefix)
 *
 * Note that this method does not return false even when the two phone numbers
 * are not exactly same; rather; we can call this method "similar()", not "equals()".
 *
 * @hide
 */
bool compareLoosely(const QString &a, const QString &b)
{
     int ia, ib;
     int matched;
     int numNonDialableCharsInA = 0;
     int numNonDialableCharsInB = 0;

     if (a.length() == 0 || b.length() == 0) {
         return false;
     }

     if (a == b) {
         return true;
     }

     ia = indexOfLastNetworkChar (a);
     ib = indexOfLastNetworkChar (b);
     matched = 0;

     while (ia >= 0 && ib >=0) {
         char ca, cb;
         bool skipCmp = false;

         ca = a.at(ia).toLatin1();

         if (!isDialable(ca)) {
             ia--;
             skipCmp = true;
             numNonDialableCharsInA++;
         }

         cb = b.at(ib).toLatin1();

         if (!isDialable(cb)) {
             ib--;
             skipCmp = true;
             numNonDialableCharsInB++;
         }

         if (!skipCmp) {
             if (cb != ca && ca != 'N' && cb != 'N') {
                 break;
             }
             ia--; ib--; matched++;
         }
     }

     if (matched < 7) {
         int effectiveALen = a.length() - numNonDialableCharsInA;
         int effectiveBLen = b.length() - numNonDialableCharsInB;


         // if the number of dialable chars in a and b match, but the matched chars < MIN_MATCH,
         // treat them as equal (i.e. 404-04 and 40404)
         if (effectiveALen == effectiveBLen && effectiveALen == matched) {
             return true;
         }

         return false;
     }

     // At least one string has matched completely;
     if (matched >= 7 && (ia < 0 || ib < 0)) {
         return true;
     }

     /*
      * Now, what remains must be one of the following for a
      * match:
      *
      *  - a '+' on one and a '00' or a '011' on the other
      *  - a '0' on one and a (+,00)<country code> on the other
      *     (for this, a '0' and a '00' prefix would have succeeded above)
      */

     if (matchIntlPrefix(a, ia + 1)
         && matchIntlPrefix (b, ib +1)
     ) {
         return true;
     }

     if (matchTrunkPrefix(a, ia + 1)
         && matchIntlPrefixAndCC(b, ib +1)
     ) {
         return true;
     }

     if (matchTrunkPrefix(b, ib + 1)
         && matchIntlPrefixAndCC(a, ia +1)
     ) {
         return true;
     }

     return false;
}

}

#endif