blob: f50f765df16734c4f53be7d01019113e46221ae5 [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
Santos Cordon681663d2014-01-30 04:32:15 -080019import android.telecomm.ICallServiceSelector;
Santos Cordon681663d2014-01-30 04:32:15 -080020
Santos Cordon681663d2014-01-30 04:32:15 -080021import com.google.common.collect.Maps;
22
23import java.util.List;
24import java.util.Map;
25import java.util.Set;
26
27/**
28 * Responsible for placing all outgoing calls. For each outgoing call, this class creates an
29 * instance of {@link OutgoingCallProcessor} which handles the details of connecting to the
30 * appropriate call service and placing the call. This class maintains a mapping from call ID
31 * to {@link OutgoingCallProcessor} so that other classes (Switchboard, CallServiceAdapter, etc),
32 * can simply call into this class instead of individual OutgoingCallProcessors.
33 */
34final class OutgoingCallsManager {
Santos Cordon681663d2014-01-30 04:32:15 -080035 private final Switchboard mSwitchboard;
36
37 /**
38 * Maps call IDs to {@link OutgoingCallProcessor}s.
39 */
40 private Map<String, OutgoingCallProcessor> mOutgoingCallProcessors = Maps.newHashMap();
41
42 /** Persists specified parameters. */
43 OutgoingCallsManager(Switchboard switchboard) {
44 mSwitchboard = switchboard;
45 }
46
47 /**
48 * Starts the process of placing a call by constructing an outgoing call processor and asking
49 * it to place the call. Upon success, execution will continue (via {@link CallServiceAdapter})
Ben Gilad13329fd2014-02-11 17:20:29 -080050 * to {@link #handleSuccessfulCallAttempt}. Upon failure, execution will return to
51 * {@link #handleFailedCallAttempt}.
Santos Cordon681663d2014-01-30 04:32:15 -080052 *
53 * @param call The call to place.
54 * @param callServices The set of call services which can potentially place the call.
55 * @param selectors The ordered list of selectors used in placing the call.
56 */
57 void placeCall(
Santos Cordonc195e362014-02-11 17:05:31 -080058 Call call, Set<CallServiceWrapper> callServices, List<ICallServiceSelector> selectors) {
Santos Cordon681663d2014-01-30 04:32:15 -080059
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080060 Log.i(this, "Placing an outgoing call (%s)", call.getId());
Santos Cordon681663d2014-01-30 04:32:15 -080061
62 // Create the processor for this (outgoing) call and store it in a map such that call
63 // attempts can be aborted etc.
64 // TODO(gilad): Consider passing mSelector as an immutable set.
65 OutgoingCallProcessor processor =
66 new OutgoingCallProcessor(call, callServices, selectors, this, mSwitchboard);
67
68 mOutgoingCallProcessors.put(call.getId(), processor);
69 processor.process();
70 }
71
72 /**
73 * Removes the outgoing call processor mapping for the successful call and returns execution to
74 * the switchboard. This method is invoked from {@link CallServiceAdapter} after a call service
75 * has notified Telecomm that it successfully placed the call.
76 *
77 * @param callId The ID of the call.
78 */
79 void handleSuccessfulCallAttempt(String callId) {
80 OutgoingCallProcessor processor = mOutgoingCallProcessors.remove(callId);
81
82 if (processor == null) {
83 // Shouldn't happen, so log a wtf if it does.
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080084 Log.wtf(this, "Received an unexpected placed-call notification.");
Santos Cordon681663d2014-01-30 04:32:15 -080085 } else {
86 processor.handleSuccessfulCallAttempt();
87 }
88 }
89
90 /**
91 * Notifies the appropriate outgoing call processor that a call attempt to place the call has
92 * failed and the processor should continue attempting to place the call with the next call
93 * service. This method is called from {@link CallServiceAdapter} after a call service has
94 * notified Telecomm that it could not place the call.
95 *
96 * @param callId The ID of the failed outgoing call.
97 * @param reason The call-service supplied reason for the failed call attempt.
98 */
99 void handleFailedCallAttempt(String callId, String reason) {
100 OutgoingCallProcessor processor = mOutgoingCallProcessors.get(callId);
101
102 // TODO(santoscordon): Consider combining the check here and in handleSuccessfulCallAttempt.
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 failed-call notification.");
Santos Cordon681663d2014-01-30 04:32:15 -0800106 } else {
107 processor.handleFailedCallAttempt(reason);
108 }
109 }
110
111 /**
112 * Removes the outgoing call processor mapping for the failed call and returns execution to the
113 * switchboard. In contrast to handleFailedCallAttempt which comes from the call-service and
114 * goes to the outgoing-call processor indicating a single failed call attempt, this method is
115 * invoked by the outgoing-call processor to indicate that the entire process has failed and we
116 * should cleanup and notify Switchboard.
117 *
118 * @param call The failed outgoing call.
119 */
120 void handleFailedOutgoingCall(Call call) {
121 mOutgoingCallProcessors.remove(call.getId());
122 mSwitchboard.handleFailedOutgoingCall(call);
123 }
Ben Gilad8e55d1d2014-02-26 16:25:56 -0800124
125 /**
126 * Aborts any ongoing attempts to connect the specified (outgoing) call.
127 *
128 * @param call The call to be aborted.
129 */
130 void abort(Call call) {
131 OutgoingCallProcessor processor = mOutgoingCallProcessors.remove(call.getId());
132 if (processor != null) {
133 processor.abort();
134 }
135 }
Santos Cordon681663d2014-01-30 04:32:15 -0800136}