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