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