• 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.annotation.TargetApi;
20 import android.os.Build.VERSION_CODES;
21 import android.support.annotation.NonNull;
22 import android.text.TextUtils;
23 import com.android.dialer.common.LogUtil;
24 import com.android.dialer.strictmode.StrictModeUtils;
25 import com.google.i18n.phonenumbers.NumberParseException;
26 import com.google.i18n.phonenumbers.PhoneNumberUtil;
27 import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
28 import java.util.Optional;
29 
30 /** Responsible for transforming numbers to make them dialable and valid when roaming. */
31 final class NumberTransformer {
32 
33   private final PhoneNumberUtil phoneNumberUtil;
34   private final Constraints constraints;
35 
NumberTransformer(Constraints constraints)36   public NumberTransformer(Constraints constraints) {
37     this.constraints = constraints;
38     this.phoneNumberUtil = StrictModeUtils.bypass(() -> PhoneNumberUtil.getInstance());
39   }
40 
41   /**
42    * Returns a boolean for callers to quickly determine whether or not the AssistedDialingMediator
43    * thinks an attempt at assisted dialing is likely to succeed.
44    */
canDoAssistedDialingTransformation( @onNull String numberToCheck, @NonNull String userHomeCountryCode, @NonNull String userRoamingCountryCode)45   public boolean canDoAssistedDialingTransformation(
46       @NonNull String numberToCheck,
47       @NonNull String userHomeCountryCode,
48       @NonNull String userRoamingCountryCode) {
49     return constraints.meetsPreconditions(
50         numberToCheck, userHomeCountryCode, userRoamingCountryCode);
51   }
52 
53   /**
54    * A method to do assisted dialing transformations.
55    *
56    * <p>The library will do its best to attempt a transformation, but, if for any reason the
57    * transformation fails, we return an empty optional. The operation can be considered a success
58    * when the Optional we return has a value set.
59    */
60   @SuppressWarnings("AndroidApiChecker") // Use of optional
61   @TargetApi(VERSION_CODES.N)
doAssistedDialingTransformation( String numbertoTransform, String userHomeCountryCode, String userRoamingCountryCode)62   public Optional<TransformationInfo> doAssistedDialingTransformation(
63       String numbertoTransform, String userHomeCountryCode, String userRoamingCountryCode) {
64 
65     if (!constraints.meetsPreconditions(
66         numbertoTransform, userHomeCountryCode, userRoamingCountryCode)) {
67       LogUtil.i(
68           "NumberTransformer.doAssistedDialingTransformation",
69           "assisted dialing failed preconditions");
70       return Optional.empty();
71     }
72 
73     PhoneNumber phoneNumber =
74         StrictModeUtils.bypass(
75             () -> {
76               try {
77                 return phoneNumberUtil.parse(numbertoTransform, userHomeCountryCode);
78               } catch (NumberParseException e) {
79                 LogUtil.i(
80                     "NumberTransformer.doAssistedDialingTransformation", "number failed to parse");
81                 return null;
82               }
83             });
84 
85     if (phoneNumber == null) {
86       return Optional.empty();
87     }
88 
89     String transformedNumber =
90         StrictModeUtils.bypass(
91             () ->
92                 phoneNumberUtil.formatNumberForMobileDialing(
93                     phoneNumber, userRoamingCountryCode, true));
94 
95     // formatNumberForMobileDialing may return an empty String.
96     if (TextUtils.isEmpty(transformedNumber)) {
97       LogUtil.i(
98           "NumberTransformer.doAssistedDialingTransformation",
99           "formatNumberForMobileDialing returned an empty string");
100       return Optional.empty();
101     }
102 
103     // TODO Verify the transformed number is still valid?
104     return Optional.of(
105         TransformationInfo.builder()
106             .setOriginalNumber(numbertoTransform)
107             .setTransformedNumber(transformedNumber)
108             .setUserHomeCountryCode(userHomeCountryCode)
109             .setUserRoamingCountryCode(userRoamingCountryCode)
110             .setTransformedNumberCountryCallingCode(phoneNumber.getCountryCode())
111             .build());
112   }
113 }
114