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; |
| 39 | import java.util.List; |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 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 | /** Maintains a binding connection to the in-call app. */ |
| 64 | private final InCallServiceConnection mConnection = new InCallServiceConnection(); |
| 65 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 66 | /** The in-call app implementation, see {@link IInCallService}. */ |
| 67 | private IInCallService mInCallService; |
| 68 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 69 | private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall"); |
| 70 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 71 | IInCallService getService() { |
| 72 | return mInCallService; |
| 73 | } |
| 74 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 75 | @Override |
| 76 | public void onCallAdded(Call call) { |
| 77 | if (mInCallService == null) { |
| 78 | bind(); |
| 79 | } else { |
| 80 | Log.i(this, "Adding call: %s", call); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 81 | if (mCallIdMapper.getCallId(call) == null) { |
| 82 | mCallIdMapper.addCall(call); |
| 83 | try { |
| 84 | mInCallService.addCall(toInCallCall(call)); |
| 85 | } catch (RemoteException ignored) { |
| 86 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 87 | } |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 91 | @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 Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 96 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 97 | mCallIdMapper.removeCall(call); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 100 | @Override |
| 101 | public void onCallStateChanged(Call call, CallState oldState, CallState newState) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 102 | updateCall(call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 105 | @Override |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 106 | public void onConnectionServiceChanged( |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 107 | Call call, |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 108 | ConnectionServiceWrapper oldService, |
| 109 | ConnectionServiceWrapper newService) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 110 | updateCall(call); |
| 111 | } |
| 112 | |
| 113 | @Override |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 114 | 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 Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 120 | } catch (RemoteException ignored) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 125 | 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 Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 135 | @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 Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 146 | @Override |
| 147 | public void onCannedSmsResponsesLoaded(Call call) { |
| 148 | updateCall(call); |
| 149 | } |
| 150 | |
Nancy Chen | a65d41f | 2014-06-24 12:06:03 -0700 | [diff] [blame] | 151 | @Override |
| 152 | public void onCallVideoProviderChanged(Call call) { |
| 153 | updateCall(call); |
| 154 | } |
| 155 | |
Tyler Gunn | e19cc00 | 2014-07-01 11:32:53 -0700 | [diff] [blame] | 156 | @Override |
| 157 | public void onFeaturesChanged(Call call) { |
| 158 | Log.v(this,"onFeaturesChanged: %d", call.getFeatures()); |
| 159 | updateCall(call); |
| 160 | } |
| 161 | |
Sailesh Nepal | 35faf8c | 2014-07-08 22:02:34 -0700 | [diff] [blame] | 162 | @Override |
| 163 | public void onStatusHintsChanged(Call call) { |
| 164 | updateCall(call); |
| 165 | } |
| 166 | |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 167 | void bringToForeground(boolean showDialpad) { |
| 168 | if (mInCallService != null) { |
| 169 | try { |
| 170 | mInCallService.bringToForeground(showDialpad); |
| 171 | } catch (RemoteException ignored) { |
| 172 | } |
| 173 | } else { |
| 174 | Log.w(this, "Asking to bring unbound in-call UI to foreground."); |
| 175 | } |
| 176 | } |
| 177 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 178 | /** |
| 179 | * Unbinds an existing bound connection to the in-call app. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 180 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 181 | private void unbind() { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 182 | ThreadUtil.checkOnMainThread(); |
| 183 | if (mInCallService != null) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 184 | Log.i(this, "Unbinding from InCallService"); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 185 | TelecommApp.getInstance().unbindService(mConnection); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 186 | mInCallService = null; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 191 | * Binds to the in-call app if not already connected by binding directly to the saved |
| 192 | * component name of the {@link IInCallService} implementation. |
| 193 | */ |
| 194 | private void bind() { |
| 195 | ThreadUtil.checkOnMainThread(); |
| 196 | if (mInCallService == null) { |
Santos Cordon | fdfcafa | 2014-06-26 14:49:05 -0700 | [diff] [blame] | 197 | Context context = TelecommApp.getInstance(); |
| 198 | Resources resources = context.getResources(); |
| 199 | ComponentName component = new ComponentName( |
| 200 | resources.getString(R.string.ui_default_package), |
| 201 | resources.getString(R.string.incall_default_class)); |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 202 | Log.i(this, "Attempting to bind to InCallService: %s", component); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 203 | |
| 204 | Intent serviceIntent = new Intent(IInCallService.class.getName()); |
| 205 | serviceIntent.setComponent(component); |
| 206 | |
Amith Yamasani | 60e7584 | 2014-05-23 10:09:14 -0700 | [diff] [blame] | 207 | if (!context.bindServiceAsUser(serviceIntent, mConnection, Context.BIND_AUTO_CREATE, |
| 208 | UserHandle.CURRENT)) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 209 | 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] | 210 | |
| 211 | // TODO(santoscordon): Implement retry or fall-back-to-default logic. |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 217 | * Persists the {@link IInCallService} instance and starts the communication between |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 218 | * 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] | 219 | * called after a successful binding connection is established. |
| 220 | * |
| 221 | * @param service The {@link IInCallService} implementation. |
| 222 | */ |
| 223 | private void onConnected(IBinder service) { |
| 224 | ThreadUtil.checkOnMainThread(); |
| 225 | mInCallService = IInCallService.Stub.asInterface(service); |
| 226 | |
| 227 | try { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 228 | mInCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(), |
| 229 | mCallIdMapper)); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 230 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 231 | Log.e(this, e, "Failed to set the in-call adapter."); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 232 | mInCallService = null; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 233 | return; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 236 | // 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] | 237 | ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls(); |
| 238 | if (!calls.isEmpty()) { |
| 239 | for (Call call : calls) { |
| 240 | onCallAdded(call); |
| 241 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 242 | onAudioStateChanged(null, CallsManager.getInstance().getAudioState()); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 243 | } else { |
| 244 | unbind(); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 245 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Cleans up the instance of in-call app after the service has been unbound. |
| 250 | */ |
| 251 | private void onDisconnected() { |
| 252 | ThreadUtil.checkOnMainThread(); |
| 253 | mInCallService = null; |
| 254 | } |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 255 | |
| 256 | private void updateCall(Call call) { |
| 257 | if (mInCallService != null) { |
| 258 | try { |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 259 | InCallCall inCallCall = toInCallCall(call); |
| 260 | Log.v(this, "updateCall %s ==> %s", call, inCallCall); |
| 261 | mInCallService.updateCall(inCallCall); |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 262 | } catch (RemoteException ignored) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | private InCallCall toInCallCall(Call call) { |
| 268 | String callId = mCallIdMapper.getCallId(call); |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 269 | CallServiceDescriptor descriptor = call.getConnectionService() != null ? |
| 270 | call.getConnectionService().getDescriptor() : null; |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 271 | |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 272 | int capabilities = CallCapabilities.HOLD | CallCapabilities.MUTE; |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 273 | if (CallsManager.getInstance().isAddCallCapable(call)) { |
| 274 | capabilities |= CallCapabilities.ADD_CALL; |
| 275 | } |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 276 | if (call.isConferenceCapable()) { |
| 277 | capabilities |= CallCapabilities.MERGE_CALLS; |
| 278 | } |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 279 | CallState state = call.getState(); |
| 280 | if (state == CallState.ABORTED) { |
| 281 | state = CallState.DISCONNECTED; |
| 282 | } |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 283 | |
| 284 | String parentCallId = null; |
| 285 | Call parentCall = call.getParentCall(); |
| 286 | if (parentCall != null) { |
| 287 | parentCallId = mCallIdMapper.getCallId(parentCall); |
| 288 | } |
| 289 | |
| 290 | long connectTimeMillis = call.getConnectTimeMillis(); |
| 291 | List<Call> childCalls = call.getChildCalls(); |
| 292 | List<String> childCallIds = new ArrayList<>(); |
| 293 | if (!childCalls.isEmpty()) { |
| 294 | connectTimeMillis = Long.MAX_VALUE; |
| 295 | for (Call child : childCalls) { |
| 296 | connectTimeMillis = Math.min(child.getConnectTimeMillis(), connectTimeMillis); |
| 297 | childCallIds.add(mCallIdMapper.getCallId(child)); |
| 298 | } |
| 299 | } |
| 300 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 301 | if (call.isRespondViaSmsCapable()) { |
| 302 | capabilities |= CallCapabilities.RESPOND_VIA_TEXT; |
| 303 | } |
| 304 | |
Ihab Awad | 0fea3f2 | 2014-06-03 18:45:05 -0700 | [diff] [blame] | 305 | return new InCallCall(callId, state, call.getDisconnectCause(), call.getDisconnectMessage(), |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 306 | call.getCannedSmsResponses(), capabilities, connectTimeMillis, call.getHandle(), |
Sailesh Nepal | b0ba087 | 2014-07-08 22:09:50 -0700 | [diff] [blame] | 307 | call.getGatewayInfo(), call.getPhoneAccount(), descriptor, |
| 308 | call.getCallVideoProvider(), parentCallId, childCallIds, call.getFeatures(), |
| 309 | call.getStatusHints()); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 310 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 311 | } |