blob: 131882588d67fd04426219f441aa13c76316d4b6 [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;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070023import android.net.Uri;
Santos Cordone3d76ab2014-01-28 17:25:20 -080024import android.os.IBinder;
25import android.os.RemoteException;
Amith Yamasani60e75842014-05-23 10:09:14 -070026import android.os.UserHandle;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070027import android.telecomm.CallAudioState;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070028import android.telecomm.CallCapabilities;
29import android.telecomm.CallServiceDescriptor;
30import android.telecomm.CallState;
31import android.telecomm.InCallCall;
Sailesh Nepal810735e2014-03-18 18:15:46 -070032import android.telecomm.CallState;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070033
34import com.android.internal.telecomm.IInCallService;
Sailesh Nepal810735e2014-03-18 18:15:46 -070035import com.google.common.collect.ImmutableCollection;
Santos Cordone3d76ab2014-01-28 17:25:20 -080036
37/**
38 * Binds to {@link IInCallService} and provides the service to {@link CallsManager} through which it
39 * 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 -070040 * a binding to the {@link IInCallService} (implemented by the in-call app).
Santos Cordone3d76ab2014-01-28 17:25:20 -080041 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070042public final class InCallController extends CallsManagerListenerBase {
Santos Cordone3d76ab2014-01-28 17:25:20 -080043 /**
44 * Used to bind to the in-call app and triggers the start of communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -070045 * this class and in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -080046 */
47 private class InCallServiceConnection implements ServiceConnection {
48 /** {@inheritDoc} */
49 @Override public void onServiceConnected(ComponentName name, IBinder service) {
50 onConnected(service);
51 }
52
53 /** {@inheritDoc} */
54 @Override public void onServiceDisconnected(ComponentName name) {
55 onDisconnected();
56 }
57 }
58
Santos Cordone3d76ab2014-01-28 17:25:20 -080059 /**
60 * Package name of the in-call app. Although in-call code in kept in its own namespace, it is
Ben Gilad13329fd2014-02-11 17:20:29 -080061 * ultimately compiled into the dialer APK, hence the difference in namespaces between this and
62 * {@link #IN_CALL_SERVICE_CLASS_NAME}.
Santos Cordone3d76ab2014-01-28 17:25:20 -080063 * TODO(santoscordon): Change this into config.xml resource entry.
64 */
65 private static final String IN_CALL_PACKAGE_NAME = "com.google.android.dialer";
66
67 /**
68 * Class name of the component within in-call app which implements {@link IInCallService}.
69 */
Sailesh Nepala439e1b2014-03-11 18:19:58 -070070 private static final String IN_CALL_SERVICE_CLASS_NAME =
71 "com.android.incallui.InCallServiceImpl";
Santos Cordone3d76ab2014-01-28 17:25:20 -080072
73 /** Maintains a binding connection to the in-call app. */
74 private final InCallServiceConnection mConnection = new InCallServiceConnection();
75
Santos Cordone3d76ab2014-01-28 17:25:20 -080076 /** The in-call app implementation, see {@link IInCallService}. */
77 private IInCallService mInCallService;
78
Sailesh Nepale59bb192014-04-01 18:33:59 -070079 private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall");
80
Santos Cordone3d76ab2014-01-28 17:25:20 -080081 IInCallService getService() {
82 return mInCallService;
83 }
84
Sailesh Nepal810735e2014-03-18 18:15:46 -070085 @Override
86 public void onCallAdded(Call call) {
87 if (mInCallService == null) {
88 bind();
89 } else {
90 Log.i(this, "Adding call: %s", call);
Sailesh Nepale59bb192014-04-01 18:33:59 -070091 mCallIdMapper.addCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070092 try {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070093 mInCallService.addCall(toInCallCall(call));
Santos Cordonf3671a62014-05-29 21:51:53 -070094 } catch (RemoteException ignored) {
Santos Cordone3d76ab2014-01-28 17:25:20 -080095 }
Santos Cordon049b7b62014-01-30 05:34:26 -080096 }
97 }
98
Sailesh Nepal810735e2014-03-18 18:15:46 -070099 @Override
100 public void onCallRemoved(Call call) {
101 if (CallsManager.getInstance().getCalls().isEmpty()) {
102 // TODO(sail): Wait for all messages to be delivered to the service before unbinding.
103 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800104 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700105 mCallIdMapper.removeCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800106 }
107
Sailesh Nepal810735e2014-03-18 18:15:46 -0700108 @Override
109 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700110 updateCall(call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700111 }
112
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700113 @Override
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700114 public void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700115 updateCall(call);
116 }
117
118 @Override
119 public void onCallServiceChanged(
120 Call call,
121 CallServiceWrapper oldCallServiceWrapper,
122 CallServiceWrapper newCallService) {
123 updateCall(call);
124 }
125
126 @Override
127 public void onCallHandoffCallServiceDescriptorChanged(
128 Call call,
129 CallServiceDescriptor oldDescriptor,
130 CallServiceDescriptor newDescriptor) {
131 updateCall(call);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700132 }
133
134 @Override
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700135 public void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
136 if (mInCallService != null) {
137 Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState,
138 newAudioState);
139 try {
140 mInCallService.onAudioStateChanged(newAudioState);
Santos Cordonf3671a62014-05-29 21:51:53 -0700141 } catch (RemoteException ignored) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700142 }
143 }
144 }
145
Santos Cordonf3671a62014-05-29 21:51:53 -0700146 void bringToForeground(boolean showDialpad) {
147 if (mInCallService != null) {
148 try {
149 mInCallService.bringToForeground(showDialpad);
150 } catch (RemoteException ignored) {
151 }
152 } else {
153 Log.w(this, "Asking to bring unbound in-call UI to foreground.");
154 }
155 }
156
Santos Cordone3d76ab2014-01-28 17:25:20 -0800157 /**
158 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800159 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700160 private void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800161 ThreadUtil.checkOnMainThread();
162 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800163 Log.i(this, "Unbinding from InCallService");
Santos Cordon049b7b62014-01-30 05:34:26 -0800164 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800165 mInCallService = null;
166 }
167 }
168
169 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800170 * Binds to the in-call app if not already connected by binding directly to the saved
171 * component name of the {@link IInCallService} implementation.
172 */
173 private void bind() {
174 ThreadUtil.checkOnMainThread();
175 if (mInCallService == null) {
176 ComponentName component =
177 new ComponentName(IN_CALL_PACKAGE_NAME, IN_CALL_SERVICE_CLASS_NAME);
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800178 Log.i(this, "Attempting to bind to InCallService: %s", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800179
180 Intent serviceIntent = new Intent(IInCallService.class.getName());
181 serviceIntent.setComponent(component);
182
183 Context context = TelecommApp.getInstance();
Amith Yamasani60e75842014-05-23 10:09:14 -0700184 if (!context.bindServiceAsUser(serviceIntent, mConnection, Context.BIND_AUTO_CREATE,
185 UserHandle.CURRENT)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800186 Log.w(this, "Could not connect to the in-call app (%s)", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800187
188 // TODO(santoscordon): Implement retry or fall-back-to-default logic.
189 }
190 }
191 }
192
193 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800194 * Persists the {@link IInCallService} instance and starts the communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -0700195 * 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 -0800196 * called after a successful binding connection is established.
197 *
198 * @param service The {@link IInCallService} implementation.
199 */
200 private void onConnected(IBinder service) {
201 ThreadUtil.checkOnMainThread();
202 mInCallService = IInCallService.Stub.asInterface(service);
203
204 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700205 mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(),
206 mCallIdMapper));
Santos Cordone3d76ab2014-01-28 17:25:20 -0800207 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800208 Log.e(this, e, "Failed to set the in-call adapter.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800209 mInCallService = null;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700210 return;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800211 }
212
Santos Cordon049b7b62014-01-30 05:34:26 -0800213 // Upon successful connection, send the state of the world to the in-call app.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700214 ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
215 if (!calls.isEmpty()) {
216 for (Call call : calls) {
217 onCallAdded(call);
218 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700219 onAudioStateChanged(null, CallsManager.getInstance().getAudioState());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700220 } else {
221 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800222 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800223 }
224
225 /**
226 * Cleans up the instance of in-call app after the service has been unbound.
227 */
228 private void onDisconnected() {
229 ThreadUtil.checkOnMainThread();
230 mInCallService = null;
231 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700232
233 private void updateCall(Call call) {
234 if (mInCallService != null) {
235 try {
236 mInCallService.updateCall(toInCallCall(call));
Santos Cordonf3671a62014-05-29 21:51:53 -0700237 } catch (RemoteException ignored) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700238 }
239 }
240 }
241
242 private InCallCall toInCallCall(Call call) {
243 String callId = mCallIdMapper.getCallId(call);
244 CallServiceDescriptor descriptor =
245 call.getCallService() != null ? call.getCallService().getDescriptor() : null;
246
247 boolean isHandoffCapable = call.getHandoffHandle() != null;
248 int capabilities = CallCapabilities.HOLD | CallCapabilities.MUTE;
249 if (call.getHandoffHandle() != null) {
250 capabilities |= CallCapabilities.CONNECTION_HANDOFF;
251 }
252 CallState state = call.getState();
253 if (state == CallState.ABORTED) {
254 state = CallState.DISCONNECTED;
255 }
256 // TODO(sail): Remove this and replace with final reconnecting code.
257 if (state == CallState.DISCONNECTED && call.getHandoffCallServiceDescriptor() != null) {
258 state = CallState.ACTIVE;
259 }
Ihab Awad0fea3f22014-06-03 18:45:05 -0700260 return new InCallCall(callId, state, call.getDisconnectCause(), call.getDisconnectMessage(),
261 capabilities, call.getConnectTimeMillis(), call.getHandle(), call.getGatewayInfo(),
262 descriptor, call.getHandoffCallServiceDescriptor());
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700263 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800264}