blob: d8e4030627df749415e8979cb332758675e5a4ff [file] [log] [blame]
Santos Cordone3d76ab2014-01-28 17:25:20 -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
17package com.android.telecomm;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
Santos Cordonfdfcafa2014-06-26 14:49:05 -070023
24import android.content.res.Resources;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070025import android.net.Uri;
Santos Cordone3d76ab2014-01-28 17:25:20 -080026import android.os.IBinder;
27import android.os.RemoteException;
Amith Yamasani60e75842014-05-23 10:09:14 -070028import android.os.UserHandle;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070029import android.telecomm.CallAudioState;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070030import android.telecomm.CallCapabilities;
Sailesh Nepale8ecb982014-07-11 17:19:42 -070031import android.telecomm.CallPropertyPresentation;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070032import android.telecomm.CallServiceDescriptor;
33import android.telecomm.CallState;
34import android.telecomm.InCallCall;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070035
36import com.android.internal.telecomm.IInCallService;
Sailesh Nepal810735e2014-03-18 18:15:46 -070037import com.google.common.collect.ImmutableCollection;
Santos Cordone3d76ab2014-01-28 17:25:20 -080038
Santos Cordona1610702014-06-04 20:22:56 -070039import java.util.ArrayList;
40import java.util.List;
Santos Cordona1610702014-06-04 20:22:56 -070041
Santos Cordone3d76ab2014-01-28 17:25:20 -080042/**
43 * Binds to {@link IInCallService} and provides the service to {@link CallsManager} through which it
44 * can send updates to the in-call app. This class is created and owned by CallsManager and retains
Sailesh Nepale59bb192014-04-01 18:33:59 -070045 * a binding to the {@link IInCallService} (implemented by the in-call app).
Santos Cordone3d76ab2014-01-28 17:25:20 -080046 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070047public final class InCallController extends CallsManagerListenerBase {
Santos Cordone3d76ab2014-01-28 17:25:20 -080048 /**
49 * Used to bind to the in-call app and triggers the start of communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -070050 * this class and in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -080051 */
52 private class InCallServiceConnection implements ServiceConnection {
53 /** {@inheritDoc} */
54 @Override public void onServiceConnected(ComponentName name, IBinder service) {
55 onConnected(service);
56 }
57
58 /** {@inheritDoc} */
59 @Override public void onServiceDisconnected(ComponentName name) {
60 onDisconnected();
61 }
62 }
63
Sailesh Nepale8ecb982014-07-11 17:19:42 -070064 private final Call.Listener mCallListener = new Call.ListenerBase() {
65 @Override
66 public void onCallCapabilitiesChanged(Call call) {
67 updateCall(call);
68 }
69
70 @Override
71 public void onCannedSmsResponsesLoaded(Call call) {
72 updateCall(call);
73 }
74
75 @Override
76 public void onCallVideoProviderChanged(Call call) {
77 updateCall(call);
78 }
79
80 @Override
81 public void onStatusHintsChanged(Call call) {
82 updateCall(call);
83 }
84
85 @Override
86 public void onHandleChanged(Call call) {
87 updateCall(call);
88 }
89
90 @Override
91 public void onCallerDisplayNameChanged(Call call) {
92 updateCall(call);
93 }
94 };
95
Santos Cordone3d76ab2014-01-28 17:25:20 -080096 /** Maintains a binding connection to the in-call app. */
97 private final InCallServiceConnection mConnection = new InCallServiceConnection();
98
Santos Cordone3d76ab2014-01-28 17:25:20 -080099 /** The in-call app implementation, see {@link IInCallService}. */
100 private IInCallService mInCallService;
101
Sailesh Nepale59bb192014-04-01 18:33:59 -0700102 private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall");
103
Santos Cordone3d76ab2014-01-28 17:25:20 -0800104 IInCallService getService() {
105 return mInCallService;
106 }
107
Sailesh Nepal810735e2014-03-18 18:15:46 -0700108 @Override
109 public void onCallAdded(Call call) {
110 if (mInCallService == null) {
111 bind();
112 } else {
113 Log.i(this, "Adding call: %s", call);
Ihab Awadff7493a2014-06-10 13:47:44 -0700114 if (mCallIdMapper.getCallId(call) == null) {
115 mCallIdMapper.addCall(call);
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700116 call.addListener(mCallListener);
Ihab Awadff7493a2014-06-10 13:47:44 -0700117 try {
118 mInCallService.addCall(toInCallCall(call));
119 } catch (RemoteException ignored) {
120 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800121 }
Santos Cordon049b7b62014-01-30 05:34:26 -0800122 }
123 }
124
Sailesh Nepal810735e2014-03-18 18:15:46 -0700125 @Override
126 public void onCallRemoved(Call call) {
127 if (CallsManager.getInstance().getCalls().isEmpty()) {
128 // TODO(sail): Wait for all messages to be delivered to the service before unbinding.
129 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800130 }
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700131 call.removeListener(mCallListener);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700132 mCallIdMapper.removeCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800133 }
134
Sailesh Nepal810735e2014-03-18 18:15:46 -0700135 @Override
136 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700137 updateCall(call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700138 }
139
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700140 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700141 public void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700142 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700143 ConnectionServiceWrapper oldService,
144 ConnectionServiceWrapper newService) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700145 updateCall(call);
146 }
147
148 @Override
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700149 public void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
150 if (mInCallService != null) {
151 Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState,
152 newAudioState);
153 try {
154 mInCallService.onAudioStateChanged(newAudioState);
Santos Cordonf3671a62014-05-29 21:51:53 -0700155 } catch (RemoteException ignored) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700156 }
157 }
158 }
159
Evan Charlton352105c2014-06-03 14:10:54 -0700160 void onPostDialWait(Call call, String remaining) {
161 if (mInCallService != null) {
162 Log.i(this, "Calling onPostDialWait, remaining = %s", remaining);
163 try {
164 mInCallService.setPostDialWait(mCallIdMapper.getCallId(call), remaining);
165 } catch (RemoteException ignored) {
166 }
167 }
168 }
169
Santos Cordona1610702014-06-04 20:22:56 -0700170 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700171 public void onIsConferencedChanged(Call call) {
172 Log.v(this, "onIsConferencedChanged %s", call);
173 updateCall(call);
174 }
175
Santos Cordonf3671a62014-05-29 21:51:53 -0700176 void bringToForeground(boolean showDialpad) {
177 if (mInCallService != null) {
178 try {
179 mInCallService.bringToForeground(showDialpad);
180 } catch (RemoteException ignored) {
181 }
182 } else {
183 Log.w(this, "Asking to bring unbound in-call UI to foreground.");
184 }
185 }
186
Santos Cordone3d76ab2014-01-28 17:25:20 -0800187 /**
188 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800189 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700190 private void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800191 ThreadUtil.checkOnMainThread();
192 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800193 Log.i(this, "Unbinding from InCallService");
Santos Cordon049b7b62014-01-30 05:34:26 -0800194 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800195 mInCallService = null;
196 }
197 }
198
199 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800200 * Binds to the in-call app if not already connected by binding directly to the saved
201 * component name of the {@link IInCallService} implementation.
202 */
203 private void bind() {
204 ThreadUtil.checkOnMainThread();
205 if (mInCallService == null) {
Santos Cordonfdfcafa2014-06-26 14:49:05 -0700206 Context context = TelecommApp.getInstance();
207 Resources resources = context.getResources();
208 ComponentName component = new ComponentName(
209 resources.getString(R.string.ui_default_package),
210 resources.getString(R.string.incall_default_class));
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800211 Log.i(this, "Attempting to bind to InCallService: %s", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800212
213 Intent serviceIntent = new Intent(IInCallService.class.getName());
214 serviceIntent.setComponent(component);
215
Amith Yamasani60e75842014-05-23 10:09:14 -0700216 if (!context.bindServiceAsUser(serviceIntent, mConnection, Context.BIND_AUTO_CREATE,
217 UserHandle.CURRENT)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800218 Log.w(this, "Could not connect to the in-call app (%s)", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800219
220 // TODO(santoscordon): Implement retry or fall-back-to-default logic.
221 }
222 }
223 }
224
225 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800226 * Persists the {@link IInCallService} instance and starts the communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -0700227 * this class and in-call app by sending the first update to in-call app. This method is
Santos Cordone3d76ab2014-01-28 17:25:20 -0800228 * called after a successful binding connection is established.
229 *
230 * @param service The {@link IInCallService} implementation.
231 */
232 private void onConnected(IBinder service) {
233 ThreadUtil.checkOnMainThread();
234 mInCallService = IInCallService.Stub.asInterface(service);
235
236 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700237 mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(),
238 mCallIdMapper));
Santos Cordone3d76ab2014-01-28 17:25:20 -0800239 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800240 Log.e(this, e, "Failed to set the in-call adapter.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800241 mInCallService = null;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700242 return;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800243 }
244
Santos Cordon049b7b62014-01-30 05:34:26 -0800245 // Upon successful connection, send the state of the world to the in-call app.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700246 ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
247 if (!calls.isEmpty()) {
248 for (Call call : calls) {
249 onCallAdded(call);
250 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700251 onAudioStateChanged(null, CallsManager.getInstance().getAudioState());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700252 } else {
253 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800254 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800255 }
256
257 /**
258 * Cleans up the instance of in-call app after the service has been unbound.
259 */
260 private void onDisconnected() {
261 ThreadUtil.checkOnMainThread();
262 mInCallService = null;
263 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700264
265 private void updateCall(Call call) {
266 if (mInCallService != null) {
267 try {
Santos Cordona1610702014-06-04 20:22:56 -0700268 InCallCall inCallCall = toInCallCall(call);
269 Log.v(this, "updateCall %s ==> %s", call, inCallCall);
270 mInCallService.updateCall(inCallCall);
Santos Cordonf3671a62014-05-29 21:51:53 -0700271 } catch (RemoteException ignored) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700272 }
273 }
274 }
275
276 private InCallCall toInCallCall(Call call) {
277 String callId = mCallIdMapper.getCallId(call);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700278 CallServiceDescriptor descriptor = call.getConnectionService() != null ?
279 call.getConnectionService().getDescriptor() : null;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700280
Sailesh Nepale20bc972014-07-09 21:22:36 -0700281 int capabilities = call.getCallCapabilities();
282 if (!CallsManager.getInstance().isAddCallCapable(call)) {
283 capabilities &= ~CallCapabilities.ADD_CALL;
Santos Cordon10838c22014-06-11 17:36:04 -0700284 }
Sailesh Nepale20bc972014-07-09 21:22:36 -0700285 if (call.isEmergencyCall()) {
286 capabilities &= ~CallCapabilities.MUTE;
Santos Cordona1610702014-06-04 20:22:56 -0700287 }
Sailesh Nepale20bc972014-07-09 21:22:36 -0700288
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700289 CallState state = call.getState();
290 if (state == CallState.ABORTED) {
291 state = CallState.DISCONNECTED;
292 }
Santos Cordona1610702014-06-04 20:22:56 -0700293
294 String parentCallId = null;
295 Call parentCall = call.getParentCall();
296 if (parentCall != null) {
297 parentCallId = mCallIdMapper.getCallId(parentCall);
298 }
299
300 long connectTimeMillis = call.getConnectTimeMillis();
301 List<Call> childCalls = call.getChildCalls();
302 List<String> childCallIds = new ArrayList<>();
303 if (!childCalls.isEmpty()) {
304 connectTimeMillis = Long.MAX_VALUE;
305 for (Call child : childCalls) {
306 connectTimeMillis = Math.min(child.getConnectTimeMillis(), connectTimeMillis);
307 childCallIds.add(mCallIdMapper.getCallId(child));
308 }
309 }
310
Ihab Awadff7493a2014-06-10 13:47:44 -0700311 if (call.isRespondViaSmsCapable()) {
312 capabilities |= CallCapabilities.RESPOND_VIA_TEXT;
313 }
314
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700315 Uri handle = call.getHandlePresentation() == CallPropertyPresentation.ALLOWED ?
316 call.getHandle() : null;
317 String callerDisplayName = call.getCallerDisplayNamePresentation() ==
318 CallPropertyPresentation.ALLOWED ? call.getCallerDisplayName() : null;
319
Ihab Awad0fea3f22014-06-03 18:45:05 -0700320 return new InCallCall(callId, state, call.getDisconnectCause(), call.getDisconnectMessage(),
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700321 call.getCannedSmsResponses(), capabilities, connectTimeMillis, handle,
322 call.getHandlePresentation(), callerDisplayName,
323 call.getCallerDisplayNamePresentation(), call.getGatewayInfo(),
324 call.getPhoneAccount(), descriptor, call.getCallVideoProvider(), parentCallId,
325 childCallIds, call.getStatusHints());
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700326 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800327}