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