1 /*
2  * Copyright 2021 HIMSA II K/S - www.himsa.com.
3  * Represented by EHIMA - www.ehima.com
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.bluetooth.tbs;
19 
20 import android.bluetooth.BluetoothLeCall;
21 import android.net.Uri;
22 
23 public class TbsCall {
24 
25     public static final int INDEX_UNASSIGNED = 0x00;
26     public static final int INDEX_MIN = 0x01;
27     public static final int INDEX_MAX = 0xFF;
28 
29     private int mState;
30     private String mUri;
31     private int mFlags;
32     private String mFriendlyName;
33 
34     /**
35      * Converts state value to human readable state string
36      *
37      * @param state state of call
38      * @return converted to string state
39      */
stateToString(Integer state)40     public static String stateToString(Integer state) {
41         if (state.equals(BluetoothLeCall.STATE_INCOMING)) {
42             return "INCOMING";
43         } else if (state.equals(BluetoothLeCall.STATE_DIALING)) {
44             return "DIALING";
45         } else if (state.equals(BluetoothLeCall.STATE_ALERTING)) {
46             return "ALERTING";
47         } else if (state.equals(BluetoothLeCall.STATE_ACTIVE)) {
48             return "ACTIVE";
49         } else if (state.equals(BluetoothLeCall.STATE_LOCALLY_HELD)) {
50             return "LOCALLY HELD";
51         } else if (state.equals(BluetoothLeCall.STATE_REMOTELY_HELD)) {
52             return "REMOTELY HELD";
53         } else if (state.equals(BluetoothLeCall.STATE_LOCALLY_AND_REMOTELY_HELD)) {
54             return "LOCALLY AND REMOTELY HELD";
55         } else {
56             return "UNKNOWN(" + state + ")";
57         }
58     }
59 
60     /**
61      * Converts call flags value to human readable flag string
62      *
63      * @param flags call flags
64      * @return converted to string flags
65      */
flagsToString(Integer flags)66     public static String flagsToString(Integer flags) {
67         String string = "";
68 
69         if (flags.equals(BluetoothLeCall.FLAG_OUTGOING_CALL)) {
70             if (string.isEmpty()) {
71                 string += "OUTGOING";
72             }
73         }
74         if (flags.equals(BluetoothLeCall.FLAG_WITHHELD_BY_SERVER)) {
75             if (!string.isEmpty()) {
76                 string += "|";
77             }
78             string += "WITHELD BY SERVER";
79         }
80         if (flags.equals(BluetoothLeCall.FLAG_WITHHELD_BY_NETWORK)) {
81             if (!string.isEmpty()) {
82                 string += "|";
83             }
84             string += "WITHELD BY NETWORK";
85         }
86 
87         return string;
88     }
89 
TbsCall(int state, String uri, int flags, String friendlyName)90     private TbsCall(int state, String uri, int flags, String friendlyName) {
91         mState = state;
92         mUri = uri;
93         mFlags = flags;
94         mFriendlyName = friendlyName;
95     }
96 
create(BluetoothLeCall call)97     public static TbsCall create(BluetoothLeCall call) {
98         return new TbsCall(
99                 call.getState(), call.getUri(), call.getCallFlags(), call.getFriendlyName());
100     }
101 
102     @Override
equals(Object o)103     public boolean equals(Object o) {
104         if (this == o) return true;
105         if (o == null || getClass() != o.getClass()) return false;
106         TbsCall that = (TbsCall) o;
107         // check the state only
108         return mState == that.mState;
109     }
110 
getState()111     public int getState() {
112         return mState;
113     }
114 
setState(int state)115     public void setState(int state) {
116         mState = state;
117     }
118 
getUri()119     public String getUri() {
120         return mUri;
121     }
122 
getSafeUri()123     public String getSafeUri() {
124         return Uri.parse(mUri).toSafeString();
125     }
126 
getFlags()127     public int getFlags() {
128         return mFlags;
129     }
130 
isIncoming()131     public boolean isIncoming() {
132         return (mFlags & BluetoothLeCall.FLAG_OUTGOING_CALL) == 0;
133     }
134 
getFriendlyName()135     public String getFriendlyName() {
136         return mFriendlyName;
137     }
138 
139     /**
140      * Converts Friendly Name to safe string (every second letter is replaced by '.')
141      *
142      * @return safe Friendly Name
143      */
getSafeFriendlyName()144     public String getSafeFriendlyName() {
145         ;
146         if (mFriendlyName == null) {
147             return null;
148         }
149 
150         /* Don't anonymize short names */
151         if (mFriendlyName.length() < 3) {
152             return mFriendlyName;
153         }
154 
155         final StringBuilder builder = new StringBuilder();
156         for (int i = 0; i < mFriendlyName.length(); i++) {
157             final char c = mFriendlyName.charAt(i);
158 
159             /* Anonymize every second letter */
160             if ((i % 2) == 0) {
161                 builder.append(c);
162             } else {
163                 builder.append('.');
164             }
165         }
166         return builder.toString();
167     }
168 }
169