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