blob: 9e20d0cb41e0f1afe12592770fd3412c3a1a2ba6 [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 Cordon61d0f702014-02-19 02:52:23 -080050 /** 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 Cordon7917d382014-02-14 02:31:18 -080053 */
Santos Cordon61d0f702014-02-19 02:52:23 -080054 private final Set<String> mPendingIncomingCallIds = Sets.newHashSet();
Santos Cordon7917d382014-02-14 02:31:18 -080055
Santos Cordon681663d2014-01-30 04:32:15 -080056 /**
57 * Persists the specified parameters.
58 */
59 CallServiceAdapter(OutgoingCallsManager outgoingCallsManager) {
60 mCallsManager = CallsManager.getInstance();
61 mOutgoingCallsManager = outgoingCallsManager;
62 }
63
64 /** {@inheritDoc} */
Santos Cordon681663d2014-01-30 04:32:15 -080065 @Override public void setCompatibleWith(String callId, boolean isCompatible) {
66 // TODO(santoscordon): fill in.
67 }
68
Santos Cordon7917d382014-02-14 02:31:18 -080069 /** {@inheritDoc} */
Santos Cordon61d0f702014-02-19 02:52:23 -080070 @Override public void handleIncomingCall(final CallInfo callInfo) {
Santos Cordon7917d382014-02-14 02:31:18 -080071 checkValidCallId(callInfo.getId());
72 mHandler.post(new Runnable() {
73 @Override public void run() {
Santos Cordon61d0f702014-02-19 02:52:23 -080074 if (mPendingIncomingCallIds.remove(callInfo.getId())) {
Santos Cordon7917d382014-02-14 02:31:18 -080075 // TODO(santoscordon): Uncomment when ready.
76 // mIncomingCallsManager.handleSuccessfulIncomingCall(callInfo);
77 } else {
Santos Cordon61d0f702014-02-19 02:52:23 -080078 Log.wtf(TAG, "Received details for an unknown incoming call " + callInfo);
Santos Cordon7917d382014-02-14 02:31:18 -080079 }
80 }
81 });
Santos Cordon681663d2014-01-30 04:32:15 -080082 }
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 Cordon61d0f702014-02-19 02:52:23 -0800145 * 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 Cordon7917d382014-02-14 02:31:18 -0800147 *
148 * @param callId The ID of the call.
149 */
Santos Cordon61d0f702014-02-19 02:52:23 -0800150 void addPendingIncomingCallId(String callId) {
151 mPendingIncomingCallIds.add(callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800152 }
153
154 /**
Santos Cordon61d0f702014-02-19 02:52:23 -0800155 * Removed a call ID from the list of pending incoming call IDs.
Santos Cordon7917d382014-02-14 02:31:18 -0800156 *
157 * @param callId The ID of the call.
158 */
Santos Cordon61d0f702014-02-19 02:52:23 -0800159 void removePendingIncomingCallId(String callId) {
160 mPendingIncomingCallIds.remove(callId);
Santos Cordon7917d382014-02-14 02:31:18 -0800161 }
162
163 /**
Santos Cordon681663d2014-01-30 04:32:15 -0800164 * 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 Cordon7917d382014-02-14 02:31:18 -0800170 throw new IllegalArgumentException("Invalid call ID.");
Santos Cordon681663d2014-01-30 04:32:15 -0800171 }
172 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800173}