1 /* 2 * Copyright (C) 2013 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 android.telecom; 18 19 import android.annotation.SystemApi; 20 import android.bluetooth.BluetoothDevice; 21 import android.compat.annotation.UnsupportedAppUsage; 22 import android.os.Build; 23 import android.os.Bundle; 24 import android.util.ArrayMap; 25 26 import com.android.internal.annotations.GuardedBy; 27 28 import java.util.Collections; 29 import java.util.List; 30 import java.util.Map; 31 import java.util.Objects; 32 import java.util.concurrent.CopyOnWriteArrayList; 33 34 /** 35 * A unified virtual device providing a means of voice (and other) communication on a device. 36 * 37 * @hide 38 * @deprecated Use {@link InCallService} directly instead of using this class. 39 */ 40 @SystemApi 41 @Deprecated 42 public final class Phone { 43 44 public abstract static class Listener { 45 /** 46 * Called when the audio state changes. 47 * 48 * @param phone The {@code Phone} calling this method. 49 * @param audioState The new {@link AudioState}. 50 * 51 * @deprecated Use {@link #onCallAudioStateChanged(Phone, CallAudioState)} instead. 52 */ 53 @Deprecated onAudioStateChanged(Phone phone, AudioState audioState)54 public void onAudioStateChanged(Phone phone, AudioState audioState) { } 55 56 /** 57 * Called when the audio state changes. 58 * 59 * @param phone The {@code Phone} calling this method. 60 * @param callAudioState The new {@link CallAudioState}. 61 */ onCallAudioStateChanged(Phone phone, CallAudioState callAudioState)62 public void onCallAudioStateChanged(Phone phone, CallAudioState callAudioState) { } 63 64 /** 65 * Called to bring the in-call screen to the foreground. The in-call experience should 66 * respond immediately by coming to the foreground to inform the user of the state of 67 * ongoing {@code Call}s. 68 * 69 * @param phone The {@code Phone} calling this method. 70 * @param showDialpad If true, put up the dialpad when the screen is shown. 71 */ onBringToForeground(Phone phone, boolean showDialpad)72 public void onBringToForeground(Phone phone, boolean showDialpad) { } 73 74 /** 75 * Called when a {@code Call} has been added to this in-call session. The in-call user 76 * experience should add necessary state listeners to the specified {@code Call} and 77 * immediately start to show the user information about the existence 78 * and nature of this {@code Call}. Subsequent invocations of {@link #getCalls()} will 79 * include this {@code Call}. 80 * 81 * @param phone The {@code Phone} calling this method. 82 * @param call A newly added {@code Call}. 83 */ onCallAdded(Phone phone, Call call)84 public void onCallAdded(Phone phone, Call call) { } 85 86 /** 87 * Called when a {@code Call} has been removed from this in-call session. The in-call user 88 * experience should remove any state listeners from the specified {@code Call} and 89 * immediately stop displaying any information about this {@code Call}. 90 * Subsequent invocations of {@link #getCalls()} will no longer include this {@code Call}. 91 * 92 * @param phone The {@code Phone} calling this method. 93 * @param call A newly removed {@code Call}. 94 */ onCallRemoved(Phone phone, Call call)95 public void onCallRemoved(Phone phone, Call call) { } 96 97 /** 98 * Called when the {@code Phone} ability to add more calls changes. If the phone cannot 99 * support more calls then {@code canAddCall} is set to {@code false}. If it can, then it 100 * is set to {@code true}. 101 * 102 * @param phone The {@code Phone} calling this method. 103 * @param canAddCall Indicates whether an additional call can be added. 104 */ onCanAddCallChanged(Phone phone, boolean canAddCall)105 public void onCanAddCallChanged(Phone phone, boolean canAddCall) { } 106 107 /** 108 * Called to silence the ringer if a ringing call exists. 109 * 110 * @param phone The {@code Phone} calling this method. 111 */ onSilenceRinger(Phone phone)112 public void onSilenceRinger(Phone phone) { } 113 } 114 115 // TODO: replace all usages of this with the actual R constant from Build.VERSION_CODES 116 /** @hide */ 117 public static final int SDK_VERSION_R = 30; 118 119 // A Map allows us to track each Call by its Telecom-specified call ID 120 @GuardedBy("mLock") 121 private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>(); 122 123 // A List allows us to keep the Calls in a stable iteration order so that casually developed 124 // user interface components do not incur any spurious jank 125 private final List<Call> mCalls = new CopyOnWriteArrayList<>(); 126 127 // An unmodifiable view of the above List can be safely shared with subclass implementations 128 private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls); 129 130 private final InCallAdapter mInCallAdapter; 131 132 private CallAudioState mCallAudioState; 133 134 private final List<Listener> mListeners = new CopyOnWriteArrayList<>(); 135 136 private boolean mCanAddCall = true; 137 138 private final String mCallingPackage; 139 140 /** 141 * The Target SDK version of the InCallService implementation. 142 */ 143 private final int mTargetSdkVersion; 144 145 private final Object mLock = new Object(); 146 Phone(InCallAdapter adapter, String callingPackage, int targetSdkVersion)147 Phone(InCallAdapter adapter, String callingPackage, int targetSdkVersion) { 148 mInCallAdapter = adapter; 149 mCallingPackage = callingPackage; 150 mTargetSdkVersion = targetSdkVersion; 151 } 152 internalAddCall(ParcelableCall parcelableCall)153 final void internalAddCall(ParcelableCall parcelableCall) { 154 if (mTargetSdkVersion < SDK_VERSION_R 155 && parcelableCall.getState() == Call.STATE_AUDIO_PROCESSING) { 156 Log.i(this, "Skipping adding audio processing call for sdk compatibility"); 157 return; 158 } 159 160 Call call = getCallById(parcelableCall.getId()); 161 if (call == null) { 162 call = new Call(this, parcelableCall.getId(), mInCallAdapter, 163 parcelableCall.getState(), mCallingPackage, mTargetSdkVersion); 164 165 synchronized (mLock) { 166 mCallByTelecomCallId.put(parcelableCall.getId(), call); 167 mCalls.add(call); 168 } 169 170 checkCallTree(parcelableCall); 171 call.internalUpdate(parcelableCall, mCallByTelecomCallId); 172 fireCallAdded(call); 173 } else { 174 Log.w(this, "Call %s added, but it was already present", call.internalGetCallId()); 175 checkCallTree(parcelableCall); 176 call.internalUpdate(parcelableCall, mCallByTelecomCallId); 177 } 178 } 179 internalRemoveCall(Call call)180 final void internalRemoveCall(Call call) { 181 synchronized (mLock) { 182 mCallByTelecomCallId.remove(call.internalGetCallId()); 183 mCalls.remove(call); 184 } 185 186 InCallService.VideoCall videoCall = call.getVideoCall(); 187 if (videoCall != null) { 188 videoCall.destroy(); 189 } 190 fireCallRemoved(call); 191 } 192 internalUpdateCall(ParcelableCall parcelableCall)193 final void internalUpdateCall(ParcelableCall parcelableCall) { 194 if (mTargetSdkVersion < SDK_VERSION_R 195 && parcelableCall.getState() == Call.STATE_AUDIO_PROCESSING) { 196 Log.i(this, "removing audio processing call during update for sdk compatibility"); 197 Call call = getCallById(parcelableCall.getId()); 198 if (call != null) { 199 internalRemoveCall(call); 200 } 201 return; 202 } 203 204 Call call = getCallById(parcelableCall.getId()); 205 if (call != null) { 206 checkCallTree(parcelableCall); 207 call.internalUpdate(parcelableCall, mCallByTelecomCallId); 208 } else { 209 // This call may have come out of audio processing. Try adding it if our target sdk 210 // version is low enough. 211 // The only two allowable states coming out of audio processing are ACTIVE and 212 // SIMULATED_RINGING. 213 if (mTargetSdkVersion < SDK_VERSION_R && (parcelableCall.getState() == Call.STATE_ACTIVE 214 || parcelableCall.getState() == Call.STATE_SIMULATED_RINGING)) { 215 Log.i(this, "adding call during update for sdk compatibility"); 216 internalAddCall(parcelableCall); 217 } 218 } 219 } 220 getCallById(String callId)221 Call getCallById(String callId) { 222 synchronized (mLock) { 223 return mCallByTelecomCallId.get(callId); 224 } 225 } 226 internalSetPostDialWait(String telecomId, String remaining)227 final void internalSetPostDialWait(String telecomId, String remaining) { 228 Call call = getCallById(telecomId); 229 if (call != null) { 230 call.internalSetPostDialWait(remaining); 231 } 232 } 233 internalCallAudioStateChanged(CallAudioState callAudioState)234 final void internalCallAudioStateChanged(CallAudioState callAudioState) { 235 if (!Objects.equals(mCallAudioState, callAudioState)) { 236 mCallAudioState = callAudioState; 237 fireCallAudioStateChanged(callAudioState); 238 } 239 } 240 internalGetCallByTelecomId(String telecomId)241 final Call internalGetCallByTelecomId(String telecomId) { 242 return getCallById(telecomId); 243 } 244 internalBringToForeground(boolean showDialpad)245 final void internalBringToForeground(boolean showDialpad) { 246 fireBringToForeground(showDialpad); 247 } 248 internalSetCanAddCall(boolean canAddCall)249 final void internalSetCanAddCall(boolean canAddCall) { 250 if (mCanAddCall != canAddCall) { 251 mCanAddCall = canAddCall; 252 fireCanAddCallChanged(canAddCall); 253 } 254 } 255 internalSilenceRinger()256 final void internalSilenceRinger() { 257 fireSilenceRinger(); 258 } 259 internalOnConnectionEvent(String telecomId, String event, Bundle extras)260 final void internalOnConnectionEvent(String telecomId, String event, Bundle extras) { 261 Call call = getCallById(telecomId); 262 if (call != null) { 263 call.internalOnConnectionEvent(event, extras); 264 } 265 } 266 internalOnRttUpgradeRequest(String callId, int requestId)267 final void internalOnRttUpgradeRequest(String callId, int requestId) { 268 Call call = getCallById(callId); 269 if (call != null) { 270 call.internalOnRttUpgradeRequest(requestId); 271 } 272 } 273 internalOnRttInitiationFailure(String callId, int reason)274 final void internalOnRttInitiationFailure(String callId, int reason) { 275 Call call = getCallById(callId); 276 if (call != null) { 277 call.internalOnRttInitiationFailure(reason); 278 } 279 } 280 internalOnHandoverFailed(String callId, int error)281 final void internalOnHandoverFailed(String callId, int error) { 282 Call call = getCallById(callId); 283 if (call != null) { 284 call.internalOnHandoverFailed(error); 285 } 286 } 287 internalOnHandoverComplete(String callId)288 final void internalOnHandoverComplete(String callId) { 289 Call call = getCallById(callId); 290 if (call != null) { 291 call.internalOnHandoverComplete(); 292 } 293 } 294 295 /** 296 * Called to destroy the phone and cleanup any lingering calls. 297 */ destroy()298 final void destroy() { 299 for (Call call : mCalls) { 300 InCallService.VideoCall videoCall = call.getVideoCall(); 301 if (videoCall != null) { 302 videoCall.destroy(); 303 } 304 if (call.getState() != Call.STATE_DISCONNECTED) { 305 call.internalSetDisconnected(); 306 } 307 } 308 } 309 310 /** 311 * Adds a listener to this {@code Phone}. 312 * 313 * @param listener A {@code Listener} object. 314 */ addListener(Listener listener)315 public final void addListener(Listener listener) { 316 mListeners.add(listener); 317 } 318 319 /** 320 * Removes a listener from this {@code Phone}. 321 * 322 * @param listener A {@code Listener} object. 323 */ removeListener(Listener listener)324 public final void removeListener(Listener listener) { 325 if (listener != null) { 326 mListeners.remove(listener); 327 } 328 } 329 330 /** 331 * Obtains the current list of {@code Call}s to be displayed by this in-call experience. 332 * 333 * @return A list of the relevant {@code Call}s. 334 */ getCalls()335 public final List<Call> getCalls() { 336 return mUnmodifiableCalls; 337 } 338 339 /** 340 * Returns if the {@code Phone} can support additional calls. 341 * 342 * @return Whether the phone supports adding more calls. 343 */ canAddCall()344 public final boolean canAddCall() { 345 return mCanAddCall; 346 } 347 348 /** 349 * Sets the microphone mute state. When this request is honored, there will be change to 350 * the {@link #getAudioState()}. 351 * 352 * @param state {@code true} if the microphone should be muted; {@code false} otherwise. 353 */ setMuted(boolean state)354 public final void setMuted(boolean state) { 355 mInCallAdapter.mute(state); 356 } 357 358 /** 359 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will 360 * be change to the {@link #getAudioState()}. 361 * 362 * @param route The audio route to use. 363 */ setAudioRoute(int route)364 public final void setAudioRoute(int route) { 365 mInCallAdapter.setAudioRoute(route); 366 } 367 368 /** 369 * Request audio routing to a specific bluetooth device. Calling this method may result in 370 * the device routing audio to a different bluetooth device than the one specified. A list of 371 * available devices can be obtained via {@link CallAudioState#getSupportedBluetoothDevices()} 372 * 373 * @param bluetoothAddress The address of the bluetooth device to connect to, as returned by 374 * {@link BluetoothDevice#getAddress()}, or {@code null} if no device is preferred. 375 */ requestBluetoothAudio(String bluetoothAddress)376 public void requestBluetoothAudio(String bluetoothAddress) { 377 mInCallAdapter.requestBluetoothAudio(bluetoothAddress); 378 } 379 380 /** 381 * Turns the proximity sensor on. When this request is made, the proximity sensor will 382 * become active, and the touch screen and display will be turned off when the user's face 383 * is detected to be in close proximity to the screen. This operation is a no-op on devices 384 * that do not have a proximity sensor. 385 * <p> 386 * This API does not actually turn on the proximity sensor; apps should do this on their own if 387 * required. 388 * @hide 389 */ 390 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196) setProximitySensorOn()391 public final void setProximitySensorOn() { 392 mInCallAdapter.turnProximitySensorOn(); 393 } 394 395 /** 396 * Turns the proximity sensor off. When this request is made, the proximity sensor will 397 * become inactive, and no longer affect the touch screen and display. This operation is a 398 * no-op on devices that do not have a proximity sensor. 399 * 400 * @param screenOnImmediately If true, the screen will be turned on immediately if it was 401 * previously off. Otherwise, the screen will only be turned on after the proximity sensor 402 * is no longer triggered. 403 * <p> 404 * This API does not actually turn of the proximity sensor; apps should do this on their own if 405 * required. 406 * @hide 407 */ 408 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196) setProximitySensorOff(boolean screenOnImmediately)409 public final void setProximitySensorOff(boolean screenOnImmediately) { 410 mInCallAdapter.turnProximitySensorOff(screenOnImmediately); 411 } 412 413 /** 414 * Obtains the current phone call audio state of the {@code Phone}. 415 * 416 * @return An object encapsulating the audio state. 417 * @deprecated Use {@link #getCallAudioState()} instead. 418 */ 419 @Deprecated getAudioState()420 public final AudioState getAudioState() { 421 return new AudioState(mCallAudioState); 422 } 423 424 /** 425 * Obtains the current phone call audio state of the {@code Phone}. 426 * 427 * @return An object encapsulating the audio state. 428 */ getCallAudioState()429 public final CallAudioState getCallAudioState() { 430 return mCallAudioState; 431 } 432 fireCallAdded(Call call)433 private void fireCallAdded(Call call) { 434 for (Listener listener : mListeners) { 435 listener.onCallAdded(this, call); 436 } 437 } 438 fireCallRemoved(Call call)439 private void fireCallRemoved(Call call) { 440 for (Listener listener : mListeners) { 441 listener.onCallRemoved(this, call); 442 } 443 } 444 fireCallAudioStateChanged(CallAudioState audioState)445 private void fireCallAudioStateChanged(CallAudioState audioState) { 446 for (Listener listener : mListeners) { 447 listener.onCallAudioStateChanged(this, audioState); 448 listener.onAudioStateChanged(this, new AudioState(audioState)); 449 } 450 } 451 fireBringToForeground(boolean showDialpad)452 private void fireBringToForeground(boolean showDialpad) { 453 for (Listener listener : mListeners) { 454 listener.onBringToForeground(this, showDialpad); 455 } 456 } 457 fireCanAddCallChanged(boolean canAddCall)458 private void fireCanAddCallChanged(boolean canAddCall) { 459 for (Listener listener : mListeners) { 460 listener.onCanAddCallChanged(this, canAddCall); 461 } 462 } 463 fireSilenceRinger()464 private void fireSilenceRinger() { 465 for (Listener listener : mListeners) { 466 listener.onSilenceRinger(this); 467 } 468 } 469 checkCallTree(ParcelableCall parcelableCall)470 private void checkCallTree(ParcelableCall parcelableCall) { 471 if (parcelableCall.getChildCallIds() != null) { 472 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) { 473 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) { 474 Log.wtf(this, "ParcelableCall %s has nonexistent child %s", 475 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i)); 476 } 477 } 478 } 479 } 480 } 481