blob: 15a889e746918d706ba6806da0645e27f80793f1 [file] [log] [blame]
Zachary Heidepriem73d995f2017-11-11 15:03:26 -08001/*
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
17package com.android.dialer.precall.impl;
18
19import android.annotation.TargetApi;
twyenaf773522017-11-16 15:39:03 -080020import android.content.Context;
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080021import android.os.Build;
22import android.os.Bundle;
erfanian8033ae22018-01-08 10:43:27 -080023import android.support.v4.os.BuildCompat;
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080024import android.telecom.PhoneAccount;
erfanian2f084812017-12-22 15:38:57 -080025import android.telephony.SubscriptionInfo;
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080026import android.telephony.TelephonyManager;
27import com.android.dialer.assisteddialing.AssistedDialingMediator;
28import com.android.dialer.assisteddialing.ConcreteCreator;
29import com.android.dialer.assisteddialing.TransformationInfo;
30import com.android.dialer.callintent.CallIntentBuilder;
31import com.android.dialer.common.Assert;
erfanian2f084812017-12-22 15:38:57 -080032import com.android.dialer.common.LogUtil;
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080033import com.android.dialer.compat.telephony.TelephonyManagerCompat;
erfanian2f084812017-12-22 15:38:57 -080034import com.android.dialer.configprovider.ConfigProvider;
35import com.android.dialer.configprovider.ConfigProviderBindings;
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080036import com.android.dialer.precall.PreCallAction;
37import com.android.dialer.precall.PreCallCoordinator;
erfanian2f084812017-12-22 15:38:57 -080038import com.android.dialer.telecom.TelecomUtil;
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080039import com.android.dialer.util.CallUtil;
40import java.util.Optional;
41
erfanian2f084812017-12-22 15:38:57 -080042/** Rewrites the call URI with country code. */
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080043public class AssistedDialAction implements PreCallAction {
44
twyenaf773522017-11-16 15:39:03 -080045 @Override
46 public boolean requiresUi(Context context, CallIntentBuilder builder) {
47 return false;
48 }
49
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080050 @SuppressWarnings("AndroidApiChecker") // Use of optional
51 @TargetApi(Build.VERSION_CODES.N)
52 @Override
twyenaf773522017-11-16 15:39:03 -080053 public void runWithoutUi(Context context, CallIntentBuilder builder) {
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080054 if (!builder.isAssistedDialAllowed()) {
55 return;
56 }
erfanian2f084812017-12-22 15:38:57 -080057
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080058 AssistedDialingMediator assistedDialingMediator =
59 ConcreteCreator.createNewAssistedDialingMediator(
erfanian2f084812017-12-22 15:38:57 -080060 getAssistedDialingTelephonyManager(context, builder), context);
erfanian8033ae22018-01-08 10:43:27 -080061 if (BuildCompat.isAtLeastP()) {
erfanian2cf2c342017-12-21 12:01:33 -080062 builder.getOutgoingCallExtras().putBoolean(TelephonyManagerCompat.USE_ASSISTED_DIALING, true);
63 }
erfanian2f084812017-12-22 15:38:57 -080064 // Checks the platform is N+ and meets other pre-flight checks.
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080065 if (!assistedDialingMediator.isPlatformEligible()) {
66 return;
67 }
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080068 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()) {
erfanian2cf2c342017-12-21 12:01:33 -080075 builder.getOutgoingCallExtras().putBoolean(TelephonyManagerCompat.USE_ASSISTED_DIALING, true);
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080076 Bundle assistedDialingExtras = transformedNumber.get().toBundle();
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080077 builder
78 .getOutgoingCallExtras()
79 .putBundle(TelephonyManagerCompat.ASSISTED_DIALING_EXTRAS, assistedDialingExtras);
80 builder.setUri(
81 CallUtil.getCallUri(Assert.isNotNull(transformedNumber.get().transformedNumber())));
erfaniane64d9fe2018-01-26 08:25:13 -080082 LogUtil.i("AssistedDialAction.runWithoutUi", "assisted dialing was used.");
Zachary Heidepriem73d995f2017-11-11 15:03:26 -080083 }
84 }
85
erfanian2f084812017-12-22 15:38:57 -080086 /**
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 Heidepriem73d995f2017-11-11 15:03:26 -0800127 @Override
twyenaf773522017-11-16 15:39:03 -0800128 public void runWithUi(PreCallCoordinator coordinator) {
129 runWithoutUi(coordinator.getActivity(), coordinator.getBuilder());
130 }
131
132 @Override
Zachary Heidepriem73d995f2017-11-11 15:03:26 -0800133 public void onDiscard() {}
134}