blob: c37665f20b2c686f348a5a2e078895a8fd90e5bb [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
22import java.util.List;
23import 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(
Santos Cordonc195e362014-02-11 17:05:31 -080057 Call call, Set<CallServiceWrapper> callServices, List<ICallServiceSelector> selectors) {
Santos Cordon681663d2014-01-30 04:32:15 -080058
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080059 Log.i(this, "Placing an outgoing call (%s)", call.getId());
Santos Cordon681663d2014-01-30 04:32:15 -080060
61 // Create the processor for this (outgoing) call and store it in a map such that call
62 // attempts can be aborted etc.
63 // TODO(gilad): Consider passing mSelector as an immutable set.
64 OutgoingCallProcessor processor =
65 new OutgoingCallProcessor(call, callServices, selectors, this, mSwitchboard);
66
67 mOutgoingCallProcessors.put(call.getId(), processor);
68 processor.process();
69 }
70
71 /**
Ben Gilad61925612014-03-11 19:06:36 -070072 * Forwards the compatibility status from the call-service implementation to the corresponding
73 * outgoing-call processor.
74 *
75 * @param callId The ID of the call.
76 * @param isCompatible True if the call-service is compatible with the corresponding call and
77 * false otherwise.
78 */
79 void setCompatibleWith(String callId, boolean isCompatible) {
80 OutgoingCallProcessor processor = mOutgoingCallProcessors.get(callId);
81 if (processor == null) {
82 // Shouldn't happen, so log a wtf if it does.
83 Log.wtf(this, "Received unexpected setCompatibleWith notification.");
84 } else {
85 processor.setCompatibleWith(callId, isCompatible);
86 }
87 }
88
89 /**
Santos Cordon681663d2014-01-30 04:32:15 -080090 * Removes the outgoing call processor mapping for the successful call and returns execution to
91 * the switchboard. This method is invoked from {@link CallServiceAdapter} after a call service
92 * has notified Telecomm that it successfully placed the call.
93 *
94 * @param callId The ID of the call.
95 */
96 void handleSuccessfulCallAttempt(String callId) {
97 OutgoingCallProcessor processor = mOutgoingCallProcessors.remove(callId);
98
99 if (processor == null) {
100 // Shouldn't happen, so log a wtf if it does.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800101 Log.wtf(this, "Received an unexpected placed-call notification.");
Santos Cordon681663d2014-01-30 04:32:15 -0800102 } else {
103 processor.handleSuccessfulCallAttempt();
104 }
105 }
106
107 /**
108 * Notifies the appropriate outgoing call processor that a call attempt to place the call has
109 * failed and the processor should continue attempting to place the call with the next call
110 * service. This method is called from {@link CallServiceAdapter} after a call service has
111 * notified Telecomm that it could not place the call.
112 *
113 * @param callId The ID of the failed outgoing call.
114 * @param reason The call-service supplied reason for the failed call attempt.
115 */
116 void handleFailedCallAttempt(String callId, String reason) {
117 OutgoingCallProcessor processor = mOutgoingCallProcessors.get(callId);
118
119 // TODO(santoscordon): Consider combining the check here and in handleSuccessfulCallAttempt.
120 if (processor == null) {
121 // Shouldn't happen, so log a wtf if it does.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800122 Log.wtf(this, "Received an unexpected failed-call notification.");
Santos Cordon681663d2014-01-30 04:32:15 -0800123 } else {
124 processor.handleFailedCallAttempt(reason);
125 }
126 }
127
128 /**
129 * Removes the outgoing call processor mapping for the failed call and returns execution to the
130 * switchboard. In contrast to handleFailedCallAttempt which comes from the call-service and
131 * goes to the outgoing-call processor indicating a single failed call attempt, this method is
132 * invoked by the outgoing-call processor to indicate that the entire process has failed and we
133 * should cleanup and notify Switchboard.
134 *
135 * @param call The failed outgoing call.
136 */
137 void handleFailedOutgoingCall(Call call) {
138 mOutgoingCallProcessors.remove(call.getId());
139 mSwitchboard.handleFailedOutgoingCall(call);
140 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800141
142 /**
143 * Aborts any ongoing attempts to connect the specified (outgoing) call.
144 *
145 * @param call The call to be aborted.
146 */
147 void abort(Call call) {
148 OutgoingCallProcessor processor = mOutgoingCallProcessors.remove(call.getId());
149 if (processor != null) {
150 processor.abort();
151 }
152 }
Santos Cordon681663d2014-01-30 04:32:15 -0800153}