1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.dialer.precall.impl;
18 
19 import android.content.Context;
20 import android.telephony.PhoneNumberUtils;
21 import com.android.dialer.common.LogUtil;
22 import com.android.dialer.configprovider.ConfigProviderComponent;
23 import com.android.dialer.precall.impl.MalformedNumberRectifier.MalformedNumberHandler;
24 import com.google.common.base.Optional;
25 
26 /**
27  * It is customary in UK to present numbers as "+44 (0) xx xxxx xxxx". This is actually a amalgam of
28  * international (+44 xx xxxx xxxx) and regional (0xx xxxx xxxx) format, and is in fact invalid. It
29  * might be rejected depending on the carrier.
30  *
31  * <p>This class removes the "0" region code prefix if the first dialable digits are "+440". UK
32  * short codes and region codes in international format will never start with a 0.
33  */
34 class UkRegionPrefixInInternationalFormatHandler implements MalformedNumberHandler {
35 
36   private static final String MALFORMED_PREFIX = "+440";
37 
38   @Override
handle(Context context, String number)39   public Optional<String> handle(Context context, String number) {
40     if (!ConfigProviderComponent.get(context)
41         .getConfigProvider()
42         .getBoolean("uk_region_prefix_in_international_format_fix_enabled", true)) {
43       return Optional.absent();
44     }
45     if (!PhoneNumberUtils.normalizeNumber(number).startsWith(MALFORMED_PREFIX)) {
46       return Optional.absent();
47     }
48     LogUtil.i("UkRegionPrefixInInternationalFormatHandler.handle", "removing (0) in UK numbers");
49 
50     // libPhoneNumber is not used because we want to keep post dial digits, and this is on the main
51     // thread.
52     String convertedNumber = PhoneNumberUtils.convertKeypadLettersToDigits(number);
53     StringBuilder result = new StringBuilder();
54     int prefixPosition = 0;
55     for (int i = 0; i < convertedNumber.length(); i++) {
56       char c = convertedNumber.charAt(i);
57       if (c != MALFORMED_PREFIX.charAt(prefixPosition)) {
58         result.append(c);
59         continue;
60       }
61       prefixPosition++;
62       if (prefixPosition == MALFORMED_PREFIX.length()) {
63         result.append(convertedNumber.substring(i + 1));
64         break;
65       }
66       result.append(c);
67     }
68     return Optional.of(result.toString());
69   }
70 }
71