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