1 /* 2 * Copyright (C) 2022 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.telephony; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 import android.telephony.Annotation.ImsCallServiceType; 25 import android.telephony.Annotation.ImsCallType; 26 import android.telephony.Annotation.NetworkType; 27 import android.telephony.Annotation.PreciseCallStates; 28 import android.telephony.ims.ImsCallProfile; 29 import android.telephony.ims.ImsCallSession; 30 31 import java.util.Objects; 32 33 /** 34 * Contains information about various states for a call. 35 * @hide 36 */ 37 @SystemApi 38 public final class CallState implements Parcelable { 39 40 /** 41 * Call classifications are just used for backward compatibility of deprecated API {@link 42 * TelephonyCallback#CallAttributesListener#onCallAttributesChanged}, Since these will be 43 * removed when the deprecated API is removed, they should not be opened. 44 */ 45 /** 46 * Call classification is not valid. It should not be opened. 47 * @hide 48 */ 49 public static final int CALL_CLASSIFICATION_UNKNOWN = -1; 50 51 /** 52 * Call classification indicating foreground call 53 * @hide 54 */ 55 public static final int CALL_CLASSIFICATION_RINGING = 0; 56 57 /** 58 * Call classification indicating background call 59 * @hide 60 */ 61 public static final int CALL_CLASSIFICATION_FOREGROUND = 1; 62 63 /** 64 * Call classification indicating ringing call 65 * @hide 66 */ 67 public static final int CALL_CLASSIFICATION_BACKGROUND = 2; 68 69 /** 70 * Call classification Max value. 71 * @hide 72 */ 73 public static final int CALL_CLASSIFICATION_MAX = CALL_CLASSIFICATION_BACKGROUND + 1; 74 75 @PreciseCallStates 76 private final int mPreciseCallState; 77 78 @NetworkType 79 private final int mNetworkType; // TelephonyManager.NETWORK_TYPE_* ints 80 private final CallQuality mCallQuality; 81 82 private final int mCallClassification; 83 /** 84 * IMS call session ID. {@link ImsCallSession#getCallId()} 85 */ 86 @Nullable 87 private String mImsCallId; 88 89 /** 90 * IMS call service type of this call 91 */ 92 @ImsCallServiceType 93 private int mImsCallServiceType; 94 95 /** 96 * IMS call type of this call. 97 */ 98 @ImsCallType 99 private int mImsCallType; 100 101 /** 102 * Constructor of CallAttributes 103 * 104 * @param callState call state defined in {@link PreciseCallState} 105 * @param networkType network type for this call attributes 106 * @param callQuality call quality for this call attributes, only CallState in 107 * {@link PreciseCallState#PRECISE_CALL_STATE_ACTIVE} will have valid call 108 * quality. 109 * @param callClassification call classification 110 * @param imsCallId IMS call session ID for this call attributes 111 * @param imsCallServiceType IMS call service type for this call attributes 112 * @param imsCallType IMS call type for this call attributes 113 */ CallState(@reciseCallStates int callState, @NetworkType int networkType, @NonNull CallQuality callQuality, int callClassification, @Nullable String imsCallId, @ImsCallServiceType int imsCallServiceType, @ImsCallType int imsCallType)114 private CallState(@PreciseCallStates int callState, @NetworkType int networkType, 115 @NonNull CallQuality callQuality, int callClassification, @Nullable String imsCallId, 116 @ImsCallServiceType int imsCallServiceType, @ImsCallType int imsCallType) { 117 this.mPreciseCallState = callState; 118 this.mNetworkType = networkType; 119 this.mCallQuality = callQuality; 120 this.mCallClassification = callClassification; 121 this.mImsCallId = imsCallId; 122 this.mImsCallServiceType = imsCallServiceType; 123 this.mImsCallType = imsCallType; 124 } 125 126 @NonNull 127 @Override toString()128 public String toString() { 129 return "mPreciseCallState=" + mPreciseCallState + " mNetworkType=" + mNetworkType 130 + " mCallQuality=" + mCallQuality + " mCallClassification" + mCallClassification 131 + " mImsCallId=" + mImsCallId + " mImsCallServiceType=" + mImsCallServiceType 132 + " mImsCallType=" + mImsCallType; 133 } 134 CallState(Parcel in)135 private CallState(Parcel in) { 136 this.mPreciseCallState = in.readInt(); 137 this.mNetworkType = in.readInt(); 138 this.mCallQuality = in.readParcelable( 139 CallQuality.class.getClassLoader(), CallQuality.class); 140 this.mCallClassification = in.readInt(); 141 this.mImsCallId = in.readString(); 142 this.mImsCallServiceType = in.readInt(); 143 this.mImsCallType = in.readInt(); 144 } 145 146 // getters 147 /** 148 * Returns the precise call state of the call. 149 */ 150 @PreciseCallStates getCallState()151 public int getCallState() { 152 return mPreciseCallState; 153 } 154 155 /** 156 * Returns the {@link TelephonyManager#NetworkType} of the call. 157 * 158 * @see TelephonyManager#NETWORK_TYPE_UNKNOWN 159 * @see TelephonyManager#NETWORK_TYPE_GPRS 160 * @see TelephonyManager#NETWORK_TYPE_EDGE 161 * @see TelephonyManager#NETWORK_TYPE_UMTS 162 * @see TelephonyManager#NETWORK_TYPE_CDMA 163 * @see TelephonyManager#NETWORK_TYPE_EVDO_0 164 * @see TelephonyManager#NETWORK_TYPE_EVDO_A 165 * @see TelephonyManager#NETWORK_TYPE_1xRTT 166 * @see TelephonyManager#NETWORK_TYPE_HSDPA 167 * @see TelephonyManager#NETWORK_TYPE_HSUPA 168 * @see TelephonyManager#NETWORK_TYPE_HSPA 169 * @see TelephonyManager#NETWORK_TYPE_IDEN 170 * @see TelephonyManager#NETWORK_TYPE_EVDO_B 171 * @see TelephonyManager#NETWORK_TYPE_LTE 172 * @see TelephonyManager#NETWORK_TYPE_EHRPD 173 * @see TelephonyManager#NETWORK_TYPE_HSPAP 174 * @see TelephonyManager#NETWORK_TYPE_GSM 175 * @see TelephonyManager#NETWORK_TYPE_TD_SCDMA 176 * @see TelephonyManager#NETWORK_TYPE_IWLAN 177 * @see TelephonyManager#NETWORK_TYPE_LTE_CA 178 * @see TelephonyManager#NETWORK_TYPE_NR 179 */ 180 @NetworkType getNetworkType()181 public int getNetworkType() { 182 return mNetworkType; 183 } 184 185 /** 186 * Returns the {#link CallQuality} of the call. 187 * @return call quality for this call attributes, only CallState in {@link 188 * PreciseCallState#PRECISE_CALL_STATE_ACTIVE} will have valid call quality. It will be 189 * null for the call which is not in {@link PreciseCallState#PRECISE_CALL_STATE_ACTIVE}. 190 */ 191 @Nullable getCallQuality()192 public CallQuality getCallQuality() { 193 return mCallQuality; 194 } 195 196 /** 197 * Returns the call classification. 198 * @hide 199 */ getCallClassification()200 public int getCallClassification() { 201 return mCallClassification; 202 } 203 204 /** 205 * Returns the IMS call session ID. 206 */ 207 @Nullable getImsCallSessionId()208 public String getImsCallSessionId() { 209 return mImsCallId; 210 } 211 212 /** 213 * Returns the IMS call service type. 214 */ 215 @ImsCallServiceType getImsCallServiceType()216 public int getImsCallServiceType() { 217 return mImsCallServiceType; 218 } 219 220 /** 221 * Returns the IMS call type. 222 */ 223 @ImsCallType getImsCallType()224 public int getImsCallType() { 225 return mImsCallType; 226 } 227 228 @Override hashCode()229 public int hashCode() { 230 return Objects.hash(mPreciseCallState, mNetworkType, mCallQuality, mCallClassification, 231 mImsCallId, mImsCallServiceType, mImsCallType); 232 } 233 234 @Override equals(@ullable Object o)235 public boolean equals(@Nullable Object o) { 236 if (o == null || !(o instanceof CallState) || hashCode() != o.hashCode()) { 237 return false; 238 } 239 240 if (this == o) { 241 return true; 242 } 243 244 CallState s = (CallState) o; 245 246 return (mPreciseCallState == s.mPreciseCallState 247 && mNetworkType == s.mNetworkType 248 && Objects.equals(mCallQuality, s.mCallQuality) 249 && mCallClassification == s.mCallClassification 250 && Objects.equals(mImsCallId, s.mImsCallId) 251 && mImsCallType == s.mImsCallType 252 && mImsCallServiceType == s.mImsCallServiceType); 253 } 254 255 /** 256 * {@link Parcelable#describeContents} 257 */ describeContents()258 public int describeContents() { 259 return 0; 260 } 261 262 /** 263 * {@link Parcelable#writeToParcel} 264 */ writeToParcel(@ullable Parcel dest, int flags)265 public void writeToParcel(@Nullable Parcel dest, int flags) { 266 dest.writeInt(mPreciseCallState); 267 dest.writeInt(mNetworkType); 268 dest.writeParcelable(mCallQuality, flags); 269 dest.writeInt(mCallClassification); 270 dest.writeString(mImsCallId); 271 dest.writeInt(mImsCallServiceType); 272 dest.writeInt(mImsCallType); 273 } 274 275 public static final @NonNull Creator<CallState> CREATOR = new Creator() { 276 public CallState createFromParcel(Parcel in) { 277 return new CallState(in); 278 } 279 280 public CallState[] newArray(int size) { 281 return new CallState[size]; 282 } 283 }; 284 285 /** 286 * Builder of {@link CallState} 287 * 288 * <p>The example below shows how you might create a new {@code CallState}. A precise call state 289 * {@link PreciseCallStates} is mandatory to build a CallState. 290 * 291 * <pre><code> 292 * 293 * CallState = new CallState.Builder({@link PreciseCallStates}) 294 * .setNetworkType({@link TelephonyManager#NETWORK_TYPE_LTE}) 295 * .setCallQuality({@link CallQuality}) 296 * .setImsCallSessionId({@link String}) 297 * .setImsCallServiceType({@link ImsCallProfile#SERVICE_TYPE_NORMAL}) 298 * .setImsCallType({@link ImsCallProfile#CALL_TYPE_VOICE}) 299 * .build(); 300 * </code></pre> 301 */ 302 public static final class Builder { 303 private @PreciseCallStates int mPreciseCallState; 304 private @NetworkType int mNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN; 305 private CallQuality mCallQuality = null; 306 private int mCallClassification = CALL_CLASSIFICATION_UNKNOWN; 307 private String mImsCallId; 308 private @ImsCallServiceType int mImsCallServiceType = ImsCallProfile.SERVICE_TYPE_NONE; 309 private @ImsCallType int mImsCallType = ImsCallProfile.CALL_TYPE_NONE; 310 311 312 /** 313 * Default constructor for the Builder. 314 */ Builder(@reciseCallStates int preciseCallState)315 public Builder(@PreciseCallStates int preciseCallState) { 316 mPreciseCallState = preciseCallState; 317 } 318 319 /** 320 * Set network type of this call. 321 * 322 * @param networkType the transport type. 323 * @return The same instance of the builder. 324 */ 325 @NonNull setNetworkType(@etworkType int networkType)326 public CallState.Builder setNetworkType(@NetworkType int networkType) { 327 this.mNetworkType = networkType; 328 return this; 329 } 330 331 /** 332 * Set the call quality {@link CallQuality} of this call. 333 * 334 * @param callQuality call quality of active call. 335 * @return The same instance of the builder. 336 */ 337 @NonNull setCallQuality(@ullable CallQuality callQuality)338 public CallState.Builder setCallQuality(@Nullable CallQuality callQuality) { 339 this.mCallQuality = callQuality; 340 return this; 341 } 342 343 /** 344 * Set call classification for this call. 345 * 346 * @param classification call classification type defined in this class. 347 * @return The same instance of the builder. 348 * @hide 349 */ 350 @NonNull setCallClassification(int classification)351 public CallState.Builder setCallClassification(int classification) { 352 this.mCallClassification = classification; 353 return this; 354 } 355 356 /** 357 * Set IMS call session ID of this call. 358 * 359 * @param imsCallId IMS call session ID. 360 * @return The same instance of the builder. 361 */ 362 @NonNull setImsCallSessionId(@ullable String imsCallId)363 public CallState.Builder setImsCallSessionId(@Nullable String imsCallId) { 364 this.mImsCallId = imsCallId; 365 return this; 366 } 367 368 /** 369 * Set IMS call service type of this call. 370 * 371 * @param serviceType IMS call service type defined in {@link ImsCallProfile}. 372 * @return The same instance of the builder. 373 */ 374 @NonNull setImsCallServiceType(@msCallServiceType int serviceType)375 public CallState.Builder setImsCallServiceType(@ImsCallServiceType int serviceType) { 376 this.mImsCallServiceType = serviceType; 377 return this; 378 } 379 380 /** 381 * Set IMS call type of this call. 382 * 383 * @param callType IMS call type defined in {@link ImsCallProfile}. 384 * @return The same instance of the builder. 385 */ 386 @NonNull setImsCallType(@msCallType int callType)387 public CallState.Builder setImsCallType(@ImsCallType int callType) { 388 this.mImsCallType = callType; 389 return this; 390 } 391 392 /** 393 * Build the {@link CallState} 394 * 395 * @return the {@link CallState} object 396 */ 397 @NonNull build()398 public CallState build() { 399 return new CallState( 400 mPreciseCallState, 401 mNetworkType, 402 mCallQuality, 403 mCallClassification, 404 mImsCallId, 405 mImsCallServiceType, 406 mImsCallType); 407 } 408 } 409 } 410