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 com.android.server.telecom.transactionalVoipApp;
18 
19 import android.os.Bundle;
20 import android.telecom.CallControlCallback;
21 import android.telecom.CallEndpoint;
22 import android.telecom.CallControl;
23 import android.telecom.CallEventCallback;
24 import android.telecom.DisconnectCause;
25 import android.util.Log;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 import androidx.annotation.NonNull;
31 
32 import java.util.function.Consumer;
33 
34 public class MyVoipCall implements CallControlCallback, CallEventCallback {
35 
36     private static final String TAG = "MyVoipCall";
37     private final String mCallId;
38     public CallControl mCallControl;
39     public CallEndpoint mCurrentEndpoint;
40     public CallEndpoint mEarpieceEndpoint;
41     public CallEndpoint mSpeakerEndpoint;
42     public CallEndpoint mBluetoothEndpoint;
43     List<CallEndpoint> mAvailableEndpoint = new ArrayList<>();
44 
MyVoipCall(String id)45     MyVoipCall(String id) {
46         mCallId = id;
47     }
48 
onAddCallControl(@onNull CallControl callControl)49     public void onAddCallControl(@NonNull CallControl callControl) {
50         mCallControl = callControl;
51     }
52 
53     @Override
onSetActive(@onNull Consumer<Boolean> wasCompleted)54     public void onSetActive(@NonNull Consumer<Boolean> wasCompleted) {
55         Log.i(TAG, String.format("onSetActive: callId=[%s]", mCallId));
56         wasCompleted.accept(Boolean.TRUE);
57     }
58 
59     @Override
onSetInactive(@onNull Consumer<Boolean> wasCompleted)60     public void onSetInactive(@NonNull Consumer<Boolean> wasCompleted) {
61         Log.i(TAG, String.format("onSetInactive: callId=[%s]", mCallId));
62         wasCompleted.accept(Boolean.TRUE);
63     }
64 
65     @Override
onAnswer(int videoState, @NonNull Consumer<Boolean> wasCompleted)66     public void onAnswer(int videoState, @NonNull Consumer<Boolean> wasCompleted) {
67         Log.i(TAG, String.format("onAnswer: callId=[%s]", mCallId));
68         wasCompleted.accept(Boolean.TRUE);
69     }
70 
71     @Override
onDisconnect(@onNull DisconnectCause cause, @NonNull Consumer<Boolean> wasCompleted)72     public void onDisconnect(@NonNull DisconnectCause cause,
73             @NonNull Consumer<Boolean> wasCompleted) {
74         Log.i(TAG, String.format("onDisconnect: callId=[%s]", mCallId));
75         wasCompleted.accept(Boolean.TRUE);
76     }
77 
78     @Override
onCallStreamingStarted(@onNull Consumer<Boolean> wasCompleted)79     public void onCallStreamingStarted(@NonNull Consumer<Boolean> wasCompleted) {
80         Log.i(TAG, String.format("onCallStreamingStarted: callId=[%s]", mCallId));
81         wasCompleted.accept(Boolean.TRUE);
82     }
83 
84     @Override
onCallStreamingFailed(int reason)85     public void onCallStreamingFailed(int reason) {
86         Log.i(TAG, String.format("onCallStreamingFailed: id=[%s], reason=[%d]", mCallId, reason));
87     }
88 
89     @Override
onEvent(String event, Bundle extras)90     public void onEvent(String event, Bundle extras) {
91         Log.i(TAG, String.format("onEvent: id=[%s], event=[%s], extras=[%s]",
92                 mCallId, event, extras));
93     }
94 
95     @Override
onCallEndpointChanged(@onNull CallEndpoint newCallEndpoint)96     public void onCallEndpointChanged(@NonNull CallEndpoint newCallEndpoint) {
97         Log.i(TAG, String.format("onCallEndpointChanged: endpoint=[%s]", newCallEndpoint));
98         mCurrentEndpoint = newCallEndpoint;
99     }
100 
101     @Override
onAvailableCallEndpointsChanged( @onNull List<CallEndpoint> availableEndpoints)102     public void onAvailableCallEndpointsChanged(
103             @NonNull List<CallEndpoint> availableEndpoints) {
104         Log.i(TAG, String.format("onAvailableCallEndpointsChanged: callId=[%s]", mCallId));
105         for (CallEndpoint endpoint : availableEndpoints) {
106             Log.i(TAG, String.format("endpoint=[%s]", endpoint));
107             if (endpoint != null && endpoint.getEndpointType() == CallEndpoint.TYPE_EARPIECE) {
108                 mEarpieceEndpoint = endpoint;
109             }
110             if (endpoint != null && endpoint.getEndpointType() == CallEndpoint.TYPE_SPEAKER) {
111                 mSpeakerEndpoint = endpoint;
112             }
113             if (endpoint != null && endpoint.getEndpointType() == CallEndpoint.TYPE_BLUETOOTH) {
114                 mBluetoothEndpoint = endpoint;
115             }
116         }
117         mAvailableEndpoint = availableEndpoints;
118     }
119 
120     @Override
onMuteStateChanged(boolean isMuted)121     public void onMuteStateChanged(boolean isMuted) {
122     }
123 }
124