blob: d456dfad21e0de497d0aa3a75bba73c4e2049697 [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
19import android.util.ArrayMap;
20
Ihab Awade63fadb2014-07-09 21:52:04 -070021import java.util.Collections;
22import java.util.List;
23import java.util.Map;
24import java.util.Objects;
Jay Shrauner229e3822014-08-15 09:23:07 -070025import java.util.concurrent.CopyOnWriteArrayList;
Ihab Awade63fadb2014-07-09 21:52:04 -070026
27/**
28 * A unified virtual device providing a means of voice (and other) communication on a device.
29 */
30public final class Phone {
31
32 public abstract static class Listener {
33 /**
34 * Called when the audio state changes.
35 *
36 * @param phone The {@code Phone} calling this method.
Ihab Awadb19a0bc2014-08-07 19:46:01 -070037 * @param audioState The new {@link AudioState}.
Ihab Awade63fadb2014-07-09 21:52:04 -070038 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070039 public void onAudioStateChanged(Phone phone, AudioState audioState) { }
Ihab Awade63fadb2014-07-09 21:52:04 -070040
41 /**
42 * Called to bring the in-call screen to the foreground. The in-call experience should
43 * respond immediately by coming to the foreground to inform the user of the state of
44 * ongoing {@code Call}s.
45 *
46 * @param phone The {@code Phone} calling this method.
47 * @param showDialpad If true, put up the dialpad when the screen is shown.
48 */
49 public void onBringToForeground(Phone phone, boolean showDialpad) { }
50
51 /**
52 * Called when a {@code Call} has been added to this in-call session. The in-call user
53 * experience should add necessary state listeners to the specified {@code Call} and
54 * immediately start to show the user information about the existence
55 * and nature of this {@code Call}. Subsequent invocations of {@link #getCalls()} will
56 * include this {@code Call}.
57 *
58 * @param phone The {@code Phone} calling this method.
59 * @param call A newly added {@code Call}.
60 */
61 public void onCallAdded(Phone phone, Call call) { }
62
63 /**
64 * Called when a {@code Call} has been removed from this in-call session. The in-call user
65 * experience should remove any state listeners from the specified {@code Call} and
66 * immediately stop displaying any information about this {@code Call}.
67 * Subsequent invocations of {@link #getCalls()} will no longer include this {@code Call}.
68 *
69 * @param phone The {@code Phone} calling this method.
70 * @param call A newly removed {@code Call}.
71 */
72 public void onCallRemoved(Phone phone, Call call) { }
Santos Cordon6c912b72014-11-07 16:05:09 -080073
74 /**
75 * Called when the {@code Phone} ability to add more calls changes. If the phone cannot
76 * support more calls then {@code canAddCall} is set to {@code false}. If it can, then it
77 * is set to {@code true}.
78 *
79 * @param phone The {@code Phone} calling this method.
80 * @param canAddCall Indicates whether an additional call can be added.
81 */
82 public void onCanAddCallChanged(Phone phone, boolean canAddCall) { }
Ihab Awade63fadb2014-07-09 21:52:04 -070083 }
84
Tyler Gunnef9f6f92014-09-12 22:16:17 -070085 // A Map allows us to track each Call by its Telecom-specified call ID
86 private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>();
Ihab Awade63fadb2014-07-09 21:52:04 -070087
88 // A List allows us to keep the Calls in a stable iteration order so that casually developed
89 // user interface components do not incur any spurious jank
Santos Cordonf30d7e92014-08-26 09:54:33 -070090 private final List<Call> mCalls = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -070091
92 // An unmodifiable view of the above List can be safely shared with subclass implementations
93 private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls);
94
95 private final InCallAdapter mInCallAdapter;
96
Ihab Awadb19a0bc2014-08-07 19:46:01 -070097 private AudioState mAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -070098
Jay Shrauner229e3822014-08-15 09:23:07 -070099 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700100
Santos Cordon6c912b72014-11-07 16:05:09 -0800101 private boolean mCanAddCall = true;
102
Ihab Awade63fadb2014-07-09 21:52:04 -0700103 /** {@hide} */
104 Phone(InCallAdapter adapter) {
105 mInCallAdapter = adapter;
106 }
107
108 /** {@hide} */
Santos Cordon88b771d2014-07-19 13:10:40 -0700109 final void internalAddCall(ParcelableCall parcelableCall) {
110 Call call = new Call(this, parcelableCall.getId(), mInCallAdapter);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700111 mCallByTelecomCallId.put(parcelableCall.getId(), call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700112 mCalls.add(call);
Santos Cordon88b771d2014-07-19 13:10:40 -0700113 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700114 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700115 fireCallAdded(call);
116 }
117
118 /** {@hide} */
119 final void internalRemoveCall(Call call) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700120 mCallByTelecomCallId.remove(call.internalGetCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700121 mCalls.remove(call);
Tyler Gunn75958422015-04-15 14:23:42 -0700122
123 InCallService.VideoCall videoCall = call.getVideoCall();
124 if (videoCall != null) {
125 videoCall.removeVideoCallListener();
126 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700127 fireCallRemoved(call);
128 }
129
130 /** {@hide} */
Santos Cordon88b771d2014-07-19 13:10:40 -0700131 final void internalUpdateCall(ParcelableCall parcelableCall) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700132 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700133 if (call != null) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700134 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700135 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700136 }
137 }
138
139 /** {@hide} */
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700140 final void internalSetPostDialWait(String telecomId, String remaining) {
141 Call call = mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700142 if (call != null) {
143 call.internalSetPostDialWait(remaining);
144 }
145 }
146
147 /** {@hide} */
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
155 /** {@hide} */
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700156 final Call internalGetCallByTelecomId(String telecomId) {
157 return mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700158 }
159
160 /** {@hide} */
161 final void internalBringToForeground(boolean showDialpad) {
162 fireBringToForeground(showDialpad);
163 }
164
Santos Cordon6c912b72014-11-07 16:05:09 -0800165 /** {@hide} */
166 final void internalSetCanAddCall(boolean canAddCall) {
167 if (mCanAddCall != canAddCall) {
168 mCanAddCall = canAddCall;
169 fireCanAddCallChanged(canAddCall);
170 }
171 }
172
Ihab Awade63fadb2014-07-09 21:52:04 -0700173 /**
Santos Cordonf30d7e92014-08-26 09:54:33 -0700174 * Called to destroy the phone and cleanup any lingering calls.
175 * @hide
176 */
177 final void destroy() {
178 for (Call call : mCalls) {
Tyler Gunn75958422015-04-15 14:23:42 -0700179 InCallService.VideoCall videoCall = call.getVideoCall();
180 if (videoCall != null) {
181 videoCall.removeVideoCallListener();
182 }
Santos Cordonf30d7e92014-08-26 09:54:33 -0700183 if (call.getState() != Call.STATE_DISCONNECTED) {
184 call.internalSetDisconnected();
185 }
186 }
187 }
188
189 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700190 * Adds a listener to this {@code Phone}.
191 *
192 * @param listener A {@code Listener} object.
193 */
194 public final void addListener(Listener listener) {
195 mListeners.add(listener);
196 }
197
198 /**
199 * Removes a listener from this {@code Phone}.
200 *
201 * @param listener A {@code Listener} object.
202 */
203 public final void removeListener(Listener listener) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700204 if (listener != null) {
205 mListeners.remove(listener);
206 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700207 }
208
209 /**
210 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
211 *
212 * @return A list of the relevant {@code Call}s.
213 */
214 public final List<Call> getCalls() {
215 return mUnmodifiableCalls;
216 }
217
218 /**
Santos Cordon6c912b72014-11-07 16:05:09 -0800219 * Returns if the {@code Phone} can support additional calls.
220 *
221 * @return Whether the phone supports adding more calls.
222 */
223 public final boolean canAddCall() {
224 return mCanAddCall;
225 }
226
227 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700228 * Sets the microphone mute state. When this request is honored, there will be change to
229 * the {@link #getAudioState()}.
230 *
231 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
232 */
233 public final void setMuted(boolean state) {
234 mInCallAdapter.mute(state);
235 }
236
237 /**
238 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
239 * be change to the {@link #getAudioState()}.
240 *
241 * @param route The audio route to use.
242 */
243 public final void setAudioRoute(int route) {
244 mInCallAdapter.setAudioRoute(route);
245 }
246
247 /**
Yorke Lee0d6ea712014-07-28 14:39:23 -0700248 * Turns the proximity sensor on. When this request is made, the proximity sensor will
249 * become active, and the touch screen and display will be turned off when the user's face
250 * is detected to be in close proximity to the screen. This operation is a no-op on devices
251 * that do not have a proximity sensor.
Yorke Lee2287a012015-04-14 15:48:19 -0700252 *
253 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700254 */
255 public final void setProximitySensorOn() {
256 mInCallAdapter.turnProximitySensorOn();
257 }
258
259 /**
260 * Turns the proximity sensor off. When this request is made, the proximity sensor will
261 * become inactive, and no longer affect the touch screen and display. This operation is a
262 * no-op on devices that do not have a proximity sensor.
263 *
264 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
265 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
266 * is no longer triggered.
Yorke Lee2287a012015-04-14 15:48:19 -0700267 *
268 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700269 */
270 public final void setProximitySensorOff(boolean screenOnImmediately) {
271 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
272 }
273
274 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700275 * Obtains the current phone call audio state of the {@code Phone}.
276 *
277 * @return An object encapsulating the audio state.
278 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700279 public final AudioState getAudioState() {
Ihab Awade63fadb2014-07-09 21:52:04 -0700280 return mAudioState;
281 }
282
283 private void fireCallAdded(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700284 for (Listener listener : mListeners) {
285 listener.onCallAdded(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700286 }
287 }
288
289 private void fireCallRemoved(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700290 for (Listener listener : mListeners) {
291 listener.onCallRemoved(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700292 }
293 }
294
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700295 private void fireAudioStateChanged(AudioState audioState) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700296 for (Listener listener : mListeners) {
297 listener.onAudioStateChanged(this, audioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700298 }
299 }
300
301 private void fireBringToForeground(boolean showDialpad) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700302 for (Listener listener : mListeners) {
303 listener.onBringToForeground(this, showDialpad);
Ihab Awade63fadb2014-07-09 21:52:04 -0700304 }
305 }
306
Santos Cordon6c912b72014-11-07 16:05:09 -0800307 private void fireCanAddCallChanged(boolean canAddCall) {
308 for (Listener listener : mListeners) {
309 listener.onCanAddCallChanged(this, canAddCall);
310 }
311 }
312
Santos Cordon88b771d2014-07-19 13:10:40 -0700313 private void checkCallTree(ParcelableCall parcelableCall) {
314 if (parcelableCall.getParentCallId() != null &&
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700315 !mCallByTelecomCallId.containsKey(parcelableCall.getParentCallId())) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700316 Log.wtf(this, "ParcelableCall %s has nonexistent parent %s",
317 parcelableCall.getId(), parcelableCall.getParentCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700318 }
Santos Cordon88b771d2014-07-19 13:10:40 -0700319 if (parcelableCall.getChildCallIds() != null) {
320 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700321 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700322 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
323 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
Ihab Awade63fadb2014-07-09 21:52:04 -0700324 }
325 }
326 }
327 }
328}