erfanian | f455e6a | 2017-09-27 12:03:20 -0700 | [diff] [blame] | 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; |
erfanian | 334c0e1 | 2018-01-25 14:31:06 -0800 | [diff] [blame] | 24 | import com.android.dialer.strictmode.StrictModeUtils; |
erfanian | f455e6a | 2017-09-27 12:03:20 -0700 | [diff] [blame] | 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 | |
erfanian | 334c0e1 | 2018-01-25 14:31:06 -0800 | [diff] [blame] | 33 | private final PhoneNumberUtil phoneNumberUtil; |
erfanian | f455e6a | 2017-09-27 12:03:20 -0700 | [diff] [blame] | 34 | private final Constraints constraints; |
| 35 | |
| 36 | public NumberTransformer(Constraints constraints) { |
| 37 | this.constraints = constraints; |
erfanian | 334c0e1 | 2018-01-25 14:31:06 -0800 | [diff] [blame] | 38 | this.phoneNumberUtil = StrictModeUtils.bypass(() -> PhoneNumberUtil.getInstance()); |
erfanian | f455e6a | 2017-09-27 12:03:20 -0700 | [diff] [blame] | 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 | */ |
| 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) |
erfanian | a21b461 | 2017-10-03 11:24:35 -0700 | [diff] [blame] | 62 | public Optional<TransformationInfo> doAssistedDialingTransformation( |
erfanian | f455e6a | 2017-09-27 12:03:20 -0700 | [diff] [blame] | 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 | |
erfanian | 334c0e1 | 2018-01-25 14:31:06 -0800 | [diff] [blame] | 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) { |
erfanian | f455e6a | 2017-09-27 12:03:20 -0700 | [diff] [blame] | 86 | return Optional.empty(); |
| 87 | } |
| 88 | |
| 89 | String transformedNumber = |
erfanian | 334c0e1 | 2018-01-25 14:31:06 -0800 | [diff] [blame] | 90 | StrictModeUtils.bypass( |
| 91 | () -> |
| 92 | phoneNumberUtil.formatNumberForMobileDialing( |
| 93 | phoneNumber, userRoamingCountryCode, true)); |
erfanian | f455e6a | 2017-09-27 12:03:20 -0700 | [diff] [blame] | 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? |
erfanian | a21b461 | 2017-10-03 11:24:35 -0700 | [diff] [blame] | 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()); |
erfanian | f455e6a | 2017-09-27 12:03:20 -0700 | [diff] [blame] | 112 | } |
| 113 | } |