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(call.getState(), call.getUri(), call.getCallFlags(), 99 call.getFriendlyName()); 100 } 101 102 @Override equals(Object o)103 public boolean equals(Object o) { 104 if (this == o) 105 return true; 106 if (o == null || getClass() != o.getClass()) 107 return false; 108 TbsCall that = (TbsCall) o; 109 // check the state only 110 return mState == that.mState; 111 } 112 getState()113 public int getState() { 114 return mState; 115 } 116 setState(int state)117 public void setState(int state) { 118 mState = state; 119 } 120 getUri()121 public String getUri() { 122 return mUri; 123 } 124 getSafeUri()125 public String getSafeUri() { 126 return Uri.parse(mUri).toSafeString(); 127 } 128 getFlags()129 public int getFlags() { 130 return mFlags; 131 } 132 isIncoming()133 public boolean isIncoming() { 134 return (mFlags & BluetoothLeCall.FLAG_OUTGOING_CALL) == 0; 135 } 136 getFriendlyName()137 public String getFriendlyName() { 138 return mFriendlyName; 139 } 140 141 /** 142 * Converts Friendly Name to safe string (every second letter is replaced by '.') 143 * 144 * @return safe Friendly Name 145 */ getSafeFriendlyName()146 public String getSafeFriendlyName() {; 147 if (mFriendlyName == null) { 148 return null; 149 } 150 151 /* Don't anonymize short names */ 152 if (mFriendlyName.length() < 3) { 153 return mFriendlyName; 154 } 155 156 final StringBuilder builder = new StringBuilder(); 157 for (int i = 0; i < mFriendlyName.length(); i++) { 158 final char c = mFriendlyName.charAt(i); 159 160 /* Anonymize every second letter */ 161 if ((i % 2) == 0) { 162 builder.append(c); 163 } else { 164 builder.append('.'); 165 } 166 } 167 return builder.toString(); 168 } 169 } 170