blob: 8eb091b732af200dd13918c24ea0d7524e7855f4 [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) { }
Ihab Awade63fadb2014-07-09 21:52:04 -0700100 }
101
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700102 // A Map allows us to track each Call by its Telecom-specified call ID
103 private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700104
105 // A List allows us to keep the Calls in a stable iteration order so that casually developed
106 // user interface components do not incur any spurious jank
Santos Cordonf30d7e92014-08-26 09:54:33 -0700107 private final List<Call> mCalls = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700108
109 // An unmodifiable view of the above List can be safely shared with subclass implementations
110 private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls);
111
112 private final InCallAdapter mInCallAdapter;
113
Yorke Lee4af59352015-05-13 14:14:54 -0700114 private CallAudioState mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700115
Jay Shrauner229e3822014-08-15 09:23:07 -0700116 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700117
Santos Cordon6c912b72014-11-07 16:05:09 -0800118 private boolean mCanAddCall = true;
119
Ihab Awade63fadb2014-07-09 21:52:04 -0700120 Phone(InCallAdapter adapter) {
121 mInCallAdapter = adapter;
122 }
123
Santos Cordon88b771d2014-07-19 13:10:40 -0700124 final void internalAddCall(ParcelableCall parcelableCall) {
125 Call call = new Call(this, parcelableCall.getId(), mInCallAdapter);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700126 mCallByTelecomCallId.put(parcelableCall.getId(), call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700127 mCalls.add(call);
Santos Cordon88b771d2014-07-19 13:10:40 -0700128 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700129 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700130 fireCallAdded(call);
131 }
132
Ihab Awade63fadb2014-07-09 21:52:04 -0700133 final void internalRemoveCall(Call call) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700134 mCallByTelecomCallId.remove(call.internalGetCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700135 mCalls.remove(call);
Tyler Gunn75958422015-04-15 14:23:42 -0700136
137 InCallService.VideoCall videoCall = call.getVideoCall();
138 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700139 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700140 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700141 fireCallRemoved(call);
142 }
143
Santos Cordon88b771d2014-07-19 13:10:40 -0700144 final void internalUpdateCall(ParcelableCall parcelableCall) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700145 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700146 if (call != null) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700147 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700148 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700149 }
150 }
151
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700152 final void internalSetPostDialWait(String telecomId, String remaining) {
153 Call call = mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700154 if (call != null) {
155 call.internalSetPostDialWait(remaining);
156 }
157 }
158
Yorke Lee4af59352015-05-13 14:14:54 -0700159 final void internalCallAudioStateChanged(CallAudioState callAudioState) {
160 if (!Objects.equals(mCallAudioState, callAudioState)) {
161 mCallAudioState = callAudioState;
162 fireCallAudioStateChanged(callAudioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700163 }
164 }
165
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700166 final Call internalGetCallByTelecomId(String telecomId) {
167 return mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700168 }
169
Ihab Awade63fadb2014-07-09 21:52:04 -0700170 final void internalBringToForeground(boolean showDialpad) {
171 fireBringToForeground(showDialpad);
172 }
173
Santos Cordon6c912b72014-11-07 16:05:09 -0800174 final void internalSetCanAddCall(boolean canAddCall) {
175 if (mCanAddCall != canAddCall) {
176 mCanAddCall = canAddCall;
177 fireCanAddCallChanged(canAddCall);
178 }
179 }
180
Ihab Awade63fadb2014-07-09 21:52:04 -0700181 /**
Santos Cordonf30d7e92014-08-26 09:54:33 -0700182 * Called to destroy the phone and cleanup any lingering calls.
Santos Cordonf30d7e92014-08-26 09:54:33 -0700183 */
184 final void destroy() {
185 for (Call call : mCalls) {
Tyler Gunn75958422015-04-15 14:23:42 -0700186 InCallService.VideoCall videoCall = call.getVideoCall();
187 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700188 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700189 }
Santos Cordonf30d7e92014-08-26 09:54:33 -0700190 if (call.getState() != Call.STATE_DISCONNECTED) {
191 call.internalSetDisconnected();
192 }
193 }
194 }
195
196 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700197 * Adds a listener to this {@code Phone}.
198 *
199 * @param listener A {@code Listener} object.
200 */
201 public final void addListener(Listener listener) {
202 mListeners.add(listener);
203 }
204
205 /**
206 * Removes a listener from this {@code Phone}.
207 *
208 * @param listener A {@code Listener} object.
209 */
210 public final void removeListener(Listener listener) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700211 if (listener != null) {
212 mListeners.remove(listener);
213 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700214 }
215
216 /**
217 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
218 *
219 * @return A list of the relevant {@code Call}s.
220 */
221 public final List<Call> getCalls() {
222 return mUnmodifiableCalls;
223 }
224
225 /**
Santos Cordon6c912b72014-11-07 16:05:09 -0800226 * Returns if the {@code Phone} can support additional calls.
227 *
228 * @return Whether the phone supports adding more calls.
229 */
230 public final boolean canAddCall() {
231 return mCanAddCall;
232 }
233
234 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700235 * Sets the microphone mute state. When this request is honored, there will be change to
236 * the {@link #getAudioState()}.
237 *
238 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
239 */
240 public final void setMuted(boolean state) {
241 mInCallAdapter.mute(state);
242 }
243
244 /**
245 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
246 * be change to the {@link #getAudioState()}.
247 *
248 * @param route The audio route to use.
249 */
250 public final void setAudioRoute(int route) {
251 mInCallAdapter.setAudioRoute(route);
252 }
253
254 /**
Yorke Lee0d6ea712014-07-28 14:39:23 -0700255 * Turns the proximity sensor on. When this request is made, the proximity sensor will
256 * become active, and the touch screen and display will be turned off when the user's face
257 * is detected to be in close proximity to the screen. This operation is a no-op on devices
258 * that do not have a proximity sensor.
Yorke Lee22244d02015-04-14 12:34:28 -0700259 *
260 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700261 */
262 public final void setProximitySensorOn() {
263 mInCallAdapter.turnProximitySensorOn();
264 }
265
266 /**
267 * Turns the proximity sensor off. When this request is made, the proximity sensor will
268 * become inactive, and no longer affect the touch screen and display. This operation is a
269 * no-op on devices that do not have a proximity sensor.
270 *
271 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
272 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
273 * is no longer triggered.
Yorke Lee22244d02015-04-14 12:34:28 -0700274 *
275 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700276 */
277 public final void setProximitySensorOff(boolean screenOnImmediately) {
278 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
279 }
280
281 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700282 * Obtains the current phone call audio state of the {@code Phone}.
283 *
284 * @return An object encapsulating the audio state.
Yorke Lee4af59352015-05-13 14:14:54 -0700285 * @deprecated Use {@link #getCallAudioState()} instead.
Ihab Awade63fadb2014-07-09 21:52:04 -0700286 */
Yorke Lee4af59352015-05-13 14:14:54 -0700287 @Deprecated
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700288 public final AudioState getAudioState() {
Yorke Lee4af59352015-05-13 14:14:54 -0700289 return new AudioState(mCallAudioState);
290 }
291
292 /**
293 * Obtains the current phone call audio state of the {@code Phone}.
294 *
295 * @return An object encapsulating the audio state.
296 */
297 public final CallAudioState getCallAudioState() {
298 return mCallAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700299 }
300
301 private void fireCallAdded(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700302 for (Listener listener : mListeners) {
303 listener.onCallAdded(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700304 }
305 }
306
307 private void fireCallRemoved(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700308 for (Listener listener : mListeners) {
309 listener.onCallRemoved(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700310 }
311 }
312
Yorke Lee4af59352015-05-13 14:14:54 -0700313 private void fireCallAudioStateChanged(CallAudioState audioState) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700314 for (Listener listener : mListeners) {
Yorke Lee4af59352015-05-13 14:14:54 -0700315 listener.onCallAudioStateChanged(this, audioState);
316 listener.onAudioStateChanged(this, new AudioState(audioState));
Ihab Awade63fadb2014-07-09 21:52:04 -0700317 }
318 }
319
320 private void fireBringToForeground(boolean showDialpad) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700321 for (Listener listener : mListeners) {
322 listener.onBringToForeground(this, showDialpad);
Ihab Awade63fadb2014-07-09 21:52:04 -0700323 }
324 }
325
Santos Cordon6c912b72014-11-07 16:05:09 -0800326 private void fireCanAddCallChanged(boolean canAddCall) {
327 for (Listener listener : mListeners) {
328 listener.onCanAddCallChanged(this, canAddCall);
329 }
330 }
331
Santos Cordon88b771d2014-07-19 13:10:40 -0700332 private void checkCallTree(ParcelableCall parcelableCall) {
333 if (parcelableCall.getParentCallId() != null &&
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700334 !mCallByTelecomCallId.containsKey(parcelableCall.getParentCallId())) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700335 Log.wtf(this, "ParcelableCall %s has nonexistent parent %s",
336 parcelableCall.getId(), parcelableCall.getParentCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700337 }
Santos Cordon88b771d2014-07-19 13:10:40 -0700338 if (parcelableCall.getChildCallIds() != null) {
339 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700340 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700341 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
342 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
Ihab Awade63fadb2014-07-09 21:52:04 -0700343 }
344 }
345 }
346 }
347}