summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/assisteddialing/Constraints.java
blob: 41a3e92b248d6233c0c3b89f8203a11ee1b9c9c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.assisteddialing;

import android.content.Context;
import android.support.annotation.NonNull;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import com.android.dialer.common.LogUtil;
import com.android.dialer.logging.DialerImpression;
import com.android.dialer.logging.Logger;
import com.android.dialer.phonenumberutil.PhoneNumberHelper;
import com.android.dialer.strictmode.StrictModeUtils;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource;
import java.util.Locale;
import java.util.Optional;

/** Ensures that a number is eligible for Assisted Dialing */
final class Constraints {
  private final PhoneNumberUtil phoneNumberUtil;
  private final Context context;
  private final CountryCodeProvider countryCodeProvider;

  /**
   * Create a new instance of Constraints.
   *
   * @param context The context used to determine whether or not a number is an emergency number.
   * @param countryCodeProvider A csv of supported country codes, e.g. "US,CA"
   */
  public Constraints(@NonNull Context context, @NonNull CountryCodeProvider countryCodeProvider) {
    if (context == null) {
      throw new NullPointerException("Provided context cannot be null");
    }
    this.context = context;

    if (countryCodeProvider == null) {
      throw new NullPointerException("Provided configProviderCountryCodes cannot be null");
    }

    this.countryCodeProvider = countryCodeProvider;
    this.phoneNumberUtil = StrictModeUtils.bypass(() -> PhoneNumberUtil.getInstance());
  }

  /**
   * Determines whether or not we think Assisted Dialing is possible given the provided parameters.
   *
   * @param numberToCheck A string containing the phone number.
   * @param userHomeCountryCode A string containing an ISO 3166-1 alpha-2 country code representing
   *     the user's home country.
   * @param userRoamingCountryCode A string containing an ISO 3166-1 alpha-2 country code
   *     representing the user's roaming country.
   * @return A boolean indicating whether or not the provided values are eligible for assisted
   *     dialing.
   */
  boolean meetsPreconditions(
      @NonNull String numberToCheck,
      @NonNull String userHomeCountryCode,
      @NonNull String userRoamingCountryCode) {

    if (TextUtils.isEmpty(numberToCheck)) {
      LogUtil.i("Constraints.meetsPreconditions", "numberToCheck was empty");
      return false;
    }

    if (TextUtils.isEmpty(userHomeCountryCode)) {
      LogUtil.i("Constraints.meetsPreconditions", "userHomeCountryCode was empty");
      return false;
    }

    if (TextUtils.isEmpty(userRoamingCountryCode)) {
      LogUtil.i("Constraints.meetsPreconditions", "userRoamingCountryCode was empty");
      return false;
    }

    userHomeCountryCode = userHomeCountryCode.toUpperCase(Locale.US);
    userRoamingCountryCode = userRoamingCountryCode.toUpperCase(Locale.US);

    Optional<PhoneNumber> parsedPhoneNumber = parsePhoneNumber(numberToCheck, userHomeCountryCode);

    if (!parsedPhoneNumber.isPresent()) {
      LogUtil.i("Constraints.meetsPreconditions", "parsedPhoneNumber was empty");
      return false;
    }

    return areSupportedCountryCodes(userHomeCountryCode, userRoamingCountryCode)
        && isUserRoaming(userHomeCountryCode, userRoamingCountryCode)
        && isNotInternationalNumber(parsedPhoneNumber)
        && isNotEmergencyNumber(numberToCheck, context)
        && isValidNumber(parsedPhoneNumber)
        && doesNotHaveExtension(parsedPhoneNumber);
  }

  /** Returns a boolean indicating the value equivalence of the provided country codes. */
  private boolean isUserRoaming(
      @NonNull String userHomeCountryCode, @NonNull String userRoamingCountryCode) {
    boolean result = !userHomeCountryCode.equals(userRoamingCountryCode);
    LogUtil.i("Constraints.isUserRoaming", String.valueOf(result));
    return result;
  }

  /**
   * Returns a boolean indicating the support of both provided country codes for assisted dialing.
   * Both country codes must be allowed for the return value to be true.
   */
  private boolean areSupportedCountryCodes(
      @NonNull String userHomeCountryCode, @NonNull String userRoamingCountryCode) {
    if (TextUtils.isEmpty(userHomeCountryCode)) {
      LogUtil.i("Constraints.areSupportedCountryCodes", "userHomeCountryCode was empty");
      return false;
    }

    if (TextUtils.isEmpty(userRoamingCountryCode)) {
      LogUtil.i("Constraints.areSupportedCountryCodes", "userRoamingCountryCode was empty");
      return false;
    }

    boolean result =
        countryCodeProvider.isSupportedCountryCode(userHomeCountryCode)
            && countryCodeProvider.isSupportedCountryCode(userRoamingCountryCode);
    LogUtil.i("Constraints.areSupportedCountryCodes", String.valueOf(result));
    return result;
  }

  /**
   * A convenience method to take a number as a String and a specified country code, and return a
   * PhoneNumber object.
   */
  private Optional<PhoneNumber> parsePhoneNumber(
      @NonNull String numberToParse, @NonNull String userHomeCountryCode) {
    return StrictModeUtils.bypass(
        () -> {
          try {
            return Optional.of(
                phoneNumberUtil.parseAndKeepRawInput(numberToParse, userHomeCountryCode));
          } catch (NumberParseException e) {
            Logger.get(context)
                .logImpression(DialerImpression.Type.ASSISTED_DIALING_CONSTRAINT_PARSING_FAILURE);
            LogUtil.i("Constraints.parsePhoneNumber", "could not parse the number");
            return Optional.empty();
          }
        });
  }

  /** Returns a boolean indicating if the provided number is already internationally formatted. */
  private boolean isNotInternationalNumber(@NonNull Optional<PhoneNumber> parsedPhoneNumber) {

    if (parsedPhoneNumber.get().hasCountryCode()
        && parsedPhoneNumber.get().getCountryCodeSource()
            != CountryCodeSource.FROM_DEFAULT_COUNTRY) {
      Logger.get(context)
          .logImpression(DialerImpression.Type.ASSISTED_DIALING_CONSTRAINT_NUMBER_HAS_COUNTRY_CODE);
      LogUtil.i(
          "Constraints.isNotInternationalNumber", "phone number already provided the country code");
      return false;
    }
    return true;
  }

  /**
   * Returns a boolean indicating if the provided number has an extension.
   *
   * <p>Extensions are currently stripped when formatting a number for mobile dialing, so we don't
   * want to purposefully truncate a number.
   */
  private boolean doesNotHaveExtension(@NonNull Optional<PhoneNumber> parsedPhoneNumber) {

    if (parsedPhoneNumber.get().hasExtension()
        && !TextUtils.isEmpty(parsedPhoneNumber.get().getExtension())) {
      Logger.get(context)
          .logImpression(DialerImpression.Type.ASSISTED_DIALING_CONSTRAINT_NUMBER_HAS_EXTENSION);
      LogUtil.i("Constraints.doesNotHaveExtension", "phone number has an extension");
      return false;
    }
    return true;
  }

  /** Returns a boolean indicating if the provided number is considered to be a valid number. */
  private boolean isValidNumber(@NonNull Optional<PhoneNumber> parsedPhoneNumber) {
    boolean result =
        StrictModeUtils.bypass(() -> phoneNumberUtil.isValidNumber(parsedPhoneNumber.get()));
    LogUtil.i("Constraints.isValidNumber", String.valueOf(result));

    return result;
  }

  /** Returns a boolean indicating if the provided number is an emergency number. */
  private boolean isNotEmergencyNumber(@NonNull String numberToCheck, @NonNull Context context) {
    // isEmergencyNumber may depend on network state, so also use isLocalEmergencyNumber when
    // roaming and out of service.
    boolean result =
        !PhoneNumberUtils.isEmergencyNumber(numberToCheck)
            && !PhoneNumberHelper.isLocalEmergencyNumber(context, numberToCheck);
    LogUtil.i("Constraints.isNotEmergencyNumber", String.valueOf(result));
    return result;
  }
}