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 | |
| 47 | /** Used to run code (e.g. messages, Runnables) on the main (UI) thread. */ |
| 48 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
| 49 | |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame^] | 50 | /** The list of unconfirmed incoming call IDs. Contains only IDs for incoming calls which are |
| 51 | * pending confirmation from the call service. Entries are added by the call service when a |
| 52 | * confirmation request is sent and removed when the confirmation is received or it times out. |
| 53 | * See {@link IncomingCallsManager} for more information about the incoming sequence and its |
| 54 | * timeouts. |
| 55 | */ |
| 56 | private final Set<String> mUnconfirmedIncomingCallIds = Sets.newHashSet(); |
| 57 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Persists the specified parameters. |
| 60 | */ |
| 61 | CallServiceAdapter(OutgoingCallsManager outgoingCallsManager) { |
| 62 | mCallsManager = CallsManager.getInstance(); |
| 63 | mOutgoingCallsManager = outgoingCallsManager; |
| 64 | } |
| 65 | |
| 66 | /** {@inheritDoc} */ |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 67 | @Override public void setCompatibleWith(String callId, boolean isCompatible) { |
| 68 | // TODO(santoscordon): fill in. |
| 69 | } |
| 70 | |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame^] | 71 | /** {@inheritDoc} */ |
| 72 | @Override public void handleConfirmedIncomingCall(final CallInfo callInfo) { |
| 73 | checkValidCallId(callInfo.getId()); |
| 74 | mHandler.post(new Runnable() { |
| 75 | @Override public void run() { |
| 76 | if (mUnconfirmedIncomingCallIds.remove(callInfo.getId())) { |
| 77 | // TODO(santoscordon): Uncomment when ready. |
| 78 | // mIncomingCallsManager.handleSuccessfulIncomingCall(callInfo); |
| 79 | } else { |
| 80 | Log.wtf(TAG, "Call service confirming unknown incoming call " + callInfo); |
| 81 | } |
| 82 | } |
| 83 | }); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** {@inheritDoc} */ |
| 87 | @Override public void handleSuccessfulOutgoingCall(final String callId) { |
| 88 | checkValidCallId(callId); |
| 89 | mHandler.post(new Runnable() { |
| 90 | @Override public void run() { |
| 91 | mOutgoingCallsManager.handleSuccessfulCallAttempt(callId); |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | /** {@inheritDoc} */ |
| 97 | @Override public void handleFailedOutgoingCall(final String callId, final String reason) { |
| 98 | checkValidCallId(callId); |
| 99 | mHandler.post(new Runnable() { |
| 100 | @Override public void run() { |
| 101 | mOutgoingCallsManager.handleFailedCallAttempt(callId, reason); |
| 102 | } |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | /** {@inheritDoc} */ |
| 107 | @Override public void setActive(final String callId) { |
| 108 | checkValidCallId(callId); |
| 109 | mHandler.post(new Runnable() { |
| 110 | @Override public void run() { |
| 111 | mCallsManager.markCallAsActive(callId); |
| 112 | } |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | /** {@inheritDoc} */ |
| 117 | @Override public void setRinging(final String callId) { |
| 118 | checkValidCallId(callId); |
| 119 | mHandler.post(new Runnable() { |
| 120 | @Override public void run() { |
| 121 | mCallsManager.markCallAsRinging(callId); |
| 122 | } |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | /** {@inheritDoc} */ |
| 127 | @Override public void setDialing(final String callId) { |
| 128 | checkValidCallId(callId); |
| 129 | mHandler.post(new Runnable() { |
| 130 | @Override public void run() { |
| 131 | mCallsManager.markCallAsDialing(callId); |
| 132 | } |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | /** {@inheritDoc} */ |
| 137 | @Override public void setDisconnected(final String callId) { |
| 138 | checkValidCallId(callId); |
| 139 | mHandler.post(new Runnable() { |
| 140 | @Override public void run() { |
| 141 | mCallsManager.markCallAsDisconnected(callId); |
| 142 | } |
| 143 | }); |
| 144 | } |
| 145 | |
| 146 | /** |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame^] | 147 | * Adds a call ID to the list of unconfirmed incoming call IDs. Only calls with call IDs in the |
| 148 | * list will be handled by {@link #handleConfirmedIncomingCall}. |
| 149 | * |
| 150 | * @param callId The ID of the call. |
| 151 | */ |
| 152 | void addUnconfirmedIncomingCallId(String callId) { |
| 153 | mUnconfirmedIncomingCallIds.add(callId); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Removed a call ID from the list of unconfirmed incoming call IDs. |
| 158 | * |
| 159 | * @param callId The ID of the call. |
| 160 | */ |
| 161 | void removeUnconfirmedIncomingCallId(String callId) { |
| 162 | mUnconfirmedIncomingCallIds.remove(callId); |
| 163 | } |
| 164 | |
| 165 | /** |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 166 | * Throws an IllegalArgumentException if the specified call ID is invalid. |
| 167 | * |
| 168 | * @param callId The call ID to check. |
| 169 | */ |
| 170 | private void checkValidCallId(String callId) { |
| 171 | if (Strings.isNullOrEmpty(callId)) { |
Santos Cordon | 7917d38 | 2014-02-14 02:31:18 -0800 | [diff] [blame^] | 172 | throw new IllegalArgumentException("Invalid call ID."); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 175 | } |