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