1 /** 2 * Copyright (C) 2021 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.car; 18 19 import android.bluetooth.BluetoothAdapter; 20 import android.bluetooth.BluetoothDevice; 21 import android.content.Context; 22 import android.telecom.PhoneAccountHandle; 23 import android.telecom.TelecomManager; 24 import android.text.TextUtils; 25 26 import androidx.annotation.NonNull; 27 import androidx.annotation.Nullable; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 import java.util.Set; 32 33 /** 34 * Utility class for Telecom related methods. 35 */ 36 public class TelecomUtils { 37 38 public static final String HFP_CLIENT_CONNECTION_SERVICE_CLASS_NAME = 39 "com.android.bluetooth.hfpclient.connserv.HfpClientConnectionService"; 40 41 private TelecomManager mTelecomManager; 42 private BluetoothAdapter mBluetoothAdapter; 43 TelecomUtils(Context context)44 public TelecomUtils(Context context) { 45 mTelecomManager = context.getSystemService(TelecomManager.class); 46 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 47 } 48 49 /** Returns the list of hfp {@link BluetoothDevice}s for current callable phone accounts. */ 50 @NonNull getHfpDeviceList()51 public List<BluetoothDevice> getHfpDeviceList() { 52 List<PhoneAccountHandle> phoneAccountHandles = 53 mTelecomManager.getCallCapablePhoneAccounts(true); 54 List<BluetoothDevice> hfpDeviceList = new ArrayList<>(); 55 for (PhoneAccountHandle phoneAccountHandle : phoneAccountHandles) { 56 BluetoothDevice bluetoothDevice = getMatchingDevice(phoneAccountHandle); 57 if (bluetoothDevice != null) { 58 hfpDeviceList.add(bluetoothDevice); 59 } 60 } 61 return hfpDeviceList; 62 } 63 64 /** 65 * Returns the {@link BluetoothDevice} for the given {@link PhoneAccountHandle} if the account 66 * is for hfp connection. 67 */ getMatchingDevice( @ullable PhoneAccountHandle phoneAccountHandle)68 public BluetoothDevice getMatchingDevice( 69 @Nullable PhoneAccountHandle phoneAccountHandle) { 70 Set<BluetoothDevice> bondedDevices = 71 mBluetoothAdapter == null ? null : mBluetoothAdapter.getBondedDevices(); 72 if (bondedDevices == null) { 73 return null; 74 } 75 if (isHfpConnectionService(phoneAccountHandle)) { 76 for (BluetoothDevice bluetoothDevice : bondedDevices) { 77 if (TextUtils.equals(bluetoothDevice.getAddress(), phoneAccountHandle.getId())) { 78 return bluetoothDevice; 79 } 80 } 81 } 82 return null; 83 } 84 85 /** Returns if the {@link PhoneAccountHandle} is from a hfp connection for bluetooth call. */ isHfpConnectionService(@ullable PhoneAccountHandle phoneAccountHandle)86 public boolean isHfpConnectionService(@Nullable PhoneAccountHandle phoneAccountHandle) { 87 return phoneAccountHandle != null 88 && HFP_CLIENT_CONNECTION_SERVICE_CLASS_NAME.equals( 89 phoneAccountHandle.getComponentName().getClassName()); 90 } 91 } 92