blob: 4cdfd2eb8f561f43a319afb95744000293420677 [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}.
Ihab Awade63fadb2014-07-09 21:52:04 -070044 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070045 public void onAudioStateChanged(Phone phone, AudioState audioState) { }
Ihab Awade63fadb2014-07-09 21:52:04 -070046
47 /**
48 * Called to bring the in-call screen to the foreground. The in-call experience should
49 * respond immediately by coming to the foreground to inform the user of the state of
50 * ongoing {@code Call}s.
51 *
52 * @param phone The {@code Phone} calling this method.
53 * @param showDialpad If true, put up the dialpad when the screen is shown.
54 */
55 public void onBringToForeground(Phone phone, boolean showDialpad) { }
56
57 /**
58 * Called when a {@code Call} has been added to this in-call session. The in-call user
59 * experience should add necessary state listeners to the specified {@code Call} and
60 * immediately start to show the user information about the existence
61 * and nature of this {@code Call}. Subsequent invocations of {@link #getCalls()} will
62 * include this {@code Call}.
63 *
64 * @param phone The {@code Phone} calling this method.
65 * @param call A newly added {@code Call}.
66 */
67 public void onCallAdded(Phone phone, Call call) { }
68
69 /**
70 * Called when a {@code Call} has been removed from this in-call session. The in-call user
71 * experience should remove any state listeners from the specified {@code Call} and
72 * immediately stop displaying any information about this {@code Call}.
73 * Subsequent invocations of {@link #getCalls()} will no longer include this {@code Call}.
74 *
75 * @param phone The {@code Phone} calling this method.
76 * @param call A newly removed {@code Call}.
77 */
78 public void onCallRemoved(Phone phone, Call call) { }
Santos Cordon6c912b72014-11-07 16:05:09 -080079
80 /**
81 * Called when the {@code Phone} ability to add more calls changes. If the phone cannot
82 * support more calls then {@code canAddCall} is set to {@code false}. If it can, then it
83 * is set to {@code true}.
84 *
85 * @param phone The {@code Phone} calling this method.
86 * @param canAddCall Indicates whether an additional call can be added.
87 */
88 public void onCanAddCallChanged(Phone phone, boolean canAddCall) { }
Ihab Awade63fadb2014-07-09 21:52:04 -070089 }
90
Tyler Gunnef9f6f92014-09-12 22:16:17 -070091 // A Map allows us to track each Call by its Telecom-specified call ID
92 private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>();
Ihab Awade63fadb2014-07-09 21:52:04 -070093
94 // A List allows us to keep the Calls in a stable iteration order so that casually developed
95 // user interface components do not incur any spurious jank
Santos Cordonf30d7e92014-08-26 09:54:33 -070096 private final List<Call> mCalls = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -070097
98 // An unmodifiable view of the above List can be safely shared with subclass implementations
99 private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls);
100
101 private final InCallAdapter mInCallAdapter;
102
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700103 private AudioState mAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700104
Jay Shrauner229e3822014-08-15 09:23:07 -0700105 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700106
Santos Cordon6c912b72014-11-07 16:05:09 -0800107 private boolean mCanAddCall = true;
108
Ihab Awade63fadb2014-07-09 21:52:04 -0700109 Phone(InCallAdapter adapter) {
110 mInCallAdapter = adapter;
111 }
112
Santos Cordon88b771d2014-07-19 13:10:40 -0700113 final void internalAddCall(ParcelableCall parcelableCall) {
114 Call call = new Call(this, parcelableCall.getId(), mInCallAdapter);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700115 mCallByTelecomCallId.put(parcelableCall.getId(), call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700116 mCalls.add(call);
Santos Cordon88b771d2014-07-19 13:10:40 -0700117 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700118 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700119 fireCallAdded(call);
120 }
121
Ihab Awade63fadb2014-07-09 21:52:04 -0700122 final void internalRemoveCall(Call call) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700123 mCallByTelecomCallId.remove(call.internalGetCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700124 mCalls.remove(call);
Tyler Gunn75958422015-04-15 14:23:42 -0700125
126 InCallService.VideoCall videoCall = call.getVideoCall();
127 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700128 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700129 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700130 fireCallRemoved(call);
131 }
132
Santos Cordon88b771d2014-07-19 13:10:40 -0700133 final void internalUpdateCall(ParcelableCall parcelableCall) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700134 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700135 if (call != null) {
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 }
139 }
140
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700141 final void internalSetPostDialWait(String telecomId, String remaining) {
142 Call call = mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700143 if (call != null) {
144 call.internalSetPostDialWait(remaining);
145 }
146 }
147
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700148 final void internalAudioStateChanged(AudioState audioState) {
149 if (!Objects.equals(mAudioState, audioState)) {
150 mAudioState = audioState;
151 fireAudioStateChanged(audioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700152 }
153 }
154
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700155 final Call internalGetCallByTelecomId(String telecomId) {
156 return mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700157 }
158
Ihab Awade63fadb2014-07-09 21:52:04 -0700159 final void internalBringToForeground(boolean showDialpad) {
160 fireBringToForeground(showDialpad);
161 }
162
Santos Cordon6c912b72014-11-07 16:05:09 -0800163 final void internalSetCanAddCall(boolean canAddCall) {
164 if (mCanAddCall != canAddCall) {
165 mCanAddCall = canAddCall;
166 fireCanAddCallChanged(canAddCall);
167 }
168 }
169
Ihab Awade63fadb2014-07-09 21:52:04 -0700170 /**
Santos Cordonf30d7e92014-08-26 09:54:33 -0700171 * Called to destroy the phone and cleanup any lingering calls.
Santos Cordonf30d7e92014-08-26 09:54:33 -0700172 */
173 final void destroy() {
174 for (Call call : mCalls) {
Tyler Gunn75958422015-04-15 14:23:42 -0700175 InCallService.VideoCall videoCall = call.getVideoCall();
176 if (videoCall != null) {
Andrew Lee011728f2015-04-23 15:47:06 -0700177 videoCall.destroy();
Tyler Gunn75958422015-04-15 14:23:42 -0700178 }
Santos Cordonf30d7e92014-08-26 09:54:33 -0700179 if (call.getState() != Call.STATE_DISCONNECTED) {
180 call.internalSetDisconnected();
181 }
182 }
183 }
184
185 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700186 * Adds a listener to this {@code Phone}.
187 *
188 * @param listener A {@code Listener} object.
189 */
190 public final void addListener(Listener listener) {
191 mListeners.add(listener);
192 }
193
194 /**
195 * Removes a listener from this {@code Phone}.
196 *
197 * @param listener A {@code Listener} object.
198 */
199 public final void removeListener(Listener listener) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700200 if (listener != null) {
201 mListeners.remove(listener);
202 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700203 }
204
205 /**
206 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
207 *
208 * @return A list of the relevant {@code Call}s.
209 */
210 public final List<Call> getCalls() {
211 return mUnmodifiableCalls;
212 }
213
214 /**
Santos Cordon6c912b72014-11-07 16:05:09 -0800215 * Returns if the {@code Phone} can support additional calls.
216 *
217 * @return Whether the phone supports adding more calls.
218 */
219 public final boolean canAddCall() {
220 return mCanAddCall;
221 }
222
223 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700224 * Sets the microphone mute state. When this request is honored, there will be change to
225 * the {@link #getAudioState()}.
226 *
227 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
228 */
229 public final void setMuted(boolean state) {
230 mInCallAdapter.mute(state);
231 }
232
233 /**
234 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
235 * be change to the {@link #getAudioState()}.
236 *
237 * @param route The audio route to use.
238 */
239 public final void setAudioRoute(int route) {
240 mInCallAdapter.setAudioRoute(route);
241 }
242
243 /**
Yorke Lee0d6ea712014-07-28 14:39:23 -0700244 * Turns the proximity sensor on. When this request is made, the proximity sensor will
245 * become active, and the touch screen and display will be turned off when the user's face
246 * is detected to be in close proximity to the screen. This operation is a no-op on devices
247 * that do not have a proximity sensor.
Yorke Lee22244d02015-04-14 12:34:28 -0700248 *
249 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700250 */
251 public final void setProximitySensorOn() {
252 mInCallAdapter.turnProximitySensorOn();
253 }
254
255 /**
256 * Turns the proximity sensor off. When this request is made, the proximity sensor will
257 * become inactive, and no longer affect the touch screen and display. This operation is a
258 * no-op on devices that do not have a proximity sensor.
259 *
260 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
261 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
262 * is no longer triggered.
Yorke Lee22244d02015-04-14 12:34:28 -0700263 *
264 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700265 */
266 public final void setProximitySensorOff(boolean screenOnImmediately) {
267 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
268 }
269
270 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700271 * Obtains the current phone call audio state of the {@code Phone}.
272 *
273 * @return An object encapsulating the audio state.
274 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700275 public final AudioState getAudioState() {
Ihab Awade63fadb2014-07-09 21:52:04 -0700276 return mAudioState;
277 }
278
279 private void fireCallAdded(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700280 for (Listener listener : mListeners) {
281 listener.onCallAdded(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700282 }
283 }
284
285 private void fireCallRemoved(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700286 for (Listener listener : mListeners) {
287 listener.onCallRemoved(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700288 }
289 }
290
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700291 private void fireAudioStateChanged(AudioState audioState) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700292 for (Listener listener : mListeners) {
293 listener.onAudioStateChanged(this, audioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700294 }
295 }
296
297 private void fireBringToForeground(boolean showDialpad) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700298 for (Listener listener : mListeners) {
299 listener.onBringToForeground(this, showDialpad);
Ihab Awade63fadb2014-07-09 21:52:04 -0700300 }
301 }
302
Santos Cordon6c912b72014-11-07 16:05:09 -0800303 private void fireCanAddCallChanged(boolean canAddCall) {
304 for (Listener listener : mListeners) {
305 listener.onCanAddCallChanged(this, canAddCall);
306 }
307 }
308
Santos Cordon88b771d2014-07-19 13:10:40 -0700309 private void checkCallTree(ParcelableCall parcelableCall) {
310 if (parcelableCall.getParentCallId() != null &&
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700311 !mCallByTelecomCallId.containsKey(parcelableCall.getParentCallId())) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700312 Log.wtf(this, "ParcelableCall %s has nonexistent parent %s",
313 parcelableCall.getId(), parcelableCall.getParentCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700314 }
Santos Cordon88b771d2014-07-19 13:10:40 -0700315 if (parcelableCall.getChildCallIds() != null) {
316 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700317 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700318 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
319 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
Ihab Awade63fadb2014-07-09 21:52:04 -0700320 }
321 }
322 }
323 }
324}