blob: c1c1129a2aab44b39d06fae8eb93fa02b7479049 [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);
125 fireCallRemoved(call);
126 }
127
Santos Cordon88b771d2014-07-19 13:10:40 -0700128 final void internalUpdateCall(ParcelableCall parcelableCall) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700129 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700130 if (call != null) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700131 checkCallTree(parcelableCall);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700132 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700133 }
134 }
135
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700136 final void internalSetPostDialWait(String telecomId, String remaining) {
137 Call call = mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700138 if (call != null) {
139 call.internalSetPostDialWait(remaining);
140 }
141 }
142
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
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700150 final Call internalGetCallByTelecomId(String telecomId) {
151 return mCallByTelecomCallId.get(telecomId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700152 }
153
Ihab Awade63fadb2014-07-09 21:52:04 -0700154 final void internalBringToForeground(boolean showDialpad) {
155 fireBringToForeground(showDialpad);
156 }
157
Santos Cordon6c912b72014-11-07 16:05:09 -0800158 final void internalSetCanAddCall(boolean canAddCall) {
159 if (mCanAddCall != canAddCall) {
160 mCanAddCall = canAddCall;
161 fireCanAddCallChanged(canAddCall);
162 }
163 }
164
Ihab Awade63fadb2014-07-09 21:52:04 -0700165 /**
Santos Cordonf30d7e92014-08-26 09:54:33 -0700166 * Called to destroy the phone and cleanup any lingering calls.
Santos Cordonf30d7e92014-08-26 09:54:33 -0700167 */
168 final void destroy() {
169 for (Call call : mCalls) {
170 if (call.getState() != Call.STATE_DISCONNECTED) {
171 call.internalSetDisconnected();
172 }
173 }
174 }
175
176 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700177 * Adds a listener to this {@code Phone}.
178 *
179 * @param listener A {@code Listener} object.
180 */
181 public final void addListener(Listener listener) {
182 mListeners.add(listener);
183 }
184
185 /**
186 * Removes a listener from this {@code Phone}.
187 *
188 * @param listener A {@code Listener} object.
189 */
190 public final void removeListener(Listener listener) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700191 if (listener != null) {
192 mListeners.remove(listener);
193 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700194 }
195
196 /**
197 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
198 *
199 * @return A list of the relevant {@code Call}s.
200 */
201 public final List<Call> getCalls() {
202 return mUnmodifiableCalls;
203 }
204
205 /**
Santos Cordon6c912b72014-11-07 16:05:09 -0800206 * Returns if the {@code Phone} can support additional calls.
207 *
208 * @return Whether the phone supports adding more calls.
209 */
210 public final boolean canAddCall() {
211 return mCanAddCall;
212 }
213
214 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700215 * Sets the microphone mute state. When this request is honored, there will be change to
216 * the {@link #getAudioState()}.
217 *
218 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
219 */
220 public final void setMuted(boolean state) {
221 mInCallAdapter.mute(state);
222 }
223
224 /**
225 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
226 * be change to the {@link #getAudioState()}.
227 *
228 * @param route The audio route to use.
229 */
230 public final void setAudioRoute(int route) {
231 mInCallAdapter.setAudioRoute(route);
232 }
233
234 /**
Yorke Lee0d6ea712014-07-28 14:39:23 -0700235 * Turns the proximity sensor on. When this request is made, the proximity sensor will
236 * become active, and the touch screen and display will be turned off when the user's face
237 * is detected to be in close proximity to the screen. This operation is a no-op on devices
238 * that do not have a proximity sensor.
Yorke Lee22244d02015-04-14 12:34:28 -0700239 *
240 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700241 */
242 public final void setProximitySensorOn() {
243 mInCallAdapter.turnProximitySensorOn();
244 }
245
246 /**
247 * Turns the proximity sensor off. When this request is made, the proximity sensor will
248 * become inactive, and no longer affect the touch screen and display. This operation is a
249 * no-op on devices that do not have a proximity sensor.
250 *
251 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
252 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
253 * is no longer triggered.
Yorke Lee22244d02015-04-14 12:34:28 -0700254 *
255 * @hide
Yorke Lee0d6ea712014-07-28 14:39:23 -0700256 */
257 public final void setProximitySensorOff(boolean screenOnImmediately) {
258 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
259 }
260
261 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700262 * Obtains the current phone call audio state of the {@code Phone}.
263 *
264 * @return An object encapsulating the audio state.
265 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700266 public final AudioState getAudioState() {
Ihab Awade63fadb2014-07-09 21:52:04 -0700267 return mAudioState;
268 }
269
270 private void fireCallAdded(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700271 for (Listener listener : mListeners) {
272 listener.onCallAdded(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700273 }
274 }
275
276 private void fireCallRemoved(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700277 for (Listener listener : mListeners) {
278 listener.onCallRemoved(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700279 }
280 }
281
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700282 private void fireAudioStateChanged(AudioState audioState) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700283 for (Listener listener : mListeners) {
284 listener.onAudioStateChanged(this, audioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700285 }
286 }
287
288 private void fireBringToForeground(boolean showDialpad) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700289 for (Listener listener : mListeners) {
290 listener.onBringToForeground(this, showDialpad);
Ihab Awade63fadb2014-07-09 21:52:04 -0700291 }
292 }
293
Santos Cordon6c912b72014-11-07 16:05:09 -0800294 private void fireCanAddCallChanged(boolean canAddCall) {
295 for (Listener listener : mListeners) {
296 listener.onCanAddCallChanged(this, canAddCall);
297 }
298 }
299
Santos Cordon88b771d2014-07-19 13:10:40 -0700300 private void checkCallTree(ParcelableCall parcelableCall) {
301 if (parcelableCall.getParentCallId() != null &&
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700302 !mCallByTelecomCallId.containsKey(parcelableCall.getParentCallId())) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700303 Log.wtf(this, "ParcelableCall %s has nonexistent parent %s",
304 parcelableCall.getId(), parcelableCall.getParentCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700305 }
Santos Cordon88b771d2014-07-19 13:10:40 -0700306 if (parcelableCall.getChildCallIds() != null) {
307 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700308 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700309 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
310 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
Ihab Awade63fadb2014-07-09 21:52:04 -0700311 }
312 }
313 }
314 }
315}