blob: 05bdad0605f2be03f9c44ecf3fa241d3072ab6a4 [file] [log] [blame]
Santos Cordon681663d2014-01-30 04:32:15 -08001/*
2 * Copyright 2014, 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.telecomm;
18
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -070019import android.telecomm.CallServiceDescriptor;
20
Sailesh Nepala439e1b2014-03-11 18:19:58 -070021import com.android.internal.telecomm.ICallServiceSelector;
Santos Cordon681663d2014-01-30 04:32:15 -080022import com.google.common.collect.Maps;
23
Sailesh Nepal18386a82014-03-19 10:22:40 -070024import java.util.Collection;
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -070025import java.util.List;
Santos Cordon681663d2014-01-30 04:32:15 -080026import java.util.Map;
27import java.util.Set;
28
29/**
30 * Responsible for placing all outgoing calls. For each outgoing call, this class creates an
31 * instance of {@link OutgoingCallProcessor} which handles the details of connecting to the
Sailesh Nepale59bb192014-04-01 18:33:59 -070032 * appropriate call service and placing the call. This class maintains a mapping from call
Santos Cordon74d420b2014-05-07 14:38:47 -070033 * to {@link OutgoingCallProcessor} so that other classes (CallServiceAdapter, etc),
Santos Cordon681663d2014-01-30 04:32:15 -080034 * can simply call into this class instead of individual OutgoingCallProcessors.
35 */
36final class OutgoingCallsManager {
Santos Cordon681663d2014-01-30 04:32:15 -080037
38 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070039 * Maps call to {@link OutgoingCallProcessor}s.
Santos Cordon681663d2014-01-30 04:32:15 -080040 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070041 private Map<Call, OutgoingCallProcessor> mOutgoingCallProcessors = Maps.newHashMap();
Santos Cordon681663d2014-01-30 04:32:15 -080042
Santos Cordon681663d2014-01-30 04:32:15 -080043 /**
44 * Starts the process of placing a call by constructing an outgoing call processor and asking
45 * it to place the call. Upon success, execution will continue (via {@link CallServiceAdapter})
Ben Gilad13329fd2014-02-11 17:20:29 -080046 * to {@link #handleSuccessfulCallAttempt}. Upon failure, execution will return to
47 * {@link #handleFailedCallAttempt}.
Santos Cordon681663d2014-01-30 04:32:15 -080048 *
49 * @param call The call to place.
Santos Cordon74d420b2014-05-07 14:38:47 -070050 * @param callServices The collection of call services which can potentially place the call.
Santos Cordon681663d2014-01-30 04:32:15 -080051 * @param selectors The ordered list of selectors used in placing the call.
52 */
53 void placeCall(
Sailesh Nepal18386a82014-03-19 10:22:40 -070054 Call call,
Santos Cordon74d420b2014-05-07 14:38:47 -070055 Collection<CallServiceWrapper> callServices,
Sailesh Nepal18386a82014-03-19 10:22:40 -070056 Collection<CallServiceSelectorWrapper> selectors) {
Santos Cordon681663d2014-01-30 04:32:15 -080057
Sailesh Nepale59bb192014-04-01 18:33:59 -070058 Log.i(this, "Placing an outgoing call: %s", call);
Santos Cordon681663d2014-01-30 04:32:15 -080059
60 // Create the processor for this (outgoing) call and store it in a map such that call
61 // attempts can be aborted etc.
62 // TODO(gilad): Consider passing mSelector as an immutable set.
63 OutgoingCallProcessor processor =
Santos Cordon74d420b2014-05-07 14:38:47 -070064 new OutgoingCallProcessor(call, callServices, selectors, this);
Santos Cordon681663d2014-01-30 04:32:15 -080065
Sailesh Nepale59bb192014-04-01 18:33:59 -070066 mOutgoingCallProcessors.put(call, processor);
Santos Cordon681663d2014-01-30 04:32:15 -080067 processor.process();
68 }
69
70 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070071 * Forwards the compatibility status from the call-service to the corresponding outgoing-call
72 * processor.
Ben Gilad61925612014-03-11 19:06:36 -070073 *
Sailesh Nepale59bb192014-04-01 18:33:59 -070074 * @param isCompatible True if the call-service is compatible with the call.
Ben Gilad61925612014-03-11 19:06:36 -070075 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070076 void setIsCompatibleWith(Call call, boolean isCompatible) {
77 Log.v(this, "setIsCompatibleWith, call %s, isCompatible: %b", call, isCompatible);
78 OutgoingCallProcessor processor = mOutgoingCallProcessors.get(call);
Ben Gilad61925612014-03-11 19:06:36 -070079 if (processor == null) {
80 // Shouldn't happen, so log a wtf if it does.
81 Log.wtf(this, "Received unexpected setCompatibleWith notification.");
82 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -070083 processor.setIsCompatibleWith(call, isCompatible);
Ben Gilad61925612014-03-11 19:06:36 -070084 }
85 }
86
87 /**
Santos Cordon681663d2014-01-30 04:32:15 -080088 * Removes the outgoing call processor mapping for the successful call and returns execution to
Santos Cordon74d420b2014-05-07 14:38:47 -070089 * the call. This method is invoked from {@link CallServiceAdapter} after a call service
Santos Cordon681663d2014-01-30 04:32:15 -080090 * has notified Telecomm that it successfully placed the call.
Santos Cordon681663d2014-01-30 04:32:15 -080091 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070092 void handleSuccessfulCallAttempt(Call call) {
93 Log.v(this, "handleSuccessfulCallAttempt, call: %s", call);
94 OutgoingCallProcessor processor = mOutgoingCallProcessors.remove(call);
Santos Cordon681663d2014-01-30 04:32:15 -080095
96 if (processor == null) {
97 // Shouldn't happen, so log a wtf if it does.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080098 Log.wtf(this, "Received an unexpected placed-call notification.");
Santos Cordon681663d2014-01-30 04:32:15 -080099 } else {
100 processor.handleSuccessfulCallAttempt();
101 }
102 }
103
104 /**
105 * Notifies the appropriate outgoing call processor that a call attempt to place the call has
106 * failed and the processor should continue attempting to place the call with the next call
107 * service. This method is called from {@link CallServiceAdapter} after a call service has
108 * notified Telecomm that it could not place the call.
109 *
Santos Cordon681663d2014-01-30 04:32:15 -0800110 * @param reason The call-service supplied reason for the failed call attempt.
111 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700112 void handleFailedCallAttempt(Call call, String reason) {
113 Log.v(this, "handleFailedCallAttempt, call: %s, reason: %s", call, reason);
114 OutgoingCallProcessor processor = mOutgoingCallProcessors.get(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800115
Santos Cordon681663d2014-01-30 04:32:15 -0800116 if (processor == null) {
117 // Shouldn't happen, so log a wtf if it does.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800118 Log.wtf(this, "Received an unexpected failed-call notification.");
Santos Cordon681663d2014-01-30 04:32:15 -0800119 } else {
120 processor.handleFailedCallAttempt(reason);
121 }
122 }
123
124 /**
125 * Removes the outgoing call processor mapping for the failed call and returns execution to the
Santos Cordon74d420b2014-05-07 14:38:47 -0700126 * call. In contrast to handleFailedCallAttempt which comes from the call-service and
Santos Cordon681663d2014-01-30 04:32:15 -0800127 * goes to the outgoing-call processor indicating a single failed call attempt, this method is
128 * invoked by the outgoing-call processor to indicate that the entire process has failed and we
Santos Cordon74d420b2014-05-07 14:38:47 -0700129 * should cleanup and notify the call.
Santos Cordon681663d2014-01-30 04:32:15 -0800130 *
131 * @param call The failed outgoing call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700132 * @param isAborted True if the call timedout and is aborted.
Santos Cordon681663d2014-01-30 04:32:15 -0800133 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700134 void handleFailedOutgoingCall(Call call, boolean isAborted) {
135 Log.v(this, "handleFailedOutgoingCall, call: %s", call);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700136 mOutgoingCallProcessors.remove(call);
Santos Cordon74d420b2014-05-07 14:38:47 -0700137 call.handleFailedOutgoing(isAborted);
Santos Cordon681663d2014-01-30 04:32:15 -0800138 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800139
140 /**
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700141 * Forwards the selected call service from the selector to the corresponding outgoing-call
142 * processor.
143 */
144 void processSelectedCallServices(Call call, List<CallServiceDescriptor> descriptors) {
145 Log.v(this, "processSelectedCallServices, call %s, descriptors: %s", call, descriptors);
146 OutgoingCallProcessor processor = mOutgoingCallProcessors.get(call);
147 if (processor == null) {
148 // Shouldn't happen, so log a wtf if it does.
149 Log.wtf(this, "Received unexpected setSelectedCallServices notification.");
150 } else {
151 processor.processSelectedCallServices(descriptors);
152 }
153 }
154
155 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800156 * Aborts any ongoing attempts to connect the specified (outgoing) call.
157 *
158 * @param call The call to be aborted.
Santos Cordonc499c1c2014-04-14 17:13:14 -0700159 * @return False if the call was not found; True otherwise, indicating that the abort was
160 * successful.
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800161 */
Santos Cordonc499c1c2014-04-14 17:13:14 -0700162 boolean abort(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700163 OutgoingCallProcessor processor = mOutgoingCallProcessors.remove(call);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800164 if (processor != null) {
Santos Cordon74d420b2014-05-07 14:38:47 -0700165 Log.v(this, "abort, call: %s", call);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800166 processor.abort();
Santos Cordonc499c1c2014-04-14 17:13:14 -0700167 return true;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800168 }
Santos Cordonc499c1c2014-04-14 17:13:14 -0700169
170 return false;
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800171 }
Santos Cordon681663d2014-01-30 04:32:15 -0800172}