blob: 08025a916099614a76a6ce9bc8f1fa3e2cd2c2e9 [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
Sailesh Nepal9d58de52014-07-18 14:53:19 -070019import android.app.PendingIntent;
Santos Cordone3d76ab2014-01-28 17:25:20 -080020import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.ServiceConnection;
Santos Cordonfdfcafa2014-06-26 14:49:05 -070024
25import android.content.res.Resources;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070026import android.net.Uri;
Santos Cordone3d76ab2014-01-28 17:25:20 -080027import android.os.IBinder;
28import android.os.RemoteException;
Amith Yamasani60e75842014-05-23 10:09:14 -070029import android.os.UserHandle;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070030import android.telecomm.CallAudioState;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070031import android.telecomm.CallCapabilities;
Sailesh Nepale8ecb982014-07-11 17:19:42 -070032import android.telecomm.CallPropertyPresentation;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070033import 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 }
Andrew Lee4a796602014-07-11 17:23:03 -070094
95 @Override
96 public void onVideoStateChanged(Call call) {
97 updateCall(call);
98 }
Sailesh Nepal9d58de52014-07-18 14:53:19 -070099
100 @Override
101 public void onStartActivityFromInCall(Call call, PendingIntent intent) {
102 if (mInCallService != null) {
103 Log.i(this, "Calling startActivity, intent: %s", intent);
104 try {
105 mInCallService.startActivity(mCallIdMapper.getCallId(call), intent);
106 } catch (RemoteException ignored) {
107 }
108 }
109 }
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700110 };
111
Santos Cordone3d76ab2014-01-28 17:25:20 -0800112 /** Maintains a binding connection to the in-call app. */
113 private final InCallServiceConnection mConnection = new InCallServiceConnection();
114
Santos Cordone3d76ab2014-01-28 17:25:20 -0800115 /** The in-call app implementation, see {@link IInCallService}. */
116 private IInCallService mInCallService;
117
Sailesh Nepale59bb192014-04-01 18:33:59 -0700118 private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall");
119
Santos Cordone3d76ab2014-01-28 17:25:20 -0800120 IInCallService getService() {
121 return mInCallService;
122 }
123
Sailesh Nepal810735e2014-03-18 18:15:46 -0700124 @Override
125 public void onCallAdded(Call call) {
126 if (mInCallService == null) {
127 bind();
128 } else {
129 Log.i(this, "Adding call: %s", call);
Ihab Awadff7493a2014-06-10 13:47:44 -0700130 if (mCallIdMapper.getCallId(call) == null) {
131 mCallIdMapper.addCall(call);
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700132 call.addListener(mCallListener);
Ihab Awadff7493a2014-06-10 13:47:44 -0700133 try {
134 mInCallService.addCall(toInCallCall(call));
135 } catch (RemoteException ignored) {
136 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800137 }
Santos Cordon049b7b62014-01-30 05:34:26 -0800138 }
139 }
140
Sailesh Nepal810735e2014-03-18 18:15:46 -0700141 @Override
142 public void onCallRemoved(Call call) {
143 if (CallsManager.getInstance().getCalls().isEmpty()) {
144 // TODO(sail): Wait for all messages to be delivered to the service before unbinding.
145 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800146 }
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700147 call.removeListener(mCallListener);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700148 mCallIdMapper.removeCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800149 }
150
Sailesh Nepal810735e2014-03-18 18:15:46 -0700151 @Override
152 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700153 updateCall(call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700154 }
155
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700156 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700157 public void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700158 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700159 ConnectionServiceWrapper oldService,
160 ConnectionServiceWrapper newService) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700161 updateCall(call);
162 }
163
164 @Override
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700165 public void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
166 if (mInCallService != null) {
167 Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState,
168 newAudioState);
169 try {
170 mInCallService.onAudioStateChanged(newAudioState);
Santos Cordonf3671a62014-05-29 21:51:53 -0700171 } catch (RemoteException ignored) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700172 }
173 }
174 }
175
Evan Charlton352105c2014-06-03 14:10:54 -0700176 void onPostDialWait(Call call, String remaining) {
177 if (mInCallService != null) {
178 Log.i(this, "Calling onPostDialWait, remaining = %s", remaining);
179 try {
180 mInCallService.setPostDialWait(mCallIdMapper.getCallId(call), remaining);
181 } catch (RemoteException ignored) {
182 }
183 }
184 }
185
Santos Cordona1610702014-06-04 20:22:56 -0700186 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700187 public void onIsConferencedChanged(Call call) {
188 Log.v(this, "onIsConferencedChanged %s", call);
189 updateCall(call);
190 }
191
Santos Cordonf3671a62014-05-29 21:51:53 -0700192 void bringToForeground(boolean showDialpad) {
193 if (mInCallService != null) {
194 try {
195 mInCallService.bringToForeground(showDialpad);
196 } catch (RemoteException ignored) {
197 }
198 } else {
199 Log.w(this, "Asking to bring unbound in-call UI to foreground.");
200 }
201 }
202
Santos Cordone3d76ab2014-01-28 17:25:20 -0800203 /**
204 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800205 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700206 private void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800207 ThreadUtil.checkOnMainThread();
208 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800209 Log.i(this, "Unbinding from InCallService");
Santos Cordon049b7b62014-01-30 05:34:26 -0800210 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800211 mInCallService = null;
212 }
213 }
214
215 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800216 * Binds to the in-call app if not already connected by binding directly to the saved
217 * component name of the {@link IInCallService} implementation.
218 */
219 private void bind() {
220 ThreadUtil.checkOnMainThread();
221 if (mInCallService == null) {
Santos Cordonfdfcafa2014-06-26 14:49:05 -0700222 Context context = TelecommApp.getInstance();
223 Resources resources = context.getResources();
224 ComponentName component = new ComponentName(
225 resources.getString(R.string.ui_default_package),
226 resources.getString(R.string.incall_default_class));
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800227 Log.i(this, "Attempting to bind to InCallService: %s", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800228
229 Intent serviceIntent = new Intent(IInCallService.class.getName());
230 serviceIntent.setComponent(component);
231
Amith Yamasani60e75842014-05-23 10:09:14 -0700232 if (!context.bindServiceAsUser(serviceIntent, mConnection, Context.BIND_AUTO_CREATE,
233 UserHandle.CURRENT)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800234 Log.w(this, "Could not connect to the in-call app (%s)", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800235
236 // TODO(santoscordon): Implement retry or fall-back-to-default logic.
237 }
238 }
239 }
240
241 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800242 * Persists the {@link IInCallService} instance and starts the communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -0700243 * 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 -0800244 * called after a successful binding connection is established.
245 *
246 * @param service The {@link IInCallService} implementation.
247 */
248 private void onConnected(IBinder service) {
249 ThreadUtil.checkOnMainThread();
250 mInCallService = IInCallService.Stub.asInterface(service);
251
252 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700253 mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(),
254 mCallIdMapper));
Santos Cordone3d76ab2014-01-28 17:25:20 -0800255 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800256 Log.e(this, e, "Failed to set the in-call adapter.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800257 mInCallService = null;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700258 return;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800259 }
260
Santos Cordon049b7b62014-01-30 05:34:26 -0800261 // Upon successful connection, send the state of the world to the in-call app.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700262 ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
263 if (!calls.isEmpty()) {
264 for (Call call : calls) {
265 onCallAdded(call);
266 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700267 onAudioStateChanged(null, CallsManager.getInstance().getAudioState());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700268 } else {
269 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800270 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800271 }
272
273 /**
274 * Cleans up the instance of in-call app after the service has been unbound.
275 */
276 private void onDisconnected() {
277 ThreadUtil.checkOnMainThread();
278 mInCallService = null;
279 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700280
281 private void updateCall(Call call) {
282 if (mInCallService != null) {
283 try {
Santos Cordona1610702014-06-04 20:22:56 -0700284 InCallCall inCallCall = toInCallCall(call);
285 Log.v(this, "updateCall %s ==> %s", call, inCallCall);
286 mInCallService.updateCall(inCallCall);
Santos Cordonf3671a62014-05-29 21:51:53 -0700287 } catch (RemoteException ignored) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700288 }
289 }
290 }
291
292 private InCallCall toInCallCall(Call call) {
293 String callId = mCallIdMapper.getCallId(call);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700294
Sailesh Nepale20bc972014-07-09 21:22:36 -0700295 int capabilities = call.getCallCapabilities();
296 if (!CallsManager.getInstance().isAddCallCapable(call)) {
297 capabilities &= ~CallCapabilities.ADD_CALL;
Santos Cordon10838c22014-06-11 17:36:04 -0700298 }
Sailesh Nepale20bc972014-07-09 21:22:36 -0700299 if (call.isEmergencyCall()) {
300 capabilities &= ~CallCapabilities.MUTE;
Santos Cordona1610702014-06-04 20:22:56 -0700301 }
Sailesh Nepale20bc972014-07-09 21:22:36 -0700302
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700303 CallState state = call.getState();
304 if (state == CallState.ABORTED) {
305 state = CallState.DISCONNECTED;
306 }
Santos Cordona1610702014-06-04 20:22:56 -0700307
308 String parentCallId = null;
309 Call parentCall = call.getParentCall();
310 if (parentCall != null) {
311 parentCallId = mCallIdMapper.getCallId(parentCall);
312 }
313
314 long connectTimeMillis = call.getConnectTimeMillis();
315 List<Call> childCalls = call.getChildCalls();
316 List<String> childCallIds = new ArrayList<>();
317 if (!childCalls.isEmpty()) {
318 connectTimeMillis = Long.MAX_VALUE;
319 for (Call child : childCalls) {
320 connectTimeMillis = Math.min(child.getConnectTimeMillis(), connectTimeMillis);
321 childCallIds.add(mCallIdMapper.getCallId(child));
322 }
323 }
324
Ihab Awadff7493a2014-06-10 13:47:44 -0700325 if (call.isRespondViaSmsCapable()) {
326 capabilities |= CallCapabilities.RESPOND_VIA_TEXT;
327 }
328
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700329 Uri handle = call.getHandlePresentation() == CallPropertyPresentation.ALLOWED ?
330 call.getHandle() : null;
331 String callerDisplayName = call.getCallerDisplayNamePresentation() ==
332 CallPropertyPresentation.ALLOWED ? call.getCallerDisplayName() : null;
333
Ihab Awad0fea3f22014-06-03 18:45:05 -0700334 return new InCallCall(callId, state, call.getDisconnectCause(), call.getDisconnectMessage(),
Sailesh Nepale8ecb982014-07-11 17:19:42 -0700335 call.getCannedSmsResponses(), capabilities, connectTimeMillis, handle,
336 call.getHandlePresentation(), callerDisplayName,
337 call.getCallerDisplayNamePresentation(), call.getGatewayInfo(),
Sailesh Nepal905dfba2014-07-14 08:20:41 -0700338 call.getPhoneAccount(), call.getCallVideoProvider(), parentCallId, childCallIds,
Andrew Lee4a796602014-07-11 17:23:03 -0700339 call.getStatusHints() , call.getVideoState());
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700340 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800341}