1 /*
2  * Copyright (C) 2024 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.voip;
18 
19 import android.telecom.CallAttributes;
20 import android.telecom.Log;
21 import android.telecom.VideoProfile;
22 
23 import com.android.server.telecom.AnomalyReporterAdapter;
24 import com.android.server.telecom.AnomalyReporterAdapterImpl;
25 
26 import java.util.UUID;
27 
28 /**
29  * This remapping class is needed because {@link VideoProfile} has more fine grain levels of video
30  * states as apposed to Transactional video states (defined in  {@link CallAttributes.CallType}.
31  * To be more specific, there are 3 video states (rx, tx, and bi-directional).
32  * {@link CallAttributes.CallType} only has 2 states (audio and video).
33  *
34  * The reason why Transactional calls have fewer states is due to the fact that the framework is
35  * only used by VoIP apps and Telecom only cares to know if the call is audio or video.
36  *
37  * Calls that are backed by a {@link android.telecom.ConnectionService} have the ability to be
38  * managed calls (non-VoIP) and Dialer needs more fine grain video states to update the UI. Thus,
39  * {@link VideoProfile} is used for {@link android.telecom.ConnectionService} backed calls.
40  */
41 public class VideoStateTranslation {
42     private static final String TAG = VideoStateTranslation.class.getSimpleName();
43 
44     /**
45      * Client --> Telecom
46      * This should be used when the client application is signaling they are changing the video
47      * state.
48      */
TransactionalVideoStateToVideoProfileState(int callType)49     public static int TransactionalVideoStateToVideoProfileState(int callType) {
50         if (callType == CallAttributes.AUDIO_CALL) {
51             Log.i(TAG, "CallAttributes.AUDIO_CALL --> VideoProfile.STATE_AUDIO_ONLY");
52             return VideoProfile.STATE_AUDIO_ONLY;
53         } else if (callType == CallAttributes.VIDEO_CALL) {
54             Log.i(TAG, "CallAttributes.VIDEO_CALL--> VideoProfile.STATE_BIDIRECTIONAL");
55             return VideoProfile.STATE_BIDIRECTIONAL;
56         } else {
57             Log.w(TAG, "CallType=[%d] does not have a VideoProfile mapping", callType);
58             return VideoProfile.STATE_AUDIO_ONLY;
59         }
60     }
61 
62     /**
63      * Telecom --> Client
64      * This should be used when Telecom is informing the client of a video state change.
65      */
VideoProfileStateToTransactionalVideoState(int videoProfileState)66     public static int VideoProfileStateToTransactionalVideoState(int videoProfileState) {
67         switch (videoProfileState) {
68             case VideoProfile.STATE_AUDIO_ONLY -> {
69                 Log.i(TAG, "%s --> CallAttributes.AUDIO_CALL",
70                         VideoProfileStateToString(videoProfileState));
71                 return CallAttributes.AUDIO_CALL;
72             }
73             case VideoProfile.STATE_BIDIRECTIONAL, VideoProfile.STATE_TX_ENABLED,
74                     VideoProfile.STATE_RX_ENABLED -> {
75                 Log.i(TAG, "%s --> CallAttributes.VIDEO_CALL",
76                         VideoProfileStateToString(videoProfileState));
77                 return CallAttributes.VIDEO_CALL;
78             }
79             default -> {
80                 Log.w(TAG, "VideoProfile=[%d] does not have a CallType mapping", videoProfileState);
81                 return CallAttributes.AUDIO_CALL;
82             }
83         }
84     }
85 
TransactionalVideoStateToString(int transactionalVideoState)86     public static String TransactionalVideoStateToString(int transactionalVideoState) {
87         if (transactionalVideoState == CallAttributes.AUDIO_CALL) {
88             return "CallAttributes.AUDIO_CALL";
89         } else if (transactionalVideoState == CallAttributes.VIDEO_CALL) {
90             return "CallAttributes.VIDEO_CALL";
91         } else {
92             return "CallAttributes.UNKNOWN";
93         }
94     }
95 
VideoProfileStateToString(int videoProfileState)96     private static String VideoProfileStateToString(int videoProfileState) {
97         switch (videoProfileState) {
98             case VideoProfile.STATE_BIDIRECTIONAL -> {
99                 return "VideoProfile.STATE_BIDIRECTIONAL";
100             }
101             case VideoProfile.STATE_RX_ENABLED -> {
102                 return "VideoProfile.STATE_RX_ENABLED";
103             }
104             case VideoProfile.STATE_TX_ENABLED -> {
105                 return "VideoProfile.STATE_TX_ENABLED";
106             }
107             case VideoProfile.STATE_AUDIO_ONLY -> {
108                 return "VideoProfile.STATE_AUDIO_ONLY";
109             }
110             default -> {
111                 return "VideoProfile.UNKNOWN";
112             }
113         }
114     }
115 }
116