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