• 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.support.annotation.RequiresApi;
23 import com.android.dialer.common.LogUtil;
24 import java.util.Optional;
25 
26 /**
27  * The Mediator for Assisted Dialing.
28  *
29  * <p>This class is responsible for mediating location discovery of the user, determining if the
30  * call is eligible for assisted dialing, and performing the transformation of numbers eligible for
31  * assisted dialing.
32  */
33 @RequiresApi(VERSION_CODES.N)
34 final class AssistedDialingMediatorImpl implements AssistedDialingMediator {
35 
36   private final LocationDetector locationDetector;
37   private final NumberTransformer numberTransformer;
38 
AssistedDialingMediatorImpl( @onNull LocationDetector locationDetector, @NonNull NumberTransformer numberTransformer)39   AssistedDialingMediatorImpl(
40       @NonNull LocationDetector locationDetector, @NonNull NumberTransformer numberTransformer) {
41     if (locationDetector == null) {
42       throw new NullPointerException("locationDetector was null");
43     }
44 
45     if (numberTransformer == null) {
46       throw new NullPointerException("numberTransformer was null");
47     }
48     this.locationDetector = locationDetector;
49     this.numberTransformer = numberTransformer;
50   }
51 
52   @Override
isPlatformEligible()53   public boolean isPlatformEligible() {
54     // This impl is only instantiated if it passes platform checks in ConcreteCreator,
55     // so we return true here.
56     return true;
57   }
58 
59   /** Returns the country code in which the library thinks the user typically resides. */
60   @Override
61   @SuppressWarnings("AndroidApiChecker") // Use of optional
62   @TargetApi(VERSION_CODES.N)
userHomeCountryCode()63   public Optional<String> userHomeCountryCode() {
64     return locationDetector.getUpperCaseUserHomeCountry();
65   }
66 
67   /**
68    * Returns an Optional of type String containing the transformed number that was provided. The
69    * transformed number should be capable of dialing out of the User's current country and
70    * successfully connecting with a contact in the User's home country.
71    */
72   @SuppressWarnings("AndroidApiChecker") // Use of optional
73   @TargetApi(VERSION_CODES.N)
74   @Override
attemptAssistedDial(@onNull String numberToTransform)75   public Optional<TransformationInfo> attemptAssistedDial(@NonNull String numberToTransform) {
76     Optional<String> userHomeCountryCode = locationDetector.getUpperCaseUserHomeCountry();
77     Optional<String> userRoamingCountryCode = locationDetector.getUpperCaseUserRoamingCountry();
78 
79     if (!userHomeCountryCode.isPresent() || !userRoamingCountryCode.isPresent()) {
80       LogUtil.i("AssistedDialingMediator.attemptAssistedDial", "Unable to determine country codes");
81       return Optional.empty();
82     }
83 
84     return numberTransformer.doAssistedDialingTransformation(
85         numberToTransform, userHomeCountryCode.get(), userRoamingCountryCode.get());
86   }
87 }
88