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