blob: d9a9cdf84da38b93aeaa7bfbf360a3cf1435d0ec [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);
122 fireCallRemoved(call);
123 }
124
125 /** {@hide} */
Santos Cordon88b771d2014-07-19 13:10:40 -0700126 final void internalUpdateCall(ParcelableCall parcelableCall) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700127 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700128 if (call != null) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700129 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700130 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700131 }
132 }
133
134 /** {@hide} */
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700135 final void internalSetPostDialWait(String telecomId, String remaining) {
136 Call call = mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700137 if (call != null) {
138 call.internalSetPostDialWait(remaining);
139 }
140 }
141
142 /** {@hide} */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700143 final void internalAudioStateChanged(AudioState audioState) {
144 if (!Objects.equals(mAudioState, audioState)) {
145 mAudioState = audioState;
146 fireAudioStateChanged(audioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700147 }
148 }
149
150 /** {@hide} */
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700151 final Call internalGetCallByTelecomId(String telecomId) {
152 return mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700153 }
154
155 /** {@hide} */
156 final void internalBringToForeground(boolean showDialpad) {
157 fireBringToForeground(showDialpad);
158 }
159
Santos Cordon6c912b72014-11-07 16:05:09 -0800160 /** {@hide} */
161 final void internalSetCanAddCall(boolean canAddCall) {
162 if (mCanAddCall != canAddCall) {
163 mCanAddCall = canAddCall;
164 fireCanAddCallChanged(canAddCall);
165 }
166 }
167
Ihab Awade63fadb2014-07-09 21:52:04 -0700168 /**
Santos Cordonf30d7e92014-08-26 09:54:33 -0700169 * Called to destroy the phone and cleanup any lingering calls.
170 * @hide
171 */
172 final void destroy() {
173 for (Call call : mCalls) {
174 if (call.getState() != Call.STATE_DISCONNECTED) {
175 call.internalSetDisconnected();
176 }
177 }
178 }
179
180 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700181 * Adds a listener to this {@code Phone}.
182 *
183 * @param listener A {@code Listener} object.
184 */
185 public final void addListener(Listener listener) {
186 mListeners.add(listener);
187 }
188
189 /**
190 * Removes a listener from this {@code Phone}.
191 *
192 * @param listener A {@code Listener} object.
193 */
194 public final void removeListener(Listener listener) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700195 if (listener != null) {
196 mListeners.remove(listener);
197 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700198 }
199
200 /**
201 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
202 *
203 * @return A list of the relevant {@code Call}s.
204 */
205 public final List<Call> getCalls() {
206 return mUnmodifiableCalls;
207 }
208
209 /**
Santos Cordon6c912b72014-11-07 16:05:09 -0800210 * Returns if the {@code Phone} can support additional calls.
211 *
212 * @return Whether the phone supports adding more calls.
213 */
214 public final boolean canAddCall() {
215 return mCanAddCall;
216 }
217
218 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700219 * Sets the microphone mute state. When this request is honored, there will be change to
220 * the {@link #getAudioState()}.
221 *
222 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
223 */
224 public final void setMuted(boolean state) {
225 mInCallAdapter.mute(state);
226 }
227
228 /**
229 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
230 * be change to the {@link #getAudioState()}.
231 *
232 * @param route The audio route to use.
233 */
234 public final void setAudioRoute(int route) {
235 mInCallAdapter.setAudioRoute(route);
236 }
237
238 /**
Yorke Lee0d6ea712014-07-28 14:39:23 -0700239 * Turns the proximity sensor on. When this request is made, the proximity sensor will
240 * become active, and the touch screen and display will be turned off when the user's face
241 * is detected to be in close proximity to the screen. This operation is a no-op on devices
242 * that do not have a proximity sensor.
Yorke Lee22244d02015-04-14 12:34:28 -0700243 *
244 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700245 */
246 public final void setProximitySensorOn() {
247 mInCallAdapter.turnProximitySensorOn();
248 }
249
250 /**
251 * Turns the proximity sensor off. When this request is made, the proximity sensor will
252 * become inactive, and no longer affect the touch screen and display. This operation is a
253 * no-op on devices that do not have a proximity sensor.
254 *
255 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
256 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
257 * is no longer triggered.
Yorke Lee22244d02015-04-14 12:34:28 -0700258 *
259 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700260 */
261 public final void setProximitySensorOff(boolean screenOnImmediately) {
262 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
263 }
264
265 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700266 * Obtains the current phone call audio state of the {@code Phone}.
267 *
268 * @return An object encapsulating the audio state.
269 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700270 public final AudioState getAudioState() {
Ihab Awade63fadb2014-07-09 21:52:04 -0700271 return mAudioState;
272 }
273
274 private void fireCallAdded(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700275 for (Listener listener : mListeners) {
276 listener.onCallAdded(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700277 }
278 }
279
280 private void fireCallRemoved(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700281 for (Listener listener : mListeners) {
282 listener.onCallRemoved(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700283 }
284 }
285
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700286 private void fireAudioStateChanged(AudioState audioState) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700287 for (Listener listener : mListeners) {
288 listener.onAudioStateChanged(this, audioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700289 }
290 }
291
292 private void fireBringToForeground(boolean showDialpad) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700293 for (Listener listener : mListeners) {
294 listener.onBringToForeground(this, showDialpad);
Ihab Awade63fadb2014-07-09 21:52:04 -0700295 }
296 }
297
Santos Cordon6c912b72014-11-07 16:05:09 -0800298 private void fireCanAddCallChanged(boolean canAddCall) {
299 for (Listener listener : mListeners) {
300 listener.onCanAddCallChanged(this, canAddCall);
301 }
302 }
303
Santos Cordon88b771d2014-07-19 13:10:40 -0700304 private void checkCallTree(ParcelableCall parcelableCall) {
305 if (parcelableCall.getParentCallId() != null &&
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700306 !mCallByTelecomCallId.containsKey(parcelableCall.getParentCallId())) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700307 Log.wtf(this, "ParcelableCall %s has nonexistent parent %s",
308 parcelableCall.getId(), parcelableCall.getParentCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700309 }
Santos Cordon88b771d2014-07-19 13:10:40 -0700310 if (parcelableCall.getChildCallIds() != null) {
311 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700312 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700313 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
314 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
Ihab Awade63fadb2014-07-09 21:52:04 -0700315 }
316 }
317 }
318 }
319}