Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 17 | package android.telecom; |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 18 | |
| 19 | import android.util.ArrayMap; |
| 20 | |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 21 | import java.util.Collections; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Objects; |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 25 | import java.util.concurrent.CopyOnWriteArrayList; |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * A unified virtual device providing a means of voice (and other) communication on a device. |
| 29 | */ |
| 30 | public 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 Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 37 | * @param audioState The new {@link AudioState}. |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 38 | */ |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 39 | public void onAudioStateChanged(Phone phone, AudioState audioState) { } |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 40 | |
| 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 Cordon | 6c912b7 | 2014-11-07 16:05:09 -0800 | [diff] [blame] | 73 | |
| 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 Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 85 | // A Map allows us to track each Call by its Telecom-specified call ID |
| 86 | private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>(); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 87 | |
| 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 Cordon | f30d7e9 | 2014-08-26 09:54:33 -0700 | [diff] [blame] | 90 | private final List<Call> mCalls = new CopyOnWriteArrayList<>(); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 91 | |
| 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 Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 97 | private AudioState mAudioState; |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 98 | |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 99 | private final List<Listener> mListeners = new CopyOnWriteArrayList<>(); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 100 | |
Santos Cordon | 6c912b7 | 2014-11-07 16:05:09 -0800 | [diff] [blame] | 101 | private boolean mCanAddCall = true; |
| 102 | |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 103 | /** {@hide} */ |
| 104 | Phone(InCallAdapter adapter) { |
| 105 | mInCallAdapter = adapter; |
| 106 | } |
| 107 | |
| 108 | /** {@hide} */ |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 109 | final void internalAddCall(ParcelableCall parcelableCall) { |
| 110 | Call call = new Call(this, parcelableCall.getId(), mInCallAdapter); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 111 | mCallByTelecomCallId.put(parcelableCall.getId(), call); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 112 | mCalls.add(call); |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 113 | checkCallTree(parcelableCall); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 114 | call.internalUpdate(parcelableCall, mCallByTelecomCallId); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 115 | fireCallAdded(call); |
| 116 | } |
| 117 | |
| 118 | /** {@hide} */ |
| 119 | final void internalRemoveCall(Call call) { |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 120 | mCallByTelecomCallId.remove(call.internalGetCallId()); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 121 | mCalls.remove(call); |
Tyler Gunn | 7595842 | 2015-04-15 14:23:42 -0700 | [diff] [blame^] | 122 | |
| 123 | InCallService.VideoCall videoCall = call.getVideoCall(); |
| 124 | if (videoCall != null) { |
| 125 | videoCall.removeVideoCallListener(); |
| 126 | } |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 127 | fireCallRemoved(call); |
| 128 | } |
| 129 | |
| 130 | /** {@hide} */ |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 131 | final void internalUpdateCall(ParcelableCall parcelableCall) { |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 132 | Call call = mCallByTelecomCallId.get(parcelableCall.getId()); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 133 | if (call != null) { |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 134 | checkCallTree(parcelableCall); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 135 | call.internalUpdate(parcelableCall, mCallByTelecomCallId); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | /** {@hide} */ |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 140 | final void internalSetPostDialWait(String telecomId, String remaining) { |
| 141 | Call call = mCallByTelecomCallId.get(telecomId); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 142 | if (call != null) { |
| 143 | call.internalSetPostDialWait(remaining); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** {@hide} */ |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 148 | final void internalAudioStateChanged(AudioState audioState) { |
| 149 | if (!Objects.equals(mAudioState, audioState)) { |
| 150 | mAudioState = audioState; |
| 151 | fireAudioStateChanged(audioState); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | /** {@hide} */ |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 156 | final Call internalGetCallByTelecomId(String telecomId) { |
| 157 | return mCallByTelecomCallId.get(telecomId); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** {@hide} */ |
| 161 | final void internalBringToForeground(boolean showDialpad) { |
| 162 | fireBringToForeground(showDialpad); |
| 163 | } |
| 164 | |
Santos Cordon | 6c912b7 | 2014-11-07 16:05:09 -0800 | [diff] [blame] | 165 | /** {@hide} */ |
| 166 | final void internalSetCanAddCall(boolean canAddCall) { |
| 167 | if (mCanAddCall != canAddCall) { |
| 168 | mCanAddCall = canAddCall; |
| 169 | fireCanAddCallChanged(canAddCall); |
| 170 | } |
| 171 | } |
| 172 | |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 173 | /** |
Santos Cordon | f30d7e9 | 2014-08-26 09:54:33 -0700 | [diff] [blame] | 174 | * Called to destroy the phone and cleanup any lingering calls. |
| 175 | * @hide |
| 176 | */ |
| 177 | final void destroy() { |
| 178 | for (Call call : mCalls) { |
Tyler Gunn | 7595842 | 2015-04-15 14:23:42 -0700 | [diff] [blame^] | 179 | InCallService.VideoCall videoCall = call.getVideoCall(); |
| 180 | if (videoCall != null) { |
| 181 | videoCall.removeVideoCallListener(); |
| 182 | } |
Santos Cordon | f30d7e9 | 2014-08-26 09:54:33 -0700 | [diff] [blame] | 183 | if (call.getState() != Call.STATE_DISCONNECTED) { |
| 184 | call.internalSetDisconnected(); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 190 | * Adds a listener to this {@code Phone}. |
| 191 | * |
| 192 | * @param listener A {@code Listener} object. |
| 193 | */ |
| 194 | public final void addListener(Listener listener) { |
| 195 | mListeners.add(listener); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Removes a listener from this {@code Phone}. |
| 200 | * |
| 201 | * @param listener A {@code Listener} object. |
| 202 | */ |
| 203 | public final void removeListener(Listener listener) { |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 204 | if (listener != null) { |
| 205 | mListeners.remove(listener); |
| 206 | } |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Obtains the current list of {@code Call}s to be displayed by this in-call experience. |
| 211 | * |
| 212 | * @return A list of the relevant {@code Call}s. |
| 213 | */ |
| 214 | public final List<Call> getCalls() { |
| 215 | return mUnmodifiableCalls; |
| 216 | } |
| 217 | |
| 218 | /** |
Santos Cordon | 6c912b7 | 2014-11-07 16:05:09 -0800 | [diff] [blame] | 219 | * Returns if the {@code Phone} can support additional calls. |
| 220 | * |
| 221 | * @return Whether the phone supports adding more calls. |
| 222 | */ |
| 223 | public final boolean canAddCall() { |
| 224 | return mCanAddCall; |
| 225 | } |
| 226 | |
| 227 | /** |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 228 | * Sets the microphone mute state. When this request is honored, there will be change to |
| 229 | * the {@link #getAudioState()}. |
| 230 | * |
| 231 | * @param state {@code true} if the microphone should be muted; {@code false} otherwise. |
| 232 | */ |
| 233 | public final void setMuted(boolean state) { |
| 234 | mInCallAdapter.mute(state); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will |
| 239 | * be change to the {@link #getAudioState()}. |
| 240 | * |
| 241 | * @param route The audio route to use. |
| 242 | */ |
| 243 | public final void setAudioRoute(int route) { |
| 244 | mInCallAdapter.setAudioRoute(route); |
| 245 | } |
| 246 | |
| 247 | /** |
Yorke Lee | 0d6ea71 | 2014-07-28 14:39:23 -0700 | [diff] [blame] | 248 | * Turns the proximity sensor on. When this request is made, the proximity sensor will |
| 249 | * become active, and the touch screen and display will be turned off when the user's face |
| 250 | * is detected to be in close proximity to the screen. This operation is a no-op on devices |
| 251 | * that do not have a proximity sensor. |
Yorke Lee | 2287a01 | 2015-04-14 15:48:19 -0700 | [diff] [blame] | 252 | * |
| 253 | * @hide |
Yorke Lee | 0d6ea71 | 2014-07-28 14:39:23 -0700 | [diff] [blame] | 254 | */ |
| 255 | public final void setProximitySensorOn() { |
| 256 | mInCallAdapter.turnProximitySensorOn(); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Turns the proximity sensor off. When this request is made, the proximity sensor will |
| 261 | * become inactive, and no longer affect the touch screen and display. This operation is a |
| 262 | * no-op on devices that do not have a proximity sensor. |
| 263 | * |
| 264 | * @param screenOnImmediately If true, the screen will be turned on immediately if it was |
| 265 | * previously off. Otherwise, the screen will only be turned on after the proximity sensor |
| 266 | * is no longer triggered. |
Yorke Lee | 2287a01 | 2015-04-14 15:48:19 -0700 | [diff] [blame] | 267 | * |
| 268 | * @hide |
Yorke Lee | 0d6ea71 | 2014-07-28 14:39:23 -0700 | [diff] [blame] | 269 | */ |
| 270 | public final void setProximitySensorOff(boolean screenOnImmediately) { |
| 271 | mInCallAdapter.turnProximitySensorOff(screenOnImmediately); |
| 272 | } |
| 273 | |
| 274 | /** |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 275 | * Obtains the current phone call audio state of the {@code Phone}. |
| 276 | * |
| 277 | * @return An object encapsulating the audio state. |
| 278 | */ |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 279 | public final AudioState getAudioState() { |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 280 | return mAudioState; |
| 281 | } |
| 282 | |
| 283 | private void fireCallAdded(Call call) { |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 284 | for (Listener listener : mListeners) { |
| 285 | listener.onCallAdded(this, call); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | private void fireCallRemoved(Call call) { |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 290 | for (Listener listener : mListeners) { |
| 291 | listener.onCallRemoved(this, call); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 295 | private void fireAudioStateChanged(AudioState audioState) { |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 296 | for (Listener listener : mListeners) { |
| 297 | listener.onAudioStateChanged(this, audioState); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | private void fireBringToForeground(boolean showDialpad) { |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 302 | for (Listener listener : mListeners) { |
| 303 | listener.onBringToForeground(this, showDialpad); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Santos Cordon | 6c912b7 | 2014-11-07 16:05:09 -0800 | [diff] [blame] | 307 | private void fireCanAddCallChanged(boolean canAddCall) { |
| 308 | for (Listener listener : mListeners) { |
| 309 | listener.onCanAddCallChanged(this, canAddCall); |
| 310 | } |
| 311 | } |
| 312 | |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 313 | private void checkCallTree(ParcelableCall parcelableCall) { |
| 314 | if (parcelableCall.getParentCallId() != null && |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 315 | !mCallByTelecomCallId.containsKey(parcelableCall.getParentCallId())) { |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 316 | Log.wtf(this, "ParcelableCall %s has nonexistent parent %s", |
| 317 | parcelableCall.getId(), parcelableCall.getParentCallId()); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 318 | } |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 319 | if (parcelableCall.getChildCallIds() != null) { |
| 320 | for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) { |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 321 | if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) { |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 322 | Log.wtf(this, "ParcelableCall %s has nonexistent child %s", |
| 323 | parcelableCall.getId(), parcelableCall.getChildCallIds().get(i)); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | } |