blob: 30ec5b350307dd79c544779a73637a24e4b017c1 [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;
Tyler Gunn876dbfb2016-03-14 15:18:07 -070020import android.os.Bundle;
Ihab Awade63fadb2014-07-09 21:52:04 -070021import android.util.ArrayMap;
22
Ihab Awade63fadb2014-07-09 21:52:04 -070023import java.util.Collections;
24import java.util.List;
25import java.util.Map;
26import java.util.Objects;
Jay Shrauner229e3822014-08-15 09:23:07 -070027import java.util.concurrent.CopyOnWriteArrayList;
Ihab Awade63fadb2014-07-09 21:52:04 -070028
29/**
30 * A unified virtual device providing a means of voice (and other) communication on a device.
Santos Cordon29886d82015-04-16 15:34:07 -070031 *
32 * @hide
33 * @deprecated Use {@link InCallService} directly instead of using this class.
Ihab Awade63fadb2014-07-09 21:52:04 -070034 */
Santos Cordon29886d82015-04-16 15:34:07 -070035@SystemApi
36@Deprecated
Ihab Awade63fadb2014-07-09 21:52:04 -070037public final class Phone {
38
39 public abstract static class Listener {
40 /**
41 * Called when the audio state changes.
42 *
43 * @param phone The {@code Phone} calling this method.
Ihab Awadb19a0bc2014-08-07 19:46:01 -070044 * @param audioState The new {@link AudioState}.
Yorke Lee4af59352015-05-13 14:14:54 -070045 *
46 * @deprecated Use {@link #onCallAudioStateChanged(Phone, CallAudioState)} instead.
Ihab Awade63fadb2014-07-09 21:52:04 -070047 */
Yorke Lee4af59352015-05-13 14:14:54 -070048 @Deprecated
Ihab Awadb19a0bc2014-08-07 19:46:01 -070049 public void onAudioStateChanged(Phone phone, AudioState audioState) { }
Ihab Awade63fadb2014-07-09 21:52:04 -070050
51 /**
Yorke Lee4af59352015-05-13 14:14:54 -070052 * Called when the audio state changes.
53 *
54 * @param phone The {@code Phone} calling this method.
55 * @param callAudioState The new {@link CallAudioState}.
56 */
57 public void onCallAudioStateChanged(Phone phone, CallAudioState callAudioState) { }
58
59 /**
Ihab Awade63fadb2014-07-09 21:52:04 -070060 * Called to bring the in-call screen to the foreground. The in-call experience should
61 * respond immediately by coming to the foreground to inform the user of the state of
62 * ongoing {@code Call}s.
63 *
64 * @param phone The {@code Phone} calling this method.
65 * @param showDialpad If true, put up the dialpad when the screen is shown.
66 */
67 public void onBringToForeground(Phone phone, boolean showDialpad) { }
68
69 /**
70 * Called when a {@code Call} has been added to this in-call session. The in-call user
71 * experience should add necessary state listeners to the specified {@code Call} and
72 * immediately start to show the user information about the existence
73 * and nature of this {@code Call}. Subsequent invocations of {@link #getCalls()} will
74 * include this {@code Call}.
75 *
76 * @param phone The {@code Phone} calling this method.
77 * @param call A newly added {@code Call}.
78 */
79 public void onCallAdded(Phone phone, Call call) { }
80
81 /**
82 * Called when a {@code Call} has been removed from this in-call session. The in-call user
83 * experience should remove any state listeners from the specified {@code Call} and
84 * immediately stop displaying any information about this {@code Call}.
85 * Subsequent invocations of {@link #getCalls()} will no longer include this {@code Call}.
86 *
87 * @param phone The {@code Phone} calling this method.
88 * @param call A newly removed {@code Call}.
89 */
90 public void onCallRemoved(Phone phone, Call call) { }
Santos Cordon6c912b72014-11-07 16:05:09 -080091
92 /**
93 * Called when the {@code Phone} ability to add more calls changes. If the phone cannot
94 * support more calls then {@code canAddCall} is set to {@code false}. If it can, then it
95 * is set to {@code true}.
96 *
97 * @param phone The {@code Phone} calling this method.
98 * @param canAddCall Indicates whether an additional call can be added.
99 */
100 public void onCanAddCallChanged(Phone phone, boolean canAddCall) { }
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800101
102 /**
103 * Called to silence the ringer if a ringing call exists.
104 *
105 * @param phone The {@code Phone} calling this method.
106 */
107 public void onSilenceRinger(Phone phone) { }
Ihab Awade63fadb2014-07-09 21:52:04 -0700108 }
109
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700110 // A Map allows us to track each Call by its Telecom-specified call ID
111 private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700112
113 // A List allows us to keep the Calls in a stable iteration order so that casually developed
114 // user interface components do not incur any spurious jank
Santos Cordonf30d7e92014-08-26 09:54:33 -0700115 private final List<Call> mCalls = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700116
117 // An unmodifiable view of the above List can be safely shared with subclass implementations
118 private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls);
119
120 private final InCallAdapter mInCallAdapter;
121
Yorke Lee4af59352015-05-13 14:14:54 -0700122 private CallAudioState mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700123
Jay Shrauner229e3822014-08-15 09:23:07 -0700124 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700125
Santos Cordon6c912b72014-11-07 16:05:09 -0800126 private boolean mCanAddCall = true;
127
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800128 private final String mCallingPackage;
129
130 Phone(InCallAdapter adapter, String callingPackage) {
Ihab Awade63fadb2014-07-09 21:52:04 -0700131 mInCallAdapter = adapter;
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800132 mCallingPackage = callingPackage;
Ihab Awade63fadb2014-07-09 21:52:04 -0700133 }
134
Santos Cordon88b771d2014-07-19 13:10:40 -0700135 final void internalAddCall(ParcelableCall parcelableCall) {
Shriram Ganeshddf570e2015-05-31 09:18:48 -0700136 Call call = new Call(this, parcelableCall.getId(), mInCallAdapter,
Tyler Gunnbf9c6fd2016-11-09 10:19:23 -0800137 parcelableCall.getState(), mCallingPackage);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700138 mCallByTelecomCallId.put(parcelableCall.getId(), call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700139 mCalls.add(call);
Santos Cordon88b771d2014-07-19 13:10:40 -0700140 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700141 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700142 fireCallAdded(call);
143 }
144
Ihab Awade63fadb2014-07-09 21:52:04 -0700145 final void internalRemoveCall(Call call) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700146 mCallByTelecomCallId.remove(call.internalGetCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700147 mCalls.remove(call);
Tyler Gunn75958422015-04-15 14:23:42 -0700148
149 InCallService.VideoCall videoCall = call.getVideoCall();
150 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700151 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700152 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700153 fireCallRemoved(call);
154 }
155
Santos Cordon88b771d2014-07-19 13:10:40 -0700156 final void internalUpdateCall(ParcelableCall parcelableCall) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700157 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700158 if (call != null) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700159 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700160 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700161 }
162 }
163
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700164 final void internalSetPostDialWait(String telecomId, String remaining) {
165 Call call = mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700166 if (call != null) {
167 call.internalSetPostDialWait(remaining);
168 }
169 }
170
Yorke Lee4af59352015-05-13 14:14:54 -0700171 final void internalCallAudioStateChanged(CallAudioState callAudioState) {
172 if (!Objects.equals(mCallAudioState, callAudioState)) {
173 mCallAudioState = callAudioState;
174 fireCallAudioStateChanged(callAudioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700175 }
176 }
177
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700178 final Call internalGetCallByTelecomId(String telecomId) {
179 return mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700180 }
181
Ihab Awade63fadb2014-07-09 21:52:04 -0700182 final void internalBringToForeground(boolean showDialpad) {
183 fireBringToForeground(showDialpad);
184 }
185
Santos Cordon6c912b72014-11-07 16:05:09 -0800186 final void internalSetCanAddCall(boolean canAddCall) {
187 if (mCanAddCall != canAddCall) {
188 mCanAddCall = canAddCall;
189 fireCanAddCallChanged(canAddCall);
190 }
191 }
192
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800193 final void internalSilenceRinger() {
194 fireSilenceRinger();
195 }
196
Tyler Gunn876dbfb2016-03-14 15:18:07 -0700197 final void internalOnConnectionEvent(String telecomId, String event, Bundle extras) {
198 Call call = mCallByTelecomCallId.get(telecomId);
199 if (call != null) {
200 call.internalOnConnectionEvent(event, extras);
201 }
202 }
203
Ihab Awade63fadb2014-07-09 21:52:04 -0700204 /**
Santos Cordonf30d7e92014-08-26 09:54:33 -0700205 * Called to destroy the phone and cleanup any lingering calls.
Santos Cordonf30d7e92014-08-26 09:54:33 -0700206 */
207 final void destroy() {
208 for (Call call : mCalls) {
Tyler Gunn75958422015-04-15 14:23:42 -0700209 InCallService.VideoCall videoCall = call.getVideoCall();
210 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700211 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700212 }
Santos Cordonf30d7e92014-08-26 09:54:33 -0700213 if (call.getState() != Call.STATE_DISCONNECTED) {
214 call.internalSetDisconnected();
215 }
216 }
217 }
218
219 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700220 * Adds a listener to this {@code Phone}.
221 *
222 * @param listener A {@code Listener} object.
223 */
224 public final void addListener(Listener listener) {
225 mListeners.add(listener);
226 }
227
228 /**
229 * Removes a listener from this {@code Phone}.
230 *
231 * @param listener A {@code Listener} object.
232 */
233 public final void removeListener(Listener listener) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700234 if (listener != null) {
235 mListeners.remove(listener);
236 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700237 }
238
239 /**
240 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
241 *
242 * @return A list of the relevant {@code Call}s.
243 */
244 public final List<Call> getCalls() {
245 return mUnmodifiableCalls;
246 }
247
248 /**
Santos Cordon6c912b72014-11-07 16:05:09 -0800249 * Returns if the {@code Phone} can support additional calls.
250 *
251 * @return Whether the phone supports adding more calls.
252 */
253 public final boolean canAddCall() {
254 return mCanAddCall;
255 }
256
257 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700258 * Sets the microphone mute state. When this request is honored, there will be change to
259 * the {@link #getAudioState()}.
260 *
261 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
262 */
263 public final void setMuted(boolean state) {
264 mInCallAdapter.mute(state);
265 }
266
267 /**
268 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
269 * be change to the {@link #getAudioState()}.
270 *
271 * @param route The audio route to use.
272 */
273 public final void setAudioRoute(int route) {
274 mInCallAdapter.setAudioRoute(route);
275 }
276
277 /**
Yorke Lee0d6ea712014-07-28 14:39:23 -0700278 * Turns the proximity sensor on. When this request is made, the proximity sensor will
279 * become active, and the touch screen and display will be turned off when the user's face
280 * is detected to be in close proximity to the screen. This operation is a no-op on devices
281 * that do not have a proximity sensor.
Yorke Lee22244d02015-04-14 12:34:28 -0700282 *
283 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700284 */
285 public final void setProximitySensorOn() {
286 mInCallAdapter.turnProximitySensorOn();
287 }
288
289 /**
290 * Turns the proximity sensor off. When this request is made, the proximity sensor will
291 * become inactive, and no longer affect the touch screen and display. This operation is a
292 * no-op on devices that do not have a proximity sensor.
293 *
294 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
295 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
296 * is no longer triggered.
Yorke Lee22244d02015-04-14 12:34:28 -0700297 *
298 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700299 */
300 public final void setProximitySensorOff(boolean screenOnImmediately) {
301 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
302 }
303
304 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700305 * Obtains the current phone call audio state of the {@code Phone}.
306 *
307 * @return An object encapsulating the audio state.
Yorke Lee4af59352015-05-13 14:14:54 -0700308 * @deprecated Use {@link #getCallAudioState()} instead.
Ihab Awade63fadb2014-07-09 21:52:04 -0700309 */
Yorke Lee4af59352015-05-13 14:14:54 -0700310 @Deprecated
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700311 public final AudioState getAudioState() {
Yorke Lee4af59352015-05-13 14:14:54 -0700312 return new AudioState(mCallAudioState);
313 }
314
315 /**
316 * Obtains the current phone call audio state of the {@code Phone}.
317 *
318 * @return An object encapsulating the audio state.
319 */
320 public final CallAudioState getCallAudioState() {
321 return mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700322 }
323
324 private void fireCallAdded(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700325 for (Listener listener : mListeners) {
326 listener.onCallAdded(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700327 }
328 }
329
330 private void fireCallRemoved(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700331 for (Listener listener : mListeners) {
332 listener.onCallRemoved(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700333 }
334 }
335
Yorke Lee4af59352015-05-13 14:14:54 -0700336 private void fireCallAudioStateChanged(CallAudioState audioState) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700337 for (Listener listener : mListeners) {
Yorke Lee4af59352015-05-13 14:14:54 -0700338 listener.onCallAudioStateChanged(this, audioState);
339 listener.onAudioStateChanged(this, new AudioState(audioState));
Ihab Awade63fadb2014-07-09 21:52:04 -0700340 }
341 }
342
343 private void fireBringToForeground(boolean showDialpad) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700344 for (Listener listener : mListeners) {
345 listener.onBringToForeground(this, showDialpad);
Ihab Awade63fadb2014-07-09 21:52:04 -0700346 }
347 }
348
Santos Cordon6c912b72014-11-07 16:05:09 -0800349 private void fireCanAddCallChanged(boolean canAddCall) {
350 for (Listener listener : mListeners) {
351 listener.onCanAddCallChanged(this, canAddCall);
352 }
353 }
354
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800355 private void fireSilenceRinger() {
356 for (Listener listener : mListeners) {
357 listener.onSilenceRinger(this);
358 }
359 }
360
Santos Cordon88b771d2014-07-19 13:10:40 -0700361 private void checkCallTree(ParcelableCall parcelableCall) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700362 if (parcelableCall.getChildCallIds() != null) {
363 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700364 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700365 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
366 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
Ihab Awade63fadb2014-07-09 21:52:04 -0700367 }
368 }
369 }
370 }
371}