Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 17 | package com.android.telecomm; |
| 18 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 19 | import android.os.Handler; |
| 20 | import android.os.Looper; |
| 21 | import android.telecomm.CallInfo; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 22 | |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 23 | import com.android.internal.telecomm.ICallServiceAdapter; |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 24 | import com.google.android.collect.Sets; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 25 | import com.google.common.base.Strings; |
| 26 | |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 27 | import java.util.Set; |
| 28 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 29 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 30 | * Used by call services in order to update state and control calls while the call service is bound |
| 31 | * to Telecomm. Each call service is given its own instance for the lifetime of the binding between |
| 32 | * Telecomm and the call service. |
| 33 | * TODO(santoscordon): Whenever we get any method invocations from the call service, we need to |
| 34 | * check that the invocation is expected from that call service. |
| 35 | * TODO(santoscordon): Move away from Runnable objects and into messages so that we create fewer |
| 36 | * objects per IPC method call. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 37 | * TODO(santoscordon): Do we need Binder.clear/restoreCallingIdentity() in the service methods? |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 38 | */ |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 39 | public final class CallServiceAdapter extends ICallServiceAdapter.Stub { |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 40 | private final CallsManager mCallsManager; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 41 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 42 | private final OutgoingCallsManager mOutgoingCallsManager; |
| 43 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 44 | private final IncomingCallsManager mIncomingCallsManager; |
| 45 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 46 | /** Used to run code (e.g. messages, Runnables) on the main (UI) thread. */ |
| 47 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
| 48 | |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 49 | /** |
| 50 | * The set of pending outgoing call IDs. Any {@link #handleSuccessfulOutgoingCall} and |
| 51 | * {@link #handleFailedOutgoingCall} invocations with a call ID that is not in this set |
| 52 | * are ignored. |
| 53 | */ |
| 54 | private final Set<String> mPendingOutgoingCallIds = Sets.newHashSet(); |
| 55 | |
| 56 | /** |
| 57 | * The set of pending incoming call IDs. Any {@link #handleIncomingCall} invocations with |
| 58 | * a call ID not in this set are ignored. |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 59 | */ |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 60 | private final Set<String> mPendingIncomingCallIds = Sets.newHashSet(); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 61 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 62 | /** |
| 63 | * Persists the specified parameters. |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 64 | * |
| 65 | * @param outgoingCallsManager Manages the placing of outgoing calls. |
| 66 | * @param incomingCallsManager Manages the incoming call initialization flow. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 67 | */ |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 68 | CallServiceAdapter( |
| 69 | OutgoingCallsManager outgoingCallsManager, IncomingCallsManager incomingCallsManager) { |
| 70 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 71 | mCallsManager = CallsManager.getInstance(); |
| 72 | mOutgoingCallsManager = outgoingCallsManager; |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 73 | mIncomingCallsManager = incomingCallsManager; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** {@inheritDoc} */ |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame^] | 77 | @Override public void setIsCompatibleWith(final String callId, final boolean isCompatible) { |
| 78 | checkValidCallId(callId); |
| 79 | mHandler.post(new Runnable() { |
| 80 | @Override public void run() { |
| 81 | if (mPendingOutgoingCallIds.contains(callId)) { |
| 82 | mOutgoingCallsManager.setIsCompatibleWith(callId, isCompatible); |
| 83 | } else { |
| 84 | Log.wtf(CallServiceAdapter.this, "Unknown outgoing call: %s", callId); |
| 85 | } |
| 86 | } |
| 87 | }); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 90 | /** {@inheritDoc} */ |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 91 | @Override public void notifyIncomingCall(final CallInfo callInfo) { |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 92 | checkValidCallId(callInfo.getId()); |
| 93 | mHandler.post(new Runnable() { |
| 94 | @Override public void run() { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 95 | if (mPendingIncomingCallIds.remove(callInfo.getId())) { |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 96 | mIncomingCallsManager.handleSuccessfulIncomingCall(callInfo); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 97 | } else { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame^] | 98 | Log.wtf(CallServiceAdapter.this, "Unknown incoming call: %s", callInfo); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | }); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** {@inheritDoc} */ |
| 105 | @Override public void handleSuccessfulOutgoingCall(final String callId) { |
| 106 | checkValidCallId(callId); |
| 107 | mHandler.post(new Runnable() { |
| 108 | @Override public void run() { |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 109 | if (mPendingOutgoingCallIds.remove(callId)) { |
| 110 | mOutgoingCallsManager.handleSuccessfulCallAttempt(callId); |
| 111 | } else { |
| 112 | // TODO(gilad): Figure out how to wire up the callService.abort() call. |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame^] | 113 | Log.wtf(CallServiceAdapter.this, "Unknown outgoing call: %s", callId); |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 114 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 115 | } |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | /** {@inheritDoc} */ |
| 120 | @Override public void handleFailedOutgoingCall(final String callId, final String reason) { |
| 121 | checkValidCallId(callId); |
| 122 | mHandler.post(new Runnable() { |
| 123 | @Override public void run() { |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 124 | if (mPendingOutgoingCallIds.remove(callId)) { |
| 125 | mOutgoingCallsManager.handleFailedCallAttempt(callId, reason); |
| 126 | } else { |
Ben Gilad | 6192561 | 2014-03-11 19:06:36 -0700 | [diff] [blame^] | 127 | Log.wtf(CallServiceAdapter.this, "Unknown outgoing call: %s", callId); |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 128 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 129 | } |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | /** {@inheritDoc} */ |
| 134 | @Override public void setActive(final String callId) { |
| 135 | checkValidCallId(callId); |
| 136 | mHandler.post(new Runnable() { |
| 137 | @Override public void run() { |
| 138 | mCallsManager.markCallAsActive(callId); |
| 139 | } |
| 140 | }); |
| 141 | } |
| 142 | |
| 143 | /** {@inheritDoc} */ |
| 144 | @Override public void setRinging(final String callId) { |
| 145 | checkValidCallId(callId); |
| 146 | mHandler.post(new Runnable() { |
| 147 | @Override public void run() { |
| 148 | mCallsManager.markCallAsRinging(callId); |
| 149 | } |
| 150 | }); |
| 151 | } |
| 152 | |
| 153 | /** {@inheritDoc} */ |
| 154 | @Override public void setDialing(final String callId) { |
| 155 | checkValidCallId(callId); |
| 156 | mHandler.post(new Runnable() { |
| 157 | @Override public void run() { |
| 158 | mCallsManager.markCallAsDialing(callId); |
| 159 | } |
| 160 | }); |
| 161 | } |
| 162 | |
| 163 | /** {@inheritDoc} */ |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 164 | // TODO(gilad): Ensure that any communication from the underlying ICallService |
| 165 | // implementation is expected (or otherwise suppressed at the adapter level). |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 166 | @Override public void setDisconnected(final String callId) { |
| 167 | checkValidCallId(callId); |
| 168 | mHandler.post(new Runnable() { |
| 169 | @Override public void run() { |
| 170 | mCallsManager.markCallAsDisconnected(callId); |
| 171 | } |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | /** |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 176 | * Adds the specified call ID to the list of pending outgoing call IDs. |
| 177 | * TODO(gilad): Consider passing the call processor (instead of the ID) both here and in the |
| 178 | * remove case (same for incoming) such that the detour via the *CallsManager can be avoided. |
| 179 | * |
| 180 | * @param callId The ID of the call. |
| 181 | */ |
| 182 | void addPendingOutgoingCallId(String callId) { |
| 183 | mPendingOutgoingCallIds.add(callId); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Removes the specified call ID from the list of pending outgoing call IDs. |
| 188 | * |
| 189 | * @param callId The ID of the call. |
| 190 | */ |
| 191 | void removePendingOutgoingCallId(String callId) { |
| 192 | mPendingOutgoingCallIds.remove(callId); |
| 193 | } |
| 194 | |
| 195 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 196 | * Adds a call ID to the list of pending incoming call IDs. Only calls with call IDs in the |
| 197 | * list will be handled by {@link #handleIncomingCall}. |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 198 | * |
| 199 | * @param callId The ID of the call. |
| 200 | */ |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 201 | void addPendingIncomingCallId(String callId) { |
| 202 | mPendingIncomingCallIds.add(callId); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /** |
Ben Gilad | 28e8ad6 | 2014-03-06 17:01:54 -0800 | [diff] [blame] | 206 | * Removes the specified call ID from the list of pending incoming call IDs. |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 207 | * |
| 208 | * @param callId The ID of the call. |
| 209 | */ |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 210 | void removePendingIncomingCallId(String callId) { |
| 211 | mPendingIncomingCallIds.remove(callId); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 215 | * Throws an IllegalArgumentException if the specified call ID is invalid. |
| 216 | * |
| 217 | * @param callId The call ID to check. |
| 218 | */ |
| 219 | private void checkValidCallId(String callId) { |
| 220 | if (Strings.isNullOrEmpty(callId)) { |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 221 | throw new IllegalArgumentException("Invalid call ID."); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 224 | } |