blob: f602d5011618f8b404055503336551fe754669ae [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;
23
Santos Cordon7917d382014-02-14 02:31:18 -080024import com.google.android.collect.Sets;
Santos Cordon681663d2014-01-30 04:32:15 -080025import com.google.common.base.Strings;
26
Santos Cordon7917d382014-02-14 02:31:18 -080027import java.util.Set;
28
Ben Gilad9f2bed32013-12-12 17:43:26 -080029/**
Santos Cordon681663d2014-01-30 04:32:15 -080030 * 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 Cordon63aeb162014-02-10 09:20:40 -080037 * TODO(santoscordon): Do we need Binder.clear/restoreCallingIdentity() in the service methods?
Ben Gilad9f2bed32013-12-12 17:43:26 -080038 */
Santos Cordon681663d2014-01-30 04:32:15 -080039public final class CallServiceAdapter extends ICallServiceAdapter.Stub {
Santos Cordon681663d2014-01-30 04:32:15 -080040 private final CallsManager mCallsManager;
Ben Gilad9f2bed32013-12-12 17:43:26 -080041
Santos Cordon681663d2014-01-30 04:32:15 -080042 private final OutgoingCallsManager mOutgoingCallsManager;
43
Santos Cordon493e8f22014-02-19 03:15:12 -080044 private final IncomingCallsManager mIncomingCallsManager;
45
Santos Cordon681663d2014-01-30 04:32:15 -080046 /** Used to run code (e.g. messages, Runnables) on the main (UI) thread. */
47 private final Handler mHandler = new Handler(Looper.getMainLooper());
48
Santos Cordon61d0f702014-02-19 02:52:23 -080049 /** 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 Cordon7917d382014-02-14 02:31:18 -080052 */
Santos Cordon61d0f702014-02-19 02:52:23 -080053 private final Set<String> mPendingIncomingCallIds = Sets.newHashSet();
Santos Cordon7917d382014-02-14 02:31:18 -080054
Santos Cordon681663d2014-01-30 04:32:15 -080055 /**
56 * Persists the specified parameters.
Santos Cordon493e8f22014-02-19 03:15:12 -080057 *
58 * @param outgoingCallsManager Manages the placing of outgoing calls.
59 * @param incomingCallsManager Manages the incoming call initialization flow.
Santos Cordon681663d2014-01-30 04:32:15 -080060 */
Santos Cordon493e8f22014-02-19 03:15:12 -080061 CallServiceAdapter(
62 OutgoingCallsManager outgoingCallsManager, IncomingCallsManager incomingCallsManager) {
63
Santos Cordon681663d2014-01-30 04:32:15 -080064 mCallsManager = CallsManager.getInstance();
65 mOutgoingCallsManager = outgoingCallsManager;
Santos Cordon493e8f22014-02-19 03:15:12 -080066 mIncomingCallsManager = incomingCallsManager;
Santos Cordon681663d2014-01-30 04:32:15 -080067 }
68
69 /** {@inheritDoc} */
Santos Cordon681663d2014-01-30 04:32:15 -080070 @Override public void setCompatibleWith(String callId, boolean isCompatible) {
71 // TODO(santoscordon): fill in.
72 }
73
Santos Cordon7917d382014-02-14 02:31:18 -080074 /** {@inheritDoc} */
Santos Cordon61d0f702014-02-19 02:52:23 -080075 @Override public void handleIncomingCall(final CallInfo callInfo) {
Santos Cordon7917d382014-02-14 02:31:18 -080076 checkValidCallId(callInfo.getId());
77 mHandler.post(new Runnable() {
78 @Override public void run() {
Santos Cordon61d0f702014-02-19 02:52:23 -080079 if (mPendingIncomingCallIds.remove(callInfo.getId())) {
Santos Cordon493e8f22014-02-19 03:15:12 -080080 mIncomingCallsManager.handleSuccessfulIncomingCall(callInfo);
Santos Cordon7917d382014-02-14 02:31:18 -080081 } else {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080082 Log.wtf(CallServiceAdapter.this,
83 "Received details for an unknown incoming call %s", callInfo);
Santos Cordon7917d382014-02-14 02:31:18 -080084 }
85 }
86 });
Santos Cordon681663d2014-01-30 04:32:15 -080087 }
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 Cordon61d0f702014-02-19 02:52:23 -0800150 * 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 Cordon7917d382014-02-14 02:31:18 -0800152 *
153 * @param callId The ID of the call.
154 */
Santos Cordon61d0f702014-02-19 02:52:23 -0800155 void addPendingIncomingCallId(String callId) {
156 mPendingIncomingCallIds.add(callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800157 }
158
159 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800160 * Removed a call ID from the list of pending incoming call IDs.
Santos Cordon7917d382014-02-14 02:31:18 -0800161 *
162 * @param callId The ID of the call.
163 */
Santos Cordon61d0f702014-02-19 02:52:23 -0800164 void removePendingIncomingCallId(String callId) {
165 mPendingIncomingCallIds.remove(callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800166 }
167
168 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800169 * 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 Cordon7917d382014-02-14 02:31:18 -0800175 throw new IllegalArgumentException("Invalid call ID.");
Santos Cordon681663d2014-01-30 04:32:15 -0800176 }
177 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800178}