blob: 61a639a1a2352fe0387ffe8b6501e4e51c211f66 [file] [log] [blame]
Ihab Awade63fadb2014-07-09 21:52:04 -07001/*
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
Tyler Gunnef9f6f92014-09-12 22:16:17 -070017package android.telecom;
Ihab Awade63fadb2014-07-09 21:52:04 -070018
Santos Cordon29886d82015-04-16 15:34:07 -070019import android.annotation.SystemApi;
Austin Wanga63a2c02019-12-19 06:38:19 +000020import android.annotation.UnsupportedAppUsage;
Hall Liua98f58b2017-11-07 17:59:28 -080021import android.bluetooth.BluetoothDevice;
Tyler Gunn17933eb2019-03-05 13:58:45 -080022import android.os.Build;
Tyler Gunn876dbfb2016-03-14 15:18:07 -070023import android.os.Bundle;
Ihab Awade63fadb2014-07-09 21:52:04 -070024import android.util.ArrayMap;
25
Ihab Awade63fadb2014-07-09 21:52:04 -070026import java.util.Collections;
27import java.util.List;
28import java.util.Map;
29import java.util.Objects;
Jay Shrauner229e3822014-08-15 09:23:07 -070030import java.util.concurrent.CopyOnWriteArrayList;
Ihab Awade63fadb2014-07-09 21:52:04 -070031
32/**
33 * A unified virtual device providing a means of voice (and other) communication on a device.
Santos Cordon29886d82015-04-16 15:34:07 -070034 *
35 * @hide
36 * @deprecated Use {@link InCallService} directly instead of using this class.
Ihab Awade63fadb2014-07-09 21:52:04 -070037 */
Santos Cordon29886d82015-04-16 15:34:07 -070038@SystemApi
39@Deprecated
Ihab Awade63fadb2014-07-09 21:52:04 -070040public final class Phone {
41
42 public abstract static class Listener {
43 /**
44 * Called when the audio state changes.
45 *
46 * @param phone The {@code Phone} calling this method.
Ihab Awadb19a0bc2014-08-07 19:46:01 -070047 * @param audioState The new {@link AudioState}.
Yorke Lee4af59352015-05-13 14:14:54 -070048 *
49 * @deprecated Use {@link #onCallAudioStateChanged(Phone, CallAudioState)} instead.
Ihab Awade63fadb2014-07-09 21:52:04 -070050 */
Yorke Lee4af59352015-05-13 14:14:54 -070051 @Deprecated
Ihab Awadb19a0bc2014-08-07 19:46:01 -070052 public void onAudioStateChanged(Phone phone, AudioState audioState) { }
Ihab Awade63fadb2014-07-09 21:52:04 -070053
54 /**
Yorke Lee4af59352015-05-13 14:14:54 -070055 * Called when the audio state changes.
56 *
57 * @param phone The {@code Phone} calling this method.
58 * @param callAudioState The new {@link CallAudioState}.
59 */
60 public void onCallAudioStateChanged(Phone phone, CallAudioState callAudioState) { }
61
62 /**
Ihab Awade63fadb2014-07-09 21:52:04 -070063 * Called to bring the in-call screen to the foreground. The in-call experience should
64 * respond immediately by coming to the foreground to inform the user of the state of
65 * ongoing {@code Call}s.
66 *
67 * @param phone The {@code Phone} calling this method.
68 * @param showDialpad If true, put up the dialpad when the screen is shown.
69 */
70 public void onBringToForeground(Phone phone, boolean showDialpad) { }
71
72 /**
73 * Called when a {@code Call} has been added to this in-call session. The in-call user
74 * experience should add necessary state listeners to the specified {@code Call} and
75 * immediately start to show the user information about the existence
76 * and nature of this {@code Call}. Subsequent invocations of {@link #getCalls()} will
77 * include this {@code Call}.
78 *
79 * @param phone The {@code Phone} calling this method.
80 * @param call A newly added {@code Call}.
81 */
82 public void onCallAdded(Phone phone, Call call) { }
83
84 /**
85 * Called when a {@code Call} has been removed from this in-call session. The in-call user
86 * experience should remove any state listeners from the specified {@code Call} and
87 * immediately stop displaying any information about this {@code Call}.
88 * Subsequent invocations of {@link #getCalls()} will no longer include this {@code Call}.
89 *
90 * @param phone The {@code Phone} calling this method.
91 * @param call A newly removed {@code Call}.
92 */
93 public void onCallRemoved(Phone phone, Call call) { }
Santos Cordon6c912b72014-11-07 16:05:09 -080094
95 /**
96 * Called when the {@code Phone} ability to add more calls changes. If the phone cannot
97 * support more calls then {@code canAddCall} is set to {@code false}. If it can, then it
98 * is set to {@code true}.
99 *
100 * @param phone The {@code Phone} calling this method.
101 * @param canAddCall Indicates whether an additional call can be added.
102 */
103 public void onCanAddCallChanged(Phone phone, boolean canAddCall) { }
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800104
105 /**
106 * Called to silence the ringer if a ringing call exists.
107 *
108 * @param phone The {@code Phone} calling this method.
109 */
110 public void onSilenceRinger(Phone phone) { }
Ihab Awade63fadb2014-07-09 21:52:04 -0700111 }
112
Hall Liu31de23d2019-10-11 15:38:29 -0700113 // TODO: replace all usages of this with the actual R constant from Build.VERSION_CODES
114 /** @hide */
115 public static final int SDK_VERSION_R = 30;
116
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700117 // A Map allows us to track each Call by its Telecom-specified call ID
118 private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700119
120 // A List allows us to keep the Calls in a stable iteration order so that casually developed
121 // user interface components do not incur any spurious jank
Santos Cordonf30d7e92014-08-26 09:54:33 -0700122 private final List<Call> mCalls = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700123
124 // An unmodifiable view of the above List can be safely shared with subclass implementations
125 private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls);
126
127 private final InCallAdapter mInCallAdapter;
128
Yorke Lee4af59352015-05-13 14:14:54 -0700129 private CallAudioState mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700130
Jay Shrauner229e3822014-08-15 09:23:07 -0700131 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700132
Santos Cordon6c912b72014-11-07 16:05:09 -0800133 private boolean mCanAddCall = true;
134
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800135 private final String mCallingPackage;
136
Tyler Gunn159f35c2017-03-02 09:28:37 -0800137 /**
138 * The Target SDK version of the InCallService implementation.
139 */
140 private final int mTargetSdkVersion;
141
142 Phone(InCallAdapter adapter, String callingPackage, int targetSdkVersion) {
Ihab Awade63fadb2014-07-09 21:52:04 -0700143 mInCallAdapter = adapter;
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800144 mCallingPackage = callingPackage;
Tyler Gunn159f35c2017-03-02 09:28:37 -0800145 mTargetSdkVersion = targetSdkVersion;
Ihab Awade63fadb2014-07-09 21:52:04 -0700146 }
147
Santos Cordon88b771d2014-07-19 13:10:40 -0700148 final void internalAddCall(ParcelableCall parcelableCall) {
Hall Liu31de23d2019-10-11 15:38:29 -0700149 if (mTargetSdkVersion < SDK_VERSION_R
150 && parcelableCall.getState() == Call.STATE_AUDIO_PROCESSING) {
151 Log.i(this, "Skipping adding audio processing call for sdk compatibility");
152 return;
153 }
154
Hall Liufc2be9c2019-11-08 18:26:47 -0800155 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
156 if (call == null) {
157 call = new Call(this, parcelableCall.getId(), mInCallAdapter,
158 parcelableCall.getState(), mCallingPackage, mTargetSdkVersion);
159 mCallByTelecomCallId.put(parcelableCall.getId(), call);
160 mCalls.add(call);
161 checkCallTree(parcelableCall);
162 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
163 fireCallAdded(call);
164 } else {
165 Log.w(this, "Call %s added, but it was already present", call.internalGetCallId());
166 checkCallTree(parcelableCall);
167 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
168 }
Hall Liu31de23d2019-10-11 15:38:29 -0700169 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700170
Ihab Awade63fadb2014-07-09 21:52:04 -0700171 final void internalRemoveCall(Call call) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700172 mCallByTelecomCallId.remove(call.internalGetCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700173 mCalls.remove(call);
Tyler Gunn75958422015-04-15 14:23:42 -0700174
175 InCallService.VideoCall videoCall = call.getVideoCall();
176 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700177 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700178 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700179 fireCallRemoved(call);
180 }
181
Santos Cordon88b771d2014-07-19 13:10:40 -0700182 final void internalUpdateCall(ParcelableCall parcelableCall) {
Hall Liu31de23d2019-10-11 15:38:29 -0700183 if (mTargetSdkVersion < SDK_VERSION_R
184 && parcelableCall.getState() == Call.STATE_AUDIO_PROCESSING) {
185 Log.i(this, "removing audio processing call during update for sdk compatibility");
186 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
187 if (call != null) {
188 internalRemoveCall(call);
189 }
190 return;
191 }
192
193 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
194 if (call != null) {
195 checkCallTree(parcelableCall);
196 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
197 } else {
198 // This call may have come out of audio processing. Try adding it if our target sdk
199 // version is low enough.
Hall Liufc2be9c2019-11-08 18:26:47 -0800200 // The only two allowable states coming out of audio processing are ACTIVE and
201 // SIMULATED_RINGING.
202 if (mTargetSdkVersion < SDK_VERSION_R && (parcelableCall.getState() == Call.STATE_ACTIVE
203 || parcelableCall.getState() == Call.STATE_SIMULATED_RINGING)) {
204 Log.i(this, "adding call during update for sdk compatibility");
Hall Liu31de23d2019-10-11 15:38:29 -0700205 internalAddCall(parcelableCall);
206 }
207 }
208 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700209
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700210 final void internalSetPostDialWait(String telecomId, String remaining) {
211 Call call = mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700212 if (call != null) {
213 call.internalSetPostDialWait(remaining);
214 }
215 }
216
Yorke Lee4af59352015-05-13 14:14:54 -0700217 final void internalCallAudioStateChanged(CallAudioState callAudioState) {
218 if (!Objects.equals(mCallAudioState, callAudioState)) {
219 mCallAudioState = callAudioState;
220 fireCallAudioStateChanged(callAudioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700221 }
222 }
223
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700224 final Call internalGetCallByTelecomId(String telecomId) {
225 return mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700226 }
227
Ihab Awade63fadb2014-07-09 21:52:04 -0700228 final void internalBringToForeground(boolean showDialpad) {
229 fireBringToForeground(showDialpad);
230 }
231
Santos Cordon6c912b72014-11-07 16:05:09 -0800232 final void internalSetCanAddCall(boolean canAddCall) {
233 if (mCanAddCall != canAddCall) {
234 mCanAddCall = canAddCall;
235 fireCanAddCallChanged(canAddCall);
236 }
237 }
238
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800239 final void internalSilenceRinger() {
240 fireSilenceRinger();
241 }
242
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700243 final void internalOnConnectionEvent(String telecomId, String event, Bundle extras) {
244 Call call = mCallByTelecomCallId.get(telecomId);
245 if (call != null) {
246 call.internalOnConnectionEvent(event, extras);
247 }
248 }
249
Hall Liu95d55872017-01-25 17:12:49 -0800250 final void internalOnRttUpgradeRequest(String callId, int requestId) {
251 Call call = mCallByTelecomCallId.get(callId);
252 if (call != null) {
253 call.internalOnRttUpgradeRequest(requestId);
254 }
255 }
256
Hall Liu57006aa2017-02-06 10:49:48 -0800257 final void internalOnRttInitiationFailure(String callId, int reason) {
258 Call call = mCallByTelecomCallId.get(callId);
259 if (call != null) {
260 call.internalOnRttInitiationFailure(reason);
261 }
262 }
263
Sanket Padawe85291f62017-12-01 13:59:27 -0800264 final void internalOnHandoverFailed(String callId, int error) {
265 Call call = mCallByTelecomCallId.get(callId);
266 if (call != null) {
267 call.internalOnHandoverFailed(error);
268 }
269 }
270
Tyler Gunn858bfaf2018-01-22 15:17:54 -0800271 final void internalOnHandoverComplete(String callId) {
272 Call call = mCallByTelecomCallId.get(callId);
273 if (call != null) {
274 call.internalOnHandoverComplete();
275 }
276 }
277
Ihab Awade63fadb2014-07-09 21:52:04 -0700278 /**
Santos Cordonf30d7e92014-08-26 09:54:33 -0700279 * Called to destroy the phone and cleanup any lingering calls.
Santos Cordonf30d7e92014-08-26 09:54:33 -0700280 */
281 final void destroy() {
282 for (Call call : mCalls) {
Tyler Gunn75958422015-04-15 14:23:42 -0700283 InCallService.VideoCall videoCall = call.getVideoCall();
284 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700285 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700286 }
Santos Cordonf30d7e92014-08-26 09:54:33 -0700287 if (call.getState() != Call.STATE_DISCONNECTED) {
288 call.internalSetDisconnected();
289 }
290 }
291 }
292
293 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700294 * Adds a listener to this {@code Phone}.
295 *
296 * @param listener A {@code Listener} object.
297 */
298 public final void addListener(Listener listener) {
299 mListeners.add(listener);
300 }
301
302 /**
303 * Removes a listener from this {@code Phone}.
304 *
305 * @param listener A {@code Listener} object.
306 */
307 public final void removeListener(Listener listener) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700308 if (listener != null) {
309 mListeners.remove(listener);
310 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700311 }
312
313 /**
314 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
315 *
316 * @return A list of the relevant {@code Call}s.
317 */
318 public final List<Call> getCalls() {
319 return mUnmodifiableCalls;
320 }
321
322 /**
Santos Cordon6c912b72014-11-07 16:05:09 -0800323 * Returns if the {@code Phone} can support additional calls.
324 *
325 * @return Whether the phone supports adding more calls.
326 */
327 public final boolean canAddCall() {
328 return mCanAddCall;
329 }
330
331 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700332 * Sets the microphone mute state. When this request is honored, there will be change to
333 * the {@link #getAudioState()}.
334 *
335 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
336 */
337 public final void setMuted(boolean state) {
338 mInCallAdapter.mute(state);
339 }
340
341 /**
342 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
343 * be change to the {@link #getAudioState()}.
344 *
345 * @param route The audio route to use.
346 */
347 public final void setAudioRoute(int route) {
348 mInCallAdapter.setAudioRoute(route);
349 }
350
351 /**
Hall Liua98f58b2017-11-07 17:59:28 -0800352 * Request audio routing to a specific bluetooth device. Calling this method may result in
353 * the device routing audio to a different bluetooth device than the one specified. A list of
354 * available devices can be obtained via {@link CallAudioState#getSupportedBluetoothDevices()}
355 *
356 * @param bluetoothAddress The address of the bluetooth device to connect to, as returned by
357 * {@link BluetoothDevice#getAddress()}, or {@code null} if no device is preferred.
358 */
359 public void requestBluetoothAudio(String bluetoothAddress) {
360 mInCallAdapter.requestBluetoothAudio(bluetoothAddress);
361 }
362
363 /**
Yorke Lee0d6ea712014-07-28 14:39:23 -0700364 * Turns the proximity sensor on. When this request is made, the proximity sensor will
365 * become active, and the touch screen and display will be turned off when the user's face
366 * is detected to be in close proximity to the screen. This operation is a no-op on devices
367 * that do not have a proximity sensor.
Tyler Gunn17933eb2019-03-05 13:58:45 -0800368 * <p>
369 * This API does not actually turn on the proximity sensor; apps should do this on their own if
370 * required.
Yorke Lee22244d02015-04-14 12:34:28 -0700371 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700372 */
Tyler Gunn17933eb2019-03-05 13:58:45 -0800373 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196)
Yorke Lee0d6ea712014-07-28 14:39:23 -0700374 public final void setProximitySensorOn() {
375 mInCallAdapter.turnProximitySensorOn();
376 }
377
378 /**
379 * Turns the proximity sensor off. When this request is made, the proximity sensor will
380 * become inactive, and no longer affect the touch screen and display. This operation is a
381 * no-op on devices that do not have a proximity sensor.
382 *
383 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
384 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
385 * is no longer triggered.
Tyler Gunn17933eb2019-03-05 13:58:45 -0800386 * <p>
387 * This API does not actually turn of the proximity sensor; apps should do this on their own if
388 * required.
Yorke Lee22244d02015-04-14 12:34:28 -0700389 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700390 */
Tyler Gunn17933eb2019-03-05 13:58:45 -0800391 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196)
Yorke Lee0d6ea712014-07-28 14:39:23 -0700392 public final void setProximitySensorOff(boolean screenOnImmediately) {
393 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
394 }
395
396 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700397 * Obtains the current phone call audio state of the {@code Phone}.
398 *
399 * @return An object encapsulating the audio state.
Yorke Lee4af59352015-05-13 14:14:54 -0700400 * @deprecated Use {@link #getCallAudioState()} instead.
Ihab Awade63fadb2014-07-09 21:52:04 -0700401 */
Yorke Lee4af59352015-05-13 14:14:54 -0700402 @Deprecated
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700403 public final AudioState getAudioState() {
Yorke Lee4af59352015-05-13 14:14:54 -0700404 return new AudioState(mCallAudioState);
405 }
406
407 /**
408 * Obtains the current phone call audio state of the {@code Phone}.
409 *
410 * @return An object encapsulating the audio state.
411 */
412 public final CallAudioState getCallAudioState() {
413 return mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700414 }
415
416 private void fireCallAdded(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700417 for (Listener listener : mListeners) {
418 listener.onCallAdded(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700419 }
420 }
421
422 private void fireCallRemoved(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700423 for (Listener listener : mListeners) {
424 listener.onCallRemoved(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700425 }
426 }
427
Yorke Lee4af59352015-05-13 14:14:54 -0700428 private void fireCallAudioStateChanged(CallAudioState audioState) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700429 for (Listener listener : mListeners) {
Yorke Lee4af59352015-05-13 14:14:54 -0700430 listener.onCallAudioStateChanged(this, audioState);
431 listener.onAudioStateChanged(this, new AudioState(audioState));
Ihab Awade63fadb2014-07-09 21:52:04 -0700432 }
433 }
434
435 private void fireBringToForeground(boolean showDialpad) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700436 for (Listener listener : mListeners) {
437 listener.onBringToForeground(this, showDialpad);
Ihab Awade63fadb2014-07-09 21:52:04 -0700438 }
439 }
440
Santos Cordon6c912b72014-11-07 16:05:09 -0800441 private void fireCanAddCallChanged(boolean canAddCall) {
442 for (Listener listener : mListeners) {
443 listener.onCanAddCallChanged(this, canAddCall);
444 }
445 }
446
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800447 private void fireSilenceRinger() {
448 for (Listener listener : mListeners) {
449 listener.onSilenceRinger(this);
450 }
451 }
452
Santos Cordon88b771d2014-07-19 13:10:40 -0700453 private void checkCallTree(ParcelableCall parcelableCall) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700454 if (parcelableCall.getChildCallIds() != null) {
455 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700456 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700457 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
458 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
Ihab Awade63fadb2014-07-09 21:52:04 -0700459 }
460 }
461 }
462 }
463}