blob: def5211744d6246e57eb7810bfdb10d175b330e4 [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 Cordon681663d2014-01-30 04:32:15 -080033 * to {@link OutgoingCallProcessor} so that other classes (Switchboard, CallServiceAdapter, etc),
34 * can simply call into this class instead of individual OutgoingCallProcessors.
35 */
36final class OutgoingCallsManager {
Santos Cordon681663d2014-01-30 04:32:15 -080037 private final Switchboard mSwitchboard;
38
39 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070040 * Maps call to {@link OutgoingCallProcessor}s.
Santos Cordon681663d2014-01-30 04:32:15 -080041 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070042 private Map<Call, OutgoingCallProcessor> mOutgoingCallProcessors = Maps.newHashMap();
Santos Cordon681663d2014-01-30 04:32:15 -080043
44 /** Persists specified parameters. */
45 OutgoingCallsManager(Switchboard switchboard) {
46 mSwitchboard = switchboard;
47 }
48
49 /**
50 * Starts the process of placing a call by constructing an outgoing call processor and asking
51 * it to place the call. Upon success, execution will continue (via {@link CallServiceAdapter})
Ben Gilad13329fd2014-02-11 17:20:29 -080052 * to {@link #handleSuccessfulCallAttempt}. Upon failure, execution will return to
53 * {@link #handleFailedCallAttempt}.
Santos Cordon681663d2014-01-30 04:32:15 -080054 *
55 * @param call The call to place.
56 * @param callServices The set of call services which can potentially place the call.
57 * @param selectors The ordered list of selectors used in placing the call.
58 */
59 void placeCall(
Sailesh Nepal18386a82014-03-19 10:22:40 -070060 Call call,
61 Set<CallServiceWrapper> callServices,
62 Collection<CallServiceSelectorWrapper> selectors) {
Santos Cordon681663d2014-01-30 04:32:15 -080063
Sailesh Nepale59bb192014-04-01 18:33:59 -070064 Log.i(this, "Placing an outgoing call: %s", call);
Santos Cordon681663d2014-01-30 04:32:15 -080065
66 // Create the processor for this (outgoing) call and store it in a map such that call
67 // attempts can be aborted etc.
68 // TODO(gilad): Consider passing mSelector as an immutable set.
69 OutgoingCallProcessor processor =
70 new OutgoingCallProcessor(call, callServices, selectors, this, mSwitchboard);
71
Sailesh Nepale59bb192014-04-01 18:33:59 -070072 mOutgoingCallProcessors.put(call, processor);
Santos Cordon681663d2014-01-30 04:32:15 -080073 processor.process();
74 }
75
76 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070077 * Forwards the compatibility status from the call-service to the corresponding outgoing-call
78 * processor.
Ben Gilad61925612014-03-11 19:06:36 -070079 *
Sailesh Nepale59bb192014-04-01 18:33:59 -070080 * @param isCompatible True if the call-service is compatible with the call.
Ben Gilad61925612014-03-11 19:06:36 -070081 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070082 void setIsCompatibleWith(Call call, boolean isCompatible) {
83 Log.v(this, "setIsCompatibleWith, call %s, isCompatible: %b", call, isCompatible);
84 OutgoingCallProcessor processor = mOutgoingCallProcessors.get(call);
Ben Gilad61925612014-03-11 19:06:36 -070085 if (processor == null) {
86 // Shouldn't happen, so log a wtf if it does.
87 Log.wtf(this, "Received unexpected setCompatibleWith notification.");
88 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -070089 processor.setIsCompatibleWith(call, isCompatible);
Ben Gilad61925612014-03-11 19:06:36 -070090 }
91 }
92
93 /**
Santos Cordon681663d2014-01-30 04:32:15 -080094 * Removes the outgoing call processor mapping for the successful call and returns execution to
95 * the switchboard. This method is invoked from {@link CallServiceAdapter} after a call service
96 * has notified Telecomm that it successfully placed the call.
Santos Cordon681663d2014-01-30 04:32:15 -080097 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070098 void handleSuccessfulCallAttempt(Call call) {
99 Log.v(this, "handleSuccessfulCallAttempt, call: %s", call);
100 OutgoingCallProcessor processor = mOutgoingCallProcessors.remove(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800101
102 if (processor == null) {
103 // Shouldn't happen, so log a wtf if it does.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800104 Log.wtf(this, "Received an unexpected placed-call notification.");
Santos Cordon681663d2014-01-30 04:32:15 -0800105 } else {
106 processor.handleSuccessfulCallAttempt();
107 }
108 }
109
110 /**
111 * Notifies the appropriate outgoing call processor that a call attempt to place the call has
112 * failed and the processor should continue attempting to place the call with the next call
113 * service. This method is called from {@link CallServiceAdapter} after a call service has
114 * notified Telecomm that it could not place the call.
115 *
Santos Cordon681663d2014-01-30 04:32:15 -0800116 * @param reason The call-service supplied reason for the failed call attempt.
117 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700118 void handleFailedCallAttempt(Call call, String reason) {
119 Log.v(this, "handleFailedCallAttempt, call: %s, reason: %s", call, reason);
120 OutgoingCallProcessor processor = mOutgoingCallProcessors.get(call);
Santos Cordon681663d2014-01-30 04:32:15 -0800121
Santos Cordon681663d2014-01-30 04:32:15 -0800122 if (processor == null) {
123 // Shouldn't happen, so log a wtf if it does.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800124 Log.wtf(this, "Received an unexpected failed-call notification.");
Santos Cordon681663d2014-01-30 04:32:15 -0800125 } else {
126 processor.handleFailedCallAttempt(reason);
127 }
128 }
129
130 /**
131 * Removes the outgoing call processor mapping for the failed call and returns execution to the
132 * switchboard. In contrast to handleFailedCallAttempt which comes from the call-service and
133 * goes to the outgoing-call processor indicating a single failed call attempt, this method is
134 * invoked by the outgoing-call processor to indicate that the entire process has failed and we
135 * should cleanup and notify Switchboard.
136 *
137 * @param call The failed outgoing call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700138 * @param isAborted True if the call timedout and is aborted.
Santos Cordon681663d2014-01-30 04:32:15 -0800139 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700140 void handleFailedOutgoingCall(Call call, boolean isAborted) {
141 Log.v(this, "handleFailedOutgoingCall, call: %s", call);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700142 mOutgoingCallProcessors.remove(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700143 mSwitchboard.handleFailedOutgoingCall(call, isAborted);
Santos Cordon681663d2014-01-30 04:32:15 -0800144 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800145
146 /**
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700147 * Forwards the selected call service from the selector to the corresponding outgoing-call
148 * processor.
149 */
150 void processSelectedCallServices(Call call, List<CallServiceDescriptor> descriptors) {
151 Log.v(this, "processSelectedCallServices, call %s, descriptors: %s", call, descriptors);
152 OutgoingCallProcessor processor = mOutgoingCallProcessors.get(call);
153 if (processor == null) {
154 // Shouldn't happen, so log a wtf if it does.
155 Log.wtf(this, "Received unexpected setSelectedCallServices notification.");
156 } else {
157 processor.processSelectedCallServices(descriptors);
158 }
159 }
160
161 /**
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800162 * Aborts any ongoing attempts to connect the specified (outgoing) call.
163 *
164 * @param call The call to be aborted.
165 */
166 void abort(Call call) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700167 Log.v(this, "abort, call: %s", call);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700168 OutgoingCallProcessor processor = mOutgoingCallProcessors.remove(call);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800169 if (processor != null) {
170 processor.abort();
171 }
172 }
Santos Cordon681663d2014-01-30 04:32:15 -0800173}