blob: 637e03e04932a97192a43b0ffc8379ee7ea0d357 [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
Evan Charlton352105c2014-06-03 14:10:54 -0700146 void onPostDialWait(Call call, String remaining) {
147 if (mInCallService != null) {
148 Log.i(this, "Calling onPostDialWait, remaining = %s", remaining);
149 try {
150 mInCallService.setPostDialWait(mCallIdMapper.getCallId(call), remaining);
151 } catch (RemoteException ignored) {
152 }
153 }
154 }
155
Santos Cordonf3671a62014-05-29 21:51:53 -0700156 void bringToForeground(boolean showDialpad) {
157 if (mInCallService != null) {
158 try {
159 mInCallService.bringToForeground(showDialpad);
160 } catch (RemoteException ignored) {
161 }
162 } else {
163 Log.w(this, "Asking to bring unbound in-call UI to foreground.");
164 }
165 }
166
Santos Cordone3d76ab2014-01-28 17:25:20 -0800167 /**
168 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800169 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700170 private void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800171 ThreadUtil.checkOnMainThread();
172 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800173 Log.i(this, "Unbinding from InCallService");
Santos Cordon049b7b62014-01-30 05:34:26 -0800174 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800175 mInCallService = null;
176 }
177 }
178
179 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800180 * Binds to the in-call app if not already connected by binding directly to the saved
181 * component name of the {@link IInCallService} implementation.
182 */
183 private void bind() {
184 ThreadUtil.checkOnMainThread();
185 if (mInCallService == null) {
186 ComponentName component =
187 new ComponentName(IN_CALL_PACKAGE_NAME, IN_CALL_SERVICE_CLASS_NAME);
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800188 Log.i(this, "Attempting to bind to InCallService: %s", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800189
190 Intent serviceIntent = new Intent(IInCallService.class.getName());
191 serviceIntent.setComponent(component);
192
193 Context context = TelecommApp.getInstance();
Amith Yamasani60e75842014-05-23 10:09:14 -0700194 if (!context.bindServiceAsUser(serviceIntent, mConnection, Context.BIND_AUTO_CREATE,
195 UserHandle.CURRENT)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800196 Log.w(this, "Could not connect to the in-call app (%s)", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800197
198 // TODO(santoscordon): Implement retry or fall-back-to-default logic.
199 }
200 }
201 }
202
203 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800204 * Persists the {@link IInCallService} instance and starts the communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -0700205 * 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 -0800206 * called after a successful binding connection is established.
207 *
208 * @param service The {@link IInCallService} implementation.
209 */
210 private void onConnected(IBinder service) {
211 ThreadUtil.checkOnMainThread();
212 mInCallService = IInCallService.Stub.asInterface(service);
213
214 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700215 mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(),
216 mCallIdMapper));
Santos Cordone3d76ab2014-01-28 17:25:20 -0800217 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800218 Log.e(this, e, "Failed to set the in-call adapter.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800219 mInCallService = null;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700220 return;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800221 }
222
Santos Cordon049b7b62014-01-30 05:34:26 -0800223 // Upon successful connection, send the state of the world to the in-call app.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700224 ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
225 if (!calls.isEmpty()) {
226 for (Call call : calls) {
227 onCallAdded(call);
228 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700229 onAudioStateChanged(null, CallsManager.getInstance().getAudioState());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700230 } else {
231 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800232 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800233 }
234
235 /**
236 * Cleans up the instance of in-call app after the service has been unbound.
237 */
238 private void onDisconnected() {
239 ThreadUtil.checkOnMainThread();
240 mInCallService = null;
241 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700242
243 private void updateCall(Call call) {
244 if (mInCallService != null) {
245 try {
246 mInCallService.updateCall(toInCallCall(call));
Santos Cordonf3671a62014-05-29 21:51:53 -0700247 } catch (RemoteException ignored) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700248 }
249 }
250 }
251
252 private InCallCall toInCallCall(Call call) {
253 String callId = mCallIdMapper.getCallId(call);
254 CallServiceDescriptor descriptor =
255 call.getCallService() != null ? call.getCallService().getDescriptor() : null;
256
257 boolean isHandoffCapable = call.getHandoffHandle() != null;
258 int capabilities = CallCapabilities.HOLD | CallCapabilities.MUTE;
259 if (call.getHandoffHandle() != null) {
260 capabilities |= CallCapabilities.CONNECTION_HANDOFF;
261 }
Santos Cordon10838c22014-06-11 17:36:04 -0700262 if (CallsManager.getInstance().isAddCallCapable(call)) {
263 capabilities |= CallCapabilities.ADD_CALL;
264 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700265 CallState state = call.getState();
266 if (state == CallState.ABORTED) {
267 state = CallState.DISCONNECTED;
268 }
269 // TODO(sail): Remove this and replace with final reconnecting code.
270 if (state == CallState.DISCONNECTED && call.getHandoffCallServiceDescriptor() != null) {
271 state = CallState.ACTIVE;
272 }
Ihab Awad0fea3f22014-06-03 18:45:05 -0700273 return new InCallCall(callId, state, call.getDisconnectCause(), call.getDisconnectMessage(),
274 capabilities, call.getConnectTimeMillis(), call.getHandle(), call.getGatewayInfo(),
275 descriptor, call.getHandoffCallServiceDescriptor());
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700276 }
Santos Cordon10838c22014-06-11 17:36:04 -0700277
Santos Cordone3d76ab2014-01-28 17:25:20 -0800278}