blob: 50c24ea2087e9b1d4850eb0008268ffec6a99818 [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 Nepala439e1b2014-03-11 18:19:58 -070019import com.android.internal.telecomm.ICallServiceSelector;
Santos Cordon681663d2014-01-30 04:32:15 -080020import com.google.common.collect.Maps;
21
Sailesh Nepal18386a82014-03-19 10:22:40 -070022import java.util.Collection;
Santos Cordon681663d2014-01-30 04:32:15 -080023import java.util.Map;
24import java.util.Set;
25
26/**
27 * Responsible for placing all outgoing calls. For each outgoing call, this class creates an
28 * instance of {@link OutgoingCallProcessor} which handles the details of connecting to the
29 * appropriate call service and placing the call. This class maintains a mapping from call ID
30 * to {@link OutgoingCallProcessor} so that other classes (Switchboard, CallServiceAdapter, etc),
31 * can simply call into this class instead of individual OutgoingCallProcessors.
32 */
33final class OutgoingCallsManager {
Santos Cordon681663d2014-01-30 04:32:15 -080034 private final Switchboard mSwitchboard;
35
36 /**
37 * Maps call IDs to {@link OutgoingCallProcessor}s.
38 */
39 private Map<String, OutgoingCallProcessor> mOutgoingCallProcessors = Maps.newHashMap();
40
41 /** Persists specified parameters. */
42 OutgoingCallsManager(Switchboard switchboard) {
43 mSwitchboard = switchboard;
44 }
45
46 /**
47 * Starts the process of placing a call by constructing an outgoing call processor and asking
48 * it to place the call. Upon success, execution will continue (via {@link CallServiceAdapter})
Ben Gilad13329fd2014-02-11 17:20:29 -080049 * to {@link #handleSuccessfulCallAttempt}. Upon failure, execution will return to
50 * {@link #handleFailedCallAttempt}.
Santos Cordon681663d2014-01-30 04:32:15 -080051 *
52 * @param call The call to place.
53 * @param callServices The set of call services which can potentially place the call.
54 * @param selectors The ordered list of selectors used in placing the call.
55 */
56 void placeCall(
Sailesh Nepal18386a82014-03-19 10:22:40 -070057 Call call,
58 Set<CallServiceWrapper> callServices,
59 Collection<CallServiceSelectorWrapper> selectors) {
Santos Cordon681663d2014-01-30 04:32:15 -080060
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080061 Log.i(this, "Placing an outgoing call (%s)", call.getId());
Santos Cordon681663d2014-01-30 04:32:15 -080062
63 // Create the processor for this (outgoing) call and store it in a map such that call
64 // attempts can be aborted etc.
65 // TODO(gilad): Consider passing mSelector as an immutable set.
66 OutgoingCallProcessor processor =
67 new OutgoingCallProcessor(call, callServices, selectors, this, mSwitchboard);
68
69 mOutgoingCallProcessors.put(call.getId(), processor);
70 processor.process();
71 }
72
73 /**
Ben Gilad61925612014-03-11 19:06:36 -070074 * Forwards the compatibility status from the call-service implementation to the corresponding
75 * outgoing-call processor.
76 *
77 * @param callId The ID of the call.
78 * @param isCompatible True if the call-service is compatible with the corresponding call and
79 * false otherwise.
80 */
Evan Charlton771ac972014-03-13 07:44:44 -070081 void setIsCompatibleWith(String callId, boolean isCompatible) {
Sailesh Nepal810735e2014-03-18 18:15:46 -070082 Log.v(this, "setIsCompatibleWith, callId %s, isCompatible: %b", callId, isCompatible);
Ben Gilad61925612014-03-11 19:06:36 -070083 OutgoingCallProcessor processor = mOutgoingCallProcessors.get(callId);
84 if (processor == null) {
85 // Shouldn't happen, so log a wtf if it does.
86 Log.wtf(this, "Received unexpected setCompatibleWith notification.");
87 } else {
Evan Charlton771ac972014-03-13 07:44:44 -070088 processor.setIsCompatibleWith(callId, isCompatible);
Ben Gilad61925612014-03-11 19:06:36 -070089 }
90 }
91
92 /**
Santos Cordon681663d2014-01-30 04:32:15 -080093 * Removes the outgoing call processor mapping for the successful call and returns execution to
94 * the switchboard. This method is invoked from {@link CallServiceAdapter} after a call service
95 * has notified Telecomm that it successfully placed the call.
96 *
97 * @param callId The ID of the call.
98 */
99 void handleSuccessfulCallAttempt(String callId) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700100 Log.v(this, "handleSuccessfulCallAttempt, callId: %s", callId);
Santos Cordon681663d2014-01-30 04:32:15 -0800101 OutgoingCallProcessor processor = mOutgoingCallProcessors.remove(callId);
102
103 if (processor == null) {
104 // Shouldn't happen, so log a wtf if it does.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800105 Log.wtf(this, "Received an unexpected placed-call notification.");
Santos Cordon681663d2014-01-30 04:32:15 -0800106 } else {
107 processor.handleSuccessfulCallAttempt();
108 }
109 }
110
111 /**
112 * Notifies the appropriate outgoing call processor that a call attempt to place the call has
113 * failed and the processor should continue attempting to place the call with the next call
114 * service. This method is called from {@link CallServiceAdapter} after a call service has
115 * notified Telecomm that it could not place the call.
116 *
117 * @param callId The ID of the failed outgoing call.
118 * @param reason The call-service supplied reason for the failed call attempt.
119 */
120 void handleFailedCallAttempt(String callId, String reason) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700121 Log.v(this, "handleFailedCallAttempt, callId: %s, reason: %s", callId, reason);
Santos Cordon681663d2014-01-30 04:32:15 -0800122 OutgoingCallProcessor processor = mOutgoingCallProcessors.get(callId);
123
124 // TODO(santoscordon): Consider combining the check here and in handleSuccessfulCallAttempt.
125 if (processor == null) {
126 // Shouldn't happen, so log a wtf if it does.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800127 Log.wtf(this, "Received an unexpected failed-call notification.");
Santos Cordon681663d2014-01-30 04:32:15 -0800128 } else {
129 processor.handleFailedCallAttempt(reason);
130 }
131 }
132
133 /**
134 * Removes the outgoing call processor mapping for the failed call and returns execution to the
135 * switchboard. In contrast to handleFailedCallAttempt which comes from the call-service and
136 * goes to the outgoing-call processor indicating a single failed call attempt, this method is
137 * invoked by the outgoing-call processor to indicate that the entire process has failed and we
138 * should cleanup and notify Switchboard.
139 *
140 * @param call The failed outgoing call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700141 * @param isAborted True if the call timedout and is aborted.
Santos Cordon681663d2014-01-30 04:32:15 -0800142 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700143 void handleFailedOutgoingCall(Call call, boolean isAborted) {
144 Log.v(this, "handleFailedOutgoingCall, call: %s", call);
Santos Cordon681663d2014-01-30 04:32:15 -0800145 mOutgoingCallProcessors.remove(call.getId());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700146 mSwitchboard.handleFailedOutgoingCall(call, isAborted);
Santos Cordon681663d2014-01-30 04:32:15 -0800147 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800148
149 /**
150 * Aborts any ongoing attempts to connect the specified (outgoing) call.
151 *
152 * @param call The call to be aborted.
153 */
154 void abort(Call call) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700155 Log.v(this, "abort, call: %s", call);
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800156 OutgoingCallProcessor processor = mOutgoingCallProcessors.remove(call.getId());
157 if (processor != null) {
158 processor.abort();
159 }
160 }
Santos Cordon681663d2014-01-30 04:32:15 -0800161}