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 | |
Santos Cordon | 29886d8 | 2015-04-16 15:34:07 -0700 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 20 | import android.util.ArrayMap; |
| 21 | |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 22 | import java.util.Collections; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import java.util.Objects; |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 26 | import java.util.concurrent.CopyOnWriteArrayList; |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * A unified virtual device providing a means of voice (and other) communication on a device. |
Santos Cordon | 29886d8 | 2015-04-16 15:34:07 -0700 | [diff] [blame] | 30 | * |
| 31 | * @hide |
| 32 | * @deprecated Use {@link InCallService} directly instead of using this class. |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 33 | */ |
Santos Cordon | 29886d8 | 2015-04-16 15:34:07 -0700 | [diff] [blame] | 34 | @SystemApi |
| 35 | @Deprecated |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 36 | public 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 Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 43 | * @param audioState The new {@link AudioState}. |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 44 | */ |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 45 | public void onAudioStateChanged(Phone phone, AudioState audioState) { } |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 46 | |
| 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 Cordon | 6c912b7 | 2014-11-07 16:05:09 -0800 | [diff] [blame] | 79 | |
| 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 Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 91 | // A Map allows us to track each Call by its Telecom-specified call ID |
| 92 | private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>(); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 93 | |
| 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 Cordon | f30d7e9 | 2014-08-26 09:54:33 -0700 | [diff] [blame] | 96 | private final List<Call> mCalls = new CopyOnWriteArrayList<>(); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 97 | |
| 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 Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 103 | private AudioState mAudioState; |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 104 | |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 105 | private final List<Listener> mListeners = new CopyOnWriteArrayList<>(); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 106 | |
Santos Cordon | 6c912b7 | 2014-11-07 16:05:09 -0800 | [diff] [blame] | 107 | private boolean mCanAddCall = true; |
| 108 | |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 109 | Phone(InCallAdapter adapter) { |
| 110 | mInCallAdapter = adapter; |
| 111 | } |
| 112 | |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 113 | final void internalAddCall(ParcelableCall parcelableCall) { |
| 114 | Call call = new Call(this, parcelableCall.getId(), mInCallAdapter); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 115 | mCallByTelecomCallId.put(parcelableCall.getId(), call); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 116 | mCalls.add(call); |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 117 | checkCallTree(parcelableCall); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 118 | call.internalUpdate(parcelableCall, mCallByTelecomCallId); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 119 | fireCallAdded(call); |
| 120 | } |
| 121 | |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 122 | final void internalRemoveCall(Call call) { |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 123 | mCallByTelecomCallId.remove(call.internalGetCallId()); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 124 | mCalls.remove(call); |
| 125 | fireCallRemoved(call); |
| 126 | } |
| 127 | |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 128 | final void internalUpdateCall(ParcelableCall parcelableCall) { |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 129 | Call call = mCallByTelecomCallId.get(parcelableCall.getId()); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 130 | if (call != null) { |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 131 | checkCallTree(parcelableCall); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 132 | call.internalUpdate(parcelableCall, mCallByTelecomCallId); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 136 | final void internalSetPostDialWait(String telecomId, String remaining) { |
| 137 | Call call = mCallByTelecomCallId.get(telecomId); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 138 | if (call != null) { |
| 139 | call.internalSetPostDialWait(remaining); |
| 140 | } |
| 141 | } |
| 142 | |
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 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 150 | final Call internalGetCallByTelecomId(String telecomId) { |
| 151 | return mCallByTelecomCallId.get(telecomId); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 154 | final void internalBringToForeground(boolean showDialpad) { |
| 155 | fireBringToForeground(showDialpad); |
| 156 | } |
| 157 | |
Santos Cordon | 6c912b7 | 2014-11-07 16:05:09 -0800 | [diff] [blame] | 158 | final void internalSetCanAddCall(boolean canAddCall) { |
| 159 | if (mCanAddCall != canAddCall) { |
| 160 | mCanAddCall = canAddCall; |
| 161 | fireCanAddCallChanged(canAddCall); |
| 162 | } |
| 163 | } |
| 164 | |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 165 | /** |
Santos Cordon | f30d7e9 | 2014-08-26 09:54:33 -0700 | [diff] [blame] | 166 | * Called to destroy the phone and cleanup any lingering calls. |
Santos Cordon | f30d7e9 | 2014-08-26 09:54:33 -0700 | [diff] [blame] | 167 | */ |
| 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 Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 177 | * 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 Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 191 | if (listener != null) { |
| 192 | mListeners.remove(listener); |
| 193 | } |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 194 | } |
| 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 Cordon | 6c912b7 | 2014-11-07 16:05:09 -0800 | [diff] [blame] | 206 | * 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 Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 215 | * 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 Lee | 0d6ea71 | 2014-07-28 14:39:23 -0700 | [diff] [blame] | 235 | * 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 Lee | 22244d0 | 2015-04-14 12:34:28 -0700 | [diff] [blame] | 239 | * |
| 240 | * @hide |
Yorke Lee | 0d6ea71 | 2014-07-28 14:39:23 -0700 | [diff] [blame] | 241 | */ |
| 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 Lee | 22244d0 | 2015-04-14 12:34:28 -0700 | [diff] [blame] | 254 | * |
| 255 | * @hide |
Yorke Lee | 0d6ea71 | 2014-07-28 14:39:23 -0700 | [diff] [blame] | 256 | */ |
| 257 | public final void setProximitySensorOff(boolean screenOnImmediately) { |
| 258 | mInCallAdapter.turnProximitySensorOff(screenOnImmediately); |
| 259 | } |
| 260 | |
| 261 | /** |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 262 | * Obtains the current phone call audio state of the {@code Phone}. |
| 263 | * |
| 264 | * @return An object encapsulating the audio state. |
| 265 | */ |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 266 | public final AudioState getAudioState() { |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 267 | return mAudioState; |
| 268 | } |
| 269 | |
| 270 | private void fireCallAdded(Call call) { |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 271 | for (Listener listener : mListeners) { |
| 272 | listener.onCallAdded(this, call); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | private void fireCallRemoved(Call call) { |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 277 | for (Listener listener : mListeners) { |
| 278 | listener.onCallRemoved(this, call); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 282 | private void fireAudioStateChanged(AudioState audioState) { |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 283 | for (Listener listener : mListeners) { |
| 284 | listener.onAudioStateChanged(this, audioState); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | private void fireBringToForeground(boolean showDialpad) { |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 289 | for (Listener listener : mListeners) { |
| 290 | listener.onBringToForeground(this, showDialpad); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Santos Cordon | 6c912b7 | 2014-11-07 16:05:09 -0800 | [diff] [blame] | 294 | private void fireCanAddCallChanged(boolean canAddCall) { |
| 295 | for (Listener listener : mListeners) { |
| 296 | listener.onCanAddCallChanged(this, canAddCall); |
| 297 | } |
| 298 | } |
| 299 | |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 300 | private void checkCallTree(ParcelableCall parcelableCall) { |
| 301 | if (parcelableCall.getParentCallId() != null && |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 302 | !mCallByTelecomCallId.containsKey(parcelableCall.getParentCallId())) { |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 303 | Log.wtf(this, "ParcelableCall %s has nonexistent parent %s", |
| 304 | parcelableCall.getId(), parcelableCall.getParentCallId()); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 305 | } |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 306 | if (parcelableCall.getChildCallIds() != null) { |
| 307 | for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) { |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 308 | if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) { |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 309 | Log.wtf(this, "ParcelableCall %s has nonexistent child %s", |
| 310 | parcelableCall.getId(), parcelableCall.getChildCallIds().get(i)); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | } |