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 | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 146 | /** The {@link ComponentName} of the default InCall UI */ |
| 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 | |
| 167 | ParcelableCall parcelableCall = toParcelableCall(call); |
| 168 | for (IInCallService inCallService : mInCallServices.values()) { |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 169 | try { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 170 | inCallService.addCall(parcelableCall); |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 171 | } catch (RemoteException ignored) { |
| 172 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 173 | } |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 177 | @Override |
| 178 | public void onCallRemoved(Call call) { |
| 179 | if (CallsManager.getInstance().getCalls().isEmpty()) { |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame] | 180 | // 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] | 181 | unbind(); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 182 | } |
Sailesh Nepal | e8ecb98 | 2014-07-11 17:19:42 -0700 | [diff] [blame] | 183 | call.removeListener(mCallListener); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 184 | mCallIdMapper.removeCall(call); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 187 | @Override |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 188 | public void onCallStateChanged(Call call, int oldState, int newState) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 189 | updateCall(call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 192 | @Override |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 193 | public void onConnectionServiceChanged( |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 194 | Call call, |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 195 | ConnectionServiceWrapper oldService, |
| 196 | ConnectionServiceWrapper newService) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 197 | updateCall(call); |
| 198 | } |
| 199 | |
| 200 | @Override |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 201 | public void onAudioStateChanged(AudioState oldAudioState, AudioState newAudioState) { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 202 | if (!mInCallServices.isEmpty()) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 203 | Log.i(this, "Calling onAudioStateChanged, audioState: %s -> %s", oldAudioState, |
| 204 | newAudioState); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 205 | for (IInCallService inCallService : mInCallServices.values()) { |
| 206 | try { |
| 207 | inCallService.onAudioStateChanged(newAudioState); |
| 208 | } catch (RemoteException ignored) { |
| 209 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 214 | void onPostDialWait(Call call, String remaining) { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 215 | if (!mInCallServices.isEmpty()) { |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 216 | Log.i(this, "Calling onPostDialWait, remaining = %s", remaining); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 217 | for (IInCallService inCallService : mInCallServices.values()) { |
| 218 | try { |
| 219 | inCallService.setPostDialWait(mCallIdMapper.getCallId(call), remaining); |
| 220 | } catch (RemoteException ignored) { |
| 221 | } |
Evan Charlton | 352105c | 2014-06-03 14:10:54 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 226 | @Override |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 227 | public void onIsConferencedChanged(Call call) { |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame] | 228 | Log.d(this, "onIsConferencedChanged %s", call); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 229 | updateCall(call); |
| 230 | } |
| 231 | |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 232 | void bringToForeground(boolean showDialpad) { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 233 | if (!mInCallServices.isEmpty()) { |
| 234 | for (IInCallService inCallService : mInCallServices.values()) { |
| 235 | try { |
| 236 | inCallService.bringToForeground(showDialpad); |
| 237 | } catch (RemoteException ignored) { |
| 238 | } |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 239 | } |
| 240 | } else { |
| 241 | Log.w(this, "Asking to bring unbound in-call UI to foreground."); |
| 242 | } |
| 243 | } |
| 244 | |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 245 | /** |
| 246 | * Unbinds an existing bound connection to the in-call app. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 247 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 248 | private void unbind() { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 249 | ThreadUtil.checkOnMainThread(); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 250 | if (!mInCallServices.isEmpty()) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 251 | Log.i(this, "Unbinding from InCallService"); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 252 | for (InCallServiceConnection connection : mServiceConnections.values()) { |
| 253 | TelecommApp.getInstance().unbindService(connection); |
| 254 | } |
| 255 | mInCallServices.clear(); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 260 | * Binds to the in-call app if not already connected by binding directly to the saved |
| 261 | * component name of the {@link IInCallService} implementation. |
| 262 | */ |
| 263 | private void bind() { |
| 264 | ThreadUtil.checkOnMainThread(); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 265 | if (mInCallServices.isEmpty()) { |
| 266 | mServiceConnections.clear(); |
Santos Cordon | fdfcafa | 2014-06-26 14:49:05 -0700 | [diff] [blame] | 267 | Context context = TelecommApp.getInstance(); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 268 | PackageManager packageManager = TelecommApp.getInstance().getPackageManager(); |
| 269 | Intent intent = new Intent(InCallService.SERVICE_INTERFACE); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 270 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 271 | for (ResolveInfo entry : packageManager.queryIntentServices(intent, 0)) { |
| 272 | ServiceInfo serviceInfo = entry.serviceInfo; |
| 273 | if (serviceInfo != null) { |
| 274 | boolean hasServiceBindPermission = serviceInfo.permission != null && |
| 275 | serviceInfo.permission.equals( |
| 276 | Manifest.permission.BIND_INCALL_SERVICE); |
| 277 | boolean hasControlInCallPermission = packageManager.checkPermission( |
| 278 | Manifest.permission.CONTROL_INCALL_EXPERIENCE, |
| 279 | serviceInfo.packageName) == PackageManager.PERMISSION_GRANTED; |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 280 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 281 | if (!hasServiceBindPermission) { |
| 282 | Log.w(this, "InCallService does not have BIND_INCALL_SERVICE permission: " + |
| 283 | serviceInfo.packageName); |
| 284 | continue; |
| 285 | } |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 286 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 287 | if (!hasControlInCallPermission) { |
| 288 | Log.w(this, |
| 289 | "InCall UI does not have CONTROL_INCALL_EXPERIENCE permission: " + |
| 290 | serviceInfo.packageName); |
| 291 | continue; |
| 292 | } |
| 293 | |
| 294 | Log.i(this, "Attempting to bind to InCall " + serviceInfo.packageName); |
| 295 | InCallServiceConnection inCallServiceConnection = new InCallServiceConnection(); |
| 296 | ComponentName componentName = new ComponentName(serviceInfo.packageName, |
| 297 | serviceInfo.name); |
| 298 | intent.setComponent(componentName); |
| 299 | |
| 300 | if (context.bindServiceAsUser(intent, inCallServiceConnection, |
| 301 | Context.BIND_AUTO_CREATE, UserHandle.CURRENT)) { |
| 302 | mServiceConnections.put(componentName, inCallServiceConnection); |
| 303 | } |
| 304 | } |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 310 | * Persists the {@link IInCallService} instance and starts the communication between |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 311 | * 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] | 312 | * called after a successful binding connection is established. |
| 313 | * |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 314 | * @param componentName The service {@link ComponentName}. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 315 | * @param service The {@link IInCallService} implementation. |
| 316 | */ |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 317 | private void onConnected(ComponentName componentName, IBinder service) { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 318 | ThreadUtil.checkOnMainThread(); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 319 | |
| 320 | IInCallService inCallService = IInCallService.Stub.asInterface(service); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 321 | |
| 322 | try { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 323 | inCallService.setInCallAdapter(new InCallAdapter(CallsManager.getInstance(), |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 324 | mCallIdMapper)); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 325 | mInCallServices.put(componentName, inCallService); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 326 | } catch (RemoteException e) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 327 | Log.e(this, e, "Failed to set the in-call adapter."); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 328 | return; |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 331 | // Upon successful connection, send the state of the world to the service. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 332 | ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls(); |
| 333 | if (!calls.isEmpty()) { |
| 334 | for (Call call : calls) { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 335 | try { |
| 336 | // Track the call if we don't already know about it. |
| 337 | addCall(call); |
| 338 | |
| 339 | inCallService.addCall(toParcelableCall(call)); |
| 340 | } catch (RemoteException ignored) { |
| 341 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 342 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 343 | onAudioStateChanged(null, CallsManager.getInstance().getAudioState()); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 344 | } else { |
| 345 | unbind(); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 346 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /** |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 350 | * Cleans up an instance of in-call app after the service has been unbound. |
| 351 | * |
| 352 | * @param disconnectedComponent The {@link ComponentName} of the service which disconnected. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 353 | */ |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 354 | private void onDisconnected(ComponentName disconnectedComponent) { |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 355 | ThreadUtil.checkOnMainThread(); |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 356 | if (mInCallServices.containsKey(disconnectedComponent)) { |
| 357 | mInCallServices.remove(disconnectedComponent); |
| 358 | } |
| 359 | |
| 360 | // If the default in-call UI has disconnected, disconnect all calls and un-bind all other |
| 361 | // InCallService implementations. |
| 362 | if (disconnectedComponent.equals(mInCallComponentName)) { |
| 363 | Log.i(this, "In-call UI %s disconnected.", disconnectedComponent); |
| 364 | CallsManager.getInstance().disconnectAllCalls(); |
| 365 | |
| 366 | // Iterate through the in-call services, removing them as they are un-bound. |
| 367 | Iterator<Map.Entry<ComponentName, IInCallService>> it = |
| 368 | mInCallServices.entrySet().iterator(); |
| 369 | while (it.hasNext()) { |
| 370 | Map.Entry<ComponentName, IInCallService> entry = it.next(); |
| 371 | ComponentName componentName = entry.getKey(); |
| 372 | |
| 373 | InCallServiceConnection connection = mServiceConnections.remove(componentName); |
| 374 | it.remove(); |
| 375 | if (connection == null) { |
| 376 | continue; |
| 377 | } |
| 378 | |
| 379 | Log.i(this, "Unbinding other InCallService %s", componentName); |
| 380 | TelecommApp.getInstance().unbindService(connection); |
| 381 | } |
| 382 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 383 | } |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 384 | |
| 385 | private void updateCall(Call call) { |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 386 | if (!mInCallServices.isEmpty()) { |
| 387 | ParcelableCall parcelableCall = toParcelableCall(call); |
| 388 | Log.v(this, "updateCall %s ==> %s", call, parcelableCall); |
| 389 | for (IInCallService inCallService : mInCallServices.values()) { |
| 390 | try { |
| 391 | inCallService.updateCall(parcelableCall); |
| 392 | } catch (RemoteException ignored) { |
| 393 | } |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
Santos Cordon | 2583b67 | 2014-07-19 13:07:33 -0700 | [diff] [blame] | 398 | private ParcelableCall toParcelableCall(Call call) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 399 | String callId = mCallIdMapper.getCallId(call); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 400 | |
Sailesh Nepal | e20bc97 | 2014-07-09 21:22:36 -0700 | [diff] [blame] | 401 | int capabilities = call.getCallCapabilities(); |
Santos Cordon | 4b034a6 | 2014-07-18 13:42:05 -0700 | [diff] [blame] | 402 | if (CallsManager.getInstance().isAddCallCapable(call)) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 403 | capabilities |= PhoneCapabilities.ADD_CALL; |
Santos Cordon | 10838c2 | 2014-06-11 17:36:04 -0700 | [diff] [blame] | 404 | } |
Santos Cordon | 4b034a6 | 2014-07-18 13:42:05 -0700 | [diff] [blame] | 405 | if (!call.isEmergencyCall()) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 406 | capabilities |= PhoneCapabilities.MUTE; |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 407 | } |
Sailesh Nepal | e20bc97 | 2014-07-09 21:22:36 -0700 | [diff] [blame] | 408 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 409 | int state = call.getState(); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 410 | if (state == CallState.ABORTED) { |
| 411 | state = CallState.DISCONNECTED; |
| 412 | } |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 413 | |
| 414 | String parentCallId = null; |
| 415 | Call parentCall = call.getParentCall(); |
| 416 | if (parentCall != null) { |
| 417 | parentCallId = mCallIdMapper.getCallId(parentCall); |
| 418 | } |
| 419 | |
| 420 | long connectTimeMillis = call.getConnectTimeMillis(); |
| 421 | List<Call> childCalls = call.getChildCalls(); |
| 422 | List<String> childCallIds = new ArrayList<>(); |
| 423 | if (!childCalls.isEmpty()) { |
| 424 | connectTimeMillis = Long.MAX_VALUE; |
| 425 | for (Call child : childCalls) { |
| 426 | connectTimeMillis = Math.min(child.getConnectTimeMillis(), connectTimeMillis); |
| 427 | childCallIds.add(mCallIdMapper.getCallId(child)); |
| 428 | } |
| 429 | } |
| 430 | |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 431 | if (call.isRespondViaSmsCapable()) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 432 | capabilities |= PhoneCapabilities.RESPOND_VIA_TEXT; |
Ihab Awad | ff7493a | 2014-06-10 13:47:44 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 435 | Uri handle = call.getHandlePresentation() == PropertyPresentation.ALLOWED ? |
Sailesh Nepal | e8ecb98 | 2014-07-11 17:19:42 -0700 | [diff] [blame] | 436 | call.getHandle() : null; |
| 437 | String callerDisplayName = call.getCallerDisplayNamePresentation() == |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 438 | PropertyPresentation.ALLOWED ? call.getCallerDisplayName() : null; |
Sailesh Nepal | e8ecb98 | 2014-07-11 17:19:42 -0700 | [diff] [blame] | 439 | |
Santos Cordon | 12d6182 | 2014-07-29 16:02:20 -0700 | [diff] [blame] | 440 | List<Call> conferenceableCalls = call.getConferenceableCalls(); |
| 441 | List<String> conferenceableCallIds = new ArrayList<String>(conferenceableCalls.size()); |
| 442 | for (Call otherCall : conferenceableCalls) { |
| 443 | String otherId = mCallIdMapper.getCallId(otherCall); |
| 444 | if (otherId != null) { |
| 445 | conferenceableCallIds.add(otherId); |
| 446 | } |
| 447 | } |
| 448 | |
Santos Cordon | 2583b67 | 2014-07-19 13:07:33 -0700 | [diff] [blame] | 449 | return new ParcelableCall( |
| 450 | callId, |
| 451 | state, |
| 452 | call.getDisconnectCause(), |
| 453 | call.getDisconnectMessage(), |
| 454 | call.getCannedSmsResponses(), |
| 455 | capabilities, |
| 456 | connectTimeMillis, |
| 457 | handle, |
| 458 | call.getHandlePresentation(), |
| 459 | callerDisplayName, |
| 460 | call.getCallerDisplayNamePresentation(), |
| 461 | call.getGatewayInfo(), |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 462 | call.getTargetPhoneAccount(), |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 463 | call.getVideoProvider(), |
Santos Cordon | 2583b67 | 2014-07-19 13:07:33 -0700 | [diff] [blame] | 464 | parentCallId, |
| 465 | childCallIds, |
| 466 | call.getStatusHints(), |
Santos Cordon | 12d6182 | 2014-07-29 16:02:20 -0700 | [diff] [blame] | 467 | call.getVideoState(), |
Nancy Chen | a9d91da | 2014-08-12 14:31:06 -0700 | [diff] [blame] | 468 | conferenceableCallIds, |
| 469 | call.getExtras()); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 470 | } |
Tyler Gunn | 61b9210 | 2014-08-19 07:42:20 -0700 | [diff] [blame^] | 471 | |
| 472 | /** |
| 473 | * Adds the call to the list of calls tracked by the {@link InCallController}. |
| 474 | * @param call The call to add. |
| 475 | */ |
| 476 | private void addCall(Call call) { |
| 477 | if (mCallIdMapper.getCallId(call) == null) { |
| 478 | mCallIdMapper.addCall(call); |
| 479 | call.addListener(mCallListener); |
| 480 | } |
| 481 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 482 | } |