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