blob: 0cc052ef340df028a1389642391a0e7f59db9901 [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;
Mathew Inwood42346d12018-08-01 11:33:05 +010020import 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;
Hall Liua98f58b2017-11-07 17:59:28 -080024import android.os.RemoteException;
Ihab Awade63fadb2014-07-09 21:52:04 -070025import android.util.ArrayMap;
26
Ihab Awade63fadb2014-07-09 21:52:04 -070027import java.util.Collections;
28import java.util.List;
29import java.util.Map;
30import java.util.Objects;
Jay Shrauner229e3822014-08-15 09:23:07 -070031import java.util.concurrent.CopyOnWriteArrayList;
Ihab Awade63fadb2014-07-09 21:52:04 -070032
33/**
34 * A unified virtual device providing a means of voice (and other) communication on a device.
Santos Cordon29886d82015-04-16 15:34:07 -070035 *
36 * @hide
37 * @deprecated Use {@link InCallService} directly instead of using this class.
Ihab Awade63fadb2014-07-09 21:52:04 -070038 */
Santos Cordon29886d82015-04-16 15:34:07 -070039@SystemApi
40@Deprecated
Ihab Awade63fadb2014-07-09 21:52:04 -070041public final class Phone {
42
43 public abstract static class Listener {
44 /**
45 * Called when the audio state changes.
46 *
47 * @param phone The {@code Phone} calling this method.
Ihab Awadb19a0bc2014-08-07 19:46:01 -070048 * @param audioState The new {@link AudioState}.
Yorke Lee4af59352015-05-13 14:14:54 -070049 *
50 * @deprecated Use {@link #onCallAudioStateChanged(Phone, CallAudioState)} instead.
Ihab Awade63fadb2014-07-09 21:52:04 -070051 */
Yorke Lee4af59352015-05-13 14:14:54 -070052 @Deprecated
Ihab Awadb19a0bc2014-08-07 19:46:01 -070053 public void onAudioStateChanged(Phone phone, AudioState audioState) { }
Ihab Awade63fadb2014-07-09 21:52:04 -070054
55 /**
Yorke Lee4af59352015-05-13 14:14:54 -070056 * Called when the audio state changes.
57 *
58 * @param phone The {@code Phone} calling this method.
59 * @param callAudioState The new {@link CallAudioState}.
60 */
61 public void onCallAudioStateChanged(Phone phone, CallAudioState callAudioState) { }
62
63 /**
Ihab Awade63fadb2014-07-09 21:52:04 -070064 * Called to bring the in-call screen to the foreground. The in-call experience should
65 * respond immediately by coming to the foreground to inform the user of the state of
66 * ongoing {@code Call}s.
67 *
68 * @param phone The {@code Phone} calling this method.
69 * @param showDialpad If true, put up the dialpad when the screen is shown.
70 */
71 public void onBringToForeground(Phone phone, boolean showDialpad) { }
72
73 /**
74 * Called when a {@code Call} has been added to this in-call session. The in-call user
75 * experience should add necessary state listeners to the specified {@code Call} and
76 * immediately start to show the user information about the existence
77 * and nature of this {@code Call}. Subsequent invocations of {@link #getCalls()} will
78 * include this {@code Call}.
79 *
80 * @param phone The {@code Phone} calling this method.
81 * @param call A newly added {@code Call}.
82 */
83 public void onCallAdded(Phone phone, Call call) { }
84
85 /**
86 * Called when a {@code Call} has been removed from this in-call session. The in-call user
87 * experience should remove any state listeners from the specified {@code Call} and
88 * immediately stop displaying any information about this {@code Call}.
89 * Subsequent invocations of {@link #getCalls()} will no longer include this {@code Call}.
90 *
91 * @param phone The {@code Phone} calling this method.
92 * @param call A newly removed {@code Call}.
93 */
94 public void onCallRemoved(Phone phone, Call call) { }
Santos Cordon6c912b72014-11-07 16:05:09 -080095
96 /**
97 * Called when the {@code Phone} ability to add more calls changes. If the phone cannot
98 * support more calls then {@code canAddCall} is set to {@code false}. If it can, then it
99 * is set to {@code true}.
100 *
101 * @param phone The {@code Phone} calling this method.
102 * @param canAddCall Indicates whether an additional call can be added.
103 */
104 public void onCanAddCallChanged(Phone phone, boolean canAddCall) { }
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800105
106 /**
107 * Called to silence the ringer if a ringing call exists.
108 *
109 * @param phone The {@code Phone} calling this method.
110 */
111 public void onSilenceRinger(Phone phone) { }
Ihab Awade63fadb2014-07-09 21:52:04 -0700112 }
113
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700114 // A Map allows us to track each Call by its Telecom-specified call ID
115 private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700116
117 // A List allows us to keep the Calls in a stable iteration order so that casually developed
118 // user interface components do not incur any spurious jank
Santos Cordonf30d7e92014-08-26 09:54:33 -0700119 private final List<Call> mCalls = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700120
121 // An unmodifiable view of the above List can be safely shared with subclass implementations
122 private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls);
123
124 private final InCallAdapter mInCallAdapter;
125
Yorke Lee4af59352015-05-13 14:14:54 -0700126 private CallAudioState mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700127
Jay Shrauner229e3822014-08-15 09:23:07 -0700128 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700129
Santos Cordon6c912b72014-11-07 16:05:09 -0800130 private boolean mCanAddCall = true;
131
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800132 private final String mCallingPackage;
133
Tyler Gunn159f35c2017-03-02 09:28:37 -0800134 /**
135 * The Target SDK version of the InCallService implementation.
136 */
137 private final int mTargetSdkVersion;
138
139 Phone(InCallAdapter adapter, String callingPackage, int targetSdkVersion) {
Ihab Awade63fadb2014-07-09 21:52:04 -0700140 mInCallAdapter = adapter;
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800141 mCallingPackage = callingPackage;
Tyler Gunn159f35c2017-03-02 09:28:37 -0800142 mTargetSdkVersion = targetSdkVersion;
Ihab Awade63fadb2014-07-09 21:52:04 -0700143 }
144
Santos Cordon88b771d2014-07-19 13:10:40 -0700145 final void internalAddCall(ParcelableCall parcelableCall) {
Shriram Ganeshddf570e2015-05-31 09:18:48 -0700146 Call call = new Call(this, parcelableCall.getId(), mInCallAdapter,
Tyler Gunn159f35c2017-03-02 09:28:37 -0800147 parcelableCall.getState(), mCallingPackage, mTargetSdkVersion);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700148 mCallByTelecomCallId.put(parcelableCall.getId(), call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700149 mCalls.add(call);
Santos Cordon88b771d2014-07-19 13:10:40 -0700150 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700151 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700152 fireCallAdded(call);
153 }
154
Ihab Awade63fadb2014-07-09 21:52:04 -0700155 final void internalRemoveCall(Call call) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700156 mCallByTelecomCallId.remove(call.internalGetCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700157 mCalls.remove(call);
Tyler Gunn75958422015-04-15 14:23:42 -0700158
159 InCallService.VideoCall videoCall = call.getVideoCall();
160 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700161 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700162 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700163 fireCallRemoved(call);
164 }
165
Santos Cordon88b771d2014-07-19 13:10:40 -0700166 final void internalUpdateCall(ParcelableCall parcelableCall) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700167 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700168 if (call != null) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700169 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700170 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700171 }
172 }
173
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700174 final void internalSetPostDialWait(String telecomId, String remaining) {
175 Call call = mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700176 if (call != null) {
177 call.internalSetPostDialWait(remaining);
178 }
179 }
180
Yorke Lee4af59352015-05-13 14:14:54 -0700181 final void internalCallAudioStateChanged(CallAudioState callAudioState) {
182 if (!Objects.equals(mCallAudioState, callAudioState)) {
183 mCallAudioState = callAudioState;
184 fireCallAudioStateChanged(callAudioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700185 }
186 }
187
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700188 final Call internalGetCallByTelecomId(String telecomId) {
189 return mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700190 }
191
Ihab Awade63fadb2014-07-09 21:52:04 -0700192 final void internalBringToForeground(boolean showDialpad) {
193 fireBringToForeground(showDialpad);
194 }
195
Santos Cordon6c912b72014-11-07 16:05:09 -0800196 final void internalSetCanAddCall(boolean canAddCall) {
197 if (mCanAddCall != canAddCall) {
198 mCanAddCall = canAddCall;
199 fireCanAddCallChanged(canAddCall);
200 }
201 }
202
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800203 final void internalSilenceRinger() {
204 fireSilenceRinger();
205 }
206
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700207 final void internalOnConnectionEvent(String telecomId, String event, Bundle extras) {
208 Call call = mCallByTelecomCallId.get(telecomId);
209 if (call != null) {
210 call.internalOnConnectionEvent(event, extras);
211 }
212 }
213
Hall Liu95d55872017-01-25 17:12:49 -0800214 final void internalOnRttUpgradeRequest(String callId, int requestId) {
215 Call call = mCallByTelecomCallId.get(callId);
216 if (call != null) {
217 call.internalOnRttUpgradeRequest(requestId);
218 }
219 }
220
Hall Liu57006aa2017-02-06 10:49:48 -0800221 final void internalOnRttInitiationFailure(String callId, int reason) {
222 Call call = mCallByTelecomCallId.get(callId);
223 if (call != null) {
224 call.internalOnRttInitiationFailure(reason);
225 }
226 }
227
Sanket Padawe85291f62017-12-01 13:59:27 -0800228 final void internalOnHandoverFailed(String callId, int error) {
229 Call call = mCallByTelecomCallId.get(callId);
230 if (call != null) {
231 call.internalOnHandoverFailed(error);
232 }
233 }
234
Tyler Gunn858bfaf2018-01-22 15:17:54 -0800235 final void internalOnHandoverComplete(String callId) {
236 Call call = mCallByTelecomCallId.get(callId);
237 if (call != null) {
238 call.internalOnHandoverComplete();
239 }
240 }
241
Ihab Awade63fadb2014-07-09 21:52:04 -0700242 /**
Santos Cordonf30d7e92014-08-26 09:54:33 -0700243 * Called to destroy the phone and cleanup any lingering calls.
Santos Cordonf30d7e92014-08-26 09:54:33 -0700244 */
245 final void destroy() {
246 for (Call call : mCalls) {
Tyler Gunn75958422015-04-15 14:23:42 -0700247 InCallService.VideoCall videoCall = call.getVideoCall();
248 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700249 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700250 }
Santos Cordonf30d7e92014-08-26 09:54:33 -0700251 if (call.getState() != Call.STATE_DISCONNECTED) {
252 call.internalSetDisconnected();
253 }
254 }
255 }
256
257 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700258 * Adds a listener to this {@code Phone}.
259 *
260 * @param listener A {@code Listener} object.
261 */
262 public final void addListener(Listener listener) {
263 mListeners.add(listener);
264 }
265
266 /**
267 * Removes a listener from this {@code Phone}.
268 *
269 * @param listener A {@code Listener} object.
270 */
271 public final void removeListener(Listener listener) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700272 if (listener != null) {
273 mListeners.remove(listener);
274 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700275 }
276
277 /**
278 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
279 *
280 * @return A list of the relevant {@code Call}s.
281 */
282 public final List<Call> getCalls() {
283 return mUnmodifiableCalls;
284 }
285
286 /**
Santos Cordon6c912b72014-11-07 16:05:09 -0800287 * Returns if the {@code Phone} can support additional calls.
288 *
289 * @return Whether the phone supports adding more calls.
290 */
291 public final boolean canAddCall() {
292 return mCanAddCall;
293 }
294
295 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700296 * Sets the microphone mute state. When this request is honored, there will be change to
297 * the {@link #getAudioState()}.
298 *
299 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
300 */
301 public final void setMuted(boolean state) {
302 mInCallAdapter.mute(state);
303 }
304
305 /**
306 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
307 * be change to the {@link #getAudioState()}.
308 *
309 * @param route The audio route to use.
310 */
311 public final void setAudioRoute(int route) {
312 mInCallAdapter.setAudioRoute(route);
313 }
314
315 /**
Hall Liua98f58b2017-11-07 17:59:28 -0800316 * Request audio routing to a specific bluetooth device. Calling this method may result in
317 * the device routing audio to a different bluetooth device than the one specified. A list of
318 * available devices can be obtained via {@link CallAudioState#getSupportedBluetoothDevices()}
319 *
320 * @param bluetoothAddress The address of the bluetooth device to connect to, as returned by
321 * {@link BluetoothDevice#getAddress()}, or {@code null} if no device is preferred.
322 */
323 public void requestBluetoothAudio(String bluetoothAddress) {
324 mInCallAdapter.requestBluetoothAudio(bluetoothAddress);
325 }
326
327 /**
Yorke Lee0d6ea712014-07-28 14:39:23 -0700328 * Turns the proximity sensor on. When this request is made, the proximity sensor will
329 * become active, and the touch screen and display will be turned off when the user's face
330 * is detected to be in close proximity to the screen. This operation is a no-op on devices
331 * that do not have a proximity sensor.
Tyler Gunn17933eb2019-03-05 13:58:45 -0800332 * <p>
333 * This API does not actually turn on the proximity sensor; apps should do this on their own if
334 * required.
Yorke Lee22244d02015-04-14 12:34:28 -0700335 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700336 */
Tyler Gunn17933eb2019-03-05 13:58:45 -0800337 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196)
Yorke Lee0d6ea712014-07-28 14:39:23 -0700338 public final void setProximitySensorOn() {
339 mInCallAdapter.turnProximitySensorOn();
340 }
341
342 /**
343 * Turns the proximity sensor off. When this request is made, the proximity sensor will
344 * become inactive, and no longer affect the touch screen and display. This operation is a
345 * no-op on devices that do not have a proximity sensor.
346 *
347 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
348 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
349 * is no longer triggered.
Tyler Gunn17933eb2019-03-05 13:58:45 -0800350 * <p>
351 * This API does not actually turn of the proximity sensor; apps should do this on their own if
352 * required.
Yorke Lee22244d02015-04-14 12:34:28 -0700353 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700354 */
Tyler Gunn17933eb2019-03-05 13:58:45 -0800355 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 127403196)
Yorke Lee0d6ea712014-07-28 14:39:23 -0700356 public final void setProximitySensorOff(boolean screenOnImmediately) {
357 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
358 }
359
360 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700361 * Obtains the current phone call audio state of the {@code Phone}.
362 *
363 * @return An object encapsulating the audio state.
Yorke Lee4af59352015-05-13 14:14:54 -0700364 * @deprecated Use {@link #getCallAudioState()} instead.
Ihab Awade63fadb2014-07-09 21:52:04 -0700365 */
Yorke Lee4af59352015-05-13 14:14:54 -0700366 @Deprecated
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700367 public final AudioState getAudioState() {
Yorke Lee4af59352015-05-13 14:14:54 -0700368 return new AudioState(mCallAudioState);
369 }
370
371 /**
372 * Obtains the current phone call audio state of the {@code Phone}.
373 *
374 * @return An object encapsulating the audio state.
375 */
376 public final CallAudioState getCallAudioState() {
377 return mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700378 }
379
380 private void fireCallAdded(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700381 for (Listener listener : mListeners) {
382 listener.onCallAdded(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700383 }
384 }
385
386 private void fireCallRemoved(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700387 for (Listener listener : mListeners) {
388 listener.onCallRemoved(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700389 }
390 }
391
Yorke Lee4af59352015-05-13 14:14:54 -0700392 private void fireCallAudioStateChanged(CallAudioState audioState) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700393 for (Listener listener : mListeners) {
Yorke Lee4af59352015-05-13 14:14:54 -0700394 listener.onCallAudioStateChanged(this, audioState);
395 listener.onAudioStateChanged(this, new AudioState(audioState));
Ihab Awade63fadb2014-07-09 21:52:04 -0700396 }
397 }
398
399 private void fireBringToForeground(boolean showDialpad) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700400 for (Listener listener : mListeners) {
401 listener.onBringToForeground(this, showDialpad);
Ihab Awade63fadb2014-07-09 21:52:04 -0700402 }
403 }
404
Santos Cordon6c912b72014-11-07 16:05:09 -0800405 private void fireCanAddCallChanged(boolean canAddCall) {
406 for (Listener listener : mListeners) {
407 listener.onCanAddCallChanged(this, canAddCall);
408 }
409 }
410
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800411 private void fireSilenceRinger() {
412 for (Listener listener : mListeners) {
413 listener.onSilenceRinger(this);
414 }
415 }
416
Santos Cordon88b771d2014-07-19 13:10:40 -0700417 private void checkCallTree(ParcelableCall parcelableCall) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700418 if (parcelableCall.getChildCallIds() != null) {
419 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700420 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700421 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
422 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
Ihab Awade63fadb2014-07-09 21:52:04 -0700423 }
424 }
425 }
426 }
427}