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