blob: d90d9549802f05b0cdb6aa7fb6fc9ea926fb1751 [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
17package android.telecomm;
18
Gabriel Pealb95f1692014-08-19 14:24:18 -070019import android.annotation.SystemApi;
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -070020import android.app.PendingIntent;
Ihab Awade63fadb2014-07-09 21:52:04 -070021import android.util.ArrayMap;
22
Ihab Awade63fadb2014-07-09 21:52:04 -070023import java.util.Collections;
24import java.util.List;
25import java.util.Map;
26import java.util.Objects;
Jay Shrauner229e3822014-08-15 09:23:07 -070027import java.util.concurrent.CopyOnWriteArrayList;
Ihab Awade63fadb2014-07-09 21:52:04 -070028
29/**
30 * A unified virtual device providing a means of voice (and other) communication on a device.
Ihab Awadb19a0bc2014-08-07 19:46:01 -070031 *
32 * {@hide}
Ihab Awade63fadb2014-07-09 21:52:04 -070033 */
Gabriel Pealb95f1692014-08-19 14:24:18 -070034@SystemApi
Ihab Awade63fadb2014-07-09 21:52:04 -070035public final class Phone {
36
37 public abstract static class Listener {
38 /**
39 * Called when the audio state changes.
40 *
41 * @param phone The {@code Phone} calling this method.
Ihab Awadb19a0bc2014-08-07 19:46:01 -070042 * @param audioState The new {@link AudioState}.
Ihab Awade63fadb2014-07-09 21:52:04 -070043 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -070044 public void onAudioStateChanged(Phone phone, AudioState audioState) { }
Ihab Awade63fadb2014-07-09 21:52:04 -070045
46 /**
47 * Called to bring the in-call screen to the foreground. The in-call experience should
48 * respond immediately by coming to the foreground to inform the user of the state of
49 * ongoing {@code Call}s.
50 *
51 * @param phone The {@code Phone} calling this method.
52 * @param showDialpad If true, put up the dialpad when the screen is shown.
53 */
54 public void onBringToForeground(Phone phone, boolean showDialpad) { }
55
56 /**
57 * Called when a {@code Call} has been added to this in-call session. The in-call user
58 * experience should add necessary state listeners to the specified {@code Call} and
59 * immediately start to show the user information about the existence
60 * and nature of this {@code Call}. Subsequent invocations of {@link #getCalls()} will
61 * include this {@code Call}.
62 *
63 * @param phone The {@code Phone} calling this method.
64 * @param call A newly added {@code Call}.
65 */
66 public void onCallAdded(Phone phone, Call call) { }
67
68 /**
69 * Called when a {@code Call} has been removed from this in-call session. The in-call user
70 * experience should remove any state listeners from the specified {@code Call} and
71 * immediately stop displaying any information about this {@code Call}.
72 * Subsequent invocations of {@link #getCalls()} will no longer include this {@code Call}.
73 *
74 * @param phone The {@code Phone} calling this method.
75 * @param call A newly removed {@code Call}.
76 */
77 public void onCallRemoved(Phone phone, Call call) { }
78 }
79
80 // A Map allows us to track each Call by its Telecomm-specified call ID
81 private final Map<String, Call> mCallByTelecommCallId = new ArrayMap<>();
82
83 // A List allows us to keep the Calls in a stable iteration order so that casually developed
84 // user interface components do not incur any spurious jank
Santos Cordonf30d7e92014-08-26 09:54:33 -070085 private final List<Call> mCalls = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -070086
87 // An unmodifiable view of the above List can be safely shared with subclass implementations
88 private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls);
89
90 private final InCallAdapter mInCallAdapter;
91
Ihab Awadb19a0bc2014-08-07 19:46:01 -070092 private AudioState mAudioState;
Ihab Awade63fadb2014-07-09 21:52:04 -070093
Jay Shrauner229e3822014-08-15 09:23:07 -070094 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
Ihab Awade63fadb2014-07-09 21:52:04 -070095
96 /** {@hide} */
97 Phone(InCallAdapter adapter) {
98 mInCallAdapter = adapter;
99 }
100
101 /** {@hide} */
Santos Cordon88b771d2014-07-19 13:10:40 -0700102 final void internalAddCall(ParcelableCall parcelableCall) {
103 Call call = new Call(this, parcelableCall.getId(), mInCallAdapter);
104 mCallByTelecommCallId.put(parcelableCall.getId(), call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700105 mCalls.add(call);
Santos Cordon88b771d2014-07-19 13:10:40 -0700106 checkCallTree(parcelableCall);
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700107 call.internalUpdate(parcelableCall, mCallByTelecommCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700108 fireCallAdded(call);
109 }
110
111 /** {@hide} */
112 final void internalRemoveCall(Call call) {
113 mCallByTelecommCallId.remove(call.internalGetCallId());
114 mCalls.remove(call);
115 fireCallRemoved(call);
116 }
117
118 /** {@hide} */
Santos Cordon88b771d2014-07-19 13:10:40 -0700119 final void internalUpdateCall(ParcelableCall parcelableCall) {
120 Call call = mCallByTelecommCallId.get(parcelableCall.getId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700121 if (call != null) {
Santos Cordon88b771d2014-07-19 13:10:40 -0700122 checkCallTree(parcelableCall);
Santos Cordon7c7bc7f2014-07-28 18:15:48 -0700123 call.internalUpdate(parcelableCall, mCallByTelecommCallId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700124 }
125 }
126
127 /** {@hide} */
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700128 final void internalSetPostDialWait(String telecommId, String remaining) {
129 Call call = mCallByTelecommCallId.get(telecommId);
Ihab Awade63fadb2014-07-09 21:52:04 -0700130 if (call != null) {
131 call.internalSetPostDialWait(remaining);
132 }
133 }
134
135 /** {@hide} */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700136 final void internalAudioStateChanged(AudioState audioState) {
137 if (!Objects.equals(mAudioState, audioState)) {
138 mAudioState = audioState;
139 fireAudioStateChanged(audioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700140 }
141 }
142
143 /** {@hide} */
144 final Call internalGetCallByTelecommId(String telecommId) {
145 return mCallByTelecommCallId.get(telecommId);
146 }
147
148 /** {@hide} */
149 final void internalBringToForeground(boolean showDialpad) {
150 fireBringToForeground(showDialpad);
151 }
152
Sailesh Nepal2ab88cc2014-07-18 14:49:18 -0700153 /** {@hide} */
154 final void internalStartActivity(String telecommId, PendingIntent intent) {
155 Call call = mCallByTelecommCallId.get(telecommId);
156 if (call != null) {
157 call.internalStartActivity(intent);
158 }
159 }
160
Ihab Awade63fadb2014-07-09 21:52:04 -0700161 /**
Santos Cordonf30d7e92014-08-26 09:54:33 -0700162 * Called to destroy the phone and cleanup any lingering calls.
163 * @hide
164 */
165 final void destroy() {
166 for (Call call : mCalls) {
167 if (call.getState() != Call.STATE_DISCONNECTED) {
168 call.internalSetDisconnected();
169 }
170 }
171 }
172
173 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700174 * Adds a listener to this {@code Phone}.
175 *
176 * @param listener A {@code Listener} object.
177 */
178 public final void addListener(Listener listener) {
179 mListeners.add(listener);
180 }
181
182 /**
183 * Removes a listener from this {@code Phone}.
184 *
185 * @param listener A {@code Listener} object.
186 */
187 public final void removeListener(Listener listener) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700188 if (listener != null) {
189 mListeners.remove(listener);
190 }
Ihab Awade63fadb2014-07-09 21:52:04 -0700191 }
192
193 /**
194 * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
195 *
196 * @return A list of the relevant {@code Call}s.
197 */
198 public final List<Call> getCalls() {
199 return mUnmodifiableCalls;
200 }
201
202 /**
203 * Sets the microphone mute state. When this request is honored, there will be change to
204 * the {@link #getAudioState()}.
205 *
206 * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
207 */
208 public final void setMuted(boolean state) {
209 mInCallAdapter.mute(state);
210 }
211
212 /**
213 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will
214 * be change to the {@link #getAudioState()}.
215 *
216 * @param route The audio route to use.
217 */
218 public final void setAudioRoute(int route) {
219 mInCallAdapter.setAudioRoute(route);
220 }
221
222 /**
Yorke Lee0d6ea712014-07-28 14:39:23 -0700223 * Turns the proximity sensor on. When this request is made, the proximity sensor will
224 * become active, and the touch screen and display will be turned off when the user's face
225 * is detected to be in close proximity to the screen. This operation is a no-op on devices
226 * that do not have a proximity sensor.
227 */
228 public final void setProximitySensorOn() {
229 mInCallAdapter.turnProximitySensorOn();
230 }
231
232 /**
233 * Turns the proximity sensor off. When this request is made, the proximity sensor will
234 * become inactive, and no longer affect the touch screen and display. This operation is a
235 * no-op on devices that do not have a proximity sensor.
236 *
237 * @param screenOnImmediately If true, the screen will be turned on immediately if it was
238 * previously off. Otherwise, the screen will only be turned on after the proximity sensor
239 * is no longer triggered.
240 */
241 public final void setProximitySensorOff(boolean screenOnImmediately) {
242 mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
243 }
244
245 /**
Ihab Awade63fadb2014-07-09 21:52:04 -0700246 * Obtains the current phone call audio state of the {@code Phone}.
247 *
248 * @return An object encapsulating the audio state.
249 */
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700250 public final AudioState getAudioState() {
Ihab Awade63fadb2014-07-09 21:52:04 -0700251 return mAudioState;
252 }
253
254 private void fireCallAdded(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700255 for (Listener listener : mListeners) {
256 listener.onCallAdded(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700257 }
258 }
259
260 private void fireCallRemoved(Call call) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700261 for (Listener listener : mListeners) {
262 listener.onCallRemoved(this, call);
Ihab Awade63fadb2014-07-09 21:52:04 -0700263 }
264 }
265
Ihab Awadb19a0bc2014-08-07 19:46:01 -0700266 private void fireAudioStateChanged(AudioState audioState) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700267 for (Listener listener : mListeners) {
268 listener.onAudioStateChanged(this, audioState);
Ihab Awade63fadb2014-07-09 21:52:04 -0700269 }
270 }
271
272 private void fireBringToForeground(boolean showDialpad) {
Jay Shrauner229e3822014-08-15 09:23:07 -0700273 for (Listener listener : mListeners) {
274 listener.onBringToForeground(this, showDialpad);
Ihab Awade63fadb2014-07-09 21:52:04 -0700275 }
276 }
277
Santos Cordon88b771d2014-07-19 13:10:40 -0700278 private void checkCallTree(ParcelableCall parcelableCall) {
279 if (parcelableCall.getParentCallId() != null &&
280 !mCallByTelecommCallId.containsKey(parcelableCall.getParentCallId())) {
281 Log.wtf(this, "ParcelableCall %s has nonexistent parent %s",
282 parcelableCall.getId(), parcelableCall.getParentCallId());
Ihab Awade63fadb2014-07-09 21:52:04 -0700283 }
Santos Cordon88b771d2014-07-19 13:10:40 -0700284 if (parcelableCall.getChildCallIds() != null) {
285 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
286 if (!mCallByTelecommCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
287 Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
288 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
Ihab Awade63fadb2014-07-09 21:52:04 -0700289 }
290 }
291 }
292 }
293}