blob: cc731095f9c07c7916a052e8191c6ea4b1e139ef [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
Gabriel Pealb95f1692014-08-19 14:24:18 -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.
Ihab Awadb19a0bc2014-08-07 19:46:01 -070030 *
31 * {@hide}
Ihab Awade63fadb2014-07-09 21:52:04 -070032 */
Gabriel Pealb95f1692014-08-19 14:24:18 -070033@SystemApi
Ihab Awade63fadb2014-07-09 21:52:04 -070034public final class Phone {
35
36 public abstract static class Listener {
37 /**
38 * Called when the audio state changes.
39 *
40 * @param phone The {@code Phone} calling this method.
Ihab Awadb19a0bc2014-08-07 19:46:01 -070041 * @param audioState The new {@link AudioState}.
Ihab Awade63fadb2014-07-09 21:52:04 -070042 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070043 public void onAudioStateChanged(Phone phone, AudioState audioState) { }
Ihab Awade63fadb2014-07-09 21:52:04 -070044
45 /**
46 * Called to bring the in-call screen to the foreground. The in-call experience should
47 * respond immediately by coming to the foreground to inform the user of the state of
48 * ongoing {@code Call}s.
49 *
50 * @param phone The {@code Phone} calling this method.
51 * @param showDialpad If true, put up the dialpad when the screen is shown.
52 */
53 public void onBringToForeground(Phone phone, boolean showDialpad) { }
54
55 /**
56 * Called when a {@code Call} has been added to this in-call session. The in-call user
57 * experience should add necessary state listeners to the specified {@code Call} and
58 * immediately start to show the user information about the existence
59 * and nature of this {@code Call}. Subsequent invocations of {@link #getCalls()} will
60 * include this {@code Call}.
61 *
62 * @param phone The {@code Phone} calling this method.
63 * @param call A newly added {@code Call}.
64 */
65 public void onCallAdded(Phone phone, Call call) { }
66
67 /**
68 * Called when a {@code Call} has been removed from this in-call session. The in-call user
69 * experience should remove any state listeners from the specified {@code Call} and
70 * immediately stop displaying any information about this {@code Call}.
71 * Subsequent invocations of {@link #getCalls()} will no longer include this {@code Call}.
72 *
73 * @param phone The {@code Phone} calling this method.
74 * @param call A newly removed {@code Call}.
75 */
76 public void onCallRemoved(Phone phone, Call call) { }
Santos Cordon6c912b72014-11-07 16:05:09 -080077
78 /**
79 * Called when the {@code Phone} ability to add more calls changes. If the phone cannot
80 * support more calls then {@code canAddCall} is set to {@code false}. If it can, then it
81 * is set to {@code true}.
82 *
83 * @param phone The {@code Phone} calling this method.
84 * @param canAddCall Indicates whether an additional call can be added.
85 */
86 public void onCanAddCallChanged(Phone phone, boolean canAddCall) { }
Ihab Awade63fadb2014-07-09 21:52:04 -070087 }
88
Tyler Gunnef9f6f92014-09-12 22:16:17 -070089 // A Map allows us to track each Call by its Telecom-specified call ID
90 private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>();
Ihab Awade63fadb2014-07-09 21:52:04 -070091
92 // A List allows us to keep the Calls in a stable iteration order so that casually developed
93 // user interface components do not incur any spurious jank
Santos Cordonf30d7e92014-08-26 09:54:33 -070094 private final List<Call> mCalls = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -070095
96 // An unmodifiable view of the above List can be safely shared with subclass implementations
97 private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls);
98
99 private final InCallAdapter mInCallAdapter;
100
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700101 private AudioState mAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -0700102
Jay Shrauner229e3822014-08-15 09:23:07 -0700103 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -0700104
Santos Cordon6c912b72014-11-07 16:05:09 -0800105 private boolean mCanAddCall = true;
106
Ihab Awade63fadb2014-07-09 21:52:04 -0700107 /** {@hide} */
108 Phone(InCallAdapter adapter) {
109 mInCallAdapter = adapter;
110 }
111
112 /** {@hide} */
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
122 /** {@hide} */
123 final void internalRemoveCall(Call call) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700124 mCallByTelecomCallId.remove(call.internalGetCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700125 mCalls.remove(call);
126 fireCallRemoved(call);
127 }
128
129 /** {@hide} */
Santos Cordon88b771d2014-07-19 13:10:40 -0700130 final void internalUpdateCall(ParcelableCall parcelableCall) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700131 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700132 if (call != null) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700133 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700134 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700135 }
136 }
137
138 /** {@hide} */
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700139 final void internalSetPostDialWait(String telecomId, String remaining) {
140 Call call = mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700141 if (call != null) {
142 call.internalSetPostDialWait(remaining);
143 }
144 }
145
146 /** {@hide} */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700147 final void internalAudioStateChanged(AudioState audioState) {
148 if (!Objects.equals(mAudioState, audioState)) {
149 mAudioState = audioState;
150 fireAudioStateChanged(audioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700151 }
152 }
153
154 /** {@hide} */
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
159 /** {@hide} */
160 final void internalBringToForeground(boolean showDialpad) {
161 fireBringToForeground(showDialpad);
162 }
163
Santos Cordon6c912b72014-11-07 16:05:09 -0800164 /** {@hide} */
165 final void internalSetCanAddCall(boolean canAddCall) {
166 if (mCanAddCall != canAddCall) {
167 mCanAddCall = canAddCall;
168 fireCanAddCallChanged(canAddCall);
169 }
170 }
171
Ihab Awade63fadb2014-07-09 21:52:04 -0700172 /**
Santos Cordonf30d7e92014-08-26 09:54:33 -0700173 * Called to destroy the phone and cleanup any lingering calls.
174 * @hide
175 */
176 final void destroy() {
177 for (Call call : mCalls) {
178 if (call.getState() != Call.STATE_DISCONNECTED) {
179 call.internalSetDisconnected();
180 }
181 }
182 }
183
184 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700185 * Adds a listener to this {@code Phone}.
186 *
187 * @param listener A {@code Listener} object.
188 */
189 public final void addListener(Listener listener) {
190 mListeners.add(listener);
191 }
192
193 /**
194 * Removes a listener from this {@code Phone}.
195 *
196 * @param listener A {@code Listener} object.
197 */
198 public final void removeListener(Listener listener) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700199 if (listener != null) {
200 mListeners.remove(listener);
201 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700202 }
203
204 /**
205 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
206 *
207 * @return A list of the relevant {@code Call}s.
208 */
209 public final List<Call> getCalls() {
210 return mUnmodifiableCalls;
211 }
212
213 /**
Santos Cordon6c912b72014-11-07 16:05:09 -0800214 * Returns if the {@code Phone} can support additional calls.
215 *
216 * @return Whether the phone supports adding more calls.
217 */
218 public final boolean canAddCall() {
219 return mCanAddCall;
220 }
221
222 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700223 * Sets the microphone mute state. When this request is honored, there will be change to
224 * the {@link #getAudioState()}.
225 *
226 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
227 */
228 public final void setMuted(boolean state) {
229 mInCallAdapter.mute(state);
230 }
231
232 /**
233 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
234 * be change to the {@link #getAudioState()}.
235 *
236 * @param route The audio route to use.
237 */
238 public final void setAudioRoute(int route) {
239 mInCallAdapter.setAudioRoute(route);
240 }
241
242 /**
Yorke Lee0d6ea712014-07-28 14:39:23 -0700243 * Turns the proximity sensor on. When this request is made, the proximity sensor will
244 * become active, and the touch screen and display will be turned off when the user's face
245 * is detected to be in close proximity to the screen. This operation is a no-op on devices
246 * that do not have a proximity sensor.
Yorke Lee22244d02015-04-14 12:34:28 -0700247 *
248 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700249 */
250 public final void setProximitySensorOn() {
251 mInCallAdapter.turnProximitySensorOn();
252 }
253
254 /**
255 * Turns the proximity sensor off. When this request is made, the proximity sensor will
256 * become inactive, and no longer affect the touch screen and display. This operation is a
257 * no-op on devices that do not have a proximity sensor.
258 *
259 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
260 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
261 * is no longer triggered.
Yorke Lee22244d02015-04-14 12:34:28 -0700262 *
263 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700264 */
265 public final void setProximitySensorOff(boolean screenOnImmediately) {
266 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
267 }
268
269 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700270 * Obtains the current phone call audio state of the {@code Phone}.
271 *
272 * @return An object encapsulating the audio state.
273 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700274 public final AudioState getAudioState() {
Ihab Awade63fadb2014-07-09 21:52:04 -0700275 return mAudioState;
276 }
277
278 private void fireCallAdded(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700279 for (Listener listener : mListeners) {
280 listener.onCallAdded(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700281 }
282 }
283
284 private void fireCallRemoved(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700285 for (Listener listener : mListeners) {
286 listener.onCallRemoved(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700287 }
288 }
289
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700290 private void fireAudioStateChanged(AudioState audioState) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700291 for (Listener listener : mListeners) {
292 listener.onAudioStateChanged(this, audioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700293 }
294 }
295
296 private void fireBringToForeground(boolean showDialpad) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700297 for (Listener listener : mListeners) {
298 listener.onBringToForeground(this, showDialpad);
Ihab Awade63fadb2014-07-09 21:52:04 -0700299 }
300 }
301
Santos Cordon6c912b72014-11-07 16:05:09 -0800302 private void fireCanAddCallChanged(boolean canAddCall) {
303 for (Listener listener : mListeners) {
304 listener.onCanAddCallChanged(this, canAddCall);
305 }
306 }
307
Santos Cordon88b771d2014-07-19 13:10:40 -0700308 private void checkCallTree(ParcelableCall parcelableCall) {
309 if (parcelableCall.getParentCallId() != null &&
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700310 !mCallByTelecomCallId.containsKey(parcelableCall.getParentCallId())) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700311 Log.wtf(this, "ParcelableCall %s has nonexistent parent %s",
312 parcelableCall.getId(), parcelableCall.getParentCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700313 }
Santos Cordon88b771d2014-07-19 13:10:40 -0700314 if (parcelableCall.getChildCallIds() != null) {
315 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700316 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700317 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
318 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
Ihab Awade63fadb2014-07-09 21:52:04 -0700319 }
320 }
321 }
322 }
323}