1 /*
2  * Copyright 2023 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 androidx.core.telecom
18 
19 import java.util.Objects
20 
21 /**
22  * CallControlResult is a return value that represents the result of one of the following
23  * [CallControlScope] methods:
24  * - [CallControlScope.setActive]
25  * - [CallControlScope.answer]
26  * - [CallControlScope.disconnect]
27  * - [CallControlScope.setInactive]
28  * - [CallControlScope.requestEndpointChange]
29  *
30  * Each of the above listed methods has the ability to fail and if it does, this will be represented
31  * by a [CallControlResult.Error] (e.g. Telecom was not able to change the call route via
32  * requestEndpointChange). Otherwise, [CallControlResult.Success] is returned to represent the
33  * operation succeeded (e.g Telecom was able to set the call active).
34  *
35  * Example usage:
36  * ```
37  * launch {
38  *     when(val result = setActive()) {
39  *       is CallControlResult.Success -> {
40  *           Log.d(TAG, "onSetActive - ${result}")
41  *           // move call to active state locally
42  *       }
43  *       is CallControlResult.Failure -> {
44  *           Log.w(TAG, "onSetActive - ${result}")
45  *          // surface error to user if required
46  *       }
47  * }
48  * ````
49  */
50 public sealed class CallControlResult {
51     /**
52      * The associated [CallControlScope] method was successful. For example, if
53      * [CallControlScope.setActive] was requested, Telecom was able to change the call state.
54      */
55     public class Success : CallControlResult() {
toStringnull56         override fun toString(): String {
57             return "CallControlResult(Success)"
58         }
59 
equalsnull60         override fun equals(other: Any?): Boolean {
61             return other is Success
62         }
63 
hashCodenull64         override fun hashCode(): Int {
65             return Objects.hash()
66         }
67     }
68 
69     /**
70      * The associated [CallControlScope] method failed. For example, if [CallControlScope.setActive]
71      * was requested, Telecom failed to transition the call to active. There are numerous reasons
72      * why the operation failed; please see the [errorCode] for details.
73      */
74     public class Error(@CallException.Companion.CallErrorCode public val errorCode: Int) :
75         CallControlResult() {
toStringnull76         override fun toString(): String {
77             return "CallControlResult(Error[errorCode=($errorCode)])"
78         }
79 
equalsnull80         override fun equals(other: Any?): Boolean {
81             return other is Error && errorCode == other.errorCode
82         }
83 
hashCodenull84         override fun hashCode(): Int {
85             return Objects.hash(errorCode)
86         }
87     }
88 }
89