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