1 /* 2 * Copyright (C) 2014 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.net.Uri; 20 import android.os.Bundle; 21 import android.os.Parcel; 22 import android.os.ParcelFileDescriptor; 23 import android.os.Parcelable; 24 25 /** 26 * Simple data container encapsulating a request to some entity to 27 * create a new {@link Connection}. 28 */ 29 public final class ConnectionRequest implements Parcelable { 30 31 /** 32 * Builder class for {@link ConnectionRequest} 33 * @hide 34 */ 35 public static final class Builder { 36 private PhoneAccountHandle mAccountHandle; 37 private Uri mAddress; 38 private Bundle mExtras; 39 private int mVideoState = VideoProfile.STATE_AUDIO_ONLY; 40 private String mTelecomCallId; 41 private boolean mShouldShowIncomingCallUi = false; 42 private ParcelFileDescriptor mRttPipeToInCall; 43 private ParcelFileDescriptor mRttPipeFromInCall; 44 Builder()45 public Builder() { } 46 47 /** 48 * Sets the phone account handle for the resulting {@link ConnectionRequest} 49 * @param accountHandle The accountHandle which should be used to place the call. 50 */ setAccountHandle(PhoneAccountHandle accountHandle)51 public Builder setAccountHandle(PhoneAccountHandle accountHandle) { 52 this.mAccountHandle = accountHandle; 53 return this; 54 } 55 56 /** 57 * Sets the address for the resulting {@link ConnectionRequest} 58 * @param address The address(e.g., phone number) to which the {@link Connection} is to 59 * connect. 60 */ setAddress(Uri address)61 public Builder setAddress(Uri address) { 62 this.mAddress = address; 63 return this; 64 } 65 66 /** 67 * Sets the extras bundle for the resulting {@link ConnectionRequest} 68 * @param extras Application-specific extra data. 69 */ setExtras(Bundle extras)70 public Builder setExtras(Bundle extras) { 71 this.mExtras = extras; 72 return this; 73 } 74 75 /** 76 * Sets the video state for the resulting {@link ConnectionRequest} 77 * @param videoState Determines the video state for the connection. 78 */ setVideoState(int videoState)79 public Builder setVideoState(int videoState) { 80 this.mVideoState = videoState; 81 return this; 82 } 83 84 /** 85 * Sets the Telecom call ID for the resulting {@link ConnectionRequest} 86 * @param telecomCallId The telecom call ID. 87 */ setTelecomCallId(String telecomCallId)88 public Builder setTelecomCallId(String telecomCallId) { 89 this.mTelecomCallId = telecomCallId; 90 return this; 91 } 92 93 /** 94 * Sets shouldShowIncomingUi for the resulting {@link ConnectionRequest} 95 * @param shouldShowIncomingCallUi For a self-managed {@link ConnectionService}, will be 96 * {@code true} if the {@link ConnectionService} should show 97 * its own incoming call UI for an incoming call. When 98 * {@code false}, Telecom shows the incoming call UI. 99 */ setShouldShowIncomingCallUi(boolean shouldShowIncomingCallUi)100 public Builder setShouldShowIncomingCallUi(boolean shouldShowIncomingCallUi) { 101 this.mShouldShowIncomingCallUi = shouldShowIncomingCallUi; 102 return this; 103 } 104 105 /** 106 * Sets the RTT pipe for transferring text into the {@link ConnectionService} for the 107 * resulting {@link ConnectionRequest} 108 * @param rttPipeFromInCall The data pipe to read from. 109 */ setRttPipeFromInCall(ParcelFileDescriptor rttPipeFromInCall)110 public Builder setRttPipeFromInCall(ParcelFileDescriptor rttPipeFromInCall) { 111 this.mRttPipeFromInCall = rttPipeFromInCall; 112 return this; 113 } 114 115 /** 116 * Sets the RTT pipe for transferring text out of {@link ConnectionService} for the 117 * resulting {@link ConnectionRequest} 118 * @param rttPipeToInCall The data pipe to write to. 119 */ setRttPipeToInCall(ParcelFileDescriptor rttPipeToInCall)120 public Builder setRttPipeToInCall(ParcelFileDescriptor rttPipeToInCall) { 121 this.mRttPipeToInCall = rttPipeToInCall; 122 return this; 123 } 124 build()125 public ConnectionRequest build() { 126 return new ConnectionRequest( 127 mAccountHandle, 128 mAddress, 129 mExtras, 130 mVideoState, 131 mTelecomCallId, 132 mShouldShowIncomingCallUi, 133 mRttPipeFromInCall, 134 mRttPipeToInCall); 135 } 136 } 137 138 private final PhoneAccountHandle mAccountHandle; 139 private final Uri mAddress; 140 private final Bundle mExtras; 141 private final int mVideoState; 142 private final String mTelecomCallId; 143 private final boolean mShouldShowIncomingCallUi; 144 private final ParcelFileDescriptor mRttPipeToInCall; 145 private final ParcelFileDescriptor mRttPipeFromInCall; 146 147 /** 148 * @param accountHandle The accountHandle which should be used to place the call. 149 * @param handle The handle (e.g., phone number) to which the {@link Connection} is to connect. 150 * @param extras Application-specific extra data. 151 */ ConnectionRequest( PhoneAccountHandle accountHandle, Uri handle, Bundle extras)152 public ConnectionRequest( 153 PhoneAccountHandle accountHandle, 154 Uri handle, 155 Bundle extras) { 156 this(accountHandle, handle, extras, VideoProfile.STATE_AUDIO_ONLY, null, false, null, null); 157 } 158 159 /** 160 * @param accountHandle The accountHandle which should be used to place the call. 161 * @param handle The handle (e.g., phone number) to which the {@link Connection} is to connect. 162 * @param extras Application-specific extra data. 163 * @param videoState Determines the video state for the connection. 164 */ ConnectionRequest( PhoneAccountHandle accountHandle, Uri handle, Bundle extras, int videoState)165 public ConnectionRequest( 166 PhoneAccountHandle accountHandle, 167 Uri handle, 168 Bundle extras, 169 int videoState) { 170 this(accountHandle, handle, extras, videoState, null, false, null, null); 171 } 172 173 /** 174 * @param accountHandle The accountHandle which should be used to place the call. 175 * @param handle The handle (e.g., phone number) to which the {@link Connection} is to connect. 176 * @param extras Application-specific extra data. 177 * @param videoState Determines the video state for the connection. 178 * @param telecomCallId The telecom call ID. 179 * @param shouldShowIncomingCallUi For a self-managed {@link ConnectionService}, will be 180 * {@code true} if the {@link ConnectionService} should show its 181 * own incoming call UI for an incoming call. When 182 * {@code false}, Telecom shows the incoming call UI. 183 * @hide 184 */ ConnectionRequest( PhoneAccountHandle accountHandle, Uri handle, Bundle extras, int videoState, String telecomCallId, boolean shouldShowIncomingCallUi)185 public ConnectionRequest( 186 PhoneAccountHandle accountHandle, 187 Uri handle, 188 Bundle extras, 189 int videoState, 190 String telecomCallId, 191 boolean shouldShowIncomingCallUi) { 192 this(accountHandle, handle, extras, videoState, telecomCallId, 193 shouldShowIncomingCallUi, null, null); 194 } 195 ConnectionRequest( PhoneAccountHandle accountHandle, Uri handle, Bundle extras, int videoState, String telecomCallId, boolean shouldShowIncomingCallUi, ParcelFileDescriptor rttPipeFromInCall, ParcelFileDescriptor rttPipeToInCall)196 private ConnectionRequest( 197 PhoneAccountHandle accountHandle, 198 Uri handle, 199 Bundle extras, 200 int videoState, 201 String telecomCallId, 202 boolean shouldShowIncomingCallUi, 203 ParcelFileDescriptor rttPipeFromInCall, 204 ParcelFileDescriptor rttPipeToInCall) { 205 mAccountHandle = accountHandle; 206 mAddress = handle; 207 mExtras = extras; 208 mVideoState = videoState; 209 mTelecomCallId = telecomCallId; 210 mShouldShowIncomingCallUi = shouldShowIncomingCallUi; 211 mRttPipeFromInCall = rttPipeFromInCall; 212 mRttPipeToInCall = rttPipeToInCall; 213 } 214 ConnectionRequest(Parcel in)215 private ConnectionRequest(Parcel in) { 216 mAccountHandle = in.readParcelable(getClass().getClassLoader()); 217 mAddress = in.readParcelable(getClass().getClassLoader()); 218 mExtras = in.readParcelable(getClass().getClassLoader()); 219 mVideoState = in.readInt(); 220 mTelecomCallId = in.readString(); 221 mShouldShowIncomingCallUi = in.readInt() == 1; 222 mRttPipeFromInCall = in.readParcelable(getClass().getClassLoader()); 223 mRttPipeToInCall = in.readParcelable(getClass().getClassLoader()); 224 } 225 226 /** 227 * The account which should be used to place the call. 228 */ getAccountHandle()229 public PhoneAccountHandle getAccountHandle() { return mAccountHandle; } 230 231 /** 232 * The handle (e.g., phone number) to which the {@link Connection} is to connect. 233 */ getAddress()234 public Uri getAddress() { return mAddress; } 235 236 /** 237 * Application-specific extra data. Used for passing back information from an incoming 238 * call {@code Intent}, and for any proprietary extensions arranged between a client 239 * and servant {@code ConnectionService} which agree on a vocabulary for such data. 240 */ getExtras()241 public Bundle getExtras() { return mExtras; } 242 243 /** 244 * Describes the video states supported by the client requesting the connection. 245 * Valid values: {@link VideoProfile#STATE_AUDIO_ONLY}, 246 * {@link VideoProfile#STATE_BIDIRECTIONAL}, 247 * {@link VideoProfile#STATE_TX_ENABLED}, 248 * {@link VideoProfile#STATE_RX_ENABLED}. 249 * 250 * @return The video state for the connection. 251 */ getVideoState()252 public int getVideoState() { 253 return mVideoState; 254 } 255 256 /** 257 * Returns the internal Telecom ID associated with the connection request. 258 * 259 * @return The Telecom ID. 260 * @hide 261 */ getTelecomCallId()262 public String getTelecomCallId() { 263 return mTelecomCallId; 264 } 265 266 /** 267 * For a self-managed {@link ConnectionService}, indicates for an incoming call whether the 268 * {@link ConnectionService} should show its own incoming call UI for an incoming call. 269 * 270 * @return {@code true} if the {@link ConnectionService} should show its own incoming call UI. 271 * When {@code false}, Telecom shows the incoming call UI for the call. 272 * @hide 273 */ shouldShowIncomingCallUi()274 public boolean shouldShowIncomingCallUi() { 275 return mShouldShowIncomingCallUi; 276 } 277 278 /** 279 * Gets the {@link ParcelFileDescriptor} that is used to send RTT text from the connection 280 * service to the in-call UI. In order to obtain an 281 * {@link java.io.InputStream} from this {@link ParcelFileDescriptor}, use 282 * {@link android.os.ParcelFileDescriptor.AutoCloseInputStream}. 283 * Only text data encoded using UTF-8 should be written into this {@link ParcelFileDescriptor}. 284 * @return The {@link ParcelFileDescriptor} that should be used for communication. 285 * Do not un-hide -- only for use by Telephony 286 * @hide 287 */ getRttPipeToInCall()288 public ParcelFileDescriptor getRttPipeToInCall() { 289 return mRttPipeToInCall; 290 } 291 292 /** 293 * Gets the {@link ParcelFileDescriptor} that is used to send RTT text from the in-call UI to 294 * the connection service. In order to obtain an 295 * {@link java.io.OutputStream} from this {@link ParcelFileDescriptor}, use 296 * {@link android.os.ParcelFileDescriptor.AutoCloseOutputStream}. 297 * The contents of this {@link ParcelFileDescriptor} will consist solely of text encoded in 298 * UTF-8. 299 * @return The {@link ParcelFileDescriptor} that should be used for communication 300 * Do not un-hide -- only for use by Telephony 301 * @hide 302 */ getRttPipeFromInCall()303 public ParcelFileDescriptor getRttPipeFromInCall() { 304 return mRttPipeFromInCall; 305 } 306 307 /** 308 * Gets the {@link android.telecom.Connection.RttTextStream} object that should be used to 309 * send and receive RTT text to/from the in-call app. 310 * @return An instance of {@link android.telecom.Connection.RttTextStream}, or {@code null} 311 * if this connection request is not requesting an RTT session upon connection establishment. 312 * @hide 313 */ getRttTextStream()314 public Connection.RttTextStream getRttTextStream() { 315 if (isRequestingRtt()) { 316 return new Connection.RttTextStream(mRttPipeToInCall, mRttPipeFromInCall); 317 } else { 318 return null; 319 } 320 } 321 322 /** 323 * Convenience method for determining whether the ConnectionRequest is requesting an RTT session 324 * @return {@code true} if RTT is requested, {@code false} otherwise. 325 * @hide 326 */ isRequestingRtt()327 public boolean isRequestingRtt() { 328 return mRttPipeFromInCall != null && mRttPipeToInCall != null; 329 } 330 331 @Override toString()332 public String toString() { 333 return String.format("ConnectionRequest %s %s", 334 mAddress == null 335 ? Uri.EMPTY 336 : Connection.toLogSafePhoneNumber(mAddress.toString()), 337 mExtras == null ? "" : mExtras); 338 } 339 340 public static final Creator<ConnectionRequest> CREATOR = new Creator<ConnectionRequest> () { 341 @Override 342 public ConnectionRequest createFromParcel(Parcel source) { 343 return new ConnectionRequest(source); 344 } 345 346 @Override 347 public ConnectionRequest[] newArray(int size) { 348 return new ConnectionRequest[size]; 349 } 350 }; 351 352 /** 353 * {@inheritDoc} 354 */ 355 @Override describeContents()356 public int describeContents() { 357 return 0; 358 } 359 360 @Override writeToParcel(Parcel destination, int flags)361 public void writeToParcel(Parcel destination, int flags) { 362 destination.writeParcelable(mAccountHandle, 0); 363 destination.writeParcelable(mAddress, 0); 364 destination.writeParcelable(mExtras, 0); 365 destination.writeInt(mVideoState); 366 destination.writeString(mTelecomCallId); 367 destination.writeInt(mShouldShowIncomingCallUi ? 1 : 0); 368 destination.writeParcelable(mRttPipeFromInCall, 0); 369 destination.writeParcelable(mRttPipeToInCall, 0); 370 } 371 } 372