Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.telecomm; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.ServiceConnection; |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 23 | import android.net.Uri; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 24 | import android.os.IBinder; |
| 25 | import android.os.RemoteException; |
Amith Yamasani | 60e7584 | 2014-05-23 10:09:14 -0700 | [diff] [blame] | 26 | import android.os.UserHandle; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 27 | import android.telecomm.CallAudioState; |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 28 | import android.telecomm.CallCapabilities; |
| 29 | import android.telecomm.CallServiceDescriptor; |
| 30 | import android.telecomm.CallState; |
| 31 | import android.telecomm.InCallCall; |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 32 | |
| 33 | import com.android.internal.telecomm.IInCallService; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 34 | import com.google.common.collect.ImmutableCollection; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 35 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame^] | 37 | import java.util.Collections; |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 38 | import java.util.List; |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 39 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Binds to {@link IInCallService} and provides the service to {@link CallsManager} through which it |
| 42 | * can send updates to the in-call app. This class is created and owned by CallsManager and retains |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 43 | * a binding to the {@link IInCallService} (implemented by the in-call app). |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 44 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 45 | public final class InCallController extends CallsManagerListenerBase { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 46 | /** |
| 47 | * Used to bind to the in-call app and triggers the start of communication between |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 48 | * this class and in-call app. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 49 | */ |
| 50 | private class InCallServiceConnection implements ServiceConnection { |
| 51 | /** {@inheritDoc} */ |
| 52 | @Override public void onServiceConnected(ComponentName name, IBinder service) { |
| 53 | onConnected(service); |
| 54 | } |
| 55 | |
| 56 | /** {@inheritDoc} */ |
| 57 | @Override public void onServiceDisconnected(ComponentName name) { |
| 58 | onDisconnected(); |
| 59 | } |
| 60 | } |
| 61 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 62 | /** |
| 63 | * Package name of the in-call app. Although in-call code in kept in its own namespace, it is |
Ben Gilad | 13329fd | 2014-02-11 17:20:29 -0800 | [diff] [blame] | 64 | * ultimately compiled into the dialer APK, hence the difference in namespaces between this and |
| 65 | * {@link #IN_CALL_SERVICE_CLASS_NAME}. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 66 | * TODO(santoscordon): Change this into config.xml resource entry. |
| 67 | */ |
| 68 | private static final String IN_CALL_PACKAGE_NAME = "com.google.android.dialer"; |
| 69 | |
| 70 | /** |
| 71 | * Class name of the component within in-call app which implements {@link IInCallService}. |
| 72 | */ |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 73 | private static final String IN_CALL_SERVICE_CLASS_NAME = |
| 74 | "com.android.incallui.InCallServiceImpl"; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 75 | |
| 76 | /** Maintains a binding connection to the in-call app. */ |
| 77 | private final InCallServiceConnection mConnection = new InCallServiceConnection(); |
| 78 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 79 | /** The in-call app implementation, see {@link IInCallService}. */ |
| 80 | private IInCallService mInCallService; |
| 81 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 82 | private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall"); |
| 83 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 84 | IInCallService getService() { |
| 85 | return mInCallService; |
| 86 | } |
| 87 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public void onCallAdded(Call call) { |
| 90 | if (mInCallService == null) { |
| 91 | bind(); |
| 92 | } else { |
| 93 | Log.i(this, "Adding call: %s", call); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame^] | 94 | if (mCallIdMapper.getCallId(call) == null) { |
| 95 | mCallIdMapper.addCall(call); |
| 96 | try { |
| 97 | mInCallService.addCall(toInCallCall(call)); |
| 98 | } catch (RemoteException ignored) { |
| 99 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 100 | } |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 104 | @Override |
| 105 | public void onCallRemoved(Call call) { |
| 106 | if (CallsManager.getInstance().getCalls().isEmpty()) { |
| 107 | // TODO(sail): Wait for all messages to be delivered to the service before unbinding. |
| 108 | unbind(); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 109 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 110 | mCallIdMapper.removeCall(call); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 113 | @Override |
| 114 | public void onCallStateChanged(Call call, CallState oldState, CallState newState) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 115 | updateCall(call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 118 | @Override |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 119 | public void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 120 | updateCall(call); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void onCallServiceChanged( |
| 125 | Call call, |
| 126 | CallServiceWrapper oldCallServiceWrapper, |
| 127 | CallServiceWrapper newCallService) { |
| 128 | updateCall(call); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void onCallHandoffCallServiceDescriptorChanged( |
| 133 | Call call, |
| 134 | CallServiceDescriptor oldDescriptor, |
| 135 | CallServiceDescriptor newDescriptor) { |
| 136 | updateCall(call); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 140 | public void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) { |
| 141 | if (mInCallService != null) { |
| 142 | Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState, |
| 143 | newAudioState); |
| 144 | try { |
| 145 | mInCallService.onAudioStateChanged(newAudioState); |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 146 | } catch (RemoteException ignored) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 151 | void onPostDialWait(Call call, String remaining) { |
| 152 | if (mInCallService != null) { |
| 153 | Log.i(this, "Calling onPostDialWait, remaining = %s", remaining); |
| 154 | try { |
| 155 | mInCallService.setPostDialWait(mCallIdMapper.getCallId(call), remaining); |
| 156 | } catch (RemoteException ignored) { |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 161 | @Override |
| 162 | public void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable) { |
| 163 | updateCall(call); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void onIsConferencedChanged(Call call) { |
| 168 | Log.v(this, "onIsConferencedChanged %s", call); |
| 169 | updateCall(call); |
| 170 | } |
| 171 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame^] | 172 | @Override |
| 173 | public void onCannedSmsResponsesLoaded(Call call) { |
| 174 | updateCall(call); |
| 175 | } |
| 176 | |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 177 | void bringToForeground(boolean showDialpad) { |
| 178 | if (mInCallService != null) { |
| 179 | try { |
| 180 | mInCallService.bringToForeground(showDialpad); |
| 181 | } catch (RemoteException ignored) { |
| 182 | } |
| 183 | } else { |
| 184 | Log.w(this, "Asking to bring unbound in-call UI to foreground."); |
| 185 | } |
| 186 | } |
| 187 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 188 | /** |
| 189 | * Unbinds an existing bound connection to the in-call app. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 190 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 191 | private void unbind() { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 192 | ThreadUtil.checkOnMainThread(); |
| 193 | if (mInCallService != null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 194 | Log.i(this, "Unbinding from InCallService"); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 195 | TelecommApp.getInstance().unbindService(mConnection); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 196 | mInCallService = null; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 201 | * Binds to the in-call app if not already connected by binding directly to the saved |
| 202 | * component name of the {@link IInCallService} implementation. |
| 203 | */ |
| 204 | private void bind() { |
| 205 | ThreadUtil.checkOnMainThread(); |
| 206 | if (mInCallService == null) { |
| 207 | ComponentName component = |
| 208 | new ComponentName(IN_CALL_PACKAGE_NAME, IN_CALL_SERVICE_CLASS_NAME); |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 209 | Log.i(this, "Attempting to bind to InCallService: %s", component); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 210 | |
| 211 | Intent serviceIntent = new Intent(IInCallService.class.getName()); |
| 212 | serviceIntent.setComponent(component); |
| 213 | |
| 214 | Context context = TelecommApp.getInstance(); |
Amith Yamasani | 60e7584 | 2014-05-23 10:09:14 -0700 | [diff] [blame] | 215 | if (!context.bindServiceAsUser(serviceIntent, mConnection, Context.BIND_AUTO_CREATE, |
| 216 | UserHandle.CURRENT)) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 217 | Log.w(this, "Could not connect to the in-call app (%s)", component); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 218 | |
| 219 | // TODO(santoscordon): Implement retry or fall-back-to-default logic. |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 225 | * Persists the {@link IInCallService} instance and starts the communication between |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 226 | * this class and in-call app by sending the first update to in-call app. This method is |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 227 | * called after a successful binding connection is established. |
| 228 | * |
| 229 | * @param service The {@link IInCallService} implementation. |
| 230 | */ |
| 231 | private void onConnected(IBinder service) { |
| 232 | ThreadUtil.checkOnMainThread(); |
| 233 | mInCallService = IInCallService.Stub.asInterface(service); |
| 234 | |
| 235 | try { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 236 | mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(), |
| 237 | mCallIdMapper)); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 238 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 239 | Log.e(this, e, "Failed to set the in-call adapter."); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 240 | mInCallService = null; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 241 | return; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 244 | // Upon successful connection, send the state of the world to the in-call app. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 245 | ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls(); |
| 246 | if (!calls.isEmpty()) { |
| 247 | for (Call call : calls) { |
| 248 | onCallAdded(call); |
| 249 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 250 | onAudioStateChanged(null, CallsManager.getInstance().getAudioState()); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 251 | } else { |
| 252 | unbind(); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 253 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Cleans up the instance of in-call app after the service has been unbound. |
| 258 | */ |
| 259 | private void onDisconnected() { |
| 260 | ThreadUtil.checkOnMainThread(); |
| 261 | mInCallService = null; |
| 262 | } |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 263 | |
| 264 | private void updateCall(Call call) { |
| 265 | if (mInCallService != null) { |
| 266 | try { |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 267 | InCallCall inCallCall = toInCallCall(call); |
| 268 | Log.v(this, "updateCall %s ==> %s", call, inCallCall); |
| 269 | mInCallService.updateCall(inCallCall); |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 270 | } catch (RemoteException ignored) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | private InCallCall toInCallCall(Call call) { |
| 276 | String callId = mCallIdMapper.getCallId(call); |
| 277 | CallServiceDescriptor descriptor = |
| 278 | call.getCallService() != null ? call.getCallService().getDescriptor() : null; |
| 279 | |
| 280 | boolean isHandoffCapable = call.getHandoffHandle() != null; |
| 281 | int capabilities = CallCapabilities.HOLD | CallCapabilities.MUTE; |
| 282 | if (call.getHandoffHandle() != null) { |
| 283 | capabilities |= CallCapabilities.CONNECTION_HANDOFF; |
| 284 | } |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 285 | if (CallsManager.getInstance().isAddCallCapable(call)) { |
| 286 | capabilities |= CallCapabilities.ADD_CALL; |
| 287 | } |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 288 | if (call.isConferenceCapable()) { |
| 289 | capabilities |= CallCapabilities.MERGE_CALLS; |
| 290 | } |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 291 | CallState state = call.getState(); |
| 292 | if (state == CallState.ABORTED) { |
| 293 | state = CallState.DISCONNECTED; |
| 294 | } |
| 295 | // TODO(sail): Remove this and replace with final reconnecting code. |
| 296 | if (state == CallState.DISCONNECTED && call.getHandoffCallServiceDescriptor() != null) { |
| 297 | state = CallState.ACTIVE; |
| 298 | } |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 299 | |
| 300 | String parentCallId = null; |
| 301 | Call parentCall = call.getParentCall(); |
| 302 | if (parentCall != null) { |
| 303 | parentCallId = mCallIdMapper.getCallId(parentCall); |
| 304 | } |
| 305 | |
| 306 | long connectTimeMillis = call.getConnectTimeMillis(); |
| 307 | List<Call> childCalls = call.getChildCalls(); |
| 308 | List<String> childCallIds = new ArrayList<>(); |
| 309 | if (!childCalls.isEmpty()) { |
| 310 | connectTimeMillis = Long.MAX_VALUE; |
| 311 | for (Call child : childCalls) { |
| 312 | connectTimeMillis = Math.min(child.getConnectTimeMillis(), connectTimeMillis); |
| 313 | childCallIds.add(mCallIdMapper.getCallId(child)); |
| 314 | } |
| 315 | } |
| 316 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame^] | 317 | if (call.isRespondViaSmsCapable()) { |
| 318 | capabilities |= CallCapabilities.RESPOND_VIA_TEXT; |
| 319 | } |
| 320 | |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 321 | return new InCallCall(callId, state, call.getDisconnectCause(), call.getDisconnectMessage(), |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame^] | 322 | call.getCannedSmsResponses(), capabilities, connectTimeMillis, call.getHandle(), |
| 323 | call.getGatewayInfo(), descriptor, call.getHandoffCallServiceDescriptor(), |
| 324 | parentCallId, childCallIds); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 325 | } |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 326 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 327 | } |