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 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Sailesh Nepal | 9d58de5 | 2014-07-18 14:53:19 -0700 | [diff] [blame] | 20 | import android.app.PendingIntent; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.ServiceConnection; |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
| 26 | import android.content.pm.ResolveInfo; |
| 27 | import android.content.pm.ServiceInfo; |
Santos Cordon | fdfcafa | 2014-06-26 14:49:05 -0700 | [diff] [blame] | 28 | import android.content.res.Resources; |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 29 | import android.net.Uri; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 30 | import android.os.IBinder; |
| 31 | import android.os.RemoteException; |
Amith Yamasani | 60e7584 | 2014-05-23 10:09:14 -0700 | [diff] [blame] | 32 | import android.os.UserHandle; |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 33 | import android.telecomm.AudioState; |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 34 | import android.telecomm.CallState; |
| 35 | import android.telecomm.InCallService; |
| 36 | import android.telecomm.ParcelableCall; |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 37 | import android.telecomm.PhoneCapabilities; |
| 38 | import android.telecomm.PropertyPresentation; |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 39 | import android.util.ArrayMap; |
Sailesh Nepal | a439e1b | 2014-03-11 18:19:58 -0700 | [diff] [blame] | 40 | |
| 41 | import com.android.internal.telecomm.IInCallService; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 42 | import com.google.common.collect.ImmutableCollection; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 43 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 44 | import java.util.ArrayList; |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 45 | import java.util.Iterator; |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 46 | import java.util.List; |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 47 | import java.util.Map; |
| 48 | import java.util.concurrent.ConcurrentHashMap; |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 49 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 50 | /** |
| 51 | * Binds to {@link IInCallService} and provides the service to {@link CallsManager} through which it |
| 52 | * 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] | 53 | * a binding to the {@link IInCallService} (implemented by the in-call app). |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 54 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 55 | public final class InCallController extends CallsManagerListenerBase { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 56 | /** |
| 57 | * 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] | 58 | * this class and in-call app. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 59 | */ |
| 60 | private class InCallServiceConnection implements ServiceConnection { |
| 61 | /** {@inheritDoc} */ |
| 62 | @Override public void onServiceConnected(ComponentName name, IBinder service) { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 63 | onConnected(name, service); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** {@inheritDoc} */ |
| 67 | @Override public void onServiceDisconnected(ComponentName name) { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 68 | onDisconnected(name); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Sailesh Nepal | e8ecb98 | 2014-07-11 17:19:42 -0700 | [diff] [blame] | 72 | private final Call.Listener mCallListener = new Call.ListenerBase() { |
| 73 | @Override |
| 74 | public void onCallCapabilitiesChanged(Call call) { |
| 75 | updateCall(call); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void onCannedSmsResponsesLoaded(Call call) { |
| 80 | updateCall(call); |
| 81 | } |
| 82 | |
| 83 | @Override |
Andrew Lee | 3bcf935 | 2014-07-23 12:36:05 -0700 | [diff] [blame] | 84 | public void onVideoCallProviderChanged(Call call) { |
Sailesh Nepal | e8ecb98 | 2014-07-11 17:19:42 -0700 | [diff] [blame] | 85 | updateCall(call); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void onStatusHintsChanged(Call call) { |
| 90 | updateCall(call); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void onHandleChanged(Call call) { |
| 95 | updateCall(call); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void onCallerDisplayNameChanged(Call call) { |
| 100 | updateCall(call); |
| 101 | } |
Andrew Lee | 4a79660 | 2014-07-11 17:23:03 -0700 | [diff] [blame] | 102 | |
| 103 | @Override |
| 104 | public void onVideoStateChanged(Call call) { |
| 105 | updateCall(call); |
| 106 | } |
Sailesh Nepal | 9d58de5 | 2014-07-18 14:53:19 -0700 | [diff] [blame] | 107 | |
| 108 | @Override |
| 109 | public void onStartActivityFromInCall(Call call, PendingIntent intent) { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 110 | if (!mInCallServices.isEmpty()) { |
Sailesh Nepal | 9d58de5 | 2014-07-18 14:53:19 -0700 | [diff] [blame] | 111 | Log.i(this, "Calling startActivity, intent: %s", intent); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 112 | for (IInCallService inCallService : mInCallServices.values()) { |
| 113 | try { |
| 114 | inCallService.startActivity(mCallIdMapper.getCallId(call), intent); |
| 115 | } catch (RemoteException ignored) { |
| 116 | } |
Sailesh Nepal | 9d58de5 | 2014-07-18 14:53:19 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | } |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 120 | |
| 121 | @Override |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 122 | public void onTargetPhoneAccountChanged(Call call) { |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 123 | updateCall(call); |
| 124 | } |
Santos Cordon | 12d6182 | 2014-07-29 16:02:20 -0700 | [diff] [blame] | 125 | |
| 126 | @Override |
| 127 | public void onConferenceableCallsChanged(Call call) { |
| 128 | updateCall(call); |
| 129 | } |
Sailesh Nepal | e8ecb98 | 2014-07-11 17:19:42 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 132 | /** |
| 133 | * Maintains a binding connection to the in-call app(s). |
| 134 | * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is |
| 135 | * load factor before resizing, 1 means we only expect a single thread to |
| 136 | * access the map so make only a single shard |
| 137 | */ |
| 138 | private final Map<ComponentName, InCallServiceConnection> mServiceConnections = |
| 139 | new ConcurrentHashMap<ComponentName, InCallServiceConnection>(8, 0.9f, 1); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 140 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 141 | /** The in-call app implementations, see {@link IInCallService}. */ |
| 142 | private final Map<ComponentName, IInCallService> mInCallServices = new ArrayMap<>(); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 143 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 144 | private final CallIdMapper mCallIdMapper = new CallIdMapper("InCall"); |
| 145 | |
Tyler Gunn | 12bccad | 2014-08-21 13:52:03 -0700 | [diff] [blame] | 146 | /** The {@link ComponentName} of the default InCall UI. */ |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 147 | private ComponentName mInCallComponentName; |
| 148 | |
| 149 | public InCallController() { |
| 150 | Context context = TelecommApp.getInstance(); |
| 151 | Resources resources = context.getResources(); |
| 152 | |
| 153 | mInCallComponentName = new ComponentName( |
| 154 | resources.getString(R.string.ui_default_package), |
| 155 | resources.getString(R.string.incall_default_class)); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 158 | @Override |
| 159 | public void onCallAdded(Call call) { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 160 | if (mInCallServices.isEmpty()) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 161 | bind(); |
| 162 | } else { |
| 163 | Log.i(this, "Adding call: %s", call); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 164 | // Track the call if we don't already know about it. |
| 165 | addCall(call); |
| 166 | |
Tyler Gunn | 12bccad | 2014-08-21 13:52:03 -0700 | [diff] [blame] | 167 | for (Map.Entry<ComponentName, IInCallService> entry : mInCallServices.entrySet()) { |
| 168 | ComponentName componentName = entry.getKey(); |
| 169 | IInCallService inCallService = entry.getValue(); |
| 170 | |
| 171 | ParcelableCall parcelableCall = toParcelableCall(call, |
| 172 | componentName.equals(mInCallComponentName) /* includeVideoProvider */); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 173 | try { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 174 | inCallService.addCall(parcelableCall); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 175 | } catch (RemoteException ignored) { |
| 176 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 177 | } |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 181 | @Override |
| 182 | public void onCallRemoved(Call call) { |
| 183 | if (CallsManager.getInstance().getCalls().isEmpty()) { |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 184 | // TODO: Wait for all messages to be delivered to the service before unbinding. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 185 | unbind(); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 186 | } |
Sailesh Nepal | e8ecb98 | 2014-07-11 17:19:42 -0700 | [diff] [blame] | 187 | call.removeListener(mCallListener); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 188 | mCallIdMapper.removeCall(call); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 191 | @Override |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 192 | public void onCallStateChanged(Call call, int oldState, int newState) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 193 | updateCall(call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 196 | @Override |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 197 | public void onConnectionServiceChanged( |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 198 | Call call, |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 199 | ConnectionServiceWrapper oldService, |
| 200 | ConnectionServiceWrapper newService) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 201 | updateCall(call); |
| 202 | } |
| 203 | |
| 204 | @Override |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 205 | public void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 206 | if (!mInCallServices.isEmpty()) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 207 | Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState, |
| 208 | newAudioState); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 209 | for (IInCallService inCallService : mInCallServices.values()) { |
| 210 | try { |
| 211 | inCallService.onAudioStateChanged(newAudioState); |
| 212 | } catch (RemoteException ignored) { |
| 213 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 218 | void onPostDialWait(Call call, String remaining) { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 219 | if (!mInCallServices.isEmpty()) { |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 220 | Log.i(this, "Calling onPostDialWait, remaining = %s", remaining); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 221 | for (IInCallService inCallService : mInCallServices.values()) { |
| 222 | try { |
| 223 | inCallService.setPostDialWait(mCallIdMapper.getCallId(call), remaining); |
| 224 | } catch (RemoteException ignored) { |
| 225 | } |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 230 | @Override |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 231 | public void onIsConferencedChanged(Call call) { |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 232 | Log.d(this, "onIsConferencedChanged %s", call); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 233 | updateCall(call); |
| 234 | } |
| 235 | |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 236 | void bringToForeground(boolean showDialpad) { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 237 | if (!mInCallServices.isEmpty()) { |
| 238 | for (IInCallService inCallService : mInCallServices.values()) { |
| 239 | try { |
| 240 | inCallService.bringToForeground(showDialpad); |
| 241 | } catch (RemoteException ignored) { |
| 242 | } |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 243 | } |
| 244 | } else { |
| 245 | Log.w(this, "Asking to bring unbound in-call UI to foreground."); |
| 246 | } |
| 247 | } |
| 248 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 249 | /** |
| 250 | * Unbinds an existing bound connection to the in-call app. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 251 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 252 | private void unbind() { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 253 | ThreadUtil.checkOnMainThread(); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 254 | if (!mInCallServices.isEmpty()) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 255 | Log.i(this, "Unbinding from InCallService"); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 256 | for (InCallServiceConnection connection : mServiceConnections.values()) { |
| 257 | TelecommApp.getInstance().unbindService(connection); |
| 258 | } |
| 259 | mInCallServices.clear(); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 264 | * Binds to the in-call app if not already connected by binding directly to the saved |
| 265 | * component name of the {@link IInCallService} implementation. |
| 266 | */ |
| 267 | private void bind() { |
| 268 | ThreadUtil.checkOnMainThread(); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 269 | if (mInCallServices.isEmpty()) { |
| 270 | mServiceConnections.clear(); |
Santos Cordon | fdfcafa | 2014-06-26 14:49:05 -0700 | [diff] [blame] | 271 | Context context = TelecommApp.getInstance(); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 272 | PackageManager packageManager = TelecommApp.getInstance().getPackageManager(); |
| 273 | Intent intent = new Intent(InCallService.SERVICE_INTERFACE); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 274 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 275 | for (ResolveInfo entry : packageManager.queryIntentServices(intent, 0)) { |
| 276 | ServiceInfo serviceInfo = entry.serviceInfo; |
| 277 | if (serviceInfo != null) { |
| 278 | boolean hasServiceBindPermission = serviceInfo.permission != null && |
| 279 | serviceInfo.permission.equals( |
| 280 | Manifest.permission.BIND_INCALL_SERVICE); |
| 281 | boolean hasControlInCallPermission = packageManager.checkPermission( |
| 282 | Manifest.permission.CONTROL_INCALL_EXPERIENCE, |
| 283 | serviceInfo.packageName) == PackageManager.PERMISSION_GRANTED; |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 284 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 285 | if (!hasServiceBindPermission) { |
| 286 | Log.w(this, "InCallService does not have BIND_INCALL_SERVICE permission: " + |
| 287 | serviceInfo.packageName); |
| 288 | continue; |
| 289 | } |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 290 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 291 | if (!hasControlInCallPermission) { |
| 292 | Log.w(this, |
| 293 | "InCall UI does not have CONTROL_INCALL_EXPERIENCE permission: " + |
| 294 | serviceInfo.packageName); |
| 295 | continue; |
| 296 | } |
| 297 | |
| 298 | Log.i(this, "Attempting to bind to InCall " + serviceInfo.packageName); |
| 299 | InCallServiceConnection inCallServiceConnection = new InCallServiceConnection(); |
| 300 | ComponentName componentName = new ComponentName(serviceInfo.packageName, |
| 301 | serviceInfo.name); |
| 302 | intent.setComponent(componentName); |
| 303 | |
| 304 | if (context.bindServiceAsUser(intent, inCallServiceConnection, |
| 305 | Context.BIND_AUTO_CREATE, UserHandle.CURRENT)) { |
| 306 | mServiceConnections.put(componentName, inCallServiceConnection); |
| 307 | } |
| 308 | } |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 314 | * Persists the {@link IInCallService} instance and starts the communication between |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 315 | * 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] | 316 | * called after a successful binding connection is established. |
| 317 | * |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 318 | * @param componentName The service {@link ComponentName}. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 319 | * @param service The {@link IInCallService} implementation. |
| 320 | */ |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 321 | private void onConnected(ComponentName componentName, IBinder service) { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 322 | ThreadUtil.checkOnMainThread(); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 323 | |
| 324 | IInCallService inCallService = IInCallService.Stub.asInterface(service); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 325 | |
| 326 | try { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 327 | inCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(), |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 328 | mCallIdMapper)); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 329 | mInCallServices.put(componentName, inCallService); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 330 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 331 | Log.e(this, e, "Failed to set the in-call adapter."); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 332 | return; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 335 | // Upon successful connection, send the state of the world to the service. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 336 | ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls(); |
| 337 | if (!calls.isEmpty()) { |
| 338 | for (Call call : calls) { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 339 | try { |
| 340 | // Track the call if we don't already know about it. |
| 341 | addCall(call); |
| 342 | |
Tyler Gunn | 12bccad | 2014-08-21 13:52:03 -0700 | [diff] [blame] | 343 | inCallService.addCall(toParcelableCall(call, |
| 344 | componentName.equals(mInCallComponentName) /* includeVideoProvider */)); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 345 | } catch (RemoteException ignored) { |
| 346 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 347 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 348 | onAudioStateChanged(null, CallsManager.getInstance().getAudioState()); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 349 | } else { |
| 350 | unbind(); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 351 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 355 | * Cleans up an instance of in-call app after the service has been unbound. |
| 356 | * |
| 357 | * @param disconnectedComponent The {@link ComponentName} of the service which disconnected. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 358 | */ |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 359 | private void onDisconnected(ComponentName disconnectedComponent) { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 360 | ThreadUtil.checkOnMainThread(); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 361 | if (mInCallServices.containsKey(disconnectedComponent)) { |
| 362 | mInCallServices.remove(disconnectedComponent); |
| 363 | } |
| 364 | |
| 365 | // If the default in-call UI has disconnected, disconnect all calls and un-bind all other |
| 366 | // InCallService implementations. |
| 367 | if (disconnectedComponent.equals(mInCallComponentName)) { |
| 368 | Log.i(this, "In-call UI %s disconnected.", disconnectedComponent); |
| 369 | CallsManager.getInstance().disconnectAllCalls(); |
| 370 | |
| 371 | // Iterate through the in-call services, removing them as they are un-bound. |
| 372 | Iterator<Map.Entry<ComponentName, IInCallService>> it = |
| 373 | mInCallServices.entrySet().iterator(); |
| 374 | while (it.hasNext()) { |
| 375 | Map.Entry<ComponentName, IInCallService> entry = it.next(); |
| 376 | ComponentName componentName = entry.getKey(); |
| 377 | |
| 378 | InCallServiceConnection connection = mServiceConnections.remove(componentName); |
| 379 | it.remove(); |
| 380 | if (connection == null) { |
| 381 | continue; |
| 382 | } |
| 383 | |
| 384 | Log.i(this, "Unbinding other InCallService %s", componentName); |
| 385 | TelecommApp.getInstance().unbindService(connection); |
| 386 | } |
| 387 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 388 | } |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 389 | |
Tyler Gunn | 12bccad | 2014-08-21 13:52:03 -0700 | [diff] [blame] | 390 | /** |
| 391 | * Informs all {@link InCallService} instances of the updated call information. Changes to the |
| 392 | * video provider are only communicated to the default in-call UI. |
| 393 | * |
| 394 | * @param call The {@link Call}. |
| 395 | */ |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 396 | private void updateCall(Call call) { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 397 | if (!mInCallServices.isEmpty()) { |
Tyler Gunn | 12bccad | 2014-08-21 13:52:03 -0700 | [diff] [blame] | 398 | for (Map.Entry<ComponentName, IInCallService> entry : mInCallServices.entrySet()) { |
| 399 | ComponentName componentName = entry.getKey(); |
| 400 | IInCallService inCallService = entry.getValue(); |
| 401 | ParcelableCall parcelableCall = toParcelableCall(call, |
| 402 | componentName.equals(mInCallComponentName) /* includeVideoProvider */); |
| 403 | |
| 404 | Log.v(this, "updateCall %s ==> %s", call, parcelableCall); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 405 | try { |
| 406 | inCallService.updateCall(parcelableCall); |
| 407 | } catch (RemoteException ignored) { |
| 408 | } |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
Tyler Gunn | 12bccad | 2014-08-21 13:52:03 -0700 | [diff] [blame] | 413 | /** |
| 414 | * Parcels all information for a {@link Call} into a new {@link ParcelableCall} instance. |
| 415 | * |
| 416 | * @param call The {@link Call} to parcel. |
| 417 | * @param includeVideoProvider When {@code true}, the {@link IVideoProvider} is included in the |
| 418 | * parcelled call. When {@code false}, the {@link IVideoProvider} is not included. |
| 419 | * @return The {@link ParcelableCall} containing all call information from the {@link Call}. |
| 420 | */ |
| 421 | private ParcelableCall toParcelableCall(Call call, boolean includeVideoProvider) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 422 | String callId = mCallIdMapper.getCallId(call); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 423 | |
Sailesh Nepal | e20bc97 | 2014-07-09 21:22:36 -0700 | [diff] [blame] | 424 | int capabilities = call.getCallCapabilities(); |
Santos Cordon | 4b034a6 | 2014-07-18 13:42:05 -0700 | [diff] [blame] | 425 | if (CallsManager.getInstance().isAddCallCapable(call)) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 426 | capabilities |= PhoneCapabilities.ADD_CALL; |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 427 | } |
Santos Cordon | 4b034a6 | 2014-07-18 13:42:05 -0700 | [diff] [blame] | 428 | if (!call.isEmergencyCall()) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 429 | capabilities |= PhoneCapabilities.MUTE; |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 430 | } |
Sailesh Nepal | e20bc97 | 2014-07-09 21:22:36 -0700 | [diff] [blame] | 431 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 432 | int state = call.getState(); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 433 | if (state == CallState.ABORTED) { |
| 434 | state = CallState.DISCONNECTED; |
| 435 | } |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 436 | |
| 437 | String parentCallId = null; |
| 438 | Call parentCall = call.getParentCall(); |
| 439 | if (parentCall != null) { |
| 440 | parentCallId = mCallIdMapper.getCallId(parentCall); |
| 441 | } |
| 442 | |
| 443 | long connectTimeMillis = call.getConnectTimeMillis(); |
| 444 | List<Call> childCalls = call.getChildCalls(); |
| 445 | List<String> childCallIds = new ArrayList<>(); |
| 446 | if (!childCalls.isEmpty()) { |
| 447 | connectTimeMillis = Long.MAX_VALUE; |
| 448 | for (Call child : childCalls) { |
| 449 | connectTimeMillis = Math.min(child.getConnectTimeMillis(), connectTimeMillis); |
| 450 | childCallIds.add(mCallIdMapper.getCallId(child)); |
| 451 | } |
| 452 | } |
| 453 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 454 | if (call.isRespondViaSmsCapable()) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 455 | capabilities |= PhoneCapabilities.RESPOND_VIA_TEXT; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 458 | Uri handle = call.getHandlePresentation() == PropertyPresentation.ALLOWED ? |
Sailesh Nepal | e8ecb98 | 2014-07-11 17:19:42 -0700 | [diff] [blame] | 459 | call.getHandle() : null; |
| 460 | String callerDisplayName = call.getCallerDisplayNamePresentation() == |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 461 | PropertyPresentation.ALLOWED ? call.getCallerDisplayName() : null; |
Sailesh Nepal | e8ecb98 | 2014-07-11 17:19:42 -0700 | [diff] [blame] | 462 | |
Santos Cordon | 12d6182 | 2014-07-29 16:02:20 -0700 | [diff] [blame] | 463 | List<Call> conferenceableCalls = call.getConferenceableCalls(); |
| 464 | List<String> conferenceableCallIds = new ArrayList<String>(conferenceableCalls.size()); |
| 465 | for (Call otherCall : conferenceableCalls) { |
| 466 | String otherId = mCallIdMapper.getCallId(otherCall); |
| 467 | if (otherId != null) { |
| 468 | conferenceableCallIds.add(otherId); |
| 469 | } |
| 470 | } |
| 471 | |
Santos Cordon | 2583b67 | 2014-07-19 13:07:33 -0700 | [diff] [blame] | 472 | return new ParcelableCall( |
| 473 | callId, |
| 474 | state, |
| 475 | call.getDisconnectCause(), |
| 476 | call.getDisconnectMessage(), |
| 477 | call.getCannedSmsResponses(), |
| 478 | capabilities, |
| 479 | connectTimeMillis, |
| 480 | handle, |
| 481 | call.getHandlePresentation(), |
| 482 | callerDisplayName, |
| 483 | call.getCallerDisplayNamePresentation(), |
| 484 | call.getGatewayInfo(), |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 485 | call.getTargetPhoneAccount(), |
Tyler Gunn | 12bccad | 2014-08-21 13:52:03 -0700 | [diff] [blame] | 486 | includeVideoProvider ? call.getVideoProvider() : null, |
Santos Cordon | 2583b67 | 2014-07-19 13:07:33 -0700 | [diff] [blame] | 487 | parentCallId, |
| 488 | childCallIds, |
| 489 | call.getStatusHints(), |
Santos Cordon | 12d6182 | 2014-07-29 16:02:20 -0700 | [diff] [blame] | 490 | call.getVideoState(), |
Nancy Chen | a9d91da | 2014-08-12 14:31:06 -0700 | [diff] [blame] | 491 | conferenceableCallIds, |
| 492 | call.getExtras()); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 493 | } |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame] | 494 | |
| 495 | /** |
| 496 | * Adds the call to the list of calls tracked by the {@link InCallController}. |
| 497 | * @param call The call to add. |
| 498 | */ |
| 499 | private void addCall(Call call) { |
| 500 | if (mCallIdMapper.getCallId(call) == null) { |
| 501 | mCallIdMapper.addCall(call); |
| 502 | call.addListener(mCallListener); |
| 503 | } |
| 504 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 505 | } |