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