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 17 package android.telecom; 18 19 import android.annotation.SystemApi; 20 import android.util.ArrayMap; 21 22 import java.util.Collections; 23 import java.util.List; 24 import java.util.Map; 25 import java.util.Objects; 26 import java.util.concurrent.CopyOnWriteArrayList; 27 28 /** 29 * A unified virtual device providing a means of voice (and other) communication on a device. 30 * 31 * @hide 32 * @deprecated Use {@link InCallService} directly instead of using this class. 33 */ 34 @SystemApi 35 @Deprecated 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. 43 * @param audioState The new {@link AudioState}. 44 * 45 * @deprecated Use {@link #onCallAudioStateChanged(Phone, CallAudioState)} instead. 46 */ 47 @Deprecated onAudioStateChanged(Phone phone, AudioState audioState)48 public void onAudioStateChanged(Phone phone, AudioState audioState) { } 49 50 /** 51 * Called when the audio state changes. 52 * 53 * @param phone The {@code Phone} calling this method. 54 * @param callAudioState The new {@link CallAudioState}. 55 */ onCallAudioStateChanged(Phone phone, CallAudioState callAudioState)56 public void onCallAudioStateChanged(Phone phone, CallAudioState callAudioState) { } 57 58 /** 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. 62 * 63 * @param phone The {@code Phone} calling this method. 64 * @param showDialpad If true, put up the dialpad when the screen is shown. 65 */ onBringToForeground(Phone phone, boolean showDialpad)66 public void onBringToForeground(Phone phone, boolean showDialpad) { } 67 68 /** 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}. 74 * 75 * @param phone The {@code Phone} calling this method. 76 * @param call A newly added {@code Call}. 77 */ onCallAdded(Phone phone, Call call)78 public void onCallAdded(Phone phone, Call call) { } 79 80 /** 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}. 85 * 86 * @param phone The {@code Phone} calling this method. 87 * @param call A newly removed {@code Call}. 88 */ onCallRemoved(Phone phone, Call call)89 public void onCallRemoved(Phone phone, Call call) { } 90 91 /** 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}. 95 * 96 * @param phone The {@code Phone} calling this method. 97 * @param canAddCall Indicates whether an additional call can be added. 98 */ onCanAddCallChanged(Phone phone, boolean canAddCall)99 public void onCanAddCallChanged(Phone phone, boolean canAddCall) { } 100 } 101 102 // A Map allows us to track each Call by its Telecom-specified call ID 103 private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>(); 104 105 // A List allows us to keep the Calls in a stable iteration order so that casually developed 106 // user interface components do not incur any spurious jank 107 private final List<Call> mCalls = new CopyOnWriteArrayList<>(); 108 109 // An unmodifiable view of the above List can be safely shared with subclass implementations 110 private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls); 111 112 private final InCallAdapter mInCallAdapter; 113 114 private CallAudioState mCallAudioState; 115 116 private final List<Listener> mListeners = new CopyOnWriteArrayList<>(); 117 118 private boolean mCanAddCall = true; 119 Phone(InCallAdapter adapter)120 Phone(InCallAdapter adapter) { 121 mInCallAdapter = adapter; 122 } 123 internalAddCall(ParcelableCall parcelableCall)124 final void internalAddCall(ParcelableCall parcelableCall) { 125 Call call = new Call(this, parcelableCall.getId(), mInCallAdapter, 126 parcelableCall.getState()); 127 mCallByTelecomCallId.put(parcelableCall.getId(), call); 128 mCalls.add(call); 129 checkCallTree(parcelableCall); 130 call.internalUpdate(parcelableCall, mCallByTelecomCallId); 131 fireCallAdded(call); 132 } 133 internalRemoveCall(Call call)134 final void internalRemoveCall(Call call) { 135 mCallByTelecomCallId.remove(call.internalGetCallId()); 136 mCalls.remove(call); 137 138 InCallService.VideoCall videoCall = call.getVideoCall(); 139 if (videoCall != null) { 140 videoCall.destroy(); 141 } 142 fireCallRemoved(call); 143 } 144 internalUpdateCall(ParcelableCall parcelableCall)145 final void internalUpdateCall(ParcelableCall parcelableCall) { 146 Call call = mCallByTelecomCallId.get(parcelableCall.getId()); 147 if (call != null) { 148 checkCallTree(parcelableCall); 149 call.internalUpdate(parcelableCall, mCallByTelecomCallId); 150 } 151 } 152 internalSetPostDialWait(String telecomId, String remaining)153 final void internalSetPostDialWait(String telecomId, String remaining) { 154 Call call = mCallByTelecomCallId.get(telecomId); 155 if (call != null) { 156 call.internalSetPostDialWait(remaining); 157 } 158 } 159 internalCallAudioStateChanged(CallAudioState callAudioState)160 final void internalCallAudioStateChanged(CallAudioState callAudioState) { 161 if (!Objects.equals(mCallAudioState, callAudioState)) { 162 mCallAudioState = callAudioState; 163 fireCallAudioStateChanged(callAudioState); 164 } 165 } 166 internalGetCallByTelecomId(String telecomId)167 final Call internalGetCallByTelecomId(String telecomId) { 168 return mCallByTelecomCallId.get(telecomId); 169 } 170 internalBringToForeground(boolean showDialpad)171 final void internalBringToForeground(boolean showDialpad) { 172 fireBringToForeground(showDialpad); 173 } 174 internalSetCanAddCall(boolean canAddCall)175 final void internalSetCanAddCall(boolean canAddCall) { 176 if (mCanAddCall != canAddCall) { 177 mCanAddCall = canAddCall; 178 fireCanAddCallChanged(canAddCall); 179 } 180 } 181 182 /** 183 * Called to destroy the phone and cleanup any lingering calls. 184 */ destroy()185 final void destroy() { 186 for (Call call : mCalls) { 187 InCallService.VideoCall videoCall = call.getVideoCall(); 188 if (videoCall != null) { 189 videoCall.destroy(); 190 } 191 if (call.getState() != Call.STATE_DISCONNECTED) { 192 call.internalSetDisconnected(); 193 } 194 } 195 } 196 197 /** 198 * Adds a listener to this {@code Phone}. 199 * 200 * @param listener A {@code Listener} object. 201 */ addListener(Listener listener)202 public final void addListener(Listener listener) { 203 mListeners.add(listener); 204 } 205 206 /** 207 * Removes a listener from this {@code Phone}. 208 * 209 * @param listener A {@code Listener} object. 210 */ removeListener(Listener listener)211 public final void removeListener(Listener listener) { 212 if (listener != null) { 213 mListeners.remove(listener); 214 } 215 } 216 217 /** 218 * Obtains the current list of {@code Call}s to be displayed by this in-call experience. 219 * 220 * @return A list of the relevant {@code Call}s. 221 */ getCalls()222 public final List<Call> getCalls() { 223 return mUnmodifiableCalls; 224 } 225 226 /** 227 * Returns if the {@code Phone} can support additional calls. 228 * 229 * @return Whether the phone supports adding more calls. 230 */ canAddCall()231 public final boolean canAddCall() { 232 return mCanAddCall; 233 } 234 235 /** 236 * Sets the microphone mute state. When this request is honored, there will be change to 237 * the {@link #getAudioState()}. 238 * 239 * @param state {@code true} if the microphone should be muted; {@code false} otherwise. 240 */ setMuted(boolean state)241 public final void setMuted(boolean state) { 242 mInCallAdapter.mute(state); 243 } 244 245 /** 246 * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will 247 * be change to the {@link #getAudioState()}. 248 * 249 * @param route The audio route to use. 250 */ setAudioRoute(int route)251 public final void setAudioRoute(int route) { 252 mInCallAdapter.setAudioRoute(route); 253 } 254 255 /** 256 * Turns the proximity sensor on. When this request is made, the proximity sensor will 257 * become active, and the touch screen and display will be turned off when the user's face 258 * is detected to be in close proximity to the screen. This operation is a no-op on devices 259 * that do not have a proximity sensor. 260 * 261 * @hide 262 */ setProximitySensorOn()263 public final void setProximitySensorOn() { 264 mInCallAdapter.turnProximitySensorOn(); 265 } 266 267 /** 268 * Turns the proximity sensor off. When this request is made, the proximity sensor will 269 * become inactive, and no longer affect the touch screen and display. This operation is a 270 * no-op on devices that do not have a proximity sensor. 271 * 272 * @param screenOnImmediately If true, the screen will be turned on immediately if it was 273 * previously off. Otherwise, the screen will only be turned on after the proximity sensor 274 * is no longer triggered. 275 * 276 * @hide 277 */ setProximitySensorOff(boolean screenOnImmediately)278 public final void setProximitySensorOff(boolean screenOnImmediately) { 279 mInCallAdapter.turnProximitySensorOff(screenOnImmediately); 280 } 281 282 /** 283 * Obtains the current phone call audio state of the {@code Phone}. 284 * 285 * @return An object encapsulating the audio state. 286 * @deprecated Use {@link #getCallAudioState()} instead. 287 */ 288 @Deprecated getAudioState()289 public final AudioState getAudioState() { 290 return new AudioState(mCallAudioState); 291 } 292 293 /** 294 * Obtains the current phone call audio state of the {@code Phone}. 295 * 296 * @return An object encapsulating the audio state. 297 */ getCallAudioState()298 public final CallAudioState getCallAudioState() { 299 return mCallAudioState; 300 } 301 fireCallAdded(Call call)302 private void fireCallAdded(Call call) { 303 for (Listener listener : mListeners) { 304 listener.onCallAdded(this, call); 305 } 306 } 307 fireCallRemoved(Call call)308 private void fireCallRemoved(Call call) { 309 for (Listener listener : mListeners) { 310 listener.onCallRemoved(this, call); 311 } 312 } 313 fireCallAudioStateChanged(CallAudioState audioState)314 private void fireCallAudioStateChanged(CallAudioState audioState) { 315 for (Listener listener : mListeners) { 316 listener.onCallAudioStateChanged(this, audioState); 317 listener.onAudioStateChanged(this, new AudioState(audioState)); 318 } 319 } 320 fireBringToForeground(boolean showDialpad)321 private void fireBringToForeground(boolean showDialpad) { 322 for (Listener listener : mListeners) { 323 listener.onBringToForeground(this, showDialpad); 324 } 325 } 326 fireCanAddCallChanged(boolean canAddCall)327 private void fireCanAddCallChanged(boolean canAddCall) { 328 for (Listener listener : mListeners) { 329 listener.onCanAddCallChanged(this, canAddCall); 330 } 331 } 332 checkCallTree(ParcelableCall parcelableCall)333 private void checkCallTree(ParcelableCall parcelableCall) { 334 if (parcelableCall.getParentCallId() != null && 335 !mCallByTelecomCallId.containsKey(parcelableCall.getParentCallId())) { 336 Log.wtf(this, "ParcelableCall %s has nonexistent parent %s", 337 parcelableCall.getId(), parcelableCall.getParentCallId()); 338 } 339 if (parcelableCall.getChildCallIds() != null) { 340 for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) { 341 if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) { 342 Log.wtf(this, "ParcelableCall %s has nonexistent child %s", 343 parcelableCall.getId(), parcelableCall.getChildCallIds().get(i)); 344 } 345 } 346 } 347 } 348 } 349