blob: d1215ae04e3971890e483354b627432eb64bee17 [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;
31import android.telecomm.CallServiceDescriptor;
32import android.telecomm.CallState;
33import android.telecomm.InCallCall;
Sailesh Nepala439e1b2014-03-11 18:19:58 -070034
35import com.android.internal.telecomm.IInCallService;
Sailesh Nepal810735e2014-03-18 18:15:46 -070036import com.google.common.collect.ImmutableCollection;
Santos Cordone3d76ab2014-01-28 17:25:20 -080037
Santos Cordona1610702014-06-04 20:22:56 -070038import java.util.ArrayList;
39import java.util.List;
Santos Cordona1610702014-06-04 20:22:56 -070040
Santos Cordone3d76ab2014-01-28 17:25:20 -080041/**
42 * Binds to {@link IInCallService} and provides the service to {@link CallsManager} through which it
43 * 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 -070044 * a binding to the {@link IInCallService} (implemented by the in-call app).
Santos Cordone3d76ab2014-01-28 17:25:20 -080045 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070046public final class InCallController extends CallsManagerListenerBase {
Santos Cordone3d76ab2014-01-28 17:25:20 -080047 /**
48 * Used to bind to the in-call app and triggers the start of communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -070049 * this class and in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -080050 */
51 private class InCallServiceConnection implements ServiceConnection {
52 /** {@inheritDoc} */
53 @Override public void onServiceConnected(ComponentName name, IBinder service) {
54 onConnected(service);
55 }
56
57 /** {@inheritDoc} */
58 @Override public void onServiceDisconnected(ComponentName name) {
59 onDisconnected();
60 }
61 }
62
Santos Cordone3d76ab2014-01-28 17:25:20 -080063 /** Maintains a binding connection to the in-call app. */
64 private final InCallServiceConnection mConnection = new InCallServiceConnection();
65
Santos Cordone3d76ab2014-01-28 17:25:20 -080066 /** The in-call app implementation, see {@link IInCallService}. */
67 private IInCallService mInCallService;
68
Sailesh Nepale59bb192014-04-01 18:33:59 -070069 private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall");
70
Santos Cordone3d76ab2014-01-28 17:25:20 -080071 IInCallService getService() {
72 return mInCallService;
73 }
74
Sailesh Nepal810735e2014-03-18 18:15:46 -070075 @Override
76 public void onCallAdded(Call call) {
77 if (mInCallService == null) {
78 bind();
79 } else {
80 Log.i(this, "Adding call: %s", call);
Ihab Awadff7493a2014-06-10 13:47:44 -070081 if (mCallIdMapper.getCallId(call) == null) {
82 mCallIdMapper.addCall(call);
83 try {
84 mInCallService.addCall(toInCallCall(call));
85 } catch (RemoteException ignored) {
86 }
Santos Cordone3d76ab2014-01-28 17:25:20 -080087 }
Santos Cordon049b7b62014-01-30 05:34:26 -080088 }
89 }
90
Sailesh Nepal810735e2014-03-18 18:15:46 -070091 @Override
92 public void onCallRemoved(Call call) {
93 if (CallsManager.getInstance().getCalls().isEmpty()) {
94 // TODO(sail): Wait for all messages to be delivered to the service before unbinding.
95 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -080096 }
Sailesh Nepale59bb192014-04-01 18:33:59 -070097 mCallIdMapper.removeCall(call);
Santos Cordon049b7b62014-01-30 05:34:26 -080098 }
99
Sailesh Nepal810735e2014-03-18 18:15:46 -0700100 @Override
101 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700102 updateCall(call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700103 }
104
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700105 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700106 public void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700107 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700108 ConnectionServiceWrapper oldService,
109 ConnectionServiceWrapper newService) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700110 updateCall(call);
111 }
112
113 @Override
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700114 public void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
115 if (mInCallService != null) {
116 Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState,
117 newAudioState);
118 try {
119 mInCallService.onAudioStateChanged(newAudioState);
Santos Cordonf3671a62014-05-29 21:51:53 -0700120 } catch (RemoteException ignored) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700121 }
122 }
123 }
124
Evan Charlton352105c2014-06-03 14:10:54 -0700125 void onPostDialWait(Call call, String remaining) {
126 if (mInCallService != null) {
127 Log.i(this, "Calling onPostDialWait, remaining = %s", remaining);
128 try {
129 mInCallService.setPostDialWait(mCallIdMapper.getCallId(call), remaining);
130 } catch (RemoteException ignored) {
131 }
132 }
133 }
134
Santos Cordona1610702014-06-04 20:22:56 -0700135 @Override
136 public void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable) {
137 updateCall(call);
138 }
139
140 @Override
141 public void onIsConferencedChanged(Call call) {
142 Log.v(this, "onIsConferencedChanged %s", call);
143 updateCall(call);
144 }
145
Ihab Awadff7493a2014-06-10 13:47:44 -0700146 @Override
147 public void onCannedSmsResponsesLoaded(Call call) {
148 updateCall(call);
149 }
150
Nancy Chena65d41f2014-06-24 12:06:03 -0700151 @Override
152 public void onCallVideoProviderChanged(Call call) {
153 updateCall(call);
154 }
155
Tyler Gunne19cc002014-07-01 11:32:53 -0700156 @Override
157 public void onFeaturesChanged(Call call) {
158 Log.v(this,"onFeaturesChanged: %d", call.getFeatures());
159 updateCall(call);
160 }
161
Santos Cordonf3671a62014-05-29 21:51:53 -0700162 void bringToForeground(boolean showDialpad) {
163 if (mInCallService != null) {
164 try {
165 mInCallService.bringToForeground(showDialpad);
166 } catch (RemoteException ignored) {
167 }
168 } else {
169 Log.w(this, "Asking to bring unbound in-call UI to foreground.");
170 }
171 }
172
Santos Cordone3d76ab2014-01-28 17:25:20 -0800173 /**
174 * Unbinds an existing bound connection to the in-call app.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800175 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700176 private void unbind() {
Santos Cordone3d76ab2014-01-28 17:25:20 -0800177 ThreadUtil.checkOnMainThread();
178 if (mInCallService != null) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800179 Log.i(this, "Unbinding from InCallService");
Santos Cordon049b7b62014-01-30 05:34:26 -0800180 TelecommApp.getInstance().unbindService(mConnection);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800181 mInCallService = null;
182 }
183 }
184
185 /**
Santos Cordon049b7b62014-01-30 05:34:26 -0800186 * Binds to the in-call app if not already connected by binding directly to the saved
187 * component name of the {@link IInCallService} implementation.
188 */
189 private void bind() {
190 ThreadUtil.checkOnMainThread();
191 if (mInCallService == null) {
Santos Cordonfdfcafa2014-06-26 14:49:05 -0700192 Context context = TelecommApp.getInstance();
193 Resources resources = context.getResources();
194 ComponentName component = new ComponentName(
195 resources.getString(R.string.ui_default_package),
196 resources.getString(R.string.incall_default_class));
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800197 Log.i(this, "Attempting to bind to InCallService: %s", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800198
199 Intent serviceIntent = new Intent(IInCallService.class.getName());
200 serviceIntent.setComponent(component);
201
Amith Yamasani60e75842014-05-23 10:09:14 -0700202 if (!context.bindServiceAsUser(serviceIntent, mConnection, Context.BIND_AUTO_CREATE,
203 UserHandle.CURRENT)) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800204 Log.w(this, "Could not connect to the in-call app (%s)", component);
Santos Cordon049b7b62014-01-30 05:34:26 -0800205
206 // TODO(santoscordon): Implement retry or fall-back-to-default logic.
207 }
208 }
209 }
210
211 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800212 * Persists the {@link IInCallService} instance and starts the communication between
Sailesh Nepale59bb192014-04-01 18:33:59 -0700213 * 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 -0800214 * called after a successful binding connection is established.
215 *
216 * @param service The {@link IInCallService} implementation.
217 */
218 private void onConnected(IBinder service) {
219 ThreadUtil.checkOnMainThread();
220 mInCallService = IInCallService.Stub.asInterface(service);
221
222 try {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700223 mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(),
224 mCallIdMapper));
Santos Cordone3d76ab2014-01-28 17:25:20 -0800225 } catch (RemoteException e) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800226 Log.e(this, e, "Failed to set the in-call adapter.");
Santos Cordone3d76ab2014-01-28 17:25:20 -0800227 mInCallService = null;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700228 return;
Santos Cordone3d76ab2014-01-28 17:25:20 -0800229 }
230
Santos Cordon049b7b62014-01-30 05:34:26 -0800231 // Upon successful connection, send the state of the world to the in-call app.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700232 ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
233 if (!calls.isEmpty()) {
234 for (Call call : calls) {
235 onCallAdded(call);
236 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700237 onAudioStateChanged(null, CallsManager.getInstance().getAudioState());
Sailesh Nepal810735e2014-03-18 18:15:46 -0700238 } else {
239 unbind();
Santos Cordon049b7b62014-01-30 05:34:26 -0800240 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800241 }
242
243 /**
244 * Cleans up the instance of in-call app after the service has been unbound.
245 */
246 private void onDisconnected() {
247 ThreadUtil.checkOnMainThread();
248 mInCallService = null;
249 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700250
251 private void updateCall(Call call) {
252 if (mInCallService != null) {
253 try {
Santos Cordona1610702014-06-04 20:22:56 -0700254 InCallCall inCallCall = toInCallCall(call);
255 Log.v(this, "updateCall %s ==> %s", call, inCallCall);
256 mInCallService.updateCall(inCallCall);
Santos Cordonf3671a62014-05-29 21:51:53 -0700257 } catch (RemoteException ignored) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700258 }
259 }
260 }
261
262 private InCallCall toInCallCall(Call call) {
263 String callId = mCallIdMapper.getCallId(call);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700264 CallServiceDescriptor descriptor = call.getConnectionService() != null ?
265 call.getConnectionService().getDescriptor() : null;
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700266
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700267 int capabilities = CallCapabilities.HOLD | CallCapabilities.MUTE;
Santos Cordon10838c22014-06-11 17:36:04 -0700268 if (CallsManager.getInstance().isAddCallCapable(call)) {
269 capabilities |= CallCapabilities.ADD_CALL;
270 }
Santos Cordona1610702014-06-04 20:22:56 -0700271 if (call.isConferenceCapable()) {
272 capabilities |= CallCapabilities.MERGE_CALLS;
273 }
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700274 CallState state = call.getState();
275 if (state == CallState.ABORTED) {
276 state = CallState.DISCONNECTED;
277 }
Santos Cordona1610702014-06-04 20:22:56 -0700278
279 String parentCallId = null;
280 Call parentCall = call.getParentCall();
281 if (parentCall != null) {
282 parentCallId = mCallIdMapper.getCallId(parentCall);
283 }
284
285 long connectTimeMillis = call.getConnectTimeMillis();
286 List<Call> childCalls = call.getChildCalls();
287 List<String> childCallIds = new ArrayList<>();
288 if (!childCalls.isEmpty()) {
289 connectTimeMillis = Long.MAX_VALUE;
290 for (Call child : childCalls) {
291 connectTimeMillis = Math.min(child.getConnectTimeMillis(), connectTimeMillis);
292 childCallIds.add(mCallIdMapper.getCallId(child));
293 }
294 }
295
Ihab Awadff7493a2014-06-10 13:47:44 -0700296 if (call.isRespondViaSmsCapable()) {
297 capabilities |= CallCapabilities.RESPOND_VIA_TEXT;
298 }
299
Ihab Awad0fea3f22014-06-03 18:45:05 -0700300 return new InCallCall(callId, state, call.getDisconnectCause(), call.getDisconnectMessage(),
Ihab Awadff7493a2014-06-10 13:47:44 -0700301 call.getCannedSmsResponses(), capabilities, connectTimeMillis, call.getHandle(),
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700302 call.getGatewayInfo(), call.getAccount(), descriptor, call.getCallVideoProvider(),
Tyler Gunne19cc002014-07-01 11:32:53 -0700303 parentCallId, childCallIds, call.getFeatures());
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700304 }
Santos Cordon10838c22014-06-11 17:36:04 -0700305
Santos Cordone3d76ab2014-01-28 17:25:20 -0800306}