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; |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 23 | import android.util.Log; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 24 | |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 25 | import com.google.android.collect.Sets; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 26 | import com.google.common.base.Strings; |
| 27 | |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 28 | import java.util.Set; |
| 29 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 30 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 31 | * Used by call services in order to update state and control calls while the call service is bound |
| 32 | * to Telecomm. Each call service is given its own instance for the lifetime of the binding between |
| 33 | * Telecomm and the call service. |
| 34 | * TODO(santoscordon): Whenever we get any method invocations from the call service, we need to |
| 35 | * check that the invocation is expected from that call service. |
| 36 | * TODO(santoscordon): Move away from Runnable objects and into messages so that we create fewer |
| 37 | * objects per IPC method call. |
Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 38 | * TODO(santoscordon): Do we need Binder.clear/restoreCallingIdentity() in the service methods? |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 39 | */ |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 40 | public final class CallServiceAdapter extends ICallServiceAdapter.Stub { |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 41 | private static final String TAG = CallServiceAdapter.class.getSimpleName(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 42 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 43 | private final CallsManager mCallsManager; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 44 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 45 | private final OutgoingCallsManager mOutgoingCallsManager; |
| 46 | |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame^] | 47 | private final IncomingCallsManager mIncomingCallsManager; |
| 48 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 49 | /** Used to run code (e.g. messages, Runnables) on the main (UI) thread. */ |
| 50 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
| 51 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 52 | /** The set of pending incoming call IDs. Contains the call IDs for which we are expecting |
| 53 | * details via {@link #handleIncomingCall}. If {@link #handleIncomingCall} is invoked for a call |
| 54 | * ID that is not in this set, it will be ignored. |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 55 | */ |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 56 | private final Set<String> mPendingIncomingCallIds = Sets.newHashSet(); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 57 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Persists the specified parameters. |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame^] | 60 | * |
| 61 | * @param outgoingCallsManager Manages the placing of outgoing calls. |
| 62 | * @param incomingCallsManager Manages the incoming call initialization flow. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 63 | */ |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame^] | 64 | CallServiceAdapter( |
| 65 | OutgoingCallsManager outgoingCallsManager, IncomingCallsManager incomingCallsManager) { |
| 66 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 67 | mCallsManager = CallsManager.getInstance(); |
| 68 | mOutgoingCallsManager = outgoingCallsManager; |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame^] | 69 | mIncomingCallsManager = incomingCallsManager; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** {@inheritDoc} */ |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 73 | @Override public void setCompatibleWith(String callId, boolean isCompatible) { |
| 74 | // TODO(santoscordon): fill in. |
| 75 | } |
| 76 | |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 77 | /** {@inheritDoc} */ |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 78 | @Override public void handleIncomingCall(final CallInfo callInfo) { |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 79 | checkValidCallId(callInfo.getId()); |
| 80 | mHandler.post(new Runnable() { |
| 81 | @Override public void run() { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 82 | if (mPendingIncomingCallIds.remove(callInfo.getId())) { |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame^] | 83 | mIncomingCallsManager.handleSuccessfulIncomingCall(callInfo); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 84 | } else { |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 85 | Log.wtf(TAG, "Received details for an unknown incoming call " + callInfo); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | }); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** {@inheritDoc} */ |
| 92 | @Override public void handleSuccessfulOutgoingCall(final String callId) { |
| 93 | checkValidCallId(callId); |
| 94 | mHandler.post(new Runnable() { |
| 95 | @Override public void run() { |
| 96 | mOutgoingCallsManager.handleSuccessfulCallAttempt(callId); |
| 97 | } |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | /** {@inheritDoc} */ |
| 102 | @Override public void handleFailedOutgoingCall(final String callId, final String reason) { |
| 103 | checkValidCallId(callId); |
| 104 | mHandler.post(new Runnable() { |
| 105 | @Override public void run() { |
| 106 | mOutgoingCallsManager.handleFailedCallAttempt(callId, reason); |
| 107 | } |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | /** {@inheritDoc} */ |
| 112 | @Override public void setActive(final String callId) { |
| 113 | checkValidCallId(callId); |
| 114 | mHandler.post(new Runnable() { |
| 115 | @Override public void run() { |
| 116 | mCallsManager.markCallAsActive(callId); |
| 117 | } |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | /** {@inheritDoc} */ |
| 122 | @Override public void setRinging(final String callId) { |
| 123 | checkValidCallId(callId); |
| 124 | mHandler.post(new Runnable() { |
| 125 | @Override public void run() { |
| 126 | mCallsManager.markCallAsRinging(callId); |
| 127 | } |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | /** {@inheritDoc} */ |
| 132 | @Override public void setDialing(final String callId) { |
| 133 | checkValidCallId(callId); |
| 134 | mHandler.post(new Runnable() { |
| 135 | @Override public void run() { |
| 136 | mCallsManager.markCallAsDialing(callId); |
| 137 | } |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | /** {@inheritDoc} */ |
| 142 | @Override public void setDisconnected(final String callId) { |
| 143 | checkValidCallId(callId); |
| 144 | mHandler.post(new Runnable() { |
| 145 | @Override public void run() { |
| 146 | mCallsManager.markCallAsDisconnected(callId); |
| 147 | } |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 152 | * Adds a call ID to the list of pending incoming call IDs. Only calls with call IDs in the |
| 153 | * list will be handled by {@link #handleIncomingCall}. |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 154 | * |
| 155 | * @param callId The ID of the call. |
| 156 | */ |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 157 | void addPendingIncomingCallId(String callId) { |
| 158 | mPendingIncomingCallIds.add(callId); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 162 | * Removed a call ID from the list of pending incoming call IDs. |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 163 | * |
| 164 | * @param callId The ID of the call. |
| 165 | */ |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 166 | void removePendingIncomingCallId(String callId) { |
| 167 | mPendingIncomingCallIds.remove(callId); |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 171 | * Throws an IllegalArgumentException if the specified call ID is invalid. |
| 172 | * |
| 173 | * @param callId The call ID to check. |
| 174 | */ |
| 175 | private void checkValidCallId(String callId) { |
| 176 | if (Strings.isNullOrEmpty(callId)) { |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame] | 177 | throw new IllegalArgumentException("Invalid call ID."); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 178 | } |
| 179 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 180 | } |