blob: 56eb7ec5fb7817774f0eb088eb00d475d95f5eba [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;
Ihab Awade63fadb2014-07-09 21:52:04 -070020import android.util.ArrayMap;
21
Ihab Awade63fadb2014-07-09 21:52:04 -070022import java.util.Collections;
23import java.util.List;
24import java.util.Map;
25import java.util.Objects;
Jay Shrauner229e3822014-08-15 09:23:07 -070026import java.util.concurrent.CopyOnWriteArrayList;
Ihab Awade63fadb2014-07-09 21:52:04 -070027
28/**
29 * A unified virtual device providing a means of voice (and other) communication on a device.
Santos Cordon29886d82015-04-16 15:34:07 -070030 *
31 * @hide
32 * @deprecated Use {@link InCallService} directly instead of using this class.
Ihab Awade63fadb2014-07-09 21:52:04 -070033 */
Santos Cordon29886d82015-04-16 15:34:07 -070034@SystemApi
35@Deprecated
Ihab Awade63fadb2014-07-09 21:52:04 -070036public final class Phone {
37
38 public abstract static class Listener {
39 /**
40 * Called when the audio state changes.
41 *
42 * @param phone The {@code Phone} calling this method.
Ihab Awadb19a0bc2014-08-07 19:46:01 -070043 * @param audioState The new {@link AudioState}.
Yorke Lee4af59352015-05-13 14:14:54 -070044 *
45 * @deprecated Use {@link #onCallAudioStateChanged(Phone, CallAudioState)} instead.
Ihab Awade63fadb2014-07-09 21:52:04 -070046 */
Yorke Lee4af59352015-05-13 14:14:54 -070047 @Deprecated
Ihab Awadb19a0bc2014-08-07 19:46:01 -070048 public void onAudioStateChanged(Phone phone, AudioState audioState) { }
Ihab Awade63fadb2014-07-09 21:52:04 -070049
50 /**
Yorke Lee4af59352015-05-13 14:14:54 -070051 * Called when the audio state changes.
52 *
53 * @param phone The {@code Phone} calling this method.
54 * @param callAudioState The new {@link CallAudioState}.
55 */
56 public void onCallAudioStateChanged(Phone phone, CallAudioState callAudioState) { }
57
58 /**
Ihab Awade63fadb2014-07-09 21:52:04 -070059 * Called to bring the in-call screen to the foreground. The in-call experience should
60 * respond immediately by coming to the foreground to inform the user of the state of
61 * ongoing {@code Call}s.
62 *
63 * @param phone The {@code Phone} calling this method.
64 * @param showDialpad If true, put up the dialpad when the screen is shown.
65 */
66 public void onBringToForeground(Phone phone, boolean showDialpad) { }
67
68 /**
69 * Called when a {@code Call} has been added to this in-call session. The in-call user
70 * experience should add necessary state listeners to the specified {@code Call} and
71 * immediately start to show the user information about the existence
72 * and nature of this {@code Call}. Subsequent invocations of {@link #getCalls()} will
73 * include this {@code Call}.
74 *
75 * @param phone The {@code Phone} calling this method.
76 * @param call A newly added {@code Call}.
77 */
78 public void onCallAdded(Phone phone, Call call) { }
79
80 /**
81 * Called when a {@code Call} has been removed from this in-call session. The in-call user
82 * experience should remove any state listeners from the specified {@code Call} and
83 * immediately stop displaying any information about this {@code Call}.
84 * Subsequent invocations of {@link #getCalls()} will no longer include this {@code Call}.
85 *
86 * @param phone The {@code Phone} calling this method.
87 * @param call A newly removed {@code Call}.
88 */
89 public void onCallRemoved(Phone phone, Call call) { }
Santos Cordon6c912b72014-11-07 16:05:09 -080090
91 /**
92 * Called when the {@code Phone} ability to add more calls changes. If the phone cannot
93 * support more calls then {@code canAddCall} is set to {@code false}. If it can, then it
94 * is set to {@code true}.
95 *
96 * @param phone The {@code Phone} calling this method.
97 * @param canAddCall Indicates whether an additional call can be added.
98 */
99 public void onCanAddCallChanged(Phone phone, boolean canAddCall) { }
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800100
101 /**
102 * Called to silence the ringer if a ringing call exists.
103 *
104 * @param phone The {@code Phone} calling this method.
105 */
106 public void onSilenceRinger(Phone phone) { }
Ihab Awade63fadb2014-07-09 21:52:04 -0700107 }
108
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700109 // A Map allows us to track each Call by its Telecom-specified call ID
110 private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700111
112 // A List allows us to keep the Calls in a stable iteration order so that casually developed
113 // user interface components do not incur any spurious jank
Santos Cordonf30d7e92014-08-26 09:54:33 -0700114 private final List<Call> mCalls = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700115
116 // An unmodifiable view of the above List can be safely shared with subclass implementations
117 private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls);
118
119 private final InCallAdapter mInCallAdapter;
120
Yorke Lee4af59352015-05-13 14:14:54 -0700121 private CallAudioState mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700122
Jay Shrauner229e3822014-08-15 09:23:07 -0700123 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700124
Santos Cordon6c912b72014-11-07 16:05:09 -0800125 private boolean mCanAddCall = true;
126
Ihab Awade63fadb2014-07-09 21:52:04 -0700127 Phone(InCallAdapter adapter) {
128 mInCallAdapter = adapter;
129 }
130
Santos Cordon88b771d2014-07-19 13:10:40 -0700131 final void internalAddCall(ParcelableCall parcelableCall) {
Shriram Ganeshddf570e2015-05-31 09:18:48 -0700132 Call call = new Call(this, parcelableCall.getId(), mInCallAdapter,
133 parcelableCall.getState());
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700134 mCallByTelecomCallId.put(parcelableCall.getId(), call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700135 mCalls.add(call);
Santos Cordon88b771d2014-07-19 13:10:40 -0700136 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700137 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700138 fireCallAdded(call);
139 }
140
Ihab Awade63fadb2014-07-09 21:52:04 -0700141 final void internalRemoveCall(Call call) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700142 mCallByTelecomCallId.remove(call.internalGetCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700143 mCalls.remove(call);
Tyler Gunn75958422015-04-15 14:23:42 -0700144
145 InCallService.VideoCall videoCall = call.getVideoCall();
146 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700147 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700148 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700149 fireCallRemoved(call);
150 }
151
Santos Cordon88b771d2014-07-19 13:10:40 -0700152 final void internalUpdateCall(ParcelableCall parcelableCall) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700153 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700154 if (call != null) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700155 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700156 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700157 }
158 }
159
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700160 final void internalSetPostDialWait(String telecomId, String remaining) {
161 Call call = mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700162 if (call != null) {
163 call.internalSetPostDialWait(remaining);
164 }
165 }
166
Yorke Lee4af59352015-05-13 14:14:54 -0700167 final void internalCallAudioStateChanged(CallAudioState callAudioState) {
168 if (!Objects.equals(mCallAudioState, callAudioState)) {
169 mCallAudioState = callAudioState;
170 fireCallAudioStateChanged(callAudioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700171 }
172 }
173
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700174 final Call internalGetCallByTelecomId(String telecomId) {
175 return mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700176 }
177
Ihab Awade63fadb2014-07-09 21:52:04 -0700178 final void internalBringToForeground(boolean showDialpad) {
179 fireBringToForeground(showDialpad);
180 }
181
Santos Cordon6c912b72014-11-07 16:05:09 -0800182 final void internalSetCanAddCall(boolean canAddCall) {
183 if (mCanAddCall != canAddCall) {
184 mCanAddCall = canAddCall;
185 fireCanAddCallChanged(canAddCall);
186 }
187 }
188
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800189 final void internalSilenceRinger() {
190 fireSilenceRinger();
191 }
192
Ihab Awade63fadb2014-07-09 21:52:04 -0700193 /**
Santos Cordonf30d7e92014-08-26 09:54:33 -0700194 * Called to destroy the phone and cleanup any lingering calls.
Santos Cordonf30d7e92014-08-26 09:54:33 -0700195 */
196 final void destroy() {
197 for (Call call : mCalls) {
Tyler Gunn75958422015-04-15 14:23:42 -0700198 InCallService.VideoCall videoCall = call.getVideoCall();
199 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700200 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700201 }
Santos Cordonf30d7e92014-08-26 09:54:33 -0700202 if (call.getState() != Call.STATE_DISCONNECTED) {
203 call.internalSetDisconnected();
204 }
205 }
206 }
207
208 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700209 * Adds a listener to this {@code Phone}.
210 *
211 * @param listener A {@code Listener} object.
212 */
213 public final void addListener(Listener listener) {
214 mListeners.add(listener);
215 }
216
217 /**
218 * Removes a listener from this {@code Phone}.
219 *
220 * @param listener A {@code Listener} object.
221 */
222 public final void removeListener(Listener listener) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700223 if (listener != null) {
224 mListeners.remove(listener);
225 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700226 }
227
228 /**
229 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
230 *
231 * @return A list of the relevant {@code Call}s.
232 */
233 public final List<Call> getCalls() {
234 return mUnmodifiableCalls;
235 }
236
237 /**
Santos Cordon6c912b72014-11-07 16:05:09 -0800238 * Returns if the {@code Phone} can support additional calls.
239 *
240 * @return Whether the phone supports adding more calls.
241 */
242 public final boolean canAddCall() {
243 return mCanAddCall;
244 }
245
246 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700247 * Sets the microphone mute state. When this request is honored, there will be change to
248 * the {@link #getAudioState()}.
249 *
250 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
251 */
252 public final void setMuted(boolean state) {
253 mInCallAdapter.mute(state);
254 }
255
256 /**
257 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
258 * be change to the {@link #getAudioState()}.
259 *
260 * @param route The audio route to use.
261 */
262 public final void setAudioRoute(int route) {
263 mInCallAdapter.setAudioRoute(route);
264 }
265
266 /**
Yorke Lee0d6ea712014-07-28 14:39:23 -0700267 * Turns the proximity sensor on. When this request is made, the proximity sensor will
268 * become active, and the touch screen and display will be turned off when the user's face
269 * is detected to be in close proximity to the screen. This operation is a no-op on devices
270 * that do not have a proximity sensor.
Yorke Lee22244d02015-04-14 12:34:28 -0700271 *
272 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700273 */
274 public final void setProximitySensorOn() {
275 mInCallAdapter.turnProximitySensorOn();
276 }
277
278 /**
279 * Turns the proximity sensor off. When this request is made, the proximity sensor will
280 * become inactive, and no longer affect the touch screen and display. This operation is a
281 * no-op on devices that do not have a proximity sensor.
282 *
283 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
284 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
285 * is no longer triggered.
Yorke Lee22244d02015-04-14 12:34:28 -0700286 *
287 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700288 */
289 public final void setProximitySensorOff(boolean screenOnImmediately) {
290 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
291 }
292
293 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700294 * Obtains the current phone call audio state of the {@code Phone}.
295 *
296 * @return An object encapsulating the audio state.
Yorke Lee4af59352015-05-13 14:14:54 -0700297 * @deprecated Use {@link #getCallAudioState()} instead.
Ihab Awade63fadb2014-07-09 21:52:04 -0700298 */
Yorke Lee4af59352015-05-13 14:14:54 -0700299 @Deprecated
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700300 public final AudioState getAudioState() {
Yorke Lee4af59352015-05-13 14:14:54 -0700301 return new AudioState(mCallAudioState);
302 }
303
304 /**
305 * Obtains the current phone call audio state of the {@code Phone}.
306 *
307 * @return An object encapsulating the audio state.
308 */
309 public final CallAudioState getCallAudioState() {
310 return mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700311 }
312
313 private void fireCallAdded(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700314 for (Listener listener : mListeners) {
315 listener.onCallAdded(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700316 }
317 }
318
319 private void fireCallRemoved(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700320 for (Listener listener : mListeners) {
321 listener.onCallRemoved(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700322 }
323 }
324
Yorke Lee4af59352015-05-13 14:14:54 -0700325 private void fireCallAudioStateChanged(CallAudioState audioState) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700326 for (Listener listener : mListeners) {
Yorke Lee4af59352015-05-13 14:14:54 -0700327 listener.onCallAudioStateChanged(this, audioState);
328 listener.onAudioStateChanged(this, new AudioState(audioState));
Ihab Awade63fadb2014-07-09 21:52:04 -0700329 }
330 }
331
332 private void fireBringToForeground(boolean showDialpad) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700333 for (Listener listener : mListeners) {
334 listener.onBringToForeground(this, showDialpad);
Ihab Awade63fadb2014-07-09 21:52:04 -0700335 }
336 }
337
Santos Cordon6c912b72014-11-07 16:05:09 -0800338 private void fireCanAddCallChanged(boolean canAddCall) {
339 for (Listener listener : mListeners) {
340 listener.onCanAddCallChanged(this, canAddCall);
341 }
342 }
343
Sailesh Nepal9c2618b2016-01-23 16:28:22 -0800344 private void fireSilenceRinger() {
345 for (Listener listener : mListeners) {
346 listener.onSilenceRinger(this);
347 }
348 }
349
Santos Cordon88b771d2014-07-19 13:10:40 -0700350 private void checkCallTree(ParcelableCall parcelableCall) {
351 if (parcelableCall.getParentCallId() != null &&
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700352 !mCallByTelecomCallId.containsKey(parcelableCall.getParentCallId())) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700353 Log.wtf(this, "ParcelableCall %s has nonexistent parent %s",
354 parcelableCall.getId(), parcelableCall.getParentCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700355 }
Santos Cordon88b771d2014-07-19 13:10:40 -0700356 if (parcelableCall.getChildCallIds() != null) {
357 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700358 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700359 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
360 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
Ihab Awade63fadb2014-07-09 21:52:04 -0700361 }
362 }
363 }
364 }
365}