blob: 17063c3c6e779df528426e61b3b0a22810bceb40 [file] [log] [blame]
Santos Cordon681663d2014-01-30 04:32:15 -08001/*
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 Gilad9f2bed32013-12-12 17:43:26 -080017package com.android.telecomm;
18
Santos Cordon681663d2014-01-30 04:32:15 -080019import android.os.Handler;
20import android.os.Looper;
21import android.telecomm.CallInfo;
22import android.telecomm.ICallServiceAdapter;
Santos Cordon7917d382014-02-14 02:31:18 -080023import android.util.Log;
Santos Cordon681663d2014-01-30 04:32:15 -080024
Santos Cordon7917d382014-02-14 02:31:18 -080025import com.google.android.collect.Sets;
Santos Cordon681663d2014-01-30 04:32:15 -080026import com.google.common.base.Strings;
27
Santos Cordon7917d382014-02-14 02:31:18 -080028import java.util.Set;
29
Ben Gilad9f2bed32013-12-12 17:43:26 -080030/**
Santos Cordon681663d2014-01-30 04:32:15 -080031 * 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 Cordon63aeb162014-02-10 09:20:40 -080038 * TODO(santoscordon): Do we need Binder.clear/restoreCallingIdentity() in the service methods?
Ben Gilad9f2bed32013-12-12 17:43:26 -080039 */
Santos Cordon681663d2014-01-30 04:32:15 -080040public final class CallServiceAdapter extends ICallServiceAdapter.Stub {
Santos Cordon7917d382014-02-14 02:31:18 -080041 private static final String TAG = CallServiceAdapter.class.getSimpleName();
Ben Gilad9f2bed32013-12-12 17:43:26 -080042
Santos Cordon681663d2014-01-30 04:32:15 -080043 private final CallsManager mCallsManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080044
Santos Cordon681663d2014-01-30 04:32:15 -080045 private final OutgoingCallsManager mOutgoingCallsManager;
46
Santos Cordon493e8f22014-02-19 03:15:12 -080047 private final IncomingCallsManager mIncomingCallsManager;
48
Santos Cordon681663d2014-01-30 04:32:15 -080049 /** Used to run code (e.g. messages, Runnables) on the main (UI) thread. */
50 private final Handler mHandler = new Handler(Looper.getMainLooper());
51
Santos Cordon61d0f702014-02-19 02:52:23 -080052 /** 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 Cordon7917d382014-02-14 02:31:18 -080055 */
Santos Cordon61d0f702014-02-19 02:52:23 -080056 private final Set<String> mPendingIncomingCallIds = Sets.newHashSet();
Santos Cordon7917d382014-02-14 02:31:18 -080057
Santos Cordon681663d2014-01-30 04:32:15 -080058 /**
59 * Persists the specified parameters.
Santos Cordon493e8f22014-02-19 03:15:12 -080060 *
61 * @param outgoingCallsManager Manages the placing of outgoing calls.
62 * @param incomingCallsManager Manages the incoming call initialization flow.
Santos Cordon681663d2014-01-30 04:32:15 -080063 */
Santos Cordon493e8f22014-02-19 03:15:12 -080064 CallServiceAdapter(
65 OutgoingCallsManager outgoingCallsManager, IncomingCallsManager incomingCallsManager) {
66
Santos Cordon681663d2014-01-30 04:32:15 -080067 mCallsManager = CallsManager.getInstance();
68 mOutgoingCallsManager = outgoingCallsManager;
Santos Cordon493e8f22014-02-19 03:15:12 -080069 mIncomingCallsManager = incomingCallsManager;
Santos Cordon681663d2014-01-30 04:32:15 -080070 }
71
72 /** {@inheritDoc} */
Santos Cordon681663d2014-01-30 04:32:15 -080073 @Override public void setCompatibleWith(String callId, boolean isCompatible) {
74 // TODO(santoscordon): fill in.
75 }
76
Santos Cordon7917d382014-02-14 02:31:18 -080077 /** {@inheritDoc} */
Santos Cordon61d0f702014-02-19 02:52:23 -080078 @Override public void handleIncomingCall(final CallInfo callInfo) {
Santos Cordon7917d382014-02-14 02:31:18 -080079 checkValidCallId(callInfo.getId());
80 mHandler.post(new Runnable() {
81 @Override public void run() {
Santos Cordon61d0f702014-02-19 02:52:23 -080082 if (mPendingIncomingCallIds.remove(callInfo.getId())) {
Santos Cordon493e8f22014-02-19 03:15:12 -080083 mIncomingCallsManager.handleSuccessfulIncomingCall(callInfo);
Santos Cordon7917d382014-02-14 02:31:18 -080084 } else {
Santos Cordon61d0f702014-02-19 02:52:23 -080085 Log.wtf(TAG, "Received details for an unknown incoming call " + callInfo);
Santos Cordon7917d382014-02-14 02:31:18 -080086 }
87 }
88 });
Santos Cordon681663d2014-01-30 04:32:15 -080089 }
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 Cordon61d0f702014-02-19 02:52:23 -0800152 * 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 Cordon7917d382014-02-14 02:31:18 -0800154 *
155 * @param callId The ID of the call.
156 */
Santos Cordon61d0f702014-02-19 02:52:23 -0800157 void addPendingIncomingCallId(String callId) {
158 mPendingIncomingCallIds.add(callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800159 }
160
161 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800162 * Removed a call ID from the list of pending incoming call IDs.
Santos Cordon7917d382014-02-14 02:31:18 -0800163 *
164 * @param callId The ID of the call.
165 */
Santos Cordon61d0f702014-02-19 02:52:23 -0800166 void removePendingIncomingCallId(String callId) {
167 mPendingIncomingCallIds.remove(callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800168 }
169
170 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800171 * 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 Cordon7917d382014-02-14 02:31:18 -0800177 throw new IllegalArgumentException("Invalid call ID.");
Santos Cordon681663d2014-01-30 04:32:15 -0800178 }
179 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800180}