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 | |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 19 | import com.android.internal.telecomm.ICallServiceSelector; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
| 21 | |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 22 | import java.util.Collection; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 23 | import java.util.Map; |
| 24 | import 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 |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 29 | * appropriate call service and placing the call. This class maintains a mapping from call |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 30 | * to {@link OutgoingCallProcessor} so that other classes (Switchboard, CallServiceAdapter, etc), |
| 31 | * can simply call into this class instead of individual OutgoingCallProcessors. |
| 32 | */ |
| 33 | final class OutgoingCallsManager { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 34 | private final Switchboard mSwitchboard; |
| 35 | |
| 36 | /** |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 37 | * Maps call to {@link OutgoingCallProcessor}s. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 38 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 39 | private Map<Call, OutgoingCallProcessor> mOutgoingCallProcessors = Maps.newHashMap(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 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 Gilad | 13329fd | 2014-02-11 17:20:29 -0800 | [diff] [blame] | 49 | * to {@link #handleSuccessfulCallAttempt}. Upon failure, execution will return to |
| 50 | * {@link #handleFailedCallAttempt}. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 51 | * |
| 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( |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 57 | Call call, |
| 58 | Set<CallServiceWrapper> callServices, |
| 59 | Collection<CallServiceSelectorWrapper> selectors) { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 60 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 61 | Log.i(this, "Placing an outgoing call: %s", call); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 62 | |
| 63 | // Create the processor for this (outgoing) call and store it in a map such that call |
| 64 | // attempts can be aborted etc. |
| 65 | // TODO(gilad): Consider passing mSelector as an immutable set. |
| 66 | OutgoingCallProcessor processor = |
| 67 | new OutgoingCallProcessor(call, callServices, selectors, this, mSwitchboard); |
| 68 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 69 | mOutgoingCallProcessors.put(call, processor); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 70 | processor.process(); |
| 71 | } |
| 72 | |
| 73 | /** |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 74 | * Forwards the compatibility status from the call-service to the corresponding outgoing-call |
| 75 | * processor. |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 76 | * |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 77 | * @param isCompatible True if the call-service is compatible with the call. |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 78 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 79 | void setIsCompatibleWith(Call call, boolean isCompatible) { |
| 80 | Log.v(this, "setIsCompatibleWith, call %s, isCompatible: %b", call, isCompatible); |
| 81 | OutgoingCallProcessor processor = mOutgoingCallProcessors.get(call); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 82 | if (processor == null) { |
| 83 | // Shouldn't happen, so log a wtf if it does. |
| 84 | Log.wtf(this, "Received unexpected setCompatibleWith notification."); |
| 85 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 86 | processor.setIsCompatibleWith(call, isCompatible); |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 91 | * Removes the outgoing call processor mapping for the successful call and returns execution to |
| 92 | * the switchboard. This method is invoked from {@link CallServiceAdapter} after a call service |
| 93 | * has notified Telecomm that it successfully placed the call. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 94 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 95 | void handleSuccessfulCallAttempt(Call call) { |
| 96 | Log.v(this, "handleSuccessfulCallAttempt, call: %s", call); |
| 97 | OutgoingCallProcessor processor = mOutgoingCallProcessors.remove(call); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 98 | |
| 99 | if (processor == null) { |
| 100 | // Shouldn't happen, so log a wtf if it does. |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 101 | Log.wtf(this, "Received an unexpected placed-call notification."); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 102 | } 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 | * |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 113 | * @param reason The call-service supplied reason for the failed call attempt. |
| 114 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 115 | void handleFailedCallAttempt(Call call, String reason) { |
| 116 | Log.v(this, "handleFailedCallAttempt, call: %s, reason: %s", call, reason); |
| 117 | OutgoingCallProcessor processor = mOutgoingCallProcessors.get(call); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 118 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 119 | if (processor == null) { |
| 120 | // Shouldn't happen, so log a wtf if it does. |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 121 | Log.wtf(this, "Received an unexpected failed-call notification."); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 122 | } else { |
| 123 | processor.handleFailedCallAttempt(reason); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Removes the outgoing call processor mapping for the failed call and returns execution to the |
| 129 | * switchboard. In contrast to handleFailedCallAttempt which comes from the call-service and |
| 130 | * goes to the outgoing-call processor indicating a single failed call attempt, this method is |
| 131 | * invoked by the outgoing-call processor to indicate that the entire process has failed and we |
| 132 | * should cleanup and notify Switchboard. |
| 133 | * |
| 134 | * @param call The failed outgoing call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 135 | * @param isAborted True if the call timedout and is aborted. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 136 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 137 | void handleFailedOutgoingCall(Call call, boolean isAborted) { |
| 138 | Log.v(this, "handleFailedOutgoingCall, call: %s", call); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 139 | mOutgoingCallProcessors.remove(call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 140 | mSwitchboard.handleFailedOutgoingCall(call, isAborted); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 141 | } |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * Aborts any ongoing attempts to connect the specified (outgoing) call. |
| 145 | * |
| 146 | * @param call The call to be aborted. |
| 147 | */ |
| 148 | void abort(Call call) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 149 | Log.v(this, "abort, call: %s", call); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 150 | OutgoingCallProcessor processor = mOutgoingCallProcessors.remove(call); |
Ben Gilad | 8e55d1d | 2014-02-26 16:25:56 -0800 | [diff] [blame] | 151 | if (processor != null) { |
| 152 | processor.abort(); |
| 153 | } |
| 154 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 155 | } |