blob: 4bba241f59978b4c0b1e684aafbde1261d22372a [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;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070026import android.telecomm.CallAudioState;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070027import android.telecomm.CallCapabilities;
28import android.telecomm.CallServiceDescriptor;
29import android.telecomm.CallState;
30import android.telecomm.InCallCall;
Sailesh Nepal810735e2014-03-18 18:15:46 -070031import android.telecomm.CallState;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070032
33import com.android.internal.telecomm.IInCallService;
Sailesh Nepal810735e2014-03-18 18:15:46 -070034import com.google.common.collect.ImmutableCollection;
Santos Cordone3d76ab2014-01-28 17:25:20 -080035
36/**
37 * Binds to {@link IInCallService} and provides the service to {@link CallsManager} through which it
38 * 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 -070039 * a binding to the {@link IInCallService} (implemented by the in-call app).
Santos Cordone3d76ab2014-01-28 17:25:20 -080040 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070041public final class InCallController extends CallsManagerListenerBase {
Santos Cordone3d76ab2014-01-28 17:25:20 -080042 /**
43 * Used to bind to the in-call app and triggers the start of communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -070044 * this class and in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -080045 */
46 private class InCallServiceConnection implements ServiceConnection {
47 /** {@inheritDoc} */
48 @Override public void onServiceConnected(ComponentName name, IBinder service) {
49 onConnected(service);
50 }
51
52 /** {@inheritDoc} */
53 @Override public void onServiceDisconnected(ComponentName name) {
54 onDisconnected();
55 }
56 }
57
Santos Cordone3d76ab2014-01-28 17:25:20 -080058 /**
59 * 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 -080060 * ultimately compiled into the dialer APK, hence the difference in namespaces between this and
61 * {@link #IN_CALL_SERVICE_CLASS_NAME}.
Santos Cordone3d76ab2014-01-28 17:25:20 -080062 * TODO(santoscordon): Change this into config.xml resource entry.
63 */
64 private static final String IN_CALL_PACKAGE_NAME = "com.google.android.dialer";
65
66 /**
67 * Class name of the component within in-call app which implements {@link IInCallService}.
68 */
Sailesh Nepala439e1b2014-03-11 18:19:58 -070069 private static final String IN_CALL_SERVICE_CLASS_NAME =
70 "com.android.incallui.InCallServiceImpl";
Santos Cordone3d76ab2014-01-28 17:25:20 -080071
72 /** Maintains a binding connection to the in-call app. */
73 private final InCallServiceConnection mConnection = new InCallServiceConnection();
74
Santos Cordone3d76ab2014-01-28 17:25:20 -080075 /** The in-call app implementation, see {@link IInCallService}. */
76 private IInCallService mInCallService;
77
Sailesh Nepale59bb192014-04-01 18:33:59 -070078 private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall");
79
Santos Cordone3d76ab2014-01-28 17:25:20 -080080 IInCallService getService() {
81 return mInCallService;
82 }
83
Sailesh Nepal810735e2014-03-18 18:15:46 -070084 @Override
85 public void onCallAdded(Call call) {
86 if (mInCallService == null) {
87 bind();
88 } else {
89 Log.i(this, "Adding call: %s", call);
Sailesh Nepale59bb192014-04-01 18:33:59 -070090 mCallIdMapper.addCall(call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070091 try {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070092 mInCallService.addCall(toInCallCall(call));
Santos Cordonf3671a62014-05-29 21:51:53 -070093 } catch (RemoteException ignored) {
Santos Cordone3d76ab2014-01-28 17:25:20 -080094 }
Santos Cordon049b7b62014-01-30 05:34:26 -080095 }
96 }
97
Sailesh Nepal810735e2014-03-18 18:15:46 -070098 @Override
99 public void onCallRemoved(Call call) {
100 if (CallsManager.getInstance().getCalls().isEmpty()) {
101 // TODO(sail): Wait for all messages to be delivered to the service before unbinding.
102 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800103 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700104 mCallIdMapper.removeCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800105 }
106
Sailesh Nepal810735e2014-03-18 18:15:46 -0700107 @Override
108 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700109 updateCall(call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700110 }
111
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700112 @Override
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700113 public void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700114 updateCall(call);
115 }
116
117 @Override
118 public void onCallServiceChanged(
119 Call call,
120 CallServiceWrapper oldCallServiceWrapper,
121 CallServiceWrapper newCallService) {
122 updateCall(call);
123 }
124
125 @Override
126 public void onCallHandoffCallServiceDescriptorChanged(
127 Call call,
128 CallServiceDescriptor oldDescriptor,
129 CallServiceDescriptor newDescriptor) {
130 updateCall(call);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700131 }
132
133 @Override
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700134 public void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
135 if (mInCallService != null) {
136 Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState,
137 newAudioState);
138 try {
139 mInCallService.onAudioStateChanged(newAudioState);
Santos Cordonf3671a62014-05-29 21:51:53 -0700140 } catch (RemoteException ignored) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700141 }
142 }
143 }
144
Santos Cordonf3671a62014-05-29 21:51:53 -0700145 void bringToForeground(boolean showDialpad) {
146 if (mInCallService != null) {
147 try {
148 mInCallService.bringToForeground(showDialpad);
149 } catch (RemoteException ignored) {
150 }
151 } else {
152 Log.w(this, "Asking to bring unbound in-call UI to foreground.");
153 }
154 }
155
Santos Cordone3d76ab2014-01-28 17:25:20 -0800156 /**
157 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800158 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700159 private void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800160 ThreadUtil.checkOnMainThread();
161 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800162 Log.i(this, "Unbinding from InCallService");
Santos Cordon049b7b62014-01-30 05:34:26 -0800163 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800164 mInCallService = null;
165 }
166 }
167
168 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800169 * Binds to the in-call app if not already connected by binding directly to the saved
170 * component name of the {@link IInCallService} implementation.
171 */
172 private void bind() {
173 ThreadUtil.checkOnMainThread();
174 if (mInCallService == null) {
175 ComponentName component =
176 new ComponentName(IN_CALL_PACKAGE_NAME, IN_CALL_SERVICE_CLASS_NAME);
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800177 Log.i(this, "Attempting to bind to InCallService: %s", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800178
179 Intent serviceIntent = new Intent(IInCallService.class.getName());
180 serviceIntent.setComponent(component);
181
182 Context context = TelecommApp.getInstance();
183 if (!context.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800184 Log.w(this, "Could not connect to the in-call app (%s)", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800185
186 // TODO(santoscordon): Implement retry or fall-back-to-default logic.
187 }
188 }
189 }
190
191 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800192 * Persists the {@link IInCallService} instance and starts the communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -0700193 * 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 -0800194 * called after a successful binding connection is established.
195 *
196 * @param service The {@link IInCallService} implementation.
197 */
198 private void onConnected(IBinder service) {
199 ThreadUtil.checkOnMainThread();
200 mInCallService = IInCallService.Stub.asInterface(service);
201
202 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700203 mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(),
204 mCallIdMapper));
Santos Cordone3d76ab2014-01-28 17:25:20 -0800205 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800206 Log.e(this, e, "Failed to set the in-call adapter.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800207 mInCallService = null;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700208 return;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800209 }
210
Santos Cordon049b7b62014-01-30 05:34:26 -0800211 // Upon successful connection, send the state of the world to the in-call app.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700212 ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
213 if (!calls.isEmpty()) {
214 for (Call call : calls) {
215 onCallAdded(call);
216 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700217 onAudioStateChanged(null, CallsManager.getInstance().getAudioState());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700218 } else {
219 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800220 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800221 }
222
223 /**
224 * Cleans up the instance of in-call app after the service has been unbound.
225 */
226 private void onDisconnected() {
227 ThreadUtil.checkOnMainThread();
228 mInCallService = null;
229 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700230
231 private void updateCall(Call call) {
232 if (mInCallService != null) {
233 try {
234 mInCallService.updateCall(toInCallCall(call));
Santos Cordonf3671a62014-05-29 21:51:53 -0700235 } catch (RemoteException ignored) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700236 }
237 }
238 }
239
240 private InCallCall toInCallCall(Call call) {
241 String callId = mCallIdMapper.getCallId(call);
242 CallServiceDescriptor descriptor =
243 call.getCallService() != null ? call.getCallService().getDescriptor() : null;
244
245 boolean isHandoffCapable = call.getHandoffHandle() != null;
246 int capabilities = CallCapabilities.HOLD | CallCapabilities.MUTE;
247 if (call.getHandoffHandle() != null) {
248 capabilities |= CallCapabilities.CONNECTION_HANDOFF;
249 }
250 CallState state = call.getState();
251 if (state == CallState.ABORTED) {
252 state = CallState.DISCONNECTED;
253 }
254 // TODO(sail): Remove this and replace with final reconnecting code.
255 if (state == CallState.DISCONNECTED && call.getHandoffCallServiceDescriptor() != null) {
256 state = CallState.ACTIVE;
257 }
258 return new InCallCall(callId, state, call.getDisconnectCause(), capabilities,
259 call.getConnectTimeMillis(), call.getHandle(), call.getGatewayInfo(), descriptor,
260 call.getHandoffCallServiceDescriptor());
261 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800262}