2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.telecom;
19 import android.annotation.SystemApi;
20 import android.util.ArrayMap;
22 import java.util.Collections;
23 import java.util.List;
25 import java.util.Objects;
26 import java.util.concurrent.CopyOnWriteArrayList;
29 * A unified virtual device providing a means of voice (and other) communication on a device.
32 * @deprecated Use {@link InCallService} directly instead of using this class.
36 public final class Phone {
38 public abstract static class Listener {
40 * Called when the audio state changes.
42 * @param phone The {@code Phone} calling this method.
43 * @param audioState The new {@link AudioState}.
45 * @deprecated Use {@link #onCallAudioStateChanged(Phone, CallAudioState)} instead.
48 public void onAudioStateChanged(Phone phone, AudioState audioState) { }
51 * Called when the audio state changes.
53 * @param phone The {@code Phone} calling this method.
54 * @param callAudioState The new {@link CallAudioState}.
56 public void onCallAudioStateChanged(Phone phone, CallAudioState callAudioState) { }
59 * Called to bring the in-call screen to the foreground. The in-call experience should
60 * respond immediately by coming to the foreground to inform the user of the state of
61 * ongoing {@code Call}s.
63 * @param phone The {@code Phone} calling this method.
64 * @param showDialpad If true, put up the dialpad when the screen is shown.
66 public void onBringToForeground(Phone phone, boolean showDialpad) { }
69 * Called when a {@code Call} has been added to this in-call session. The in-call user
70 * experience should add necessary state listeners to the specified {@code Call} and
71 * immediately start to show the user information about the existence
72 * and nature of this {@code Call}. Subsequent invocations of {@link #getCalls()} will
73 * include this {@code Call}.
75 * @param phone The {@code Phone} calling this method.
76 * @param call A newly added {@code Call}.
78 public void onCallAdded(Phone phone, Call call) { }
81 * Called when a {@code Call} has been removed from this in-call session. The in-call user
82 * experience should remove any state listeners from the specified {@code Call} and
83 * immediately stop displaying any information about this {@code Call}.
84 * Subsequent invocations of {@link #getCalls()} will no longer include this {@code Call}.
86 * @param phone The {@code Phone} calling this method.
87 * @param call A newly removed {@code Call}.
89 public void onCallRemoved(Phone phone, Call call) { }
92 * Called when the {@code Phone} ability to add more calls changes. If the phone cannot
93 * support more calls then {@code canAddCall} is set to {@code false}. If it can, then it
94 * is set to {@code true}.
96 * @param phone The {@code Phone} calling this method.
97 * @param canAddCall Indicates whether an additional call can be added.
99 public void onCanAddCallChanged(Phone phone, boolean canAddCall) { }
102 * Called to silence the ringer if a ringing call exists.
104 * @param phone The {@code Phone} calling this method.
106 public void onSilenceRinger(Phone phone) { }
109 // A Map allows us to track each Call by its Telecom-specified call ID
110 private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>();
112 // A List allows us to keep the Calls in a stable iteration order so that casually developed
113 // user interface components do not incur any spurious jank
114 private final List<Call> mCalls = new CopyOnWriteArrayList<>();
116 // An unmodifiable view of the above List can be safely shared with subclass implementations
117 private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls);
119 private final InCallAdapter mInCallAdapter;
121 private CallAudioState mCallAudioState;
123 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
125 private boolean mCanAddCall = true;
127 Phone(InCallAdapter adapter) {
128 mInCallAdapter = adapter;
131 final void internalAddCall(ParcelableCall parcelableCall) {
132 Call call = new Call(this, parcelableCall.getId(), mInCallAdapter,
133 parcelableCall.getState());
134 mCallByTelecomCallId.put(parcelableCall.getId(), call);
136 checkCallTree(parcelableCall);
137 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
141 final void internalRemoveCall(Call call) {
142 mCallByTelecomCallId.remove(call.internalGetCallId());
145 InCallService.VideoCall videoCall = call.getVideoCall();
146 if (videoCall != null) {
149 fireCallRemoved(call);
152 final void internalUpdateCall(ParcelableCall parcelableCall) {
153 Call call = mCallByTelecomCallId.get(parcelableCall.getId());
155 checkCallTree(parcelableCall);
156 call.internalUpdate(parcelableCall, mCallByTelecomCallId);
160 final void internalSetPostDialWait(String telecomId, String remaining) {
161 Call call = mCallByTelecomCallId.get(telecomId);
163 call.internalSetPostDialWait(remaining);
167 final void internalCallAudioStateChanged(CallAudioState callAudioState) {
168 if (!Objects.equals(mCallAudioState, callAudioState)) {
169 mCallAudioState = callAudioState;
170 fireCallAudioStateChanged(callAudioState);
174 final Call internalGetCallByTelecomId(String telecomId) {
175 return mCallByTelecomCallId.get(telecomId);
178 final void internalBringToForeground(boolean showDialpad) {
179 fireBringToForeground(showDialpad);
182 final void internalSetCanAddCall(boolean canAddCall) {
183 if (mCanAddCall != canAddCall) {
184 mCanAddCall = canAddCall;
185 fireCanAddCallChanged(canAddCall);
189 final void internalSilenceRinger() {
194 * Called to destroy the phone and cleanup any lingering calls.
196 final void destroy() {
197 for (Call call : mCalls) {
198 InCallService.VideoCall videoCall = call.getVideoCall();
199 if (videoCall != null) {
202 if (call.getState() != Call.STATE_DISCONNECTED) {
203 call.internalSetDisconnected();
209 * Adds a listener to this {@code Phone}.
211 * @param listener A {@code Listener} object.
213 public final void addListener(Listener listener) {
214 mListeners.add(listener);
218 * Removes a listener from this {@code Phone}.
220 * @param listener A {@code Listener} object.
222 public final void removeListener(Listener listener) {
223 if (listener != null) {
224 mListeners.remove(listener);
229 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
231 * @return A list of the relevant {@code Call}s.
233 public final List<Call> getCalls() {
234 return mUnmodifiableCalls;
238 * Returns if the {@code Phone} can support additional calls.
240 * @return Whether the phone supports adding more calls.
242 public final boolean canAddCall() {
247 * Sets the microphone mute state. When this request is honored, there will be change to
248 * the {@link #getAudioState()}.
250 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
252 public final void setMuted(boolean state) {
253 mInCallAdapter.mute(state);
257 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
258 * be change to the {@link #getAudioState()}.
260 * @param route The audio route to use.
262 public final void setAudioRoute(int route) {
263 mInCallAdapter.setAudioRoute(route);
267 * Turns the proximity sensor on. When this request is made, the proximity sensor will
268 * become active, and the touch screen and display will be turned off when the user's face
269 * is detected to be in close proximity to the screen. This operation is a no-op on devices
270 * that do not have a proximity sensor.
274 public final void setProximitySensorOn() {
275 mInCallAdapter.turnProximitySensorOn();
279 * Turns the proximity sensor off. When this request is made, the proximity sensor will
280 * become inactive, and no longer affect the touch screen and display. This operation is a
281 * no-op on devices that do not have a proximity sensor.
283 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
284 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
285 * is no longer triggered.
289 public final void setProximitySensorOff(boolean screenOnImmediately) {
290 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
294 * Obtains the current phone call audio state of the {@code Phone}.
296 * @return An object encapsulating the audio state.
297 * @deprecated Use {@link #getCallAudioState()} instead.
300 public final AudioState getAudioState() {
301 return new AudioState(mCallAudioState);
305 * Obtains the current phone call audio state of the {@code Phone}.
307 * @return An object encapsulating the audio state.
309 public final CallAudioState getCallAudioState() {
310 return mCallAudioState;
313 private void fireCallAdded(Call call) {
314 for (Listener listener : mListeners) {
315 listener.onCallAdded(this, call);
319 private void fireCallRemoved(Call call) {
320 for (Listener listener : mListeners) {
321 listener.onCallRemoved(this, call);
325 private void fireCallAudioStateChanged(CallAudioState audioState) {
326 for (Listener listener : mListeners) {
327 listener.onCallAudioStateChanged(this, audioState);
328 listener.onAudioStateChanged(this, new AudioState(audioState));
332 private void fireBringToForeground(boolean showDialpad) {
333 for (Listener listener : mListeners) {
334 listener.onBringToForeground(this, showDialpad);
338 private void fireCanAddCallChanged(boolean canAddCall) {
339 for (Listener listener : mListeners) {
340 listener.onCanAddCallChanged(this, canAddCall);
344 private void fireSilenceRinger() {
345 for (Listener listener : mListeners) {
346 listener.onSilenceRinger(this);
350 private void checkCallTree(ParcelableCall parcelableCall) {
351 if (parcelableCall.getChildCallIds() != null) {
352 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
353 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
354 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
355 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));