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; |
| 22 | import android.telecomm.ICallServiceAdapter; |
| 23 | |
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 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 49 | /** The set of pending incoming call IDs. Contains the call IDs for which we are expecting |
| 50 | * details via {@link #handleIncomingCall}. If {@link #handleIncomingCall} is invoked for a call |
| 51 | * ID that is not in this set, it will be ignored. |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 52 | */ |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 53 | private final Set<String> mPendingIncomingCallIds = Sets.newHashSet(); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 54 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Persists the specified parameters. |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 57 | * |
| 58 | * @param outgoingCallsManager Manages the placing of outgoing calls. |
| 59 | * @param incomingCallsManager Manages the incoming call initialization flow. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 60 | */ |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 61 | CallServiceAdapter( |
| 62 | OutgoingCallsManager outgoingCallsManager, IncomingCallsManager incomingCallsManager) { |
| 63 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 64 | mCallsManager = CallsManager.getInstance(); |
| 65 | mOutgoingCallsManager = outgoingCallsManager; |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 66 | mIncomingCallsManager = incomingCallsManager; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** {@inheritDoc} */ |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 70 | @Override public void setCompatibleWith(String callId, boolean isCompatible) { |
| 71 | // TODO(santoscordon): fill in. |
| 72 | } |
| 73 | |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 74 | /** {@inheritDoc} */ |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 75 | @Override public void handleIncomingCall(final CallInfo callInfo) { |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 76 | checkValidCallId(callInfo.getId()); |
| 77 | mHandler.post(new Runnable() { |
| 78 | @Override public void run() { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 79 | if (mPendingIncomingCallIds.remove(callInfo.getId())) { |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 80 | mIncomingCallsManager.handleSuccessfulIncomingCall(callInfo); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 81 | } else { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame^] | 82 | Log.wtf(CallServiceAdapter.this, |
| 83 | "Received details for an unknown incoming call %s", callInfo); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | }); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** {@inheritDoc} */ |
| 90 | @Override public void handleSuccessfulOutgoingCall(final String callId) { |
| 91 | checkValidCallId(callId); |
| 92 | mHandler.post(new Runnable() { |
| 93 | @Override public void run() { |
| 94 | mOutgoingCallsManager.handleSuccessfulCallAttempt(callId); |
| 95 | } |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | /** {@inheritDoc} */ |
| 100 | @Override public void handleFailedOutgoingCall(final String callId, final String reason) { |
| 101 | checkValidCallId(callId); |
| 102 | mHandler.post(new Runnable() { |
| 103 | @Override public void run() { |
| 104 | mOutgoingCallsManager.handleFailedCallAttempt(callId, reason); |
| 105 | } |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | /** {@inheritDoc} */ |
| 110 | @Override public void setActive(final String callId) { |
| 111 | checkValidCallId(callId); |
| 112 | mHandler.post(new Runnable() { |
| 113 | @Override public void run() { |
| 114 | mCallsManager.markCallAsActive(callId); |
| 115 | } |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | /** {@inheritDoc} */ |
| 120 | @Override public void setRinging(final String callId) { |
| 121 | checkValidCallId(callId); |
| 122 | mHandler.post(new Runnable() { |
| 123 | @Override public void run() { |
| 124 | mCallsManager.markCallAsRinging(callId); |
| 125 | } |
| 126 | }); |
| 127 | } |
| 128 | |
| 129 | /** {@inheritDoc} */ |
| 130 | @Override public void setDialing(final String callId) { |
| 131 | checkValidCallId(callId); |
| 132 | mHandler.post(new Runnable() { |
| 133 | @Override public void run() { |
| 134 | mCallsManager.markCallAsDialing(callId); |
| 135 | } |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | /** {@inheritDoc} */ |
| 140 | @Override public void setDisconnected(final String callId) { |
| 141 | checkValidCallId(callId); |
| 142 | mHandler.post(new Runnable() { |
| 143 | @Override public void run() { |
| 144 | mCallsManager.markCallAsDisconnected(callId); |
| 145 | } |
| 146 | }); |
| 147 | } |
| 148 | |
| 149 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 150 | * Adds a call ID to the list of pending incoming call IDs. Only calls with call IDs in the |
| 151 | * list will be handled by {@link #handleIncomingCall}. |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 152 | * |
| 153 | * @param callId The ID of the call. |
| 154 | */ |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 155 | void addPendingIncomingCallId(String callId) { |
| 156 | mPendingIncomingCallIds.add(callId); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 160 | * Removed a call ID from the list of pending incoming call IDs. |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 161 | * |
| 162 | * @param callId The ID of the call. |
| 163 | */ |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 164 | void removePendingIncomingCallId(String callId) { |
| 165 | mPendingIncomingCallIds.remove(callId); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 169 | * Throws an IllegalArgumentException if the specified call ID is invalid. |
| 170 | * |
| 171 | * @param callId The call ID to check. |
| 172 | */ |
| 173 | private void checkValidCallId(String callId) { |
| 174 | if (Strings.isNullOrEmpty(callId)) { |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 175 | throw new IllegalArgumentException("Invalid call ID."); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 176 | } |
| 177 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 178 | } |