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