• 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.content.Context;
21 import android.os.Build;
22 import android.os.Build.VERSION_CODES;
23 import android.preference.PreferenceManager;
24 import android.support.annotation.NonNull;
25 import android.support.annotation.VisibleForTesting;
26 import android.support.v4.os.UserManagerCompat;
27 import android.telephony.TelephonyManager;
28 import com.android.dialer.common.LogUtil;
29 import com.android.dialer.configprovider.ConfigProvider;
30 import com.android.dialer.configprovider.ConfigProviderBindings;
31 import com.android.dialer.strictmode.StrictModeUtils;
32 
33 /**
34  * A Creator for AssistedDialingMediators.
35  *
36  * <p>This helps keep the dependencies required by AssistedDialingMediator for assisted dialing
37  * explicit.
38  */
39 @TargetApi(VERSION_CODES.N)
40 public final class ConcreteCreator {
41 
42   // Floor set at N due to use of Optional.
43   @VisibleForTesting public static final int BUILD_CODE_FLOOR = Build.VERSION_CODES.N;
44   // Ceiling set at P because this feature will ship as part of the framework in Q.
45   @VisibleForTesting public static final int BUILD_CODE_CEILING = 28;
46 
47   /**
48    * Creates a new AssistedDialingMediator
49    *
50    * @param telephonyManager The telephony manager used to determine user location.
51    * @param context The context used to determine whether or not a provided number is an emergency
52    *     number.
53    * @return An AssistedDialingMediator
54    */
createNewAssistedDialingMediator( @onNull TelephonyManager telephonyManager, @NonNull Context context)55   public static AssistedDialingMediator createNewAssistedDialingMediator(
56       @NonNull TelephonyManager telephonyManager, @NonNull Context context) {
57 
58     ConfigProvider configProvider = ConfigProviderBindings.get(context);
59 
60     if (telephonyManager == null) {
61       LogUtil.i(
62           "ConcreteCreator.createNewAssistedDialingMediator", "provided TelephonyManager was null");
63       throw new NullPointerException("Provided TelephonyManager was null");
64     }
65     if (context == null) {
66       LogUtil.i("ConcreteCreator.createNewAssistedDialingMediator", "provided context was null");
67       throw new NullPointerException("Provided context was null");
68     }
69 
70     if (!UserManagerCompat.isUserUnlocked(context)) {
71       // To avoid any issues reading preferences, we disable the feature when the user is in a
72       // locked state.
73       LogUtil.i("ConcreteCreator.createNewAssistedDialingMediator", "user is locked");
74       return new AssistedDialingMediatorStub();
75     }
76 
77     if (!isAssistedDialingEnabled(configProvider)) {
78       LogUtil.i("ConcreteCreator.createNewAssistedDialingMediator", "feature not enabled");
79       return new AssistedDialingMediatorStub();
80     }
81 
82     if (!PreferenceManager.getDefaultSharedPreferences(context)
83         .getBoolean(context.getString(R.string.assisted_dialing_setting_toggle_key), true)) {
84       LogUtil.i("ConcreteCreator.createNewAssistedDialingMediator", "disabled by local setting");
85 
86       return new AssistedDialingMediatorStub();
87     }
88 
89     Constraints constraints = new Constraints(context, getCountryCodeProvider(configProvider));
90     return new AssistedDialingMediatorImpl(
91         new LocationDetector(
92             telephonyManager,
93             StrictModeUtils.bypass(
94                 () ->
95                     PreferenceManager.getDefaultSharedPreferences(context)
96                         .getString(
97                             context.getString(R.string.assisted_dialing_setting_cc_key), null))),
98         new NumberTransformer(constraints));
99   }
100 
101   /** Returns a boolean indicating whether or not the assisted dialing feature is enabled. */
isAssistedDialingEnabled(@onNull ConfigProvider configProvider)102   public static boolean isAssistedDialingEnabled(@NonNull ConfigProvider configProvider) {
103     if (configProvider == null) {
104       LogUtil.i("ConcreteCreator.isAssistedDialingEnabled", "provided configProvider was null");
105       throw new NullPointerException("Provided configProvider was null");
106     }
107 
108     return (Build.VERSION.SDK_INT >= BUILD_CODE_FLOOR
109             && Build.VERSION.SDK_INT <= BUILD_CODE_CEILING)
110         && configProvider.getBoolean("assisted_dialing_enabled", false);
111   }
112 
113   /**
114    * Returns a CountryCodeProvider responsible for providing countries eligible for assisted Dialing
115    */
getCountryCodeProvider(ConfigProvider configProvider)116   public static CountryCodeProvider getCountryCodeProvider(ConfigProvider configProvider) {
117     if (configProvider == null) {
118       LogUtil.i("ConcreteCreator.getCountryCodeProvider", "provided configProvider was null");
119       throw new NullPointerException("Provided configProvider was null");
120     }
121 
122     return new CountryCodeProvider(configProvider);
123   }
124 }
125