blob: b5394b9b0290885c77b5e8f9c0afb14cdf76c251 [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;
Hall Liua98f58b2017-11-07 17:59:28 -080020import android.bluetooth.BluetoothDevice;
Tyler Gunn876dbfb2016-03-14 15:18:07 -070021import android.os.Bundle;
Hall Liua98f58b2017-11-07 17:59:28 -080022import android.os.RemoteException;
Ihab Awade63fadb2014-07-09 21:52:04 -070023import android.util.ArrayMap;
24
Ihab Awade63fadb2014-07-09 21:52:04 -070025import java.util.Collections;
26import java.util.List;
27import java.util.Map;
28import java.util.Objects;
Jay Shrauner229e3822014-08-15 09:23:07 -070029import java.util.concurrent.CopyOnWriteArrayList;
Ihab Awade63fadb2014-07-09 21:52:04 -070030
31/**
32 * A unified virtual device providing a means of voice (and other) communication on a device.
Santos Cordon29886d82015-04-16 15:34:07 -070033 *
34 * @hide
35 * @deprecated Use {@link InCallService} directly instead of using this class.
Ihab Awade63fadb2014-07-09 21:52:04 -070036 */
Santos Cordon29886d82015-04-16 15:34:07 -070037@SystemApi
38@Deprecated
Ihab Awade63fadb2014-07-09 21:52:04 -070039public final class Phone {
40
41 public abstract static class Listener {
42 /**
43 * Called when the audio state changes.
44 *
45 * @param phone The {@code Phone} calling this method.
Ihab Awadb19a0bc2014-08-07 19:46:01 -070046 * @param audioState The new {@link AudioState}.
Yorke Lee4af59352015-05-13 14:14:54 -070047 *
48 * @deprecated Use {@link #onCallAudioStateChanged(Phone, CallAudioState)} instead.
Ihab Awade63fadb2014-07-09 21:52:04 -070049 */
Yorke Lee4af59352015-05-13 14:14:54 -070050 @Deprecated
Ihab Awadb19a0bc2014-08-07 19:46:01 -070051 public void onAudioStateChanged(Phone phone, AudioState audioState) { }
Ihab Awade63fadb2014-07-09 21:52:04 -070052
53 /**
Yorke Lee4af59352015-05-13 14:14:54 -070054 * Called when the audio state changes.
55 *
56 * @param phone The {@code Phone} calling this method.
57 * @param callAudioState The new {@link CallAudioState}.
58 */
59 public void onCallAudioStateChanged(Phone phone, CallAudioState callAudioState) { }
60
61 /**
Ihab Awade63fadb2014-07-09 21:52:04 -070062 * Called to bring the in-call screen to the foreground. The in-call experience should
63 * respond immediately by coming to the foreground to inform the user of the state of
64 * ongoing {@code Call}s.
65 *
66 * @param phone The {@code Phone} calling this method.
67 * @param showDialpad If true, put up the dialpad when the screen is shown.
68 */
69 public void onBringToForeground(Phone phone, boolean showDialpad) { }
70
71 /**
72 * Called when a {@code Call} has been added to this in-call session. The in-call user
73 * experience should add necessary state listeners to the specified {@code Call} and
74 * immediately start to show the user information about the existence
75 * and nature of this {@code Call}. Subsequent invocations of {@link #getCalls()} will
76 * include this {@code Call}.
77 *
78 * @param phone The {@code Phone} calling this method.
79 * @param call A newly added {@code Call}.
80 */
81 public void onCallAdded(Phone phone, Call call) { }
82
83 /**
84 * Called when a {@code Call} has been removed from this in-call session. The in-call user
85 * experience should remove any state listeners from the specified {@code Call} and
86 * immediately stop displaying any information about this {@code Call}.
87 * Subsequent invocations of {@link #getCalls()} will no longer include this {@code Call}.
88 *
89 * @param phone The {@code Phone} calling this method.
90 * @param call A newly removed {@code Call}.
91 */
92 public void onCallRemoved(Phone phone, Call call) { }
Santos Cordon6c912b72014-11-07 16:05:09 -080093
94 /**
95 * Called when the {@code Phone} ability to add more calls changes. If the phone cannot
96 * support more calls then {@code canAddCall} is set to {@code false}. If it can, then it
97 * is set to {@code true}.
98 *
99 * @param phone The {@code Phone} calling this method.
100 * @param canAddCall Indicates whether an additional call can be added.
101 */
102 public void onCanAddCallChanged(Phone phone, boolean canAddCall) { }
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800103
104 /**
105 * Called to silence the ringer if a ringing call exists.
106 *
107 * @param phone The {@code Phone} calling this method.
108 */
109 public void onSilenceRinger(Phone phone) { }
Ihab Awade63fadb2014-07-09 21:52:04 -0700110 }
111
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700112 // A Map allows us to track each Call by its Telecom-specified call ID
113 private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700114
115 // A List allows us to keep the Calls in a stable iteration order so that casually developed
116 // user interface components do not incur any spurious jank
Santos Cordonf30d7e92014-08-26 09:54:33 -0700117 private final List<Call> mCalls = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700118
119 // An unmodifiable view of the above List can be safely shared with subclass implementations
120 private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls);
121
122 private final InCallAdapter mInCallAdapter;
123
Yorke Lee4af59352015-05-13 14:14:54 -0700124 private CallAudioState mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700125
Jay Shrauner229e3822014-08-15 09:23:07 -0700126 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700127
Santos Cordon6c912b72014-11-07 16:05:09 -0800128 private boolean mCanAddCall = true;
129
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800130 private final String mCallingPackage;
131
Tyler Gunn159f35c2017-03-02 09:28:37 -0800132 /**
133 * The Target SDK version of the InCallService implementation.
134 */
135 private final int mTargetSdkVersion;
136
137 Phone(InCallAdapter adapter, String callingPackage, int targetSdkVersion) {
Ihab Awade63fadb2014-07-09 21:52:04 -0700138 mInCallAdapter = adapter;
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800139 mCallingPackage = callingPackage;
Tyler Gunn159f35c2017-03-02 09:28:37 -0800140 mTargetSdkVersion = targetSdkVersion;
Ihab Awade63fadb2014-07-09 21:52:04 -0700141 }
142
Santos Cordon88b771d2014-07-19 13:10:40 -0700143 final void internalAddCall(ParcelableCall parcelableCall) {
Shriram Ganeshddf570e2015-05-31 09:18:48 -0700144 Call call = new Call(this, parcelableCall.getId(), mInCallAdapter,
Tyler Gunn159f35c2017-03-02 09:28:37 -0800145 parcelableCall.getState(), mCallingPackage, mTargetSdkVersion);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700146 mCallByTelecomCallId.put(parcelableCall.getId(), call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700147 mCalls.add(call);
Santos Cordon88b771d2014-07-19 13:10:40 -0700148 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700149 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700150 fireCallAdded(call);
151 }
152
Ihab Awade63fadb2014-07-09 21:52:04 -0700153 final void internalRemoveCall(Call call) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700154 mCallByTelecomCallId.remove(call.internalGetCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700155 mCalls.remove(call);
Tyler Gunn75958422015-04-15 14:23:42 -0700156
157 InCallService.VideoCall videoCall = call.getVideoCall();
158 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700159 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700160 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700161 fireCallRemoved(call);
162 }
163
Santos Cordon88b771d2014-07-19 13:10:40 -0700164 final void internalUpdateCall(ParcelableCall parcelableCall) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700165 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700166 if (call != null) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700167 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700168 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700169 }
170 }
171
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700172 final void internalSetPostDialWait(String telecomId, String remaining) {
173 Call call = mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700174 if (call != null) {
175 call.internalSetPostDialWait(remaining);
176 }
177 }
178
Yorke Lee4af59352015-05-13 14:14:54 -0700179 final void internalCallAudioStateChanged(CallAudioState callAudioState) {
180 if (!Objects.equals(mCallAudioState, callAudioState)) {
181 mCallAudioState = callAudioState;
182 fireCallAudioStateChanged(callAudioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700183 }
184 }
185
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700186 final Call internalGetCallByTelecomId(String telecomId) {
187 return mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700188 }
189
Ihab Awade63fadb2014-07-09 21:52:04 -0700190 final void internalBringToForeground(boolean showDialpad) {
191 fireBringToForeground(showDialpad);
192 }
193
Santos Cordon6c912b72014-11-07 16:05:09 -0800194 final void internalSetCanAddCall(boolean canAddCall) {
195 if (mCanAddCall != canAddCall) {
196 mCanAddCall = canAddCall;
197 fireCanAddCallChanged(canAddCall);
198 }
199 }
200
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800201 final void internalSilenceRinger() {
202 fireSilenceRinger();
203 }
204
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700205 final void internalOnConnectionEvent(String telecomId, String event, Bundle extras) {
206 Call call = mCallByTelecomCallId.get(telecomId);
207 if (call != null) {
208 call.internalOnConnectionEvent(event, extras);
209 }
210 }
211
Hall Liu95d55872017-01-25 17:12:49 -0800212 final void internalOnRttUpgradeRequest(String callId, int requestId) {
213 Call call = mCallByTelecomCallId.get(callId);
214 if (call != null) {
215 call.internalOnRttUpgradeRequest(requestId);
216 }
217 }
218
Hall Liu57006aa2017-02-06 10:49:48 -0800219 final void internalOnRttInitiationFailure(String callId, int reason) {
220 Call call = mCallByTelecomCallId.get(callId);
221 if (call != null) {
222 call.internalOnRttInitiationFailure(reason);
223 }
224 }
225
Sanket Padawee29a2662017-12-01 13:59:27 -0800226 final void internalOnHandoverFailed(String callId, int error) {
227 Call call = mCallByTelecomCallId.get(callId);
228 if (call != null) {
229 call.internalOnHandoverFailed(error);
230 }
231 }
232
Ihab Awade63fadb2014-07-09 21:52:04 -0700233 /**
Santos Cordonf30d7e92014-08-26 09:54:33 -0700234 * Called to destroy the phone and cleanup any lingering calls.
Santos Cordonf30d7e92014-08-26 09:54:33 -0700235 */
236 final void destroy() {
237 for (Call call : mCalls) {
Tyler Gunn75958422015-04-15 14:23:42 -0700238 InCallService.VideoCall videoCall = call.getVideoCall();
239 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700240 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700241 }
Santos Cordonf30d7e92014-08-26 09:54:33 -0700242 if (call.getState() != Call.STATE_DISCONNECTED) {
243 call.internalSetDisconnected();
244 }
245 }
246 }
247
248 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700249 * Adds a listener to this {@code Phone}.
250 *
251 * @param listener A {@code Listener} object.
252 */
253 public final void addListener(Listener listener) {
254 mListeners.add(listener);
255 }
256
257 /**
258 * Removes a listener from this {@code Phone}.
259 *
260 * @param listener A {@code Listener} object.
261 */
262 public final void removeListener(Listener listener) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700263 if (listener != null) {
264 mListeners.remove(listener);
265 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700266 }
267
268 /**
269 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
270 *
271 * @return A list of the relevant {@code Call}s.
272 */
273 public final List<Call> getCalls() {
274 return mUnmodifiableCalls;
275 }
276
277 /**
Santos Cordon6c912b72014-11-07 16:05:09 -0800278 * Returns if the {@code Phone} can support additional calls.
279 *
280 * @return Whether the phone supports adding more calls.
281 */
282 public final boolean canAddCall() {
283 return mCanAddCall;
284 }
285
286 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700287 * Sets the microphone mute state. When this request is honored, there will be change to
288 * the {@link #getAudioState()}.
289 *
290 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
291 */
292 public final void setMuted(boolean state) {
293 mInCallAdapter.mute(state);
294 }
295
296 /**
297 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
298 * be change to the {@link #getAudioState()}.
299 *
300 * @param route The audio route to use.
301 */
302 public final void setAudioRoute(int route) {
303 mInCallAdapter.setAudioRoute(route);
304 }
305
306 /**
Hall Liua98f58b2017-11-07 17:59:28 -0800307 * Request audio routing to a specific bluetooth device. Calling this method may result in
308 * the device routing audio to a different bluetooth device than the one specified. A list of
309 * available devices can be obtained via {@link CallAudioState#getSupportedBluetoothDevices()}
310 *
311 * @param bluetoothAddress The address of the bluetooth device to connect to, as returned by
312 * {@link BluetoothDevice#getAddress()}, or {@code null} if no device is preferred.
313 */
314 public void requestBluetoothAudio(String bluetoothAddress) {
315 mInCallAdapter.requestBluetoothAudio(bluetoothAddress);
316 }
317
318 /**
Yorke Lee0d6ea712014-07-28 14:39:23 -0700319 * Turns the proximity sensor on. When this request is made, the proximity sensor will
320 * become active, and the touch screen and display will be turned off when the user's face
321 * is detected to be in close proximity to the screen. This operation is a no-op on devices
322 * that do not have a proximity sensor.
Yorke Lee22244d02015-04-14 12:34:28 -0700323 *
324 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700325 */
326 public final void setProximitySensorOn() {
327 mInCallAdapter.turnProximitySensorOn();
328 }
329
330 /**
331 * Turns the proximity sensor off. When this request is made, the proximity sensor will
332 * become inactive, and no longer affect the touch screen and display. This operation is a
333 * no-op on devices that do not have a proximity sensor.
334 *
335 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
336 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
337 * is no longer triggered.
Yorke Lee22244d02015-04-14 12:34:28 -0700338 *
339 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700340 */
341 public final void setProximitySensorOff(boolean screenOnImmediately) {
342 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
343 }
344
345 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700346 * Obtains the current phone call audio state of the {@code Phone}.
347 *
348 * @return An object encapsulating the audio state.
Yorke Lee4af59352015-05-13 14:14:54 -0700349 * @deprecated Use {@link #getCallAudioState()} instead.
Ihab Awade63fadb2014-07-09 21:52:04 -0700350 */
Yorke Lee4af59352015-05-13 14:14:54 -0700351 @Deprecated
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700352 public final AudioState getAudioState() {
Yorke Lee4af59352015-05-13 14:14:54 -0700353 return new AudioState(mCallAudioState);
354 }
355
356 /**
357 * Obtains the current phone call audio state of the {@code Phone}.
358 *
359 * @return An object encapsulating the audio state.
360 */
361 public final CallAudioState getCallAudioState() {
362 return mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700363 }
364
365 private void fireCallAdded(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700366 for (Listener listener : mListeners) {
367 listener.onCallAdded(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700368 }
369 }
370
371 private void fireCallRemoved(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700372 for (Listener listener : mListeners) {
373 listener.onCallRemoved(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700374 }
375 }
376
Yorke Lee4af59352015-05-13 14:14:54 -0700377 private void fireCallAudioStateChanged(CallAudioState audioState) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700378 for (Listener listener : mListeners) {
Yorke Lee4af59352015-05-13 14:14:54 -0700379 listener.onCallAudioStateChanged(this, audioState);
380 listener.onAudioStateChanged(this, new AudioState(audioState));
Ihab Awade63fadb2014-07-09 21:52:04 -0700381 }
382 }
383
384 private void fireBringToForeground(boolean showDialpad) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700385 for (Listener listener : mListeners) {
386 listener.onBringToForeground(this, showDialpad);
Ihab Awade63fadb2014-07-09 21:52:04 -0700387 }
388 }
389
Santos Cordon6c912b72014-11-07 16:05:09 -0800390 private void fireCanAddCallChanged(boolean canAddCall) {
391 for (Listener listener : mListeners) {
392 listener.onCanAddCallChanged(this, canAddCall);
393 }
394 }
395
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800396 private void fireSilenceRinger() {
397 for (Listener listener : mListeners) {
398 listener.onSilenceRinger(this);
399 }
400 }
401
Santos Cordon88b771d2014-07-19 13:10:40 -0700402 private void checkCallTree(ParcelableCall parcelableCall) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700403 if (parcelableCall.getChildCallIds() != null) {
404 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700405 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700406 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
407 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
Ihab Awade63fadb2014-07-09 21:52:04 -0700408 }
409 }
410 }
411 }
412}