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