blob: 9a9d0dbbbcf05195c7cb6eea543f0858b3cd56a0 [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
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 Cordon7917d382014-02-14 02:31:18 -080050 /** 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 Cordon681663d2014-01-30 04:32:15 -080058 /**
59 * Persists the specified parameters.
60 */
61 CallServiceAdapter(OutgoingCallsManager outgoingCallsManager) {
62 mCallsManager = CallsManager.getInstance();
63 mOutgoingCallsManager = outgoingCallsManager;
64 }
65
66 /** {@inheritDoc} */
Santos Cordon681663d2014-01-30 04:32:15 -080067 @Override public void setCompatibleWith(String callId, boolean isCompatible) {
68 // TODO(santoscordon): fill in.
69 }
70
Santos Cordon7917d382014-02-14 02:31:18 -080071 /** {@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 Cordon681663d2014-01-30 04:32:15 -080084 }
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 Cordon7917d382014-02-14 02:31:18 -0800147 * 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 Cordon681663d2014-01-30 04:32:15 -0800166 * 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 Cordon7917d382014-02-14 02:31:18 -0800172 throw new IllegalArgumentException("Invalid call ID.");
Santos Cordon681663d2014-01-30 04:32:15 -0800173 }
174 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800175}