blob: d3ccd2cadbf5729286ba15f91a31350da28a9d52 [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 Liua98f58b52017-11-07 17:59:28 -080021import android.bluetooth.BluetoothDevice;
Tyler Gunn876dbfb2016-03-14 15:18:07 -070022import android.os.Bundle;
Hall Liua98f58b52017-11-07 17:59:28 -080023import android.os.RemoteException;
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
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700113 // A Map allows us to track each Call by its Telecom-specified call ID
114 private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700115
116 // A List allows us to keep the Calls in a stable iteration order so that casually developed
117 // user interface components do not incur any spurious jank
Santos Cordonf30d7e92014-08-26 09:54:33 -0700118 private final List<Call> mCalls = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700119
120 // An unmodifiable view of the above List can be safely shared with subclass implementations
121 private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls);
122
123 private final InCallAdapter mInCallAdapter;
124
Yorke Lee4af59352015-05-13 14:14:54 -0700125 private CallAudioState mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700126
Jay Shrauner229e3822014-08-15 09:23:07 -0700127 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700128
Santos Cordon6c912b72014-11-07 16:05:09 -0800129 private boolean mCanAddCall = true;
130
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800131 private final String mCallingPackage;
132
Tyler Gunn159f35c2017-03-02 09:28:37 -0800133 /**
134 * The Target SDK version of the InCallService implementation.
135 */
136 private final int mTargetSdkVersion;
137
138 Phone(InCallAdapter adapter, String callingPackage, int targetSdkVersion) {
Ihab Awade63fadb2014-07-09 21:52:04 -0700139 mInCallAdapter = adapter;
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800140 mCallingPackage = callingPackage;
Tyler Gunn159f35c2017-03-02 09:28:37 -0800141 mTargetSdkVersion = targetSdkVersion;
Ihab Awade63fadb2014-07-09 21:52:04 -0700142 }
143
Santos Cordon88b771d2014-07-19 13:10:40 -0700144 final void internalAddCall(ParcelableCall parcelableCall) {
Shriram Ganeshddf570e2015-05-31 09:18:48 -0700145 Call call = new Call(this, parcelableCall.getId(), mInCallAdapter,
Tyler Gunn159f35c2017-03-02 09:28:37 -0800146 parcelableCall.getState(), mCallingPackage, mTargetSdkVersion);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700147 mCallByTelecomCallId.put(parcelableCall.getId(), call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700148 mCalls.add(call);
Santos Cordon88b771d2014-07-19 13:10:40 -0700149 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700150 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700151 fireCallAdded(call);
152 }
153
Ihab Awade63fadb2014-07-09 21:52:04 -0700154 final void internalRemoveCall(Call call) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700155 mCallByTelecomCallId.remove(call.internalGetCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700156 mCalls.remove(call);
Tyler Gunn75958422015-04-15 14:23:42 -0700157
158 InCallService.VideoCall videoCall = call.getVideoCall();
159 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700160 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700161 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700162 fireCallRemoved(call);
163 }
164
Santos Cordon88b771d2014-07-19 13:10:40 -0700165 final void internalUpdateCall(ParcelableCall parcelableCall) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700166 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700167 if (call != null) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700168 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700169 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700170 }
171 }
172
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700173 final void internalSetPostDialWait(String telecomId, String remaining) {
174 Call call = mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700175 if (call != null) {
176 call.internalSetPostDialWait(remaining);
177 }
178 }
179
Yorke Lee4af59352015-05-13 14:14:54 -0700180 final void internalCallAudioStateChanged(CallAudioState callAudioState) {
181 if (!Objects.equals(mCallAudioState, callAudioState)) {
182 mCallAudioState = callAudioState;
183 fireCallAudioStateChanged(callAudioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700184 }
185 }
186
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700187 final Call internalGetCallByTelecomId(String telecomId) {
188 return mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700189 }
190
Ihab Awade63fadb2014-07-09 21:52:04 -0700191 final void internalBringToForeground(boolean showDialpad) {
192 fireBringToForeground(showDialpad);
193 }
194
Santos Cordon6c912b72014-11-07 16:05:09 -0800195 final void internalSetCanAddCall(boolean canAddCall) {
196 if (mCanAddCall != canAddCall) {
197 mCanAddCall = canAddCall;
198 fireCanAddCallChanged(canAddCall);
199 }
200 }
201
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800202 final void internalSilenceRinger() {
203 fireSilenceRinger();
204 }
205
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700206 final void internalOnConnectionEvent(String telecomId, String event, Bundle extras) {
207 Call call = mCallByTelecomCallId.get(telecomId);
208 if (call != null) {
209 call.internalOnConnectionEvent(event, extras);
210 }
211 }
212
Hall Liu95d55872017-01-25 17:12:49 -0800213 final void internalOnRttUpgradeRequest(String callId, int requestId) {
214 Call call = mCallByTelecomCallId.get(callId);
215 if (call != null) {
216 call.internalOnRttUpgradeRequest(requestId);
217 }
218 }
219
Hall Liu57006aa2017-02-06 10:49:48 -0800220 final void internalOnRttInitiationFailure(String callId, int reason) {
221 Call call = mCallByTelecomCallId.get(callId);
222 if (call != null) {
223 call.internalOnRttInitiationFailure(reason);
224 }
225 }
226
Sanket Padawe85291f62017-12-01 13:59:27 -0800227 final void internalOnHandoverFailed(String callId, int error) {
228 Call call = mCallByTelecomCallId.get(callId);
229 if (call != null) {
230 call.internalOnHandoverFailed(error);
231 }
232 }
233
Tyler Gunn858bfaf2018-01-22 15:17:54 -0800234 final void internalOnHandoverComplete(String callId) {
235 Call call = mCallByTelecomCallId.get(callId);
236 if (call != null) {
237 call.internalOnHandoverComplete();
238 }
239 }
240
Ihab Awade63fadb2014-07-09 21:52:04 -0700241 /**
Santos Cordonf30d7e92014-08-26 09:54:33 -0700242 * Called to destroy the phone and cleanup any lingering calls.
Santos Cordonf30d7e92014-08-26 09:54:33 -0700243 */
244 final void destroy() {
245 for (Call call : mCalls) {
Tyler Gunn75958422015-04-15 14:23:42 -0700246 InCallService.VideoCall videoCall = call.getVideoCall();
247 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700248 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700249 }
Santos Cordonf30d7e92014-08-26 09:54:33 -0700250 if (call.getState() != Call.STATE_DISCONNECTED) {
251 call.internalSetDisconnected();
252 }
253 }
254 }
255
256 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700257 * Adds a listener to this {@code Phone}.
258 *
259 * @param listener A {@code Listener} object.
260 */
261 public final void addListener(Listener listener) {
262 mListeners.add(listener);
263 }
264
265 /**
266 * Removes a listener from this {@code Phone}.
267 *
268 * @param listener A {@code Listener} object.
269 */
270 public final void removeListener(Listener listener) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700271 if (listener != null) {
272 mListeners.remove(listener);
273 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700274 }
275
276 /**
277 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
278 *
279 * @return A list of the relevant {@code Call}s.
280 */
281 public final List<Call> getCalls() {
282 return mUnmodifiableCalls;
283 }
284
285 /**
Santos Cordon6c912b72014-11-07 16:05:09 -0800286 * Returns if the {@code Phone} can support additional calls.
287 *
288 * @return Whether the phone supports adding more calls.
289 */
290 public final boolean canAddCall() {
291 return mCanAddCall;
292 }
293
294 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700295 * Sets the microphone mute state. When this request is honored, there will be change to
296 * the {@link #getAudioState()}.
297 *
298 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
299 */
300 public final void setMuted(boolean state) {
301 mInCallAdapter.mute(state);
302 }
303
304 /**
305 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
306 * be change to the {@link #getAudioState()}.
307 *
308 * @param route The audio route to use.
309 */
310 public final void setAudioRoute(int route) {
311 mInCallAdapter.setAudioRoute(route);
312 }
313
314 /**
Hall Liua98f58b52017-11-07 17:59:28 -0800315 * Request audio routing to a specific bluetooth device. Calling this method may result in
316 * the device routing audio to a different bluetooth device than the one specified. A list of
317 * available devices can be obtained via {@link CallAudioState#getSupportedBluetoothDevices()}
318 *
319 * @param bluetoothAddress The address of the bluetooth device to connect to, as returned by
320 * {@link BluetoothDevice#getAddress()}, or {@code null} if no device is preferred.
321 */
322 public void requestBluetoothAudio(String bluetoothAddress) {
323 mInCallAdapter.requestBluetoothAudio(bluetoothAddress);
324 }
325
326 /**
Yorke Lee0d6ea712014-07-28 14:39:23 -0700327 * Turns the proximity sensor on. When this request is made, the proximity sensor will
328 * become active, and the touch screen and display will be turned off when the user's face
329 * is detected to be in close proximity to the screen. This operation is a no-op on devices
330 * that do not have a proximity sensor.
Yorke Lee22244d02015-04-14 12:34:28 -0700331 *
332 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700333 */
Mathew Inwood42346d12018-08-01 11:33:05 +0100334 @UnsupportedAppUsage
Yorke Lee0d6ea712014-07-28 14:39:23 -0700335 public final void setProximitySensorOn() {
336 mInCallAdapter.turnProximitySensorOn();
337 }
338
339 /**
340 * Turns the proximity sensor off. When this request is made, the proximity sensor will
341 * become inactive, and no longer affect the touch screen and display. This operation is a
342 * no-op on devices that do not have a proximity sensor.
343 *
344 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
345 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
346 * is no longer triggered.
Yorke Lee22244d02015-04-14 12:34:28 -0700347 *
348 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700349 */
Mathew Inwood42346d12018-08-01 11:33:05 +0100350 @UnsupportedAppUsage
Yorke Lee0d6ea712014-07-28 14:39:23 -0700351 public final void setProximitySensorOff(boolean screenOnImmediately) {
352 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
353 }
354
355 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700356 * Obtains the current phone call audio state of the {@code Phone}.
357 *
358 * @return An object encapsulating the audio state.
Yorke Lee4af59352015-05-13 14:14:54 -0700359 * @deprecated Use {@link #getCallAudioState()} instead.
Ihab Awade63fadb2014-07-09 21:52:04 -0700360 */
Yorke Lee4af59352015-05-13 14:14:54 -0700361 @Deprecated
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700362 public final AudioState getAudioState() {
Yorke Lee4af59352015-05-13 14:14:54 -0700363 return new AudioState(mCallAudioState);
364 }
365
366 /**
367 * Obtains the current phone call audio state of the {@code Phone}.
368 *
369 * @return An object encapsulating the audio state.
370 */
371 public final CallAudioState getCallAudioState() {
372 return mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700373 }
374
375 private void fireCallAdded(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700376 for (Listener listener : mListeners) {
377 listener.onCallAdded(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700378 }
379 }
380
381 private void fireCallRemoved(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700382 for (Listener listener : mListeners) {
383 listener.onCallRemoved(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700384 }
385 }
386
Yorke Lee4af59352015-05-13 14:14:54 -0700387 private void fireCallAudioStateChanged(CallAudioState audioState) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700388 for (Listener listener : mListeners) {
Yorke Lee4af59352015-05-13 14:14:54 -0700389 listener.onCallAudioStateChanged(this, audioState);
390 listener.onAudioStateChanged(this, new AudioState(audioState));
Ihab Awade63fadb2014-07-09 21:52:04 -0700391 }
392 }
393
394 private void fireBringToForeground(boolean showDialpad) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700395 for (Listener listener : mListeners) {
396 listener.onBringToForeground(this, showDialpad);
Ihab Awade63fadb2014-07-09 21:52:04 -0700397 }
398 }
399
Santos Cordon6c912b72014-11-07 16:05:09 -0800400 private void fireCanAddCallChanged(boolean canAddCall) {
401 for (Listener listener : mListeners) {
402 listener.onCanAddCallChanged(this, canAddCall);
403 }
404 }
405
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800406 private void fireSilenceRinger() {
407 for (Listener listener : mListeners) {
408 listener.onSilenceRinger(this);
409 }
410 }
411
Santos Cordon88b771d2014-07-19 13:10:40 -0700412 private void checkCallTree(ParcelableCall parcelableCall) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700413 if (parcelableCall.getChildCallIds() != null) {
414 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700415 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700416 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
417 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
Ihab Awade63fadb2014-07-09 21:52:04 -0700418 }
419 }
420 }
421 }
422}