blob: 421b1a4b22a5e51b0f6e480840a1ad6ef2e0022b [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 Liua98f58b52017-11-07 17:59:28 -080020import android.bluetooth.BluetoothDevice;
Tyler Gunn876dbfb2016-03-14 15:18:07 -070021import android.os.Bundle;
Hall Liua98f58b52017-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
Ihab Awade63fadb2014-07-09 21:52:04 -0700226 /**
Santos Cordonf30d7e92014-08-26 09:54:33 -0700227 * Called to destroy the phone and cleanup any lingering calls.
Santos Cordonf30d7e92014-08-26 09:54:33 -0700228 */
229 final void destroy() {
230 for (Call call : mCalls) {
Tyler Gunn75958422015-04-15 14:23:42 -0700231 InCallService.VideoCall videoCall = call.getVideoCall();
232 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700233 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700234 }
Santos Cordonf30d7e92014-08-26 09:54:33 -0700235 if (call.getState() != Call.STATE_DISCONNECTED) {
236 call.internalSetDisconnected();
237 }
238 }
239 }
240
241 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700242 * Adds a listener to this {@code Phone}.
243 *
244 * @param listener A {@code Listener} object.
245 */
246 public final void addListener(Listener listener) {
247 mListeners.add(listener);
248 }
249
250 /**
251 * Removes a listener from this {@code Phone}.
252 *
253 * @param listener A {@code Listener} object.
254 */
255 public final void removeListener(Listener listener) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700256 if (listener != null) {
257 mListeners.remove(listener);
258 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700259 }
260
261 /**
262 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
263 *
264 * @return A list of the relevant {@code Call}s.
265 */
266 public final List<Call> getCalls() {
267 return mUnmodifiableCalls;
268 }
269
270 /**
Santos Cordon6c912b72014-11-07 16:05:09 -0800271 * Returns if the {@code Phone} can support additional calls.
272 *
273 * @return Whether the phone supports adding more calls.
274 */
275 public final boolean canAddCall() {
276 return mCanAddCall;
277 }
278
279 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700280 * Sets the microphone mute state. When this request is honored, there will be change to
281 * the {@link #getAudioState()}.
282 *
283 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
284 */
285 public final void setMuted(boolean state) {
286 mInCallAdapter.mute(state);
287 }
288
289 /**
290 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
291 * be change to the {@link #getAudioState()}.
292 *
293 * @param route The audio route to use.
294 */
295 public final void setAudioRoute(int route) {
296 mInCallAdapter.setAudioRoute(route);
297 }
298
299 /**
Hall Liua98f58b52017-11-07 17:59:28 -0800300 * Request audio routing to a specific bluetooth device. Calling this method may result in
301 * the device routing audio to a different bluetooth device than the one specified. A list of
302 * available devices can be obtained via {@link CallAudioState#getSupportedBluetoothDevices()}
303 *
304 * @param bluetoothAddress The address of the bluetooth device to connect to, as returned by
305 * {@link BluetoothDevice#getAddress()}, or {@code null} if no device is preferred.
306 */
307 public void requestBluetoothAudio(String bluetoothAddress) {
308 mInCallAdapter.requestBluetoothAudio(bluetoothAddress);
309 }
310
311 /**
Yorke Lee0d6ea712014-07-28 14:39:23 -0700312 * Turns the proximity sensor on. When this request is made, the proximity sensor will
313 * become active, and the touch screen and display will be turned off when the user's face
314 * is detected to be in close proximity to the screen. This operation is a no-op on devices
315 * that do not have a proximity sensor.
Yorke Lee22244d02015-04-14 12:34:28 -0700316 *
317 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700318 */
319 public final void setProximitySensorOn() {
320 mInCallAdapter.turnProximitySensorOn();
321 }
322
323 /**
324 * Turns the proximity sensor off. When this request is made, the proximity sensor will
325 * become inactive, and no longer affect the touch screen and display. This operation is a
326 * no-op on devices that do not have a proximity sensor.
327 *
328 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
329 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
330 * is no longer triggered.
Yorke Lee22244d02015-04-14 12:34:28 -0700331 *
332 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700333 */
334 public final void setProximitySensorOff(boolean screenOnImmediately) {
335 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
336 }
337
338 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700339 * Obtains the current phone call audio state of the {@code Phone}.
340 *
341 * @return An object encapsulating the audio state.
Yorke Lee4af59352015-05-13 14:14:54 -0700342 * @deprecated Use {@link #getCallAudioState()} instead.
Ihab Awade63fadb2014-07-09 21:52:04 -0700343 */
Yorke Lee4af59352015-05-13 14:14:54 -0700344 @Deprecated
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700345 public final AudioState getAudioState() {
Yorke Lee4af59352015-05-13 14:14:54 -0700346 return new AudioState(mCallAudioState);
347 }
348
349 /**
350 * Obtains the current phone call audio state of the {@code Phone}.
351 *
352 * @return An object encapsulating the audio state.
353 */
354 public final CallAudioState getCallAudioState() {
355 return mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700356 }
357
358 private void fireCallAdded(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700359 for (Listener listener : mListeners) {
360 listener.onCallAdded(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700361 }
362 }
363
364 private void fireCallRemoved(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700365 for (Listener listener : mListeners) {
366 listener.onCallRemoved(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700367 }
368 }
369
Yorke Lee4af59352015-05-13 14:14:54 -0700370 private void fireCallAudioStateChanged(CallAudioState audioState) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700371 for (Listener listener : mListeners) {
Yorke Lee4af59352015-05-13 14:14:54 -0700372 listener.onCallAudioStateChanged(this, audioState);
373 listener.onAudioStateChanged(this, new AudioState(audioState));
Ihab Awade63fadb2014-07-09 21:52:04 -0700374 }
375 }
376
377 private void fireBringToForeground(boolean showDialpad) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700378 for (Listener listener : mListeners) {
379 listener.onBringToForeground(this, showDialpad);
Ihab Awade63fadb2014-07-09 21:52:04 -0700380 }
381 }
382
Santos Cordon6c912b72014-11-07 16:05:09 -0800383 private void fireCanAddCallChanged(boolean canAddCall) {
384 for (Listener listener : mListeners) {
385 listener.onCanAddCallChanged(this, canAddCall);
386 }
387 }
388
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800389 private void fireSilenceRinger() {
390 for (Listener listener : mListeners) {
391 listener.onSilenceRinger(this);
392 }
393 }
394
Santos Cordon88b771d2014-07-19 13:10:40 -0700395 private void checkCallTree(ParcelableCall parcelableCall) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700396 if (parcelableCall.getChildCallIds() != null) {
397 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700398 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700399 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
400 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
Ihab Awade63fadb2014-07-09 21:52:04 -0700401 }
402 }
403 }
404 }
405}