blob: 8bbad329d24dffc28cf59ed6cd146332bf679f6c [file] [log] [blame]
erfanianf455e6a2017-09-27 12:03:20 -07001/*
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
17package com.android.dialer.assisteddialing;
18
19import android.annotation.TargetApi;
20import android.os.Build.VERSION_CODES;
21import android.support.annotation.NonNull;
22import android.text.TextUtils;
23import com.android.dialer.common.LogUtil;
erfanian334c0e12018-01-25 14:31:06 -080024import com.android.dialer.strictmode.StrictModeUtils;
erfanianf455e6a2017-09-27 12:03:20 -070025import com.google.i18n.phonenumbers.NumberParseException;
26import com.google.i18n.phonenumbers.PhoneNumberUtil;
27import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
28import java.util.Optional;
29
30/** Responsible for transforming numbers to make them dialable and valid when roaming. */
31final class NumberTransformer {
32
erfanian334c0e12018-01-25 14:31:06 -080033 private final PhoneNumberUtil phoneNumberUtil;
erfanianf455e6a2017-09-27 12:03:20 -070034 private final Constraints constraints;
35
36 public NumberTransformer(Constraints constraints) {
37 this.constraints = constraints;
erfanian334c0e12018-01-25 14:31:06 -080038 this.phoneNumberUtil = StrictModeUtils.bypass(() -> PhoneNumberUtil.getInstance());
erfanianf455e6a2017-09-27 12:03:20 -070039 }
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)
erfaniana21b4612017-10-03 11:24:35 -070062 public Optional<TransformationInfo> doAssistedDialingTransformation(
erfanianf455e6a2017-09-27 12:03:20 -070063 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
erfanian334c0e12018-01-25 14:31:06 -080073 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) {
erfanianf455e6a2017-09-27 12:03:20 -070086 return Optional.empty();
87 }
88
89 String transformedNumber =
erfanian334c0e12018-01-25 14:31:06 -080090 StrictModeUtils.bypass(
91 () ->
92 phoneNumberUtil.formatNumberForMobileDialing(
93 phoneNumber, userRoamingCountryCode, true));
erfanianf455e6a2017-09-27 12:03:20 -070094
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?
erfaniana21b4612017-10-03 11:24:35 -0700104 return Optional.of(
105 TransformationInfo.builder()
106 .setOriginalNumber(numbertoTransform)
107 .setTransformedNumber(transformedNumber)
108 .setUserHomeCountryCode(userHomeCountryCode)
109 .setUserRoamingCountryCode(userRoamingCountryCode)
110 .setTransformedNumberCountryCallingCode(phoneNumber.getCountryCode())
111 .build());
erfanianf455e6a2017-09-27 12:03:20 -0700112 }
113}