• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.assisteddialing;
18 
19 import android.text.TextUtils;
20 import com.android.dialer.common.LogUtil;
21 import com.android.dialer.strictmode.StrictModeUtils;
22 import com.google.i18n.phonenumbers.NumberParseException;
23 import com.google.i18n.phonenumbers.PhoneNumberUtil;
24 import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
25 import java.util.Optional;
26 
27 /** Responsible for transforming numbers to make them dialable and valid when roaming. */
28 final class NumberTransformer {
29 
30   private final PhoneNumberUtil phoneNumberUtil;
31   private final Constraints constraints;
32 
NumberTransformer(Constraints constraints)33   NumberTransformer(Constraints constraints) {
34     this.constraints = constraints;
35     this.phoneNumberUtil = StrictModeUtils.bypass(PhoneNumberUtil::getInstance);
36   }
37 
38   /**
39    * A method to do assisted dialing transformations.
40    *
41    * <p>The library will do its best to attempt a transformation, but, if for any reason the
42    * transformation fails, we return an empty optional. The operation can be considered a success
43    * when the Optional we return has a value set.
44    */
doAssistedDialingTransformation( String numbertoTransform, String userHomeCountryCode, String userRoamingCountryCode)45   Optional<TransformationInfo> doAssistedDialingTransformation(
46       String numbertoTransform, String userHomeCountryCode, String userRoamingCountryCode) {
47 
48     if (!constraints.meetsPreconditions(
49         numbertoTransform, userHomeCountryCode, userRoamingCountryCode)) {
50       LogUtil.i(
51           "NumberTransformer.doAssistedDialingTransformation",
52           "assisted dialing failed preconditions");
53       return Optional.empty();
54     }
55 
56     PhoneNumber phoneNumber =
57         StrictModeUtils.bypass(
58             () -> {
59               try {
60                 return phoneNumberUtil.parse(numbertoTransform, userHomeCountryCode);
61               } catch (NumberParseException e) {
62                 LogUtil.i(
63                     "NumberTransformer.doAssistedDialingTransformation", "number failed to parse");
64                 return null;
65               }
66             });
67 
68     if (phoneNumber == null) {
69       return Optional.empty();
70     }
71 
72     String transformedNumber =
73         StrictModeUtils.bypass(
74             () ->
75                 phoneNumberUtil.formatNumberForMobileDialing(
76                     phoneNumber, userRoamingCountryCode, true));
77 
78     // formatNumberForMobileDialing may return an empty String.
79     if (TextUtils.isEmpty(transformedNumber)) {
80       LogUtil.i(
81           "NumberTransformer.doAssistedDialingTransformation",
82           "formatNumberForMobileDialing returned an empty string");
83       return Optional.empty();
84     }
85 
86     // TODO Verify the transformed number is still valid?
87     return Optional.of(
88         TransformationInfo.builder()
89             .setOriginalNumber(numbertoTransform)
90             .setTransformedNumber(transformedNumber)
91             .setUserHomeCountryCode(userHomeCountryCode)
92             .setUserRoamingCountryCode(userRoamingCountryCode)
93             .setTransformedNumberCountryCallingCode(phoneNumber.getCountryCode())
94             .build());
95   }
96 }
97