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.precall.impl; 18 19 import android.annotation.TargetApi; 20 import android.content.Context; 21 import android.os.Build; 22 import android.os.Bundle; 23 import android.telecom.PhoneAccount; 24 import android.telephony.SubscriptionInfo; 25 import android.telephony.TelephonyManager; 26 import com.android.dialer.assisteddialing.AssistedDialingMediator; 27 import com.android.dialer.assisteddialing.ConcreteCreator; 28 import com.android.dialer.assisteddialing.TransformationInfo; 29 import com.android.dialer.callintent.CallIntentBuilder; 30 import com.android.dialer.common.Assert; 31 import com.android.dialer.common.LogUtil; 32 import com.android.dialer.compat.telephony.TelephonyManagerCompat; 33 import com.android.dialer.configprovider.ConfigProvider; 34 import com.android.dialer.configprovider.ConfigProviderBindings; 35 import com.android.dialer.precall.PreCallAction; 36 import com.android.dialer.precall.PreCallCoordinator; 37 import com.android.dialer.telecom.TelecomUtil; 38 import com.android.dialer.util.CallUtil; 39 import java.util.Optional; 40 41 /** Rewrites the call URI with country code. */ 42 public class AssistedDialAction implements PreCallAction { 43 44 @Override 45 public boolean requiresUi(Context context, CallIntentBuilder builder) { 46 return false; 47 } 48 49 @SuppressWarnings("AndroidApiChecker") // Use of optional 50 @TargetApi(Build.VERSION_CODES.N) 51 @Override 52 public void runWithoutUi(Context context, CallIntentBuilder builder) { 53 if (!builder.isAssistedDialAllowed()) { 54 return; 55 } 56 57 AssistedDialingMediator assistedDialingMediator = 58 ConcreteCreator.createNewAssistedDialingMediator( 59 getAssistedDialingTelephonyManager(context, builder), context); 60 61 // Checks the platform is N+ and meets other pre-flight checks. 62 if (!assistedDialingMediator.isPlatformEligible()) { 63 return; 64 } 65 String phoneNumber = 66 builder.getUri().getScheme().equals(PhoneAccount.SCHEME_TEL) 67 ? builder.getUri().getSchemeSpecificPart() 68 : ""; 69 Optional<TransformationInfo> transformedNumber = 70 assistedDialingMediator.attemptAssistedDial(phoneNumber); 71 if (transformedNumber.isPresent()) { 72 builder.getOutgoingCallExtras().putBoolean(TelephonyManagerCompat.USE_ASSISTED_DIALING, true); 73 Bundle assistedDialingExtras = transformedNumber.get().toBundle(); 74 builder 75 .getOutgoingCallExtras() 76 .putBundle(TelephonyManagerCompat.ASSISTED_DIALING_EXTRAS, assistedDialingExtras); 77 builder.setUri( 78 CallUtil.getCallUri(Assert.isNotNull(transformedNumber.get().transformedNumber()))); 79 LogUtil.i("AssistedDialAction.runWithoutUi", "assisted dialing was used."); 80 } 81 } 82 83 /** 84 * A convenience method to return the proper TelephonyManager in possible multi-sim environments. 85 */ 86 @SuppressWarnings("AndroidApiChecker") // Use of createForSubscriptionId 87 @TargetApi(Build.VERSION_CODES.N) 88 private TelephonyManager getAssistedDialingTelephonyManager( 89 Context context, CallIntentBuilder builder) { 90 91 ConfigProvider configProvider = ConfigProviderBindings.get(context); 92 TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class); 93 // None of this will be required in the framework because the PhoneAccountHandle 94 // is already mapped to the request in the TelecomConnection. 95 if (builder.getPhoneAccountHandle() == null) { 96 return telephonyManager; 97 } 98 99 if (!configProvider.getBoolean("assisted_dialing_dual_sim_enabled", false)) { 100 return telephonyManager; 101 } 102 103 com.google.common.base.Optional<SubscriptionInfo> subscriptionInfo = 104 TelecomUtil.getSubscriptionInfo(context, builder.getPhoneAccountHandle()); 105 if (!subscriptionInfo.isPresent()) { 106 LogUtil.i( 107 "AssistedDialAction.getAssistedDialingTelephonyManager", "subcriptionInfo was absent."); 108 return telephonyManager; 109 } 110 TelephonyManager pinnedtelephonyManager = 111 telephonyManager.createForSubscriptionId(subscriptionInfo.get().getSubscriptionId()); 112 if (pinnedtelephonyManager == null) { 113 LogUtil.i( 114 "AssistedDialAction.getAssistedDialingTelephonyManager", 115 "createForSubscriptionId pinnedtelephonyManager was null."); 116 return telephonyManager; 117 } 118 LogUtil.i( 119 "AssistedDialAction.getAssistedDialingTelephonyManager", 120 "createForPhoneAccountHandle using pinnedtelephonyManager from subscription id."); 121 return pinnedtelephonyManager; 122 } 123 124 @Override 125 public void runWithUi(PreCallCoordinator coordinator) { 126 runWithoutUi(coordinator.getActivity(), coordinator.getBuilder()); 127 } 128 129 @Override 130 public void onDiscard() {} 131 } 132