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